diff --git a/lib/pages/cashfusion/cashfusion_view.dart b/lib/pages/cashfusion/cashfusion_view.dart index e0ca79d29..429daf9b5 100644 --- a/lib/pages/cashfusion/cashfusion_view.dart +++ b/lib/pages/cashfusion/cashfusion_view.dart @@ -55,7 +55,7 @@ class _CashFusionViewState extends ConsumerState { late final FocusNode portFocusNode; late final TextEditingController fusionRoundController; late final FocusNode fusionRoundFocusNode; - Coin? coin; + late final Coin coin; bool _enableSSLCheckbox = false; bool _enableStartButton = false; @@ -89,11 +89,7 @@ class _CashFusionViewState extends ConsumerState { ); // update user prefs (persistent) - if (coin == Coin.bitcoincash) { - ref.read(prefsChangeNotifierProvider).fusionServerInfoBch = newInfo; - } else { - ref.read(prefsChangeNotifierProvider).fusionServerInfoXec = newInfo; - } + ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo); unawaited( fusionWallet.fuse( @@ -123,9 +119,8 @@ class _CashFusionViewState extends ConsumerState { .wallet .coin; - final info = (coin == Coin.bitcoincash) - ? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch - : ref.read(prefsChangeNotifierProvider).fusionServerInfoXec; + final info = + ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin); serverController.text = info.host; portController.text = info.port.toString(); diff --git a/lib/pages/cashfusion/fusion_progress_view.dart b/lib/pages/cashfusion/fusion_progress_view.dart index 78265d95d..a8b7bfe58 100644 --- a/lib/pages/cashfusion/fusion_progress_view.dart +++ b/lib/pages/cashfusion/fusion_progress_view.dart @@ -43,7 +43,7 @@ class FusionProgressView extends ConsumerStatefulWidget { } class _FusionProgressViewState extends ConsumerState { - Coin? coin; + late final Coin coin; Future _requestAndProcessCancel() async { final shouldCancel = await showDialog( @@ -88,6 +88,16 @@ class _FusionProgressViewState extends ConsumerState { } } + @override + void initState() { + coin = ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .wallet + .coin; + super.initState(); + } + @override Widget build(BuildContext context) { final bool _succeeded = @@ -100,12 +110,6 @@ class _FusionProgressViewState extends ConsumerState { .watch(fusionProgressUIStateProvider(widget.walletId)) .fusionRoundsCompleted; - coin = ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) - .wallet - .coin; - return WillPopScope( onWillPop: () async { return await _requestAndProcessCancel(); @@ -234,9 +238,8 @@ class _FusionProgressViewState extends ConsumerState { final fusionWallet = ref.read(pWallets).getWallet(widget.walletId) as CashFusion; - final fusionInfo = (coin == Coin.bitcoincash) - ? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch - : ref.read(prefsChangeNotifierProvider).fusionServerInfoXec; + final fusionInfo = + ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin); try { fusionWallet.uiState = ref.read( diff --git a/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart b/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart index 5b6ab9129..3c8c16b16 100644 --- a/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart +++ b/lib/pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart @@ -93,11 +93,8 @@ class _DesktopCashFusion extends ConsumerState { ); // update user prefs (persistent) - if (coin == Coin.bitcoincash) { - ref.read(prefsChangeNotifierProvider).fusionServerInfoBch = newInfo; - } else { - ref.read(prefsChangeNotifierProvider).fusionServerInfoXec = newInfo; - } + + ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo); unawaited( fusionWallet.fuse( @@ -132,9 +129,8 @@ class _DesktopCashFusion extends ConsumerState { .wallet .coin; - final info = (coin == Coin.bitcoincash) - ? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch - : ref.read(prefsChangeNotifierProvider).fusionServerInfoXec; + final info = + ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin); serverController.text = info.host; portController.text = info.port.toString(); 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 0cba721bb..dd471af24 100644 --- a/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart +++ b/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart @@ -40,7 +40,7 @@ class FusionDialogView extends ConsumerStatefulWidget { } class _FusionDialogViewState extends ConsumerState { - Coin? coin; + late final Coin coin; Future _requestAndProcessCancel() async { if (!ref.read(fusionProgressUIStateProvider(widget.walletId)).running) { @@ -142,6 +142,16 @@ class _FusionDialogViewState extends ConsumerState { } } + @override + void initState() { + coin = ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .wallet + .coin; + super.initState(); + } + @override Widget build(BuildContext context) { final bool _succeeded = @@ -154,12 +164,6 @@ class _FusionDialogViewState extends ConsumerState { .watch(fusionProgressUIStateProvider(widget.walletId)) .fusionRoundsCompleted; - coin = ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) - .wallet - .coin; - return DesktopDialog( maxHeight: 600, child: SingleChildScrollView( @@ -293,9 +297,8 @@ class _FusionDialogViewState extends ConsumerState { final fusionWallet = ref.read(pWallets).getWallet(widget.walletId) as CashFusion; - final fusionInfo = (coin == Coin.bitcoincash) - ? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch - : ref.read(prefsChangeNotifierProvider).fusionServerInfoXec; + final fusionInfo = + ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin); try { fusionWallet.uiState = ref.read( diff --git a/lib/utilities/prefs.dart b/lib/utilities/prefs.dart index c4d3e1739..e6f8d67ab 100644 --- a/lib/utilities/prefs.dart +++ b/lib/utilities/prefs.dart @@ -8,6 +8,8 @@ * */ +import 'dart:async'; + import 'package:flutter/cupertino.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/services/event_bus/events/global/tor_status_changed_event.dart'; @@ -65,8 +67,7 @@ class Prefs extends ChangeNotifier { await _setAmountUnits(); await _setMaxDecimals(); _useTor = await _getUseTor(); - _fusionServerInfoBch = await _getFusionServerInfoBch(); - _fusionServerInfoXec = await _getFusionServerInfoXec(); + _fusionServerInfo = await _getFusionServerInfo(); _initialized = true; } @@ -937,59 +938,84 @@ class Prefs extends ChangeNotifier { // fusion server info - FusionInfo _fusionServerInfoBch = FusionInfo.BCH_DEFAULTS; - FusionInfo _fusionServerInfoXec = FusionInfo.XEC_DEFAULTS; + final Map _fusionServerInfoDefaults = { + Coin.bitcoincash: FusionInfo.BCH_DEFAULTS, + Coin.bitcoincashTestnet: FusionInfo.BCH_DEFAULTS, + Coin.eCash: FusionInfo.XEC_DEFAULTS, + }; - FusionInfo get fusionServerInfoBch => _fusionServerInfoBch; - FusionInfo get fusionServerInfoXec => _fusionServerInfoXec; + Map _fusionServerInfo = { + Coin.bitcoincash: FusionInfo.BCH_DEFAULTS, + Coin.bitcoincashTestnet: FusionInfo.BCH_DEFAULTS, + Coin.eCash: FusionInfo.XEC_DEFAULTS, + }; + + FusionInfo getFusionServerInfo(Coin coin) { + return _fusionServerInfo[coin] ?? _fusionServerInfoDefaults[coin]!; + } + + void setFusionServerInfo(Coin coin, FusionInfo fusionServerInfo) { + if (_fusionServerInfo[coin] != fusionServerInfo) { + _fusionServerInfo[coin] = fusionServerInfo; - set fusionServerInfoBch(FusionInfo fusionServerInfo) { - if (fusionServerInfoBch != fusionServerInfo) { DB.instance.put( + boxName: DB.boxNamePrefs, + key: "fusionServerInfoMap", + value: _fusionServerInfo.map( + (key, value) => MapEntry( + key.name, + value.toJsonString(), + ), + ), + ); + notifyListeners(); + } + } + + Future> _getFusionServerInfo() async { + final map = await DB.instance.get( + boxName: DB.boxNamePrefs, + key: "fusionServerInfoMap", + ) as Map?; + + if (map == null) { + return _fusionServerInfo; + } + + final actualMap = Map.from(map).map( + (key, value) => MapEntry( + coinFromPrettyName(key), + FusionInfo.fromJsonString(value), + ), + ); + + // legacy bch check + if (actualMap[Coin.bitcoincash] == null || + actualMap[Coin.bitcoincashTestnet] == null) { + final saved = await DB.instance.get( boxName: DB.boxNamePrefs, key: "fusionServerInfo", - value: fusionServerInfo.toJsonString(), - ); - _fusionServerInfoBch = fusionServerInfo; - notifyListeners(); + ) as String?; + + if (saved != null) { + final bchInfo = FusionInfo.fromJsonString(saved); + actualMap[Coin.bitcoincash] = bchInfo; + actualMap[Coin.bitcoincashTestnet] = bchInfo; + unawaited( + DB.instance.put( + boxName: DB.boxNamePrefs, + key: "fusionServerInfoMap", + value: actualMap.map( + (key, value) => MapEntry( + key.name, + value.toJsonString(), + ), + ), + ), + ); + } } - } - set fusionServerInfoXec(FusionInfo fusionServerInfo) { - if (fusionServerInfoXec != fusionServerInfo) { - DB.instance.put( - boxName: DB.boxNamePrefs, - key: "fusionServerInfoXec", - value: fusionServerInfo.toJsonString(), - ); - _fusionServerInfoXec = fusionServerInfo; - notifyListeners(); - } - } - - Future _getFusionServerInfoBch() async { - final saved = await DB.instance.get( - boxName: DB.boxNamePrefs, - key: "fusionServerInfoBch", - ) as String?; - - try { - return FusionInfo.fromJsonString(saved!); - } catch (_) { - return FusionInfo.BCH_DEFAULTS; - } - } - - Future _getFusionServerInfoXec() async { - final saved = await DB.instance.get( - boxName: DB.boxNamePrefs, - key: "fusionServerInfoXec", - ) as String?; - - try { - return FusionInfo.fromJsonString(saved!); - } catch (_) { - return FusionInfo.XEC_DEFAULTS; - } + return actualMap; } }