2023-10-16 21:28:09 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by julian on 2023-10-16
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-10-19 17:52:38 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-10-16 21:28:09 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-10-18 03:17:16 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-10-16 22:08:15 +00:00
|
|
|
import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_progress.dart';
|
2023-10-19 17:52:38 +00:00
|
|
|
import 'package:stackwallet/providers/cash_fusion/fusion_progress_ui_state_provider.dart';
|
|
|
|
import 'package:stackwallet/providers/global/prefs_provider.dart';
|
2023-10-18 03:17:16 +00:00
|
|
|
import 'package:stackwallet/providers/global/wallets_provider.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
|
2023-10-16 21:28:09 +00:00
|
|
|
import 'package:stackwallet/themes/stack_colors.dart';
|
2023-10-18 15:02:25 +00:00
|
|
|
import 'package:stackwallet/utilities/show_loading.dart';
|
2023-10-16 21:28:09 +00:00
|
|
|
import 'package:stackwallet/utilities/text_styles.dart';
|
2023-10-18 15:02:25 +00:00
|
|
|
import 'package:stackwallet/utilities/util.dart';
|
2023-10-16 21:28:09 +00:00
|
|
|
import 'package:stackwallet/widgets/background.dart';
|
|
|
|
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
|
2023-10-18 15:02:25 +00:00
|
|
|
import 'package:stackwallet/widgets/desktop/primary_button.dart';
|
2023-10-16 21:28:09 +00:00
|
|
|
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
|
2023-10-19 19:43:07 +00:00
|
|
|
import 'package:stackwallet/widgets/rounded_container.dart';
|
2023-10-18 15:02:25 +00:00
|
|
|
import 'package:stackwallet/widgets/stack_dialog.dart';
|
2023-10-16 21:28:09 +00:00
|
|
|
|
2023-10-18 03:17:16 +00:00
|
|
|
class FusionProgressView extends ConsumerStatefulWidget {
|
2023-10-16 21:28:09 +00:00
|
|
|
const FusionProgressView({
|
|
|
|
super.key,
|
|
|
|
required this.walletId,
|
|
|
|
});
|
|
|
|
|
|
|
|
static const routeName = "/cashFusionProgressView";
|
|
|
|
|
|
|
|
final String walletId;
|
|
|
|
|
|
|
|
@override
|
2023-10-18 03:17:16 +00:00
|
|
|
ConsumerState<FusionProgressView> createState() => _FusionProgressViewState();
|
2023-10-16 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 03:17:16 +00:00
|
|
|
class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
|
2023-10-18 15:02:25 +00:00
|
|
|
Future<bool> _requestAndProcessCancel() async {
|
|
|
|
final shouldCancel = await showDialog<bool?>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (_) => StackDialog(
|
|
|
|
title: "Cancel fusion?",
|
|
|
|
leftButton: SecondaryButton(
|
|
|
|
label: "No",
|
2023-10-23 21:47:08 +00:00
|
|
|
buttonHeight: null,
|
2023-10-18 15:02:25 +00:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
rightButton: PrimaryButton(
|
|
|
|
label: "Yes",
|
2023-10-23 21:47:08 +00:00
|
|
|
buttonHeight: null,
|
2023-10-18 15:02:25 +00:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (shouldCancel == true && mounted) {
|
2023-11-03 19:46:55 +00:00
|
|
|
final fusionWallet = ref.read(pWallets).getWallet(widget.walletId)
|
2023-10-31 17:15:59 +00:00
|
|
|
as FusionWalletInterface;
|
2023-10-18 15:02:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-10-16 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-19 21:26:56 +00:00
|
|
|
final bool _succeeded =
|
2023-10-19 17:54:22 +00:00
|
|
|
ref.watch(fusionProgressUIStateProvider(widget.walletId)).succeeded;
|
|
|
|
|
2023-10-19 21:26:56 +00:00
|
|
|
final bool _failed =
|
2023-10-19 17:54:22 +00:00
|
|
|
ref.watch(fusionProgressUIStateProvider(widget.walletId)).failed;
|
|
|
|
|
2023-10-19 21:26:56 +00:00
|
|
|
final int _fusionRoundsCompleted = ref
|
2023-10-19 19:43:07 +00:00
|
|
|
.watch(fusionProgressUIStateProvider(widget.walletId))
|
|
|
|
.fusionRoundsCompleted;
|
|
|
|
|
2023-10-16 21:28:09 +00:00
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () async {
|
2023-10-18 15:02:25 +00:00
|
|
|
return await _requestAndProcessCancel();
|
2023-10-16 21:28:09 +00:00
|
|
|
},
|
|
|
|
child: Background(
|
|
|
|
child: SafeArea(
|
|
|
|
child: Scaffold(
|
|
|
|
backgroundColor:
|
|
|
|
Theme.of(context).extension<StackColors>()!.background,
|
|
|
|
appBar: AppBar(
|
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
leading: AppBarBackButton(
|
|
|
|
onPressed: () async {
|
2023-10-18 15:02:25 +00:00
|
|
|
if (await _requestAndProcessCancel()) {
|
2023-10-16 21:28:09 +00:00
|
|
|
if (mounted) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
"Fusion progress",
|
|
|
|
style: STextStyles.navBarTitle(context),
|
|
|
|
),
|
|
|
|
titleSpacing: 0,
|
|
|
|
),
|
|
|
|
body: LayoutBuilder(
|
|
|
|
builder: (builderContext, constraints) {
|
|
|
|
return SingleChildScrollView(
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
minHeight: constraints.maxHeight,
|
|
|
|
),
|
|
|
|
child: IntrinsicHeight(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
child: Column(
|
2023-10-23 21:11:42 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
2023-10-16 21:28:09 +00:00
|
|
|
children: [
|
2023-10-19 22:05:28 +00:00
|
|
|
if (_fusionRoundsCompleted == 0)
|
2023-10-23 21:11:42 +00:00
|
|
|
RoundedContainer(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.snackBarBackError,
|
|
|
|
child: Text(
|
|
|
|
"Do not close this window. If you exit, "
|
|
|
|
"the process will be canceled.",
|
|
|
|
style:
|
|
|
|
STextStyles.smallMed14(context).copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.snackBarTextError,
|
2023-10-19 22:05:28 +00:00
|
|
|
),
|
2023-10-23 21:11:42 +00:00
|
|
|
textAlign: TextAlign.center,
|
2023-10-19 22:05:28 +00:00
|
|
|
),
|
|
|
|
),
|
2023-10-19 19:50:30 +00:00
|
|
|
if (_fusionRoundsCompleted > 0)
|
2023-10-23 21:11:42 +00:00
|
|
|
RoundedContainer(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.snackBarBackInfo,
|
|
|
|
child: Text(
|
|
|
|
"Fusion rounds completed: $_fusionRoundsCompleted",
|
|
|
|
style: STextStyles.w500_14(context).copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.snackBarTextInfo,
|
2023-10-19 19:43:07 +00:00
|
|
|
),
|
2023-10-23 21:11:42 +00:00
|
|
|
textAlign: TextAlign.center,
|
2023-10-19 19:43:07 +00:00
|
|
|
),
|
|
|
|
),
|
2023-10-19 22:05:28 +00:00
|
|
|
const SizedBox(
|
|
|
|
height: 20,
|
|
|
|
),
|
2023-10-16 22:08:15 +00:00
|
|
|
FusionProgress(
|
|
|
|
walletId: widget.walletId,
|
2023-10-16 21:28:09 +00:00
|
|
|
),
|
2023-10-24 22:11:53 +00:00
|
|
|
const Spacer(),
|
|
|
|
const SizedBox(
|
|
|
|
height: 16,
|
|
|
|
),
|
2023-10-19 17:54:22 +00:00
|
|
|
if (_succeeded)
|
2023-10-24 18:59:09 +00:00
|
|
|
PrimaryButton(
|
|
|
|
label: "Fuse again",
|
2023-10-24 22:43:41 +00:00
|
|
|
onPressed: _fuseAgain,
|
2023-10-19 17:52:38 +00:00
|
|
|
),
|
2023-10-24 22:11:53 +00:00
|
|
|
if (_succeeded)
|
|
|
|
const SizedBox(
|
|
|
|
height: 16,
|
|
|
|
),
|
2023-10-19 17:54:22 +00:00
|
|
|
if (_failed)
|
2023-10-24 18:59:09 +00:00
|
|
|
PrimaryButton(
|
|
|
|
label: "Try again",
|
2023-10-24 22:43:41 +00:00
|
|
|
onPressed: _fuseAgain,
|
2023-10-19 17:52:38 +00:00
|
|
|
),
|
2023-10-24 22:11:53 +00:00
|
|
|
if (_failed)
|
|
|
|
const SizedBox(
|
|
|
|
height: 16,
|
|
|
|
),
|
2023-10-16 21:28:09 +00:00
|
|
|
SecondaryButton(
|
2023-10-18 15:02:25 +00:00
|
|
|
label: "Cancel",
|
2023-10-16 21:28:09 +00:00
|
|
|
onPressed: () async {
|
2023-10-18 15:02:25 +00:00
|
|
|
if (await _requestAndProcessCancel()) {
|
2023-10-16 21:28:09 +00:00
|
|
|
if (mounted) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-10-19 17:52:38 +00:00
|
|
|
|
|
|
|
/// Fuse again.
|
|
|
|
void _fuseAgain() async {
|
2023-11-03 19:46:55 +00:00
|
|
|
final fusionWallet =
|
|
|
|
ref.read(pWallets).getWallet(widget.walletId) as FusionWalletInterface;
|
2023-10-19 17:52:38 +00:00
|
|
|
|
|
|
|
final fusionInfo = ref.read(prefsChangeNotifierProvider).fusionServerInfo;
|
|
|
|
|
2023-10-24 23:04:09 +00:00
|
|
|
try {
|
|
|
|
fusionWallet.uiState = ref.read(
|
|
|
|
fusionProgressUIStateProvider(widget.walletId),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.toString().contains(
|
|
|
|
"FusionProgressUIState was already set for ${widget.walletId}")) {
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:52:38 +00:00
|
|
|
unawaited(fusionWallet.fuse(fusionInfo: fusionInfo));
|
|
|
|
}
|
2023-10-16 21:28:09 +00:00
|
|
|
}
|