/* * 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 * */ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:wakelock_plus/wakelock_plus.dart'; import '../../pages_desktop_specific/cashfusion/sub_widgets/fusion_progress.dart'; import '../../providers/cash_fusion/fusion_progress_ui_state_provider.dart'; import '../../providers/global/prefs_provider.dart'; import '../../providers/global/wallets_provider.dart'; import '../../themes/stack_colors.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/providers/wallet_info_provider.dart'; import '../../wallets/wallet/wallet_mixin_interfaces/cash_fusion_interface.dart'; import '../../widgets/background.dart'; import '../../widgets/custom_buttons/app_bar_icon_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 FusionProgressView extends ConsumerStatefulWidget { const FusionProgressView({ super.key, required this.walletId, }); static const routeName = "/cashFusionProgressView"; final String walletId; @override ConsumerState createState() => _FusionProgressViewState(); } class _FusionProgressViewState extends ConsumerState { late final CryptoCurrency coin; Future _requestAndProcessCancel() async { final shouldCancel = await showDialog( context: context, barrierDismissible: false, builder: (_) => StackDialog( title: "Cancel fusion?", leftButton: SecondaryButton( label: "No", buttonHeight: null, onPressed: () { Navigator.of(context).pop(false); }, ), rightButton: PrimaryButton( label: "Yes", buttonHeight: null, onPressed: () { Navigator.of(context).pop(true); }, ), ), ); if (shouldCancel == true && mounted) { final fusionWallet = ref.read(pWallets).getWallet(widget.walletId) as CashFusionInterface; await showLoading( whileFuture: Future.wait([ fusionWallet.stop(), Future.delayed(const Duration(seconds: 2)), ]), context: context, rootNavigator: Util.isDesktop, message: "Stopping fusion", ); await WakelockPlus.disable(); return true; } else { return false; } } @override void initState() { coin = ref.read(pWalletCoin(widget.walletId)); super.initState(); } @override void dispose() { WakelockPlus.disable(); super.dispose(); } @override Widget build(BuildContext context) { final bool _succeeded = ref.watch(fusionProgressUIStateProvider(widget.walletId)).succeeded; final bool _failed = ref.watch(fusionProgressUIStateProvider(widget.walletId)).failed; final int _fusionRoundsCompleted = ref .watch(fusionProgressUIStateProvider(widget.walletId)) .fusionRoundsCompleted; WakelockPlus.enable(); return WillPopScope( onWillPop: () async { return await _requestAndProcessCancel(); }, child: Background( child: SafeArea( child: Scaffold( backgroundColor: Theme.of(context).extension()!.background, appBar: AppBar( automaticallyImplyLeading: false, leading: AppBarBackButton( onPressed: () async { if (await _requestAndProcessCancel()) { 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( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (_fusionRoundsCompleted == 0) RoundedContainer( color: Theme.of(context) .extension()! .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()! .snackBarTextError, ), textAlign: TextAlign.center, ), ), if (_fusionRoundsCompleted > 0) RoundedContainer( color: Theme.of(context) .extension()! .snackBarBackInfo, child: Text( "Fusion rounds completed: $_fusionRoundsCompleted", style: STextStyles.w500_14(context).copyWith( color: Theme.of(context) .extension()! .snackBarTextInfo, ), textAlign: TextAlign.center, ), ), const SizedBox( height: 20, ), FusionProgress( walletId: widget.walletId, ), const Spacer(), const SizedBox( height: 16, ), if (_succeeded) PrimaryButton( label: "Fuse again", onPressed: _fuseAgain, ), if (_succeeded) const SizedBox( height: 16, ), if (_failed) PrimaryButton( label: "Try again", onPressed: _fuseAgain, ), if (_failed) const SizedBox( height: 16, ), SecondaryButton( label: "Cancel", onPressed: () async { if (await _requestAndProcessCancel()) { if (mounted) { Navigator.of(context).pop(); } } }, ), ], ), ), ), ), ); }, ), ), ), ), ); } /// Fuse again. void _fuseAgain() async { final fusionWallet = ref.read(pWallets).getWallet(widget.walletId) as CashFusionInterface; final fusionInfo = ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin); try { fusionWallet.uiState = ref.read( fusionProgressUIStateProvider(widget.walletId), ); } catch (e) { if (!e.toString().contains( "FusionProgressUIState was already set for ${widget.walletId}", )) { rethrow; } } unawaited(fusionWallet.fuse(fusionInfo: fusionInfo)); } }