Skip to content

Commit

Permalink
opt.: sync immediately after changes (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit authored Sep 14, 2024
1 parent d611fdc commit 90b88ed
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 124 deletions.
8 changes: 4 additions & 4 deletions lib/core/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/data/model/app/backup.dart';
import 'package:server_box/data/store/no_backup.dart';

const sync = Sync._();
const bakSync = BakSyncer._();

final class Sync extends SyncCfg {
const Sync._() : super();
final class BakSyncer extends SyncIface<Backup> {
const BakSyncer._() : super();

@override
Future<void> saveToFile() => Backup.backup();

@override
Future<Mergeable> fromFile(String path) async {
Future<Backup> fromFile(String path) async {
final content = await File(path).readAsString();
return Backup.fromJsonString(content);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/data/provider/private_key.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/core/sync.dart';
import 'package:server_box/data/model/server/private_key_info.dart';
import 'package:server_box/data/res/store.dart';

Expand All @@ -18,12 +19,14 @@ class PrivateKeyProvider extends Provider {
pkis.value.add(info);
pkis.notify();
Stores.key.put(info);
bakSync.sync(milliDelay: 1000);
}

static void delete(PrivateKeyInfo info) {
pkis.value.removeWhere((e) => e.id == info.id);
pkis.notify();
Stores.key.delete(info);
bakSync.sync(milliDelay: 1000);
}

static void update(PrivateKeyInfo old, PrivateKeyInfo newInfo) {
Expand All @@ -37,5 +40,6 @@ class PrivateKeyProvider extends Provider {
Stores.key.put(newInfo);
}
pkis.notify();
bakSync.sync(milliDelay: 1000);
}
}
4 changes: 4 additions & 0 deletions lib/data/provider/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:computer/computer.dart';
import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/core/extension/ssh_client.dart';
import 'package:server_box/core/sync.dart';
import 'package:server_box/core/utils/ssh_auth.dart';
import 'package:server_box/data/model/app/error.dart';
import 'package:server_box/data/model/app/shell_func.dart';
Expand Down Expand Up @@ -190,6 +191,7 @@ class ServerProvider extends Provider {
Stores.setting.serverOrder.put(serverOrder.value);
_updateTags();
refresh(spi: spi);
bakSync.sync(milliDelay: 1000);
}

static void delServer(String id) {
Expand All @@ -199,6 +201,7 @@ class ServerProvider extends Provider {
Stores.setting.serverOrder.put(serverOrder.value);
Stores.server.delete(id);
_updateTags();
bakSync.sync(milliDelay: 1000);
}

static void deleteAll() {
Expand Down Expand Up @@ -234,6 +237,7 @@ class ServerProvider extends Provider {
}
}
_updateTags();
bakSync.sync();
}

static void _setServerState(VNode<Server> s, ServerConn ss) {
Expand Down
5 changes: 5 additions & 0 deletions lib/data/provider/snippet.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/core/sync.dart';
import 'package:server_box/data/model/server/snippet.dart';
import 'package:server_box/data/res/store.dart';

Expand Down Expand Up @@ -44,13 +45,15 @@ class SnippetProvider extends Provider {
snippets.notify();
Stores.snippet.put(snippet);
_updateTags();
bakSync.sync(milliDelay: 1000);
}

static void del(Snippet snippet) {
snippets.value.remove(snippet);
snippets.notify();
Stores.snippet.delete(snippet);
_updateTags();
bakSync.sync(milliDelay: 1000);
}

static void update(Snippet old, Snippet newOne) {
Expand All @@ -60,6 +63,7 @@ class SnippetProvider extends Provider {
Stores.snippet.delete(old);
Stores.snippet.put(newOne);
_updateTags();
bakSync.sync(milliDelay: 1000);
}

static void renameTag(String old, String newOne) {
Expand All @@ -71,5 +75,6 @@ class SnippetProvider extends Provider {
}
}
_updateTags();
bakSync.sync(milliDelay: 1000);
}
}
33 changes: 18 additions & 15 deletions lib/data/res/store.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/data/store/container.dart';
import 'package:server_box/data/store/history.dart';
import 'package:server_box/data/store/no_backup.dart';
import 'package:server_box/data/store/private_key.dart';
import 'package:server_box/data/store/server.dart';
import 'package:server_box/data/store/setting.dart';
import 'package:server_box/data/store/snippet.dart';

abstract final class Stores {
static final setting = SettingStore();
static final server = ServerStore();
static final container = ContainerStore();
static final history = HistoryStore();
static final key = PrivateKeyStore();
static final snippet = SnippetStore();
static final setting = SettingStore.instance;
static final server = ServerStore.instance;
static final container = ContainerStore.instance;
static final key = PrivateKeyStore.instance;
static final snippet = SnippetStore.instance;
static final history = HistoryStore.instance;

static final List<PersistentStore> all = [
setting,
server,
container,
history,
key,
snippet,
/// All stores that need backup
static final List<PersistentStore> _allBackup = [
SettingStore.instance,
ServerStore.instance,
ContainerStore.instance,
PrivateKeyStore.instance,
SnippetStore.instance,
HistoryStore.instance,
];

static Future<void> init() async {
await Future.wait(all.map((store) => store.init()));
await Future.wait(_allBackup.map((store) => store.init()));
await NoBackupStore.instance.init();
}

static int? get lastModTime {
int? lastModTime = 0;
for (final store in all) {
for (final store in _allBackup) {
final last = store.box.lastModified ?? 0;
if (last > (lastModTime ?? 0)) {
lastModTime = last;
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:server_box/data/res/store.dart';
const _keyConfig = 'providerConfig';

class ContainerStore extends PersistentStore {
ContainerStore() : super('docker');
ContainerStore._() : super('docker');

static final instance = ContainerStore._();

String? fetch(String? id) {
return box.get(id);
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class _MapHistory {
}

class HistoryStore extends PersistentStore {
HistoryStore() : super('history');
HistoryStore._() : super('history');

static final instance = HistoryStore._();

/// Paths that user has visited by 'Locate' button
late final sftpGoPath = _ListHistory(box: box, name: 'sftpPath');
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/private_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/data/model/server/private_key_info.dart';

class PrivateKeyStore extends PersistentStore {
PrivateKeyStore() : super('key');
PrivateKeyStore._() : super('key');

static final instance = PrivateKeyStore._();

void put(PrivateKeyInfo info) {
box.put(info.id, info);
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/data/model/server/server_private_info.dart';

class ServerStore extends PersistentStore {
ServerStore() : super('server');
ServerStore._() : super('server');

static final instance = ServerStore._();

void put(Spi info) {
box.put(info.id, info);
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'package:server_box/data/model/app/net_view.dart';
import 'package:server_box/data/res/default.dart';

class SettingStore extends PersistentStore {
SettingStore() : super('setting');
SettingStore._() : super('setting');

static final instance = SettingStore._();

// ------BEGIN------
//
Expand Down
4 changes: 3 additions & 1 deletion lib/data/store/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/data/model/server/snippet.dart';

class SnippetStore extends PersistentStore {
SnippetStore() : super('snippet');
SnippetStore._() : super('snippet');

static final instance = SnippetStore._();

void put(Snippet snippet) {
box.put(snippet.name, snippet);
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void _doPlatformRelated() async {
// Plus 1 to avoid 0.
Computer.shared.turnOn(workersCount: (serversCount / 3).round() + 1);

sync.sync();
bakSync.sync();
}

// It may contains some async heavy funcs.
Expand Down
4 changes: 2 additions & 2 deletions lib/view/page/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class BackupPage extends StatelessWidget {
callback: (val) async {
if (val) {
icloudLoading.value = true;
await sync.sync(rs: icloud);
await bakSync.sync(rs: icloud);
icloudLoading.value = false;
}
},
Expand Down Expand Up @@ -147,7 +147,7 @@ class BackupPage extends StatelessWidget {
callback: (val) async {
if (val) {
webdavLoading.value = true;
await sync.sync(rs: webdav);
await bakSync.sync(rs: webdav);
webdavLoading.value = false;
}
},
Expand Down
2 changes: 1 addition & 1 deletion lib/view/page/home/appbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class _AppBar extends CustomAppBar {
@override
Widget build(BuildContext context) {
final placeholder = SizedBox(
height: CustomAppBar.barHeight ?? 0 + MediaQuery.of(context).padding.top,
height: CustomAppBar.sysStatusBarHeight ?? 0 + MediaQuery.of(context).padding.top,
);
return selectIndex.listenVal(
(idx) {
Expand Down
4 changes: 2 additions & 2 deletions lib/view/page/ssh/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class SSHPageState extends State<SSHPage>
_media.padding.top,
child: Padding(
padding: EdgeInsets.only(
top: widget.notFromTab ? CustomAppBar.barHeight ?? 0 : 0,
top: widget.notFromTab ? CustomAppBar.sysStatusBarHeight ?? 0 : 0,
left: _horizonPadding,
right: _horizonPadding,
),
Expand All @@ -155,7 +155,7 @@ class SSHPageState extends State<SSHPage>
showToolbar: isMobile,
viewOffset: Offset(
2 * _horizonPadding,
CustomAppBar.barHeight ?? _media.padding.top,
CustomAppBar.sysStatusBarHeight ?? _media.padding.top,
),
hideScrollBar: false,
focusNode: widget.focusNode,
Expand Down
Loading

0 comments on commit 90b88ed

Please sign in to comment.