feat: stop all servers in noti center (#901)

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-09-06 14:04:53 +08:00
committed by GitHub
parent 0c7b72fb2c
commit 05a927753f
5 changed files with 124 additions and 80 deletions

View File

@@ -77,8 +77,10 @@ abstract final class MethodChans {
}
/// Register a handler for native -> Flutter callbacks.
/// Currently handles: `disconnectSession` with argument map {id: string}
static void registerHandler(Future<void> Function(String id) onDisconnect) {
/// Currently handles:
/// - `disconnectSession` with argument map {id: string}
/// - `stopAllConnections` with no arguments
static void registerHandler(Future<void> Function(String id) onDisconnect, [VoidCallback? onStopAll]) {
_channel.setMethodCallHandler((call) async {
switch (call.method) {
case 'disconnectSession':
@@ -88,6 +90,9 @@ abstract final class MethodChans {
await onDisconnect(id);
}
return;
case 'stopAllConnections':
onStopAll?.call();
return;
default:
return;
}

View File

@@ -51,12 +51,31 @@ abstract final class TermSessionManager {
static void init() {
if (isAndroid) {
MethodChans.registerHandler((id) async {
_entries[id]?.disconnect?.call();
});
MethodChans.registerHandler(
(id) async {
_entries[id]?.disconnect?.call();
},
() {
// Stop all connections when notification "Stop All" is pressed
stopAllConnections();
},
);
}
}
/// Called when Android notification "Stop All" button is pressed
static void stopAllConnections() {
// Disconnect all sessions
final disconnectCallbacks = _entries.values.map((e) => e.disconnect).where((cb) => cb != null).toList();
for (final disconnect in disconnectCallbacks) {
disconnect!();
}
// Clear all entries
_entries.clear();
_activeId = null;
_sync();
}
/// Add a session record and push update to Android.
static void add({
required String id,

View File

@@ -98,7 +98,7 @@ class _ConnectionStatsPageState extends State<ConnectionStatsPage> {
),
),
Text(
'${libL10n.success}: $successRate%',
'${libL10n.success}: $successRate',
style: TextStyle(
fontSize: 16,
color: stats.successRate >= 0.8