Skip to content

Commit

Permalink
refactor: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Aug 15, 2024
1 parent 95b8eef commit fbc4886
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 19 deletions.
18 changes: 6 additions & 12 deletions lib/data/provider/base.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import 'dart:async';

import 'package:flutter/foundation.dart';

abstract class Provider<T> {
const Provider();

FutureOr<void> _init() {
/// (Re)Load data from store / network / etc.
@mustCallSuper
FutureOr<void> load() {
all.add(this);
return load();
debugPrint('$runtimeType added');
}

/// (Re)Load data from store / network / etc.
FutureOr<void> load();

static final all = <Provider>[];

static Future<void> init() {
if (all.isNotEmpty) return Future.value();

// [FutureOr<void>] isn't returnable by [Future.wait], so add [await]
return Future.wait(all.map((e) async => await e._init()));
}

static Future<void> reload() {
return Future.wait(all.map((e) async => await e.load()));
}
Expand Down
2 changes: 2 additions & 0 deletions lib/data/provider/private_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import 'package:server_box/data/res/store.dart';

class PrivateKeyProvider extends Provider {
const PrivateKeyProvider._();
static const instance = PrivateKeyProvider._();

static final pkis = <PrivateKeyInfo>[].vn;

@override
void load() {
super.load();
pkis.value = Stores.key.fetch();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/data/provider/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import 'package:server_box/data/model/server/try_limiter.dart';
import 'package:server_box/data/res/status.dart';

class ServerProvider extends Provider {
const ServerProvider._() : super();
const ServerProvider._();
static const instance = ServerProvider._();

static final Map<String, VNode<Server>> servers = {};
static final serverOrder = <String>[].vn;
Expand All @@ -35,6 +36,7 @@ class ServerProvider extends Provider {

@override
Future<void> load() async {
super.load();
// #147
// Clear all servers because of restarting app will cause duplicate servers
final oldServers = Map<String, VNode<Server>>.from(servers);
Expand Down
4 changes: 1 addition & 3 deletions lib/data/provider/sftp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:server_box/data/provider/base.dart';

class SftpProvider extends Provider {
const SftpProvider._();
static const instance = SftpProvider._();

static final status = <SftpReqStatus>[].vn;

Expand Down Expand Up @@ -38,7 +39,4 @@ class SftpProvider extends Provider {
status.value.removeAt(idx);
status.notify();
}

@override
FutureOr<void> load() {}
}
2 changes: 2 additions & 0 deletions lib/data/provider/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import 'package:server_box/data/res/store.dart';

class SnippetProvider extends Provider {
const SnippetProvider._();
static const instance = SnippetProvider._();

static final snippets = <Snippet>[].vn;
static final tags = <String>{}.vn;

@override
void load() {
super.load();
final snippets_ = Stores.snippet.fetch();
final order = Stores.setting.snippetOrder.fetch();
if (order.isNotEmpty) {
Expand Down
11 changes: 9 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import 'package:server_box/data/model/server/server_private_info.dart';
import 'package:server_box/data/model/server/snippet.dart';
import 'package:server_box/data/model/server/wol_cfg.dart';
import 'package:server_box/data/model/ssh/virtual_key.dart';
import 'package:server_box/data/provider/base.dart';
import 'package:server_box/data/provider/private_key.dart';
import 'package:server_box/data/provider/server.dart';
import 'package:server_box/data/provider/sftp.dart';
import 'package:server_box/data/provider/snippet.dart';
import 'package:server_box/data/res/build_data.dart';
import 'package:server_box/data/res/misc.dart';
import 'package:server_box/data/res/store.dart';
Expand Down Expand Up @@ -83,7 +86,11 @@ Future<void> _initData() async {
await PrefStore.init(); // Call this before accessing any store
await Stores.init();

Provider.init();
// DO NOT change the order of these providers.
PrivateKeyProvider.instance.load();
SnippetProvider.instance.load();
ServerProvider.instance.load();
SftpProvider.instance.load();

if (Stores.setting.betaTest.fetch()) AppUpdate.chan = AppUpdateChan.beta;
}
Expand Down
1 change: 1 addition & 0 deletions lib/view/page/server/tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ ${ss.err?.message ?? 'null'}

@override
Future<void> afterFirstLayout(BuildContext context) async {
ServerProvider.refresh();
ServerProvider.startAutoRefresh();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/view/page/storage/sftp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
delegate: SearchPage(
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 3),
future: (q) => find(q).toList(),
builder: (ctx, e) => _buildItem(e, beforeTap: () => ctx.pop()),
builder: (ctx, e) => _buildItem(e, beforeTap: ctx.pop),
),
);
},
Expand Down

0 comments on commit fbc4886

Please sign in to comment.