mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-01-31 13:25:10 +01:00
opt.: reconnect logic (#258)
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:toolbox/core/utils/misc.dart';
|
||||
|
||||
// abstract final class SecureStore {
|
||||
@@ -29,6 +30,8 @@ import 'package:toolbox/core/utils/misc.dart';
|
||||
// }
|
||||
// }
|
||||
|
||||
final _logger = Logger('Store');
|
||||
|
||||
class PersistentStore {
|
||||
late final Box box;
|
||||
|
||||
@@ -142,8 +145,12 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
|
||||
T fetch() {
|
||||
final stored = _box.get(_key);
|
||||
if (stored == null || stored is! T) {
|
||||
if (decoder != null) {
|
||||
return decoder!(stored);
|
||||
try {
|
||||
if (decoder != null) {
|
||||
return decoder!(stored);
|
||||
}
|
||||
} catch (_) {
|
||||
_logger.warning('Failed to decode "$_key"');
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -193,9 +200,15 @@ class _StoreListProperty<T> implements StorePropertyBase<List<T>> {
|
||||
if (val is! List) {
|
||||
throw Exception('StoreListProperty("$_key") is: ${val.runtimeType}');
|
||||
}
|
||||
return decoder == null
|
||||
? List<T>.from(val)
|
||||
: val.map((e) => decoder!.call(e)).toList();
|
||||
if (decoder != null) {
|
||||
try {
|
||||
return List<T>.from(val.map(decoder!));
|
||||
} catch (_) {
|
||||
_logger.warning('Failed to decode "$_key"');
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
return List<T>.from(val);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -36,12 +36,22 @@ class ServerProvider extends ChangeNotifier {
|
||||
Future<void> load() async {
|
||||
// Issue #147
|
||||
// Clear all servers because of restarting app will cause duplicate servers
|
||||
final oldServers = Map<String, Server>.from(_servers);
|
||||
_servers.clear();
|
||||
_serverOrder.clear();
|
||||
|
||||
final spis = Stores.server.fetch();
|
||||
for (int idx = 0; idx < spis.length; idx++) {
|
||||
_servers[spis[idx].id] = genServer(spis[idx]);
|
||||
final spi = spis[idx];
|
||||
final originServer = oldServers[spi.id];
|
||||
final newServer = genServer(spi);
|
||||
|
||||
/// Issues #258
|
||||
/// If not [shouldReconnect], then keep the old state.
|
||||
if (originServer != null && !originServer.spi.shouldReconnect(spi)) {
|
||||
newServer.state = originServer.state;
|
||||
}
|
||||
_servers[spi.id] = newServer;
|
||||
}
|
||||
final serverOrder_ = Stores.setting.serverOrder.fetch();
|
||||
if (serverOrder_.isNotEmpty) {
|
||||
@@ -107,7 +117,7 @@ class ServerProvider extends ChangeNotifier {
|
||||
|
||||
/// if [spi] is specificed then only refresh this server
|
||||
/// [onlyFailed] only refresh failed servers
|
||||
Future<void> refreshData({
|
||||
Future<void> refresh({
|
||||
ServerPrivateInfo? spi,
|
||||
bool onlyFailed = false,
|
||||
}) async {
|
||||
@@ -149,7 +159,7 @@ class ServerProvider extends ChangeNotifier {
|
||||
}
|
||||
refreshKey.currentState?.show();
|
||||
_timer = Timer.periodic(Duration(seconds: duration), (_) async {
|
||||
await refreshData();
|
||||
await refresh();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -192,7 +202,7 @@ class ServerProvider extends ChangeNotifier {
|
||||
_serverOrder.add(spi.id);
|
||||
Stores.setting.serverOrder.put(_serverOrder);
|
||||
_updateTags();
|
||||
refreshData(spi: spi);
|
||||
refresh(spi: spi);
|
||||
}
|
||||
|
||||
void delServer(String id) {
|
||||
@@ -233,7 +243,7 @@ class ServerProvider extends ChangeNotifier {
|
||||
if (newSpi.shouldReconnect(old)) {
|
||||
// Use [newSpi.id] instead of [old.id] because [old.id] may be changed
|
||||
TryLimiter.reset(newSpi.id);
|
||||
refreshData(spi: newSpi);
|
||||
refresh(spi: newSpi);
|
||||
}
|
||||
|
||||
// Only update if [spi.tags] changed
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
class BuildData {
|
||||
static const String name = "ServerBox";
|
||||
static const int build = 725;
|
||||
static const int build = 726;
|
||||
static const String engine = "3.16.8";
|
||||
static const String buildAt = "2024-01-27 21:27:49";
|
||||
static const int modifications = 4;
|
||||
static const String buildAt = "2024-01-27 23:32:47";
|
||||
static const int modifications = 3;
|
||||
static const int script = 36;
|
||||
}
|
||||
|
||||
@@ -365,7 +365,7 @@ class _FullScreenPageState extends State<FullScreenPage> with AfterLayoutMixin {
|
||||
}
|
||||
await GetIt.I.allReady();
|
||||
await Pros.server.load();
|
||||
await Pros.server.refreshData();
|
||||
await Pros.server.refresh();
|
||||
if (!Analysis.enabled) {
|
||||
await Analysis.init();
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ ${GithubIds.participants.map((e) => '[$e](${e.url})').join(' ')}
|
||||
HomeWidgetMC.update();
|
||||
await GetIt.I.allReady();
|
||||
await Pros.server.load();
|
||||
await Pros.server.refreshData();
|
||||
await Pros.server.refresh();
|
||||
}
|
||||
|
||||
Future<void> _onLongPressSetting() async {
|
||||
|
||||
@@ -107,7 +107,7 @@ class _ServerPageState extends State<ServerPage>
|
||||
|
||||
return RefreshIndicator(
|
||||
key: ServerProvider.refreshKey,
|
||||
onRefresh: () async => await Pros.server.refreshData(onlyFailed: true),
|
||||
onRefresh: () async => await Pros.server.refresh(onlyFailed: true),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
@@ -323,7 +323,7 @@ class _ServerPageState extends State<ServerPage>
|
||||
rightCorner = InkWell(
|
||||
onTap: () {
|
||||
TryLimiter.reset(spi.id);
|
||||
Pros.server.refreshData(spi: spi);
|
||||
Pros.server.refresh(spi: spi);
|
||||
},
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 7),
|
||||
@@ -336,7 +336,7 @@ class _ServerPageState extends State<ServerPage>
|
||||
);
|
||||
} else if (!(spi.autoConnect ?? true) && cs == ServerState.disconnected) {
|
||||
rightCorner = InkWell(
|
||||
onTap: () => Pros.server.refreshData(spi: spi),
|
||||
onTap: () => Pros.server.refresh(spi: spi),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 7),
|
||||
child: Icon(
|
||||
|
||||
Reference in New Issue
Block a user