From 8b8123df79e84a1d0a76ee7614a1acdcfcf9409a Mon Sep 17 00:00:00 2001 From: Hanh Date: Mon, 22 Aug 2022 14:37:29 +0800 Subject: [PATCH] Remove network initialization from startup page --- lib/account_manager.dart | 7 ++--- lib/accounts.dart | 62 ++++++++++++++++++++++++++++------------ lib/backup.dart | 4 +++ lib/home.dart | 10 ++++++- lib/main.dart | 8 ------ lib/restore.dart | 1 + native/zcash-sync | 2 +- pubspec.yaml | 2 +- 8 files changed, 62 insertions(+), 34 deletions(-) diff --git a/lib/account_manager.dart b/lib/account_manager.dart index d2f5391..377caf2 100644 --- a/lib/account_manager.dart +++ b/lib/account_manager.dart @@ -163,14 +163,13 @@ class AccountManagerState extends State { void _onDismissed(int index, Account account) async { await accounts.delete(account.coin, account.id); - accounts.refresh(); - final id = await active.refreshId(active.coin); - if (id == 0) - active.reset(); + await accounts.refresh(); + await active.checkAndUpdate(); } _selectAccount(Account account) async { await active.setActiveAccount(account.coin, account.id); + await active.refreshAccount(); if (syncStatus.accountRestored) { syncStatus.setAccountRestored(false); final height = await rescanDialog(context); diff --git a/lib/accounts.dart b/lib/accounts.dart index 9e2b2bc..2d20a9a 100644 --- a/lib/accounts.dart +++ b/lib/accounts.dart @@ -55,8 +55,7 @@ abstract class _AccountManager2 with Store { @action Future delete(int coin, int id) async { WarpApi.deleteAccount(coin, id); - if (active.coin == coin && active.id == id) - active.reset(); + await active.checkAndUpdate(); } @action @@ -157,40 +156,62 @@ abstract class _ActiveAccount with Store { @observable bool pnlDesc = false; + AccountId toId() { return AccountId(coin, id); } + @action Future restore() async { final prefs = await SharedPreferences.getInstance(); final coin = prefs.getInt('coin') ?? 0; - final id = prefs.getInt('account') ?? 0; - await setActiveAccount(coin, id); + var id = prefs.getInt('account') ?? 0; + active.coin = coin; + active.id = id; + await checkAndUpdate(); + await refreshAccount(); } - Future reset() async { - for (var coin_data in settings.coins) { - if (await refreshId(coin_data.coin) != 0) - return; + Future checkAndUpdate() async { + final aid = await getAvailableId(active.toId()); + if (aid == null) { + await setActiveAccount(0, 0); + } + else if (aid != active.toId()) { + await setActiveAccount(aid.coin, aid.id); + await refreshAccount(); } - await setActiveAccount(0, 0); } - // check that the active account still exists + Future getAvailableId(AccountId aid) async { + final nid = await getAvailableIdForCoin(aid.coin, aid.id); + if (nid != null) return nid; + for (var coin_data in settings.coins) { + // look for an account in any other coin + if (coin_data.coin != coin) { + final nid = await getAvailableIdForCoin(coin_data.coin, coin_data.active); + if (nid != null) + return nid; + } + } + // We have no accounts + return null; + } + + // check that the account still exists // if not, pick any account - // if there are none, return null - Future refreshId(int coin) async { + // if there are none, return 0 + Future getAvailableIdForCoin(int coin, int id) async { coinDef = settings.coins[coin].def; final db = coinDef.db; final List res1 = await db.rawQuery( - "SELECT 1 FROM accounts WHERE id_account = ?1", [active.id]); + "SELECT 1 FROM accounts WHERE id_account = ?1", [id]); if (res1.isNotEmpty) - return active.id; + return AccountId(coin, id); final List res2 = await db.rawQuery( "SELECT id_account FROM accounts", []); if (res2.isNotEmpty) { final id = res2[0]['id_account']; - await setActiveAccount(coin, id); - return id; + return AccountId(coin, id); } - return 0; + return null; } @action @@ -204,7 +225,11 @@ abstract class _ActiveAccount with Store { final prefs = await SharedPreferences.getInstance(); prefs.setInt('coin', coin); prefs.setInt('account', id); + WarpApi.setActiveAccount(coin, id); + } + @action + Future refreshAccount() async { coinDef = settings.coins[coin].def; final db = coinDef.db; @@ -222,10 +247,9 @@ abstract class _ActiveAccount with Store { showTAddr = false; balances = Balances.zero; - WarpApi.setActiveAccount(coin, id); await update(); - await priceStore.updateChart(); + Future.microtask(priceStore.updateChart); } @action diff --git a/lib/backup.dart b/lib/backup.dart index e774571..891ce4e 100644 --- a/lib/backup.dart +++ b/lib/backup.dart @@ -10,6 +10,10 @@ class AccountId { final int coin; final int id; AccountId(this.coin, this.id); + + bool operator ==(covariant AccountId other) { + return coin == other.coin && id == other.id; + } } class BackupPage extends StatefulWidget { diff --git a/lib/home.dart b/lib/home.dart index d147474..e5a9633 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -38,7 +38,15 @@ class HomeState extends State { @override void initState() { super.initState(); - if (active.id == 0) return; + if (active.id == 0) { + Future.microtask(() async { + await syncStatus.update(); + for (var c in settings.coins) { + syncStatus.markAsSynced(c.coin); + } + }); + return; + } Future.microtask(() async { await syncStatus.update(); diff --git a/lib/main.dart b/lib/main.dart index ca2b5cc..b02ff04 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -344,8 +344,6 @@ class ZWalletAppState extends State { await accounts.refresh(); _setProgress(0.7, 'Restoring Active Account'); await active.restore(); - _setProgress(0.8, 'Checking Sync Status'); - await syncStatus.update(); if (isMobile()) { _setProgress(0.9, 'Setting Dashboard Shortcuts'); @@ -378,12 +376,6 @@ class ZWalletAppState extends State { } await active.restore(); - if (active.id == 0) { - for (var c in settings.coins) { - syncStatus.markAsSynced(c.coin); - } - await syncStatus.update(); - } return true; } catch (e) { diff --git a/lib/restore.dart b/lib/restore.dart index d2ac5f7..2a83724 100644 --- a/lib/restore.dart +++ b/lib/restore.dart @@ -188,6 +188,7 @@ class _AddAccountPageState extends State { } else { await accounts.refresh(); await active.setActiveAccount(_coin, account); + await active.refreshAccount(); final nav = Navigator.of(context); if (_keyController.text != "") { syncStatus.setAccountRestored(true); diff --git a/native/zcash-sync b/native/zcash-sync index de506ba..c51cb51 160000 --- a/native/zcash-sync +++ b/native/zcash-sync @@ -1 +1 @@ -Subproject commit de506ba0743b0cb489b5bcc3d5cfaf171f077c06 +Subproject commit c51cb5181806cd5600c1fe59219b5205b31c9538 diff --git a/pubspec.yaml b/pubspec.yaml index ead9314..cd1d2aa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.2.12+270 +version: 1.2.12+272 environment: sdk: ">=2.12.0 <3.0.0"