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