From b876981243e9cc1645d5d0bb59a1d9376a3ed2fb Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Thu, 9 May 2024 22:29:44 +0800 Subject: [PATCH] feat: manually close conn --- lib/data/provider/server.dart | 20 ++++++------ lib/view/page/server/tab.dart | 50 ++++++++++++++++++------------ lib/view/widget/icon_text_btn.dart | 38 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 lib/view/widget/icon_text_btn.dart diff --git a/lib/data/provider/server.dart b/lib/data/provider/server.dart index 2757a4ec..81d0bfd8 100644 --- a/lib/data/provider/server.dart +++ b/lib/data/provider/server.dart @@ -37,6 +37,8 @@ class ServerProvider extends ChangeNotifier { Timer? _timer; + final _manualDisconnectedIds = {}; + Future load() async { // Issue #147 // Clear all servers because of restarting app will cause duplicate servers @@ -139,13 +141,9 @@ class ServerProvider extends ChangeNotifier { TryLimiter.reset(s.spi.id); } - /// If [spi.autoConnect] is false and server is disconnected, then skip. - /// - /// If [spi.autoConnect] is false and server is connected, then refresh. - /// If no this, the server will only refresh once by clicking refresh button. - /// - /// If [spi.autoConnect] is true, then refresh. - if (!(s.spi.autoConnect ?? true) && s.conn == ServerConn.disconnected) { + if (!(s.spi.autoConnect ?? true) && + s.conn == ServerConn.disconnected || + _manualDisconnectedIds.contains(s.spi.id)) { return; } return await _getData(s.spi); @@ -192,8 +190,12 @@ class ServerProvider extends ChangeNotifier { } void _closeOneServer(String id) { - _servers[id]?.client?.close(); - _servers[id]?.client = null; + final item = _servers[id]; + item?.client?.close(); + item?.client = null; + item?.conn = ServerConn.disconnected; + _manualDisconnectedIds.add(id); + notifyListeners(); } void addServer(ServerPrivateInfo spi) { diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index 8109f2af..f533ccb5 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -4,6 +4,7 @@ import 'dart:math' as math; import 'package:after_layout/after_layout.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; +import 'package:icons_plus/icons_plus.dart'; import 'package:provider/provider.dart'; import 'package:toolbox/core/extension/context/common.dart'; import 'package:toolbox/core/extension/context/dialog.dart'; @@ -19,6 +20,7 @@ import 'package:toolbox/data/res/color.dart'; import 'package:toolbox/data/res/provider.dart'; import 'package:toolbox/data/res/store.dart'; import 'package:toolbox/view/widget/auto_hide.dart'; +import 'package:toolbox/view/widget/icon_text_btn.dart'; import 'package:toolbox/view/widget/markdown.dart'; import 'package:toolbox/view/widget/percent_circle.dart'; @@ -350,7 +352,7 @@ class _ServerPageState extends State Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ - IconButton( + IconTextBtn( onPressed: () => _askFor( func: () async { if (Stores.setting.showSuspendTip.fetch()) { @@ -369,10 +371,10 @@ class _ServerPageState extends State typ: l10n.suspend, name: srv.spi.name, ), - icon: const Icon(Icons.stop), - tooltip: l10n.suspend, + icon: Icons.stop, + text: l10n.suspend, ), - IconButton( + IconTextBtn( onPressed: () => _askFor( func: () => srv.client?.execWithPwd( ShellFunc.shutdown.exec, @@ -382,10 +384,10 @@ class _ServerPageState extends State typ: l10n.shutdown, name: srv.spi.name, ), - icon: const Icon(Icons.power_off), - tooltip: l10n.shutdown, + icon: Icons.power_off, + text: l10n.shutdown, ), - IconButton( + IconTextBtn( onPressed: () => _askFor( func: () => srv.client?.execWithPwd( ShellFunc.reboot.exec, @@ -395,13 +397,13 @@ class _ServerPageState extends State typ: l10n.reboot, name: srv.spi.name, ), - icon: const Icon(Icons.restart_alt), - tooltip: l10n.reboot, + icon: Icons.restart_alt, + text: l10n.reboot, ), - IconButton( + IconTextBtn( onPressed: () => AppRoute.serverEdit(spi: srv.spi).go(context), - icon: const Icon(Icons.edit), - tooltip: l10n.edit, + icon: Icons.edit, + text: l10n.edit, ) ], ) @@ -468,7 +470,7 @@ class _ServerPageState extends State ServerConn.loading || ServerConn.connected => Padding( - padding: const EdgeInsets.symmetric(horizontal: 7), + padding: const EdgeInsets.only(left: 11, right: 3), child: SizedBox( width: 19, height: 19, @@ -484,7 +486,7 @@ class _ServerPageState extends State Pros.server.refresh(spi: s.spi); }, child: const Padding( - padding: EdgeInsets.symmetric(horizontal: 7), + padding: EdgeInsets.only(left: 11, right: 3), child: Icon( Icons.refresh, size: 21, @@ -492,20 +494,30 @@ class _ServerPageState extends State ), ), ), - ServerConn.disconnected when !(s.spi.autoConnect ?? true) => InkWell( + ServerConn.disconnected => InkWell( onTap: () => Pros.server.refresh(spi: s.spi), child: const Padding( - padding: EdgeInsets.symmetric(horizontal: 7), + padding: EdgeInsets.only(left: 11, right: 3), child: Icon( - Icons.link, + BoxIcons.bx_link, size: 21, color: Colors.grey, ), ), ), + ServerConn.finished => InkWell( + onTap: () => Pros.server.closeServer(id: s.spi.id), + child: const Padding( + padding: EdgeInsets.only(left: 11, right: 3), + child: Icon( + BoxIcons.bx_unlink, + size: 17, + color: Colors.grey, + ), + ), + ), _ when Stores.setting.serverTabUseOldUI.fetch() => ServerFuncBtnsTopRight(spi: s.spi), - _ => UIs.placeholder, }; } @@ -648,7 +660,7 @@ ${ss.err?.message ?? l10n.unknownError} return 23.0; } if (flip) { - return 80.0; + return 97.0; } if (Stores.setting.moveOutServerTabFuncBtns.fetch() && // Discussion #146 diff --git a/lib/view/widget/icon_text_btn.dart b/lib/view/widget/icon_text_btn.dart new file mode 100644 index 00000000..0963a4da --- /dev/null +++ b/lib/view/widget/icon_text_btn.dart @@ -0,0 +1,38 @@ +import 'package:flutter/material.dart'; +import 'package:toolbox/data/res/ui.dart'; + +final class IconTextBtn extends StatelessWidget { + final String text; + final IconData icon; + final VoidCallback? onPressed; + final Orientation orientation; + + const IconTextBtn({ + super.key, + required this.text, + required this.icon, + this.onPressed, + this.orientation = Orientation.portrait, + }); + + @override + Widget build(BuildContext context) { + return IconButton( + onPressed: onPressed, + tooltip: text, + icon: orientation == Orientation.landscape ? Row( + children: [ + Icon(icon), + UIs.width7, + Text(text, style: UIs.text13Grey), + ], + ) : Column( + children: [ + Icon(icon), + UIs.height7, + Text(text, style: UIs.text13Grey), + ], + ) + ); + } +}