show loading n cancel

This commit is contained in:
julian 2023-10-18 09:02:25 -06:00
parent d2791e005d
commit 91baaa0f16
5 changed files with 205 additions and 147 deletions

View file

@ -60,6 +60,49 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
FusionOption _option = FusionOption.continuous; FusionOption _option = FusionOption.continuous;
Future<void> _startFusion() async {
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
try {
fusionWallet.uiState = ref.read(
fusionProgressUIStateProvider(widget.walletId),
);
} catch (e) {
if (!e.toString().contains(
"FusionProgressUIState was already set for ${widget.walletId}")) {
rethrow;
}
}
final int rounds = _option == FusionOption.continuous
? 0
: int.parse(fusionRoundController.text);
final newInfo = FusionInfo(
host: serverController.text,
port: int.parse(portController.text),
ssl: _enableSSLCheckbox,
rounds: rounds,
);
// update user prefs (persistent)
ref.read(prefsChangeNotifierProvider).fusionServerInfo = newInfo;
unawaited(
fusionWallet.fuse(
fusionInfo: newInfo,
),
);
await Navigator.of(context).pushNamed(
FusionProgressView.routeName,
arguments: widget.walletId,
);
}
@override @override
void initState() { void initState() {
serverController = TextEditingController(); serverController = TextEditingController();
@ -388,52 +431,7 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
PrimaryButton( PrimaryButton(
label: "Start", label: "Start",
enabled: _enableStartButton, enabled: _enableStartButton,
onPressed: () async { onPressed: _startFusion,
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
try {
fusionWallet.uiState = ref.read(
fusionProgressUIStateProvider(
widget.walletId),
);
} catch (e) {
if (!e.toString().contains(
"FusionProgressUIState was already set for ${widget.walletId}")) {
rethrow;
}
}
final int rounds =
_option == FusionOption.continuous
? 0
: int.parse(fusionRoundController.text);
final newInfo = FusionInfo(
host: serverController.text,
port: int.parse(portController.text),
ssl: _enableSSLCheckbox,
rounds: rounds,
);
// update user prefs (persistent)
ref
.read(prefsChangeNotifierProvider)
.fusionServerInfo = newInfo;
unawaited(
fusionWallet.fuse(
fusionInfo: newInfo,
),
);
await Navigator.of(context).pushNamed(
FusionProgressView.routeName,
arguments: widget.walletId,
);
},
), ),
], ],
), ),

View file

@ -14,10 +14,14 @@ import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion
import 'package:stackwallet/providers/global/wallets_provider.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart'; import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart'; import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/stack_dialog.dart';
class FusionProgressView extends ConsumerStatefulWidget { class FusionProgressView extends ConsumerStatefulWidget {
const FusionProgressView({ const FusionProgressView({
@ -34,17 +38,56 @@ class FusionProgressView extends ConsumerStatefulWidget {
} }
class _FusionProgressViewState extends ConsumerState<FusionProgressView> { class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
/// return true on will cancel, false if cancel cancelled Future<bool> _requestAndProcessCancel() async {
Future<bool> _requestCancel() async { final shouldCancel = await showDialog<bool?>(
// TODO context: context,
return false; barrierDismissible: false,
builder: (_) => StackDialog(
title: "Cancel fusion?",
leftButton: SecondaryButton(
label: "No",
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(false);
},
),
rightButton: PrimaryButton(
label: "Yes",
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(true);
},
),
),
);
if (shouldCancel == true && mounted) {
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
await showLoading(
whileFuture: Future.wait([
fusionWallet.stop(),
Future<void>.delayed(const Duration(seconds: 2)),
]),
context: context,
isDesktop: Util.isDesktop,
message: "Stopping fusion",
);
return true;
} else {
return false;
}
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WillPopScope( return WillPopScope(
onWillPop: () async { onWillPop: () async {
return await _requestCancel(); return await _requestAndProcessCancel();
}, },
child: Background( child: Background(
child: SafeArea( child: SafeArea(
@ -55,7 +98,7 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
automaticallyImplyLeading: false, automaticallyImplyLeading: false,
leading: AppBarBackButton( leading: AppBarBackButton(
onPressed: () async { onPressed: () async {
if (await _requestCancel()) { if (await _requestAndProcessCancel()) {
if (mounted) { if (mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
@ -90,17 +133,9 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
// TODO: various button states // TODO: various button states
// tempt only show cancel button // tempt only show cancel button
SecondaryButton( SecondaryButton(
label: "Cancela", label: "Cancel",
onPressed: () async { onPressed: () async {
final fusionWallet = ref if (await _requestAndProcessCancel()) {
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
await fusionWallet.stop();
// TODO should this stop be unawaited?
if (await _requestCancel()) {
if (mounted) { if (mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }

View file

@ -64,6 +64,55 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
FusionOption _roundType = FusionOption.continuous; FusionOption _roundType = FusionOption.continuous;
Future<void> _startFusion() async {
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
try {
fusionWallet.uiState = ref.read(
fusionProgressUIStateProvider(widget.walletId),
);
} catch (e) {
if (!e.toString().contains(
"FusionProgressUIState was already set for ${widget.walletId}")) {
rethrow;
}
}
final int rounds = _roundType == FusionOption.continuous
? 0
: int.parse(fusionRoundController.text);
final newInfo = FusionInfo(
host: serverController.text,
port: int.parse(portController.text),
ssl: _enableSSLCheckbox,
rounds: rounds,
);
// update user prefs (persistent)
ref.read(prefsChangeNotifierProvider).fusionServerInfo = newInfo;
unawaited(
fusionWallet.fuse(
fusionInfo: newInfo,
),
);
// unawaited(fusionWallet.stepThruUiStates());
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) {
return FusionDialogView(
walletId: widget.walletId,
);
},
);
}
@override @override
void initState() { void initState() {
serverController = TextEditingController(); serverController = TextEditingController();
@ -522,57 +571,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
PrimaryButton( PrimaryButton(
label: "Start", label: "Start",
enabled: _enableStartButton, enabled: _enableStartButton,
onPressed: () async { onPressed: _startFusion,
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
try {
fusionWallet.uiState = ref.read(
fusionProgressUIStateProvider(widget.walletId),
);
} catch (e) {
if (!e.toString().contains(
"FusionProgressUIState was already set for ${widget.walletId}")) {
rethrow;
}
}
final int rounds =
_roundType == FusionOption.continuous
? 0
: int.parse(fusionRoundController.text);
final newInfo = FusionInfo(
host: serverController.text,
port: int.parse(portController.text),
ssl: _enableSSLCheckbox,
rounds: rounds,
);
// update user prefs (persistent)
ref
.read(prefsChangeNotifierProvider)
.fusionServerInfo = newInfo;
unawaited(
fusionWallet.fuse(
fusionInfo: newInfo,
),
);
// unawaited(fusionWallet.stepThruUiStates());
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) {
return FusionDialogView(
walletId: widget.walletId,
);
},
);
},
), ),
], ],
), ),

View file

@ -3,10 +3,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_progress.dart'; import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_progress.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart'; import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/desktop/desktop_dialog.dart'; import 'package:stackwallet/widgets/desktop/desktop_dialog.dart';
import 'package:stackwallet/widgets/desktop/desktop_dialog_close_button.dart'; import 'package:stackwallet/widgets/desktop/desktop_dialog_close_button.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart'; import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/stack_dialog.dart';
enum CashFusionStatus { waiting, running, success, failed } enum CashFusionStatus { waiting, running, success, failed }
@ -30,6 +33,51 @@ class FusionDialogView extends ConsumerStatefulWidget {
} }
class _FusionDialogViewState extends ConsumerState<FusionDialogView> { class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
Future<bool> _requestAndProcessCancel() async {
final shouldCancel = await showDialog<bool?>(
context: context,
barrierDismissible: false,
builder: (_) => StackDialog(
title: "Cancel fusion?",
leftButton: SecondaryButton(
label: "No",
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(false);
},
),
rightButton: PrimaryButton(
label: "Yes",
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(true);
},
),
),
);
if (shouldCancel == true && mounted) {
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
await showLoading(
whileFuture: Future.wait([
fusionWallet.stop(),
Future<void>.delayed(const Duration(seconds: 2)),
]),
context: context,
isDesktop: true,
message: "Stopping fusion",
);
return true;
} else {
return false;
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return DesktopDialog( return DesktopDialog(
@ -55,8 +103,12 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
), ),
), ),
DesktopDialogCloseButton( DesktopDialogCloseButton(
onPressedOverride: () { onPressedOverride: () async {
_stop(); if (await _requestAndProcessCancel()) {
if (mounted) {
Navigator.of(context).pop();
}
}
}, },
), ),
], ],
@ -83,19 +135,11 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
enabled: true, enabled: true,
label: "Cancel", label: "Cancel",
onPressed: () async { onPressed: () async {
final fusionWallet = ref if (await _requestAndProcessCancel()) {
.read(walletsChangeNotifierProvider) if (mounted) {
.getManager(widget.walletId) Navigator.of(context).pop();
.wallet as FusionWalletInterface; }
await fusionWallet.stop();
// TODO should this stop be unawaited?
// if (await _requestCancel()) {
if (mounted) {
Navigator.of(context).pop();
} }
// }
}, },
), ),
], ],
@ -109,22 +153,4 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
), ),
); );
} }
/// Stops the fusion process.
///
/// This is called when the user presses the back button.
void _stop() async {
print(12121212);
final fusionWallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as FusionWalletInterface;
await fusionWallet.stop();
// TODO await successful cancellation and showLoading while it stops.
if (mounted) {
Navigator.of(context).pop();
}
}
} }

View file

@ -531,7 +531,7 @@ mixin FusionWalletInterface {
/// ///
/// This function is called when the user taps the "Cancel" button in the UI /// This function is called when the user taps the "Cancel" button in the UI
/// or closes the fusion progress dialog. /// or closes the fusion progress dialog.
Future<void>? stop() { Future<void> stop() async {
return _mainFusionObject?.stop(); await _mainFusionObject?.stop();
} }
} }