diff --git a/lib/core/persistant_store.dart b/lib/core/persistant_store.dart index 5a9c3e4b..e9f7bb4b 100644 --- a/lib/core/persistant_store.dart +++ b/lib/core/persistant_store.dart @@ -39,6 +39,9 @@ class PersistentStore { files.map((e) => e.path.replaceFirst('$docPath/', '')).toList(); return paths; } + + /// Convert db to json + Map toJson() => {for (var e in box.keys) e: box.get(e)}; } abstract class StorePropertyBase { diff --git a/lib/data/model/app/backup.dart b/lib/data/model/app/backup.dart index 4ae89a6e..34d19172 100644 --- a/lib/data/model/app/backup.dart +++ b/lib/data/model/app/backup.dart @@ -60,11 +60,11 @@ class Backup { spis = Stores.server.fetch(), snippets = Stores.snippet.fetch(), keys = Stores.key.fetch(), - dockerHosts = Stores.docker.fetchAll(), + dockerHosts = Stores.docker.toJson(), settings = Stores.setting.toJson(); static Future backup() async { - final result = _diyEncrtpt(json.encode(Backup.loadFromStore())); + final result = _diyEncrypt(json.encode(Backup.loadFromStore())); final path = await Paths.bak; await File(path).writeAsString(result); return path; @@ -95,7 +95,7 @@ class Backup { : this.fromJson(json.decode(_diyDecrypt(raw))); } -String _diyEncrtpt(String raw) => json.encode( +String _diyEncrypt(String raw) => json.encode( raw.codeUnits.map((e) => e * 2 + 1).toList(growable: false), ); diff --git a/lib/data/model/pkg/manager.dart b/lib/data/model/pkg/manager.dart index fb31edc4..5f4389e9 100644 --- a/lib/data/model/pkg/manager.dart +++ b/lib/data/model/pkg/manager.dart @@ -68,7 +68,9 @@ enum PkgManager { list.removeWhere((element) => element.isEmpty); final endLine = list.lastIndexWhere( (element) => element.contains('Obsoleting Packages')); - list = list.sublist(0, endLine); + if (endLine != -1 && list.isNotEmpty) { + list = list.sublist(0, endLine); + } break; case PkgManager.apt: // avoid other outputs diff --git a/lib/data/res/logger.dart b/lib/data/res/logger.dart index 5cb20694..37f8a345 100644 --- a/lib/data/res/logger.dart +++ b/lib/data/res/logger.dart @@ -3,6 +3,7 @@ import 'package:logging/logging.dart'; class Loggers { const Loggers._(); + static final root = Logger('Root'); static final app = Logger('App'); static final parse = Logger('Parse'); } diff --git a/lib/data/res/store.dart b/lib/data/res/store.dart index 552e583e..2ad31488 100644 --- a/lib/data/res/store.dart +++ b/lib/data/res/store.dart @@ -1,6 +1,5 @@ import 'package:toolbox/core/persistant_store.dart'; import 'package:toolbox/data/store/docker.dart'; -import 'package:toolbox/data/store/first.dart'; import 'package:toolbox/data/store/history.dart'; import 'package:toolbox/data/store/private_key.dart'; import 'package:toolbox/data/store/server.dart'; @@ -17,7 +16,6 @@ class Stores { static final history = locator(); static final key = locator(); static final snippet = locator(); - static final first = locator(); static final List all = [ setting, @@ -26,6 +24,5 @@ class Stores { history, key, snippet, - first, ]; } diff --git a/lib/data/store/docker.dart b/lib/data/store/docker.dart index 80cb4c6f..6773c659 100644 --- a/lib/data/store/docker.dart +++ b/lib/data/store/docker.dart @@ -10,8 +10,4 @@ class DockerStore extends PersistentStore { void put(String id, String host) { box.put(id, host); } - - Map fetchAll() { - return box.toMap().cast(); - } } diff --git a/lib/data/store/first.dart b/lib/data/store/first.dart deleted file mode 100644 index 46c7541c..00000000 --- a/lib/data/store/first.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:toolbox/core/persistant_store.dart'; - -/// It stores whether is the first time of some. -class FirstStore extends PersistentStore { - FirstStore() : super('first'); - - /// Show tip of suspend - late final showSuspendTip = StoreProperty(box, 'showSuspendTip', true); -} diff --git a/lib/data/store/history.dart b/lib/data/store/history.dart index 96c259ae..26e401a8 100644 --- a/lib/data/store/history.dart +++ b/lib/data/store/history.dart @@ -1,12 +1,9 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:toolbox/core/persistant_store.dart'; -typedef _HistoryList = List; -typedef _HistoryMap = Map; - /// index from 0 -> n : latest -> oldest class _ListHistory { - final _HistoryList _history; + final List _history; final String _name; final Box _box; @@ -15,7 +12,7 @@ class _ListHistory { required String name, }) : _box = box, _name = name, - _history = box.get(name, defaultValue: [])!; + _history = box.get(name, defaultValue: [])!; void add(String path) { _history.remove(path); @@ -23,11 +20,11 @@ class _ListHistory { _box.put(_name, _history); } - _HistoryList get all => _history; + List get all => _history; } class _MapHistory { - final _HistoryMap _history; + final Map _history; final String _name; final Box _box; @@ -36,7 +33,7 @@ class _MapHistory { required String name, }) : _box = box, _name = name, - _history = box.get(name, defaultValue: {})!; + _history = box.get(name, defaultValue: {})!; void put(String id, String val) { _history[id] = val; diff --git a/lib/data/store/setting.dart b/lib/data/store/setting.dart index a146506a..2725e5cb 100644 --- a/lib/data/store/setting.dart +++ b/lib/data/store/setting.dart @@ -8,9 +8,6 @@ import '../res/default.dart'; class SettingStore extends PersistentStore { SettingStore() : super('setting'); - /// Convert all settings into json - Map toJson() => {for (var e in box.keys) e: box.get(e)}; - // ------BEGIN------ // // These settings are not displayed in the settings page @@ -220,6 +217,9 @@ class SettingStore extends PersistentStore { /// Open SFTP with last viewed path late final sftpOpenLastPath = StoreProperty(box, 'sftpOpenLastPath', true); + /// Show tip of suspend + late final showSuspendTip = StoreProperty(box, 'showSuspendTip', true); + // Never show these settings for users // // ------BEGIN------ diff --git a/lib/locator.dart b/lib/locator.dart index 6640816f..652a3397 100644 --- a/lib/locator.dart +++ b/lib/locator.dart @@ -1,5 +1,4 @@ import 'package:get_it/get_it.dart'; -import 'package:toolbox/data/store/first.dart'; import 'data/provider/app.dart'; import 'data/provider/debug.dart'; @@ -58,10 +57,6 @@ Future _setupLocatorForStores() async { final history = HistoryStore(); await history.init(); locator.registerSingleton(history); - - final first = FirstStore(); - await first.init(); - locator.registerSingleton(first); } Future setupLocator() async { diff --git a/lib/main.dart b/lib/main.dart index 2b0c3c6c..27c9acff 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,6 +8,7 @@ import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:toolbox/core/channel/bg_run.dart'; import 'package:toolbox/core/utils/platform/base.dart'; +import 'package:toolbox/data/res/logger.dart'; import 'package:toolbox/data/res/provider.dart'; import 'package:toolbox/data/res/store.dart'; @@ -59,7 +60,10 @@ void _runInZone(void Function() body) { runZonedGuarded( body, - (obj, trace) => Analysis.recordException(trace), + (obj, trace) { + Analysis.recordException(trace); + Loggers.root.warning(obj, trace); + }, zoneSpecification: zoneSpec, ); } @@ -94,7 +98,7 @@ void _setupProviders() { Future _initHive() async { await Hive.initFlutter(); - // 以 typeId 为顺序 + // Ordered by typeId Hive.registerAdapter(PrivateKeyInfoAdapter()); // 1 Hive.registerAdapter(SnippetAdapter()); // 2 Hive.registerAdapter(ServerPrivateInfoAdapter()); // 3 diff --git a/lib/view/page/backup.dart b/lib/view/page/backup.dart index 5dcc6943..ec1c9a0b 100644 --- a/lib/view/page/backup.dart +++ b/lib/view/page/backup.dart @@ -44,13 +44,13 @@ class BackupPage extends StatelessWidget { return ListView( padding: const EdgeInsets.all(17), children: [ - if (isMacOS || isIOS) _buildIcloudSync(context), - _buildManual(context), + if (isMacOS || isIOS) _buildIcloud(context), + _buildFile(context), ], ); } - Widget _buildManual(BuildContext context) { + Widget _buildFile(BuildContext context) { return CardX( ExpandTile( title: Text(l10n.files), @@ -75,7 +75,7 @@ class BackupPage extends StatelessWidget { ); } - Widget _buildIcloudSync(BuildContext context) { + Widget _buildIcloud(BuildContext context) { return CardX( ExpandTile( title: const Text('iCloud'), @@ -109,8 +109,8 @@ class BackupPage extends StatelessWidget { if (icloudLoading.value) { return UIs.centerSizedLoadingSmall; } - return SizedBox( - width: 120, + return ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 137), child: Row( children: [ TextButton( @@ -153,7 +153,6 @@ class BackupPage extends StatelessWidget { /// Issue #188 if (isWindows) { await Shares.text(await File(path).readAsString()); - } else { await Shares.files([path]); } @@ -195,10 +194,10 @@ class BackupPage extends StatelessWidget { ), TextButton( onPressed: () async { - backup.restore(); + await backup.restore(); + Pros.reload(); context.pop(); RebuildNodes.app.rebuild(); - Pros.reload(); }, child: Text(l10n.ok), ), diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index bdded850..edc3af45 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -247,12 +247,12 @@ class _ServerPageState extends State IconButton( onPressed: () => _askFor( func: () async { - if (Stores.first.showSuspendTip.fetch()) { + if (Stores.setting.showSuspendTip.fetch()) { await context.showRoundDialog( title: Text(l10n.attention), child: Text(l10n.suspendTip), ); - Stores.first.showSuspendTip.put(false); + Stores.setting.showSuspendTip.put(false); } srv.client?.execWithPwd( ShellFunc.suspend.exec, diff --git a/lib/view/page/storage/sftp.dart b/lib/view/page/storage/sftp.dart index 9786150d..fcb924a6 100644 --- a/lib/view/page/storage/sftp.dart +++ b/lib/view/page/storage/sftp.dart @@ -208,8 +208,10 @@ class _SftpPageState extends State with AfterLayoutMixin { if (!Stores.setting.recordHistory.fetch()) { return []; } - return Stores.history.sftpGoPath.all.where( - (element) => element.contains(val.text), + return List.from( + Stores.history.sftpGoPath.all.where( + (element) => element.contains(val.text), + ), ); }, fieldViewBuilder: (_, controller, node, __) {