From bc0f182b0b05172133b671c2b99adad3e2aac080 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 28 Sep 2023 14:16:48 -0600 Subject: [PATCH] Simplify fusion_progress_ui_state.dart to work with .family provider, and WIP hook it up to the fusion wallet interface --- lib/models/fusion_progress_state.dart | 44 ------------ lib/models/fusion_progress_ui_state.dart | 69 +++---------------- .../cashfusion/desktop_cashfusion_view.dart | 24 ++++--- .../cashfusion/sub_widgets/fusion_dialog.dart | 28 +++++--- .../fusion_progress_ui_state_provider.dart | 7 +- .../mixins/fusion_wallet_interface.dart | 25 ++++++- 6 files changed, 69 insertions(+), 128 deletions(-) delete mode 100644 lib/models/fusion_progress_state.dart diff --git a/lib/models/fusion_progress_state.dart b/lib/models/fusion_progress_state.dart deleted file mode 100644 index a382d71c4..000000000 --- a/lib/models/fusion_progress_state.dart +++ /dev/null @@ -1,44 +0,0 @@ -import 'package:flutter/cupertino.dart'; -import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart'; -import 'package:stackwallet/services/coins/manager.dart'; -import 'package:stackwallet/utilities/enums/coin_enum.dart'; - -class FusionProgressState extends ChangeNotifier { - final String walletId; - final String walletName; - final Coin coin; - late CashFusionStatus _fusionStatus; - Manager? manager; - String? address; - - CashFusionStatus get fusionState => _fusionStatus; - set fusionState(CashFusionStatus fusionStatus) { - _fusionStatus = fusionStatus; - notifyListeners(); - } - - FusionProgressState({ - required this.walletId, - required this.walletName, - required this.coin, - required CashFusionStatus fusionStatus, - this.manager, - this.address, - }) { - _fusionStatus = fusionStatus; - } - - FusionProgressState copyWith({ - CashFusionStatus? fusionStatus, - String? address, - }) { - return FusionProgressState( - walletId: walletId, - walletName: walletName, - coin: coin, - fusionStatus: fusionStatus ?? _fusionStatus, - manager: manager, - address: this.address, - ); - } -} diff --git a/lib/models/fusion_progress_ui_state.dart b/lib/models/fusion_progress_ui_state.dart index 79faeef36..09bc81556 100644 --- a/lib/models/fusion_progress_ui_state.dart +++ b/lib/models/fusion_progress_ui_state.dart @@ -1,12 +1,8 @@ import 'package:flutter/cupertino.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart'; -import 'package:stackwallet/services/coins/manager.dart'; - -import 'fusion_progress_state.dart'; class FusionProgressUIState extends ChangeNotifier { - bool _ableToConnect = false; + bool _ableToConnect = true; // set to true for now bool get done { if (!_ableToConnect) { @@ -24,10 +20,8 @@ class FusionProgressUIState extends ChangeNotifier { _done &= (_complete == CashFusionStatus.success) || (_complete == CashFusionStatus.failed); - for (final wallet in _fusionState.values) { - _done &= (wallet.fusionState == CashFusionStatus.success) || - (wallet.fusionState == CashFusionStatus.failed); - } + _done &= (fusionState == CashFusionStatus.success) || + (fusionState == CashFusionStatus.failed); return _done; } @@ -43,9 +37,7 @@ class FusionProgressUIState extends ChangeNotifier { _succeeded &= _fusing == CashFusionStatus.success; _succeeded &= _complete == CashFusionStatus.success; - for (final wallet in _fusionState.values) { - _succeeded &= wallet.fusionState == CashFusionStatus.success; - } + _succeeded &= fusionState == CashFusionStatus.success; return _succeeded; } @@ -85,55 +77,10 @@ class FusionProgressUIState extends ChangeNotifier { notifyListeners(); } - List get managers { - List _managers = []; - for (final item in _fusionState.values) { - if (item.manager != null) { - _managers.add(item.manager!); - } - } - return _managers; - } - - Map _fusionState = {}; - Map> - _fusionStateProviders = {}; - Map> - get fusionStateProviders => _fusionStateProviders; - - set fusionState(Map state) { - _fusionState = state; - _fusionStateProviders = {}; - for (final wallet in _fusionState.values) { - _fusionStateProviders[wallet.walletId] = - ChangeNotifierProvider((_) => wallet); - } - - /// todo: is this true - _ableToConnect = true; - notifyListeners(); - } - - FusionProgressState getFusionProgressState(String walletId) { - return _fusionState[walletId]!; - } - - ChangeNotifierProvider getFusionProgressStateProvider( - String walletId) { - return _fusionStateProviders[walletId]!; - } - - void update({ - required String walletId, - required CashFusionStatus fusionStatus, - Manager? manager, - String? address, - }) { - _fusionState[walletId]!.fusionState = fusionStatus; - _fusionState[walletId]!.manager = - manager ?? _fusionState[walletId]!.manager; - _fusionState[walletId]!.address = - address ?? _fusionState[walletId]!.address; + CashFusionStatus _fusionStatus = CashFusionStatus.waiting; + CashFusionStatus get fusionState => _fusionStatus; + set fusionState(CashFusionStatus fusionStatus) { + _fusionStatus = fusionStatus; notifyListeners(); } } diff --git a/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart b/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart index 67d6cdc36..0c1809450 100644 --- a/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart +++ b/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart @@ -20,6 +20,7 @@ import 'package:flutter_svg/svg.dart'; import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_menu.dart'; import 'package:stackwallet/pages_desktop_specific/settings/settings_menu.dart'; +import 'package:stackwallet/providers/cash_fusion/fusion_progress_ui_state_provider.dart'; import 'package:stackwallet/providers/desktop/current_desktop_menu_item.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart'; import 'package:stackwallet/providers/ui/check_box_state_provider.dart'; @@ -630,20 +631,23 @@ class _DesktopCashFusion extends ConsumerState { PrimaryButton( label: "Start", onPressed: () async { - await (ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) - .wallet as FusionWalletInterface) - .fuse(); + final fusionWallet = ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .wallet as FusionWalletInterface; - // have nullable of variable type that can be set or unset; of notifier - // when pressed, grab wallet as wallet id - // add to fusion wallet interface and connect to provider + fusionWallet.uiState = ref.read( + fusionProgressUIStateProvider(widget.walletId), + ); - await showDialog( + unawaited(fusionWallet.fuse()); + + await showDialog( context: context, builder: (context) { - return FusionDialog(); + return FusionDialog( + walletId: widget.walletId, + ); }, ); }, diff --git a/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart b/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart index 5d2f50744..88394410b 100644 --- a/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart +++ b/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart @@ -16,8 +16,11 @@ enum CashFusionStatus { waiting, fusing, success, failed } class FusionDialog extends StatelessWidget { const FusionDialog({ Key? key, + required this.walletId, }) : super(key: key); + final String walletId; + @override Widget build(BuildContext context) { Widget _getIconForState(CashFusionStatus state) { @@ -103,8 +106,9 @@ class FusionDialog extends StatelessWidget { height: 20, ), Consumer(builder: (_, ref, __) { - final state = ref.watch(fusionProgressUIStateProvider - .select((value) => value.connecting)); + final state = ref.watch( + fusionProgressUIStateProvider(walletId) + .select((value) => value.connecting)); return RoundedContainer( padding: EdgeInsets.zero, color: @@ -143,8 +147,9 @@ class FusionDialog extends StatelessWidget { height: 12, ), Consumer(builder: (_, ref, __) { - final state = ref.watch(fusionProgressUIStateProvider - .select((value) => value.outputs)); + final state = ref.watch( + fusionProgressUIStateProvider(walletId) + .select((value) => value.outputs)); return RoundedContainer( padding: EdgeInsets.zero, color: @@ -183,8 +188,9 @@ class FusionDialog extends StatelessWidget { height: 12, ), Consumer(builder: (_, ref, __) { - final state = ref.watch(fusionProgressUIStateProvider - .select((value) => value.peers)); + final state = ref.watch( + fusionProgressUIStateProvider(walletId) + .select((value) => value.peers)); return RoundedContainer( padding: EdgeInsets.zero, color: @@ -223,8 +229,9 @@ class FusionDialog extends StatelessWidget { height: 12, ), Consumer(builder: (_, ref, __) { - final state = ref.watch(fusionProgressUIStateProvider - .select((value) => value.fusing)); + final state = ref.watch( + fusionProgressUIStateProvider(walletId) + .select((value) => value.fusing)); return RoundedContainer( padding: EdgeInsets.zero, color: @@ -263,8 +270,9 @@ class FusionDialog extends StatelessWidget { height: 12, ), Consumer(builder: (_, ref, __) { - final state = ref.watch(fusionProgressUIStateProvider - .select((value) => value.complete)); + final state = ref.watch( + fusionProgressUIStateProvider(walletId) + .select((value) => value.complete)); return RoundedContainer( padding: EdgeInsets.zero, color: diff --git a/lib/providers/cash_fusion/fusion_progress_ui_state_provider.dart b/lib/providers/cash_fusion/fusion_progress_ui_state_provider.dart index 2fb74b1f6..872b6586a 100644 --- a/lib/providers/cash_fusion/fusion_progress_ui_state_provider.dart +++ b/lib/providers/cash_fusion/fusion_progress_ui_state_provider.dart @@ -12,5 +12,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/models/fusion_progress_ui_state.dart'; final fusionProgressUIStateProvider = - ChangeNotifierProvider( - (ref) => FusionProgressUIState()); + ChangeNotifierProvider.family( + (ref, walletId) { + return FusionProgressUIState(); + }, +); diff --git a/lib/services/mixins/fusion_wallet_interface.dart b/lib/services/mixins/fusion_wallet_interface.dart index 2e7d93f15..96cf64ef3 100644 --- a/lib/services/mixins/fusion_wallet_interface.dart +++ b/lib/services/mixins/fusion_wallet_interface.dart @@ -7,6 +7,7 @@ import 'package:fusiondart/fusiondart.dart' as fusion; import 'package:isar/isar.dart'; import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart'; +import 'package:stackwallet/models/fusion_progress_ui_state.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/services/fusion_tor_service.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; @@ -24,6 +25,22 @@ mixin FusionWalletInterface { late final MainDB _db; late final FusionTorService _torService; + // setting values on this should notify any listeners (the GUI) + FusionProgressUIState? _uiState; + FusionProgressUIState get uiState { + if (_uiState == null) { + throw Exception("FusionProgressUIState has not been set for $_walletId"); + } + return _uiState!; + } + + set uiState(FusionProgressUIState state) { + if (_uiState != null) { + throw Exception("FusionProgressUIState was already set for $_walletId"); + } + _uiState = state; + } + // Passed in wallet functions. late final Future
Function() _getNextUnusedChangeAddress; late final CachedElectrumX Function() _getWalletCachedElectrumX; @@ -59,6 +76,12 @@ mixin FusionWalletInterface { _getChainHeight = getChainHeight; } + // callback to update the ui state object + void updateStatus(fusion.FusionStatus fusionStatus) { + // TODO: this + // set _uiState states + } + /// Returns a list of all addresses in the wallet. Future> getFusionAddresses() async { List
_addresses = await _db.getAddresses(_walletId).findAll(); @@ -213,10 +236,10 @@ mixin FusionWalletInterface { getAddresses: getFusionAddresses, getTransactionsByAddress: getTransactionsByAddress, getInputsByAddress: getInputsByAddress, - /*createNewReservedChangeAddress: createNewReservedChangeAddress,*/ getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses, getSocksProxyAddress: getSocksProxyAddress, getChainHeight: _getChainHeight, + updateStatusCallback: updateStatus, ); // Add stack UTXOs.