comment _loadV1 and _loadV2

reduces usages of wallet fns that aren't ultimately used
This commit is contained in:
sneurlax 2024-09-05 16:35:29 -05:00
parent 4b2d10b9b2
commit ea69ab6c10

View file

@ -151,233 +151,233 @@ class Wallets {
return await _loadV3(prefs, mainDB);
}
Future<void> _loadV1(Prefs prefs, MainDB mainDB) async {
if (hasLoaded) {
return;
}
hasLoaded = true;
// clear out any wallet hive boxes where the wallet was deleted in previous app run
for (final walletId in DB.instance
.values<String>(boxName: DB.boxNameWalletsToDeleteOnStart)) {
await mainDB.isar.writeTxn(
() async => await mainDB.isar.walletInfo
.where()
.walletIdEqualTo(walletId)
.deleteAll(),
);
}
// clear list
await DB.instance
.deleteAll<String>(boxName: DB.boxNameWalletsToDeleteOnStart);
final walletInfoList = await mainDB.isar.walletInfo.where().findAll();
if (walletInfoList.isEmpty) {
return;
}
final List<Future<void>> walletInitFutures = [];
final List<({Wallet wallet, bool shouldAutoSync})> walletsToInitLinearly =
[];
final List<String> walletIdsToEnableAutoSync = [];
bool shouldAutoSyncAll = false;
switch (prefs.syncType) {
case SyncingType.currentWalletOnly:
// do nothing as this will be set when going into a wallet from the main screen
break;
case SyncingType.selectedWalletsAtStartup:
walletIdsToEnableAutoSync.addAll(prefs.walletIdsSyncOnStartup);
break;
case SyncingType.allWalletsOnStartup:
shouldAutoSyncAll = true;
break;
}
for (final walletInfo in walletInfoList) {
try {
final isVerified = await walletInfo.isMnemonicVerified(mainDB.isar);
Logging.instance.log(
"LOADING WALLET: ${walletInfo.name}:${walletInfo.walletId} "
"IS VERIFIED: $isVerified",
level: LogLevel.Info,
);
if (isVerified) {
// TODO: integrate this into the new wallets somehow?
// requires some thinking
// final txTracker =
// TransactionNotificationTracker(walletId: walletInfo.walletId);
final wallet = await Wallet.load(
walletId: walletInfo.walletId,
mainDB: mainDB,
secureStorageInterface: nodeService.secureStorageInterface,
nodeService: nodeService,
prefs: prefs,
);
final shouldSetAutoSync = shouldAutoSyncAll ||
walletIdsToEnableAutoSync.contains(walletInfo.walletId);
if (wallet is CwBasedInterface) {
// walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync));
} else {
walletInitFutures.add(
wallet.init().then((_) {
if (shouldSetAutoSync) {
wallet.shouldAutoSync = true;
}
}),
);
}
_wallets[wallet.walletId] = wallet;
} else {
// wallet creation was not completed by user so we remove it completely
await _deleteWallet(walletInfo.walletId);
// await walletsService.deleteWallet(walletInfo.name, false);
}
} catch (e, s) {
Logging.instance.log("$e $s", level: LogLevel.Fatal);
continue;
}
}
if (walletInitFutures.isNotEmpty && walletsToInitLinearly.isNotEmpty) {
await Future.wait([
_initLinearly(walletsToInitLinearly),
...walletInitFutures,
]);
} else if (walletInitFutures.isNotEmpty) {
await Future.wait(walletInitFutures);
} else if (walletsToInitLinearly.isNotEmpty) {
await _initLinearly(walletsToInitLinearly);
}
}
/// should be fastest but big ui performance hit
Future<void> _loadV2(Prefs prefs, MainDB mainDB) async {
if (hasLoaded) {
return;
}
hasLoaded = true;
// clear out any wallet hive boxes where the wallet was deleted in previous app run
for (final walletId in DB.instance
.values<String>(boxName: DB.boxNameWalletsToDeleteOnStart)) {
await mainDB.isar.writeTxn(
() async => await mainDB.isar.walletInfo
.where()
.walletIdEqualTo(walletId)
.deleteAll(),
);
}
// clear list
await DB.instance
.deleteAll<String>(boxName: DB.boxNameWalletsToDeleteOnStart);
final walletInfoList = await mainDB.isar.walletInfo.where().findAll();
if (walletInfoList.isEmpty) {
return;
}
final List<Future<String>> walletIDInitFutures = [];
final List<Future<void>> deleteFutures = [];
final List<({Wallet wallet, bool shouldAutoSync})> walletsToInitLinearly =
[];
final List<String> walletIdsToEnableAutoSync = [];
bool shouldAutoSyncAll = false;
switch (prefs.syncType) {
case SyncingType.currentWalletOnly:
// do nothing as this will be set when going into a wallet from the main screen
break;
case SyncingType.selectedWalletsAtStartup:
walletIdsToEnableAutoSync.addAll(prefs.walletIdsSyncOnStartup);
break;
case SyncingType.allWalletsOnStartup:
shouldAutoSyncAll = true;
break;
}
for (final walletInfo in walletInfoList) {
try {
final isVerified = await walletInfo.isMnemonicVerified(mainDB.isar);
Logging.instance.log(
"LOADING WALLET: ${walletInfo.name}:${walletInfo.walletId} "
"IS VERIFIED: $isVerified",
level: LogLevel.Info,
);
if (isVerified) {
// TODO: integrate this into the new wallets somehow?
// requires some thinking
// final txTracker =
// TransactionNotificationTracker(walletId: walletInfo.walletId);
final walletIdCompleter = Completer<String>();
walletIDInitFutures.add(walletIdCompleter.future);
await Wallet.load(
walletId: walletInfo.walletId,
mainDB: mainDB,
secureStorageInterface: nodeService.secureStorageInterface,
nodeService: nodeService,
prefs: prefs,
).then((wallet) {
if (wallet is CwBasedInterface) {
// walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync));
walletIdCompleter.complete("dummy_ignore");
} else {
walletIdCompleter.complete(wallet.walletId);
}
_wallets[wallet.walletId] = wallet;
});
} else {
// wallet creation was not completed by user so we remove it completely
deleteFutures.add(_deleteWallet(walletInfo.walletId));
}
} catch (e, s) {
Logging.instance.log("$e $s", level: LogLevel.Fatal);
continue;
}
}
final asyncWalletIds = await Future.wait(walletIDInitFutures);
asyncWalletIds.removeWhere((e) => e == "dummy_ignore");
final List<Future<void>> walletInitFutures = asyncWalletIds
.map(
(id) => _wallets[id]!.init().then(
(_) {
if (shouldAutoSyncAll || walletIdsToEnableAutoSync.contains(id)) {
_wallets[id]!.shouldAutoSync = true;
}
},
),
)
.toList();
if (walletInitFutures.isNotEmpty && walletsToInitLinearly.isNotEmpty) {
unawaited(
Future.wait([
_initLinearly(walletsToInitLinearly),
...walletInitFutures,
]),
);
} else if (walletInitFutures.isNotEmpty) {
unawaited(Future.wait(walletInitFutures));
} else if (walletsToInitLinearly.isNotEmpty) {
unawaited(_initLinearly(walletsToInitLinearly));
}
// finally await any deletions that haven't completed yet
await Future.wait(deleteFutures);
}
// Future<void> _loadV1(Prefs prefs, MainDB mainDB) async {
// if (hasLoaded) {
// return;
// }
// hasLoaded = true;
//
// // clear out any wallet hive boxes where the wallet was deleted in previous app run
// for (final walletId in DB.instance
// .values<String>(boxName: DB.boxNameWalletsToDeleteOnStart)) {
// await mainDB.isar.writeTxn(
// () async => await mainDB.isar.walletInfo
// .where()
// .walletIdEqualTo(walletId)
// .deleteAll(),
// );
// }
// // clear list
// await DB.instance
// .deleteAll<String>(boxName: DB.boxNameWalletsToDeleteOnStart);
//
// final walletInfoList = await mainDB.isar.walletInfo.where().findAll();
// if (walletInfoList.isEmpty) {
// return;
// }
//
// final List<Future<void>> walletInitFutures = [];
// final List<({Wallet wallet, bool shouldAutoSync})> walletsToInitLinearly =
// [];
//
// final List<String> walletIdsToEnableAutoSync = [];
// bool shouldAutoSyncAll = false;
// switch (prefs.syncType) {
// case SyncingType.currentWalletOnly:
// // do nothing as this will be set when going into a wallet from the main screen
// break;
// case SyncingType.selectedWalletsAtStartup:
// walletIdsToEnableAutoSync.addAll(prefs.walletIdsSyncOnStartup);
// break;
// case SyncingType.allWalletsOnStartup:
// shouldAutoSyncAll = true;
// break;
// }
//
// for (final walletInfo in walletInfoList) {
// try {
// final isVerified = await walletInfo.isMnemonicVerified(mainDB.isar);
// Logging.instance.log(
// "LOADING WALLET: ${walletInfo.name}:${walletInfo.walletId} "
// "IS VERIFIED: $isVerified",
// level: LogLevel.Info,
// );
//
// if (isVerified) {
// // TODO: integrate this into the new wallets somehow?
// // requires some thinking
// // final txTracker =
// // TransactionNotificationTracker(walletId: walletInfo.walletId);
//
// final wallet = await Wallet.load(
// walletId: walletInfo.walletId,
// mainDB: mainDB,
// secureStorageInterface: nodeService.secureStorageInterface,
// nodeService: nodeService,
// prefs: prefs,
// );
//
// final shouldSetAutoSync = shouldAutoSyncAll ||
// walletIdsToEnableAutoSync.contains(walletInfo.walletId);
//
// if (wallet is CwBasedInterface) {
// // walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync));
// } else {
// walletInitFutures.add(
// wallet.init().then((_) {
// if (shouldSetAutoSync) {
// wallet.shouldAutoSync = true;
// }
// }),
// );
// }
//
// _wallets[wallet.walletId] = wallet;
// } else {
// // wallet creation was not completed by user so we remove it completely
// await _deleteWallet(walletInfo.walletId);
// // await walletsService.deleteWallet(walletInfo.name, false);
// }
// } catch (e, s) {
// Logging.instance.log("$e $s", level: LogLevel.Fatal);
// continue;
// }
// }
//
// if (walletInitFutures.isNotEmpty && walletsToInitLinearly.isNotEmpty) {
// await Future.wait([
// _initLinearly(walletsToInitLinearly),
// ...walletInitFutures,
// ]);
// } else if (walletInitFutures.isNotEmpty) {
// await Future.wait(walletInitFutures);
// } else if (walletsToInitLinearly.isNotEmpty) {
// await _initLinearly(walletsToInitLinearly);
// }
// }
//
// /// should be fastest but big ui performance hit
// Future<void> _loadV2(Prefs prefs, MainDB mainDB) async {
// if (hasLoaded) {
// return;
// }
// hasLoaded = true;
//
// // clear out any wallet hive boxes where the wallet was deleted in previous app run
// for (final walletId in DB.instance
// .values<String>(boxName: DB.boxNameWalletsToDeleteOnStart)) {
// await mainDB.isar.writeTxn(
// () async => await mainDB.isar.walletInfo
// .where()
// .walletIdEqualTo(walletId)
// .deleteAll(),
// );
// }
// // clear list
// await DB.instance
// .deleteAll<String>(boxName: DB.boxNameWalletsToDeleteOnStart);
//
// final walletInfoList = await mainDB.isar.walletInfo.where().findAll();
// if (walletInfoList.isEmpty) {
// return;
// }
//
// final List<Future<String>> walletIDInitFutures = [];
// final List<Future<void>> deleteFutures = [];
// final List<({Wallet wallet, bool shouldAutoSync})> walletsToInitLinearly =
// [];
//
// final List<String> walletIdsToEnableAutoSync = [];
// bool shouldAutoSyncAll = false;
// switch (prefs.syncType) {
// case SyncingType.currentWalletOnly:
// // do nothing as this will be set when going into a wallet from the main screen
// break;
// case SyncingType.selectedWalletsAtStartup:
// walletIdsToEnableAutoSync.addAll(prefs.walletIdsSyncOnStartup);
// break;
// case SyncingType.allWalletsOnStartup:
// shouldAutoSyncAll = true;
// break;
// }
//
// for (final walletInfo in walletInfoList) {
// try {
// final isVerified = await walletInfo.isMnemonicVerified(mainDB.isar);
// Logging.instance.log(
// "LOADING WALLET: ${walletInfo.name}:${walletInfo.walletId} "
// "IS VERIFIED: $isVerified",
// level: LogLevel.Info,
// );
//
// if (isVerified) {
// // TODO: integrate this into the new wallets somehow?
// // requires some thinking
// // final txTracker =
// // TransactionNotificationTracker(walletId: walletInfo.walletId);
//
// final walletIdCompleter = Completer<String>();
//
// walletIDInitFutures.add(walletIdCompleter.future);
//
// await Wallet.load(
// walletId: walletInfo.walletId,
// mainDB: mainDB,
// secureStorageInterface: nodeService.secureStorageInterface,
// nodeService: nodeService,
// prefs: prefs,
// ).then((wallet) {
// if (wallet is CwBasedInterface) {
// // walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync));
//
// walletIdCompleter.complete("dummy_ignore");
// } else {
// walletIdCompleter.complete(wallet.walletId);
// }
//
// _wallets[wallet.walletId] = wallet;
// });
// } else {
// // wallet creation was not completed by user so we remove it completely
// deleteFutures.add(_deleteWallet(walletInfo.walletId));
// }
// } catch (e, s) {
// Logging.instance.log("$e $s", level: LogLevel.Fatal);
// continue;
// }
// }
//
// final asyncWalletIds = await Future.wait(walletIDInitFutures);
// asyncWalletIds.removeWhere((e) => e == "dummy_ignore");
//
// final List<Future<void>> walletInitFutures = asyncWalletIds
// .map(
// (id) => _wallets[id]!.init().then(
// (_) {
// if (shouldAutoSyncAll || walletIdsToEnableAutoSync.contains(id)) {
// _wallets[id]!.shouldAutoSync = true;
// }
// },
// ),
// )
// .toList();
//
// if (walletInitFutures.isNotEmpty && walletsToInitLinearly.isNotEmpty) {
// unawaited(
// Future.wait([
// _initLinearly(walletsToInitLinearly),
// ...walletInitFutures,
// ]),
// );
// } else if (walletInitFutures.isNotEmpty) {
// unawaited(Future.wait(walletInitFutures));
// } else if (walletsToInitLinearly.isNotEmpty) {
// unawaited(_initLinearly(walletsToInitLinearly));
// }
//
// // finally await any deletions that haven't completed yet
// await Future.wait(deleteFutures);
// }
/// should be best performance
Future<void> _loadV3(Prefs prefs, MainDB mainDB) async {