Skip to content

Commit

Permalink
option to rescan on lelantus scanning enable
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-CStack committed Jan 31, 2025
1 parent 75f6e65 commit 90e4211
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@
*
*/

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import '../../../../providers/db/main_db_provider.dart';
import '../../../../providers/global/wallets_provider.dart';
import '../../../../themes/stack_colors.dart';
import '../../../../utilities/logger.dart';
import '../../../../utilities/show_loading.dart';
import '../../../../utilities/text_styles.dart';
import '../../../../utilities/util.dart';
import '../../../../wallets/isar/models/wallet_info.dart';
import '../../../../wallets/isar/providers/wallet_info_provider.dart';
import '../../../../widgets/background.dart';
import '../../../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../../../widgets/custom_buttons/draggable_switch_button.dart';
import '../../../../widgets/desktop/primary_button.dart';
import '../../../../widgets/desktop/secondary_button.dart';
import '../../../../widgets/stack_dialog.dart';

class LelantusSettingsView extends ConsumerStatefulWidget {
const LelantusSettingsView({
Expand Down Expand Up @@ -50,12 +60,91 @@ class _LelantusSettingsViewState extends ConsumerState<LelantusSettingsView> {
},
isar: ref.read(mainDBProvider).isar,
);
if (newValue) {
await _doRescanMaybe();
}
} finally {
// ensure _isUpdatingLelantusScanning is set to false no matter what
_isUpdatingLelantusScanning = false;
}
}

Future<void> _doRescanMaybe() async {
final shouldRescan = await showDialog<bool>(
context: context,
builder: (context) {
return StackDialog(
title: "Rescan may be required",
message: "A blockchain rescan may be required to fully recover all "
"lelantus history. This may take a while.",
leftButton: SecondaryButton(
label: "Rescan now",
onPressed: () {
Navigator.of(context).pop(true);
},
),
rightButton: PrimaryButton(
label: "Later",
onPressed: () => Navigator.of(context).pop(false),
),
);
},
);

if (mounted && shouldRescan == true) {
try {
if (!Platform.isLinux) await WakelockPlus.enable();

Exception? e;
if (mounted) {
await showLoading(
whileFuture: ref.read(pWallets).getWallet(widget.walletId).recover(
isRescan: true,
),
context: context,
message: "Rescanning blockchain",
subMessage: "This may take a while."
"\nPlease do not exit this screen.",
rootNavigator: Util.isDesktop,
onException: (ex) => e = ex,
);

if (e != null) {
throw e!;
}
}
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Error);
if (mounted) {
// show error
await showDialog<dynamic>(
context: context,
useSafeArea: false,
barrierDismissible: true,
builder: (context) => StackDialog(
title: "Rescan failed",
message: e.toString(),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Ok",
style: STextStyles.itemSubtitle12(context),
),
onPressed: () {
Navigator.of(context, rootNavigator: Util.isDesktop).pop();
},
),
),
);
}
} finally {
if (!Platform.isLinux) await WakelockPlus.disable();
}
}
}

@override
Widget build(BuildContext context) {
return Background(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
*
*/

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import '../../../../../app_config.dart';
import '../../../../../db/sqlite/firo_cache.dart';
Expand All @@ -20,7 +23,10 @@ import '../../../../../providers/global/prefs_provider.dart';
import '../../../../../providers/global/wallets_provider.dart';
import '../../../../../themes/stack_colors.dart';
import '../../../../../utilities/assets.dart';
import '../../../../../utilities/logger.dart';
import '../../../../../utilities/show_loading.dart';
import '../../../../../utilities/text_styles.dart';
import '../../../../../utilities/util.dart';
import '../../../../../wallets/crypto_currency/crypto_currency.dart';
import '../../../../../wallets/isar/models/wallet_info.dart';
import '../../../../../wallets/isar/providers/wallet_info_provider.dart';
Expand All @@ -40,6 +46,7 @@ import '../../../../../widgets/desktop/desktop_dialog_close_button.dart';
import '../../../../../widgets/desktop/primary_button.dart';
import '../../../../../widgets/desktop/secondary_button.dart';
import '../../../../../widgets/rounded_container.dart';
import '../../../../../widgets/stack_dialog.dart';

class MoreFeaturesDialog extends ConsumerStatefulWidget {
const MoreFeaturesDialog({
Expand Down Expand Up @@ -88,6 +95,10 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
},
isar: ref.read(mainDBProvider).isar,
);

if (newValue) {
await _doRescanMaybe();
}
} finally {
// ensure _isUpdatingLelantusScanning is set to false no matter what
_isUpdatingLelantusScanning = false;
Expand Down Expand Up @@ -115,6 +126,124 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
}
}

Future<void> _doRescanMaybe() async {
final shouldRescan = await showDialog<bool>(
context: context,
builder: (context) {
return DesktopDialog(
maxWidth: 700,
child: Column(
children: [
const DesktopDialogCloseButton(),
const SizedBox(
height: 5,
),
Text(
"Rescan may be required",
style: STextStyles.desktopH2(context),
textAlign: TextAlign.left,
),
const SizedBox(
height: 16,
),
const Spacer(),
Text(
"A blockchain rescan may be required to fully recover all lelantus history."
"\nThis may take a while.",
style: STextStyles.desktopTextMedium(context).copyWith(
color: Theme.of(context).extension<StackColors>()!.textDark3,
),
textAlign: TextAlign.center,
),
const Spacer(),
Padding(
padding: const EdgeInsets.only(
left: 32,
right: 32,
bottom: 32,
),
child: Row(
children: [
Expanded(
child: SecondaryButton(
label: "Rescan now",
onPressed: () {
Navigator.of(context).pop(true);
},
),
),
const SizedBox(
width: 16,
),
Expanded(
child: PrimaryButton(
label: "Later",
onPressed: () => Navigator.of(context).pop(false),
),
),
],
),
),
],
),
);
},
);

if (mounted && shouldRescan == true) {
try {
if (!Platform.isLinux) await WakelockPlus.enable();

Exception? e;
if (mounted) {
await showLoading(
whileFuture: ref.read(pWallets).getWallet(widget.walletId).recover(
isRescan: true,
),
context: context,
message: "Rescanning blockchain",
subMessage:
"This may take a while.\nPlease do not exit this screen.",
rootNavigator: Util.isDesktop,
onException: (ex) => e = ex,
);

if (e != null) {
throw e!;
}
}
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Error);
if (mounted) {
// show error
await showDialog<dynamic>(
context: context,
useSafeArea: false,
barrierDismissible: true,
builder: (context) => StackDialog(
title: "Rescan failed",
message: e.toString(),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Ok",
style: STextStyles.itemSubtitle12(context),
),
onPressed: () {
Navigator.of(context, rootNavigator: Util.isDesktop).pop();
},
),
),
);
}
} finally {
if (!Platform.isLinux) await WakelockPlus.disable();
}
}
}

late final DSBController _switchController;

bool _switchReuseAddressToggledLock = false; // Mutex.
Expand Down

0 comments on commit 90e4211

Please sign in to comment.