Skip to content

Commit

Permalink
opt.: migrate to new fl_lib (#649)
Browse files Browse the repository at this point in the history
Fixes #648
  • Loading branch information
lollipopkit authored Dec 2, 2024
1 parent b882bae commit ddd32e8
Show file tree
Hide file tree
Showing 50 changed files with 10,858 additions and 225 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ec6ef69056f066e8b21a3391082f23b5ad2d37f8

COCOAPODS: 1.15.2
COCOAPODS: 1.16.2
18 changes: 9 additions & 9 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -818,7 +818,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -846,7 +846,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -880,7 +880,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
Expand Down Expand Up @@ -919,7 +919,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -955,7 +955,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -990,7 +990,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
Expand Down Expand Up @@ -1031,7 +1031,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand Down Expand Up @@ -1069,7 +1069,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.1104;
MARKETING_VERSION = 1.0.1110;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand Down
2 changes: 2 additions & 0 deletions l10n.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: l10n.dart
output-dir: lib/generated/l10n
synthetic-package: false
untranslated-messages-file: untranlated.json
5 changes: 3 additions & 2 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:dynamic_color/dynamic_color.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:fl_lib/l10n/gen_l10n/lib_l10n.dart';
import 'package:fl_lib/generated/l10n/lib_l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/data/res/build_data.dart';
import 'package:server_box/data/res/rebuild.dart';
import 'package:server_box/data/res/store.dart';
import 'package:server_box/generated/l10n/l10n.dart';
import 'package:server_box/view/page/home/home.dart';
import 'package:icons_plus/icons_plus.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/core/extension/context/locale.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_gen/gen_l10n/l10n_en.dart';
import 'package:server_box/generated/l10n/l10n.dart';
import 'package:server_box/generated/l10n/l10n_en.dart';

AppLocalizations l10n = AppLocalizationsEn();
25 changes: 11 additions & 14 deletions lib/core/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import 'dart:io';

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 bakSync = BakSyncer._();

final icloud = ICloud(containerId: 'iCloud.tech.lolli.serverbox');

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

@override
void init() {
Webdav.shared.prefix = 'serverbox/';
}

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

Expand All @@ -19,21 +25,12 @@ final class BakSyncer extends SyncIface<Backup> {
}

@override
Future<RemoteStorage?> get remoteStorage async {
if (isMacOS || isIOS) await icloud.init('iCloud.tech.lolli.serverbox');
final settings = NoBackupStore.instance;
await webdav.init(WebdavInitArgs(
url: settings.webdavUrl.fetch(),
user: settings.webdavUser.fetch(),
pwd: settings.webdavPwd.fetch(),
prefix: 'serverbox/',
));

final icloudEnabled = settings.icloudSync.fetch();
RemoteStorage? get remoteStorage {
final icloudEnabled = PrefProps.icloudSync.get();
if (icloudEnabled) return icloud;

final webdavEnabled = settings.webdavSync.fetch();
if (webdavEnabled) return webdav;
final webdavEnabled = PrefProps.webdavSync.get();
if (webdavEnabled) return Webdav.shared;

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum GenSSHClientStatus {
}

String getPrivateKey(String id) {
final pki = Stores.key.get(id);
final pki = Stores.key.fetchOne(id);
if (pki == null) {
throw SSHErr(
type: SSHErrType.noPrivateKey,
Expand Down
29 changes: 17 additions & 12 deletions lib/data/model/app/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,32 @@ class Backup implements Mergeable {

Map<String, dynamic> toJson() => _$BackupToJson(this);

Backup.loadFromStore()
: version = backupFormatVersion,
date = DateTime.now().toString().split('.').firstOrNull ?? '',
spis = Stores.server.fetch(),
snippets = Stores.snippet.fetch(),
keys = Stores.key.fetch(),
container = Stores.container.box.toJson(),
lastModTime = Stores.lastModTime,
history = Stores.history.box.toJson(),
settings = Stores.setting.box.toJson();
static Future<Backup> loadFromStore() async {
final lastModTime = Stores.lastModTime?.millisecondsSinceEpoch;
return Backup(
version: backupFormatVersion,
date: DateTime.now().toString().split('.').firstOrNull ?? '',
spis: Stores.server.fetch(),
snippets: Stores.snippet.fetch(),
keys: Stores.key.fetch(),
container: await Stores.container.getAllMap(),
lastModTime: lastModTime,
history: await Stores.history.getAllMap(),
settings: await Stores.setting.getAllMap(),
);
}

static Future<String> backup([String? name]) async {
final result = _diyEncrypt(json.encode(Backup.loadFromStore().toJson()));
final bak = await Backup.loadFromStore();
final result = _diyEncrypt(json.encode(bak.toJson()));
final path = Paths.doc.joinPath(name ?? Miscs.bakFileName);
await File(path).writeAsString(result);
return path;
}

@override
Future<void> merge({bool force = false}) async {
final curTime = Stores.lastModTime ?? 0;
final curTime = Stores.lastModTime?.millisecondsSinceEpoch ?? 0;
final bakTime = lastModTime ?? 0;
final shouldRestore = force || curTime < bakTime;
if (!shouldRestore) {
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/sftp/browser_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _AbsolutePath {
_path = newPath;
return;
}
_path = _path.joinPath(newPath, seperator: _sep);
_path = _path.joinPath(newPath, separator: _sep);
}

bool undo() {
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/sftp/req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SftpReq {
}
if (spi.jumpId != null) {
jumpSpi = Stores.server.box.get(spi.jumpId);
jumpPrivateKey = Stores.key.get(jumpSpi?.keyId)?.key;
jumpPrivateKey = Stores.key.fetchOne(jumpSpi?.keyId)?.key;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/data/res/build_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

abstract class BuildData {
static const String name = "ServerBox";
static const int build = 1104;
static const int script = 58;
static const int build = 1110;
static const int script = 59;
}
15 changes: 10 additions & 5 deletions lib/data/res/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract final class Stores {
static final history = HistoryStore.instance;

/// All stores that need backup
static final List<PersistentStore> _allBackup = [
static final List<HiveStore> _allBackup = [
SettingStore.instance,
ServerStore.instance,
ContainerStore.instance,
Expand All @@ -30,11 +30,16 @@ abstract final class Stores {
await NoBackupStore.instance.init();
}

static int? get lastModTime {
int? lastModTime = 0;
static DateTime? get lastModTime {
DateTime? lastModTime;
for (final store in _allBackup) {
final last = store.box.lastModified ?? 0;
if (last > (lastModTime ?? 0)) {
final last = store.lastUpdateTs;
if (last == null) {
continue;
}
if (lastModTime == null) {
lastModTime = last;
} else if (last.isAfter(lastModTime)) {
lastModTime = last;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/data/store/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:server_box/data/res/store.dart';

const _keyConfig = 'providerConfig';

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

static final instance = ContainerStore._();
Expand All @@ -15,7 +15,7 @@ class ContainerStore extends PersistentStore {

void put(String id, String host) {
box.put(id, host);
box.updateLastModified();
updateLastUpdateTs();
}

ContainerType getType([String id = '']) {
Expand All @@ -30,7 +30,7 @@ class ContainerStore extends PersistentStore {
}

ContainerType get defaultType {
if (Stores.setting.usePodman.fetch()) return ContainerType.podman;
if (Stores.setting.usePodman.get()) return ContainerType.podman;
return ContainerType.docker;
}

Expand All @@ -40,6 +40,6 @@ class ContainerStore extends PersistentStore {
} else {
box.put(_keyConfig + id, type.toString());
}
box.updateLastModified();
updateLastUpdateTs();
}
}
7 changes: 3 additions & 4 deletions lib/data/store/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class _ListHistory {
_history.remove(path);
_history.insert(0, path);
_box.put(_name, _history);
_box.updateLastModified();
}

List get all => _history;
Expand All @@ -39,13 +38,12 @@ class _MapHistory {
void put(String id, String val) {
_history[id] = val;
_box.put(_name, _history);
_box.updateLastModified();
}

String? fetch(String id) => _history[id];
}

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

static final instance = HistoryStore._();
Expand All @@ -58,5 +56,6 @@ class HistoryStore extends PersistentStore {
late final sshCmds = _ListHistory(box: box, name: 'sshCmds');

/// Notify users that this app will write script to server to works properly
late final writeScriptTipShown = property('writeScriptTipShown', false);
late final writeScriptTipShown =
propertyDefault('writeScriptTipShown', false);
}
Loading

0 comments on commit ddd32e8

Please sign in to comment.