Skip to content

Commit

Permalink
feat: manually close conn
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed May 9, 2024
1 parent 7767cc4 commit b876981
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
20 changes: 11 additions & 9 deletions lib/data/provider/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ServerProvider extends ChangeNotifier {

Timer? _timer;

final _manualDisconnectedIds = <String>{};

Future<void> load() async {
// Issue #147
// Clear all servers because of restarting app will cause duplicate servers
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
50 changes: 31 additions & 19 deletions lib/view/page/server/tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -350,7 +352,7 @@ class _ServerPageState extends State<ServerPage>
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
IconTextBtn(
onPressed: () => _askFor(
func: () async {
if (Stores.setting.showSuspendTip.fetch()) {
Expand All @@ -369,10 +371,10 @@ class _ServerPageState extends State<ServerPage>
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,
Expand All @@ -382,10 +384,10 @@ class _ServerPageState extends State<ServerPage>
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,
Expand All @@ -395,13 +397,13 @@ class _ServerPageState extends State<ServerPage>
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,
)
],
)
Expand Down Expand Up @@ -468,7 +470,7 @@ class _ServerPageState extends State<ServerPage>
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,
Expand All @@ -484,28 +486,38 @@ class _ServerPageState extends State<ServerPage>
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,
color: Colors.grey,
),
),
),
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,
};
}

Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions lib/view/widget/icon_text_btn.dart
Original file line number Diff line number Diff line change
@@ -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),
],
)
);
}
}

0 comments on commit b876981

Please sign in to comment.