refactor fusion server prefs

This commit is contained in:
julian 2023-11-14 16:23:57 -06:00
parent 28efe3e18d
commit be66c71154
5 changed files with 109 additions and 86 deletions

View file

@ -55,7 +55,7 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
late final FocusNode portFocusNode; late final FocusNode portFocusNode;
late final TextEditingController fusionRoundController; late final TextEditingController fusionRoundController;
late final FocusNode fusionRoundFocusNode; late final FocusNode fusionRoundFocusNode;
Coin? coin; late final Coin coin;
bool _enableSSLCheckbox = false; bool _enableSSLCheckbox = false;
bool _enableStartButton = false; bool _enableStartButton = false;
@ -89,11 +89,7 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
); );
// update user prefs (persistent) // update user prefs (persistent)
if (coin == Coin.bitcoincash) { ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo);
ref.read(prefsChangeNotifierProvider).fusionServerInfoBch = newInfo;
} else {
ref.read(prefsChangeNotifierProvider).fusionServerInfoXec = newInfo;
}
unawaited( unawaited(
fusionWallet.fuse( fusionWallet.fuse(
@ -123,9 +119,8 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
.wallet .wallet
.coin; .coin;
final info = (coin == Coin.bitcoincash) final info =
? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
: ref.read(prefsChangeNotifierProvider).fusionServerInfoXec;
serverController.text = info.host; serverController.text = info.host;
portController.text = info.port.toString(); portController.text = info.port.toString();

View file

@ -43,7 +43,7 @@ class FusionProgressView extends ConsumerStatefulWidget {
} }
class _FusionProgressViewState extends ConsumerState<FusionProgressView> { class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
Coin? coin; late final Coin coin;
Future<bool> _requestAndProcessCancel() async { Future<bool> _requestAndProcessCancel() async {
final shouldCancel = await showDialog<bool?>( final shouldCancel = await showDialog<bool?>(
@ -88,6 +88,16 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
} }
} }
@override
void initState() {
coin = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet
.coin;
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bool _succeeded = final bool _succeeded =
@ -100,12 +110,6 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
.watch(fusionProgressUIStateProvider(widget.walletId)) .watch(fusionProgressUIStateProvider(widget.walletId))
.fusionRoundsCompleted; .fusionRoundsCompleted;
coin = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet
.coin;
return WillPopScope( return WillPopScope(
onWillPop: () async { onWillPop: () async {
return await _requestAndProcessCancel(); return await _requestAndProcessCancel();
@ -234,9 +238,8 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
final fusionWallet = final fusionWallet =
ref.read(pWallets).getWallet(widget.walletId) as CashFusion; ref.read(pWallets).getWallet(widget.walletId) as CashFusion;
final fusionInfo = (coin == Coin.bitcoincash) final fusionInfo =
? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
: ref.read(prefsChangeNotifierProvider).fusionServerInfoXec;
try { try {
fusionWallet.uiState = ref.read( fusionWallet.uiState = ref.read(

View file

@ -93,11 +93,8 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
); );
// update user prefs (persistent) // update user prefs (persistent)
if (coin == Coin.bitcoincash) {
ref.read(prefsChangeNotifierProvider).fusionServerInfoBch = newInfo; ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo);
} else {
ref.read(prefsChangeNotifierProvider).fusionServerInfoXec = newInfo;
}
unawaited( unawaited(
fusionWallet.fuse( fusionWallet.fuse(
@ -132,9 +129,8 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
.wallet .wallet
.coin; .coin;
final info = (coin == Coin.bitcoincash) final info =
? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
: ref.read(prefsChangeNotifierProvider).fusionServerInfoXec;
serverController.text = info.host; serverController.text = info.host;
portController.text = info.port.toString(); portController.text = info.port.toString();

View file

@ -40,7 +40,7 @@ class FusionDialogView extends ConsumerStatefulWidget {
} }
class _FusionDialogViewState extends ConsumerState<FusionDialogView> { class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
Coin? coin; late final Coin coin;
Future<bool> _requestAndProcessCancel() async { Future<bool> _requestAndProcessCancel() async {
if (!ref.read(fusionProgressUIStateProvider(widget.walletId)).running) { if (!ref.read(fusionProgressUIStateProvider(widget.walletId)).running) {
@ -142,6 +142,16 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
} }
} }
@override
void initState() {
coin = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet
.coin;
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bool _succeeded = final bool _succeeded =
@ -154,12 +164,6 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
.watch(fusionProgressUIStateProvider(widget.walletId)) .watch(fusionProgressUIStateProvider(widget.walletId))
.fusionRoundsCompleted; .fusionRoundsCompleted;
coin = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet
.coin;
return DesktopDialog( return DesktopDialog(
maxHeight: 600, maxHeight: 600,
child: SingleChildScrollView( child: SingleChildScrollView(
@ -293,9 +297,8 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
final fusionWallet = final fusionWallet =
ref.read(pWallets).getWallet(widget.walletId) as CashFusion; ref.read(pWallets).getWallet(widget.walletId) as CashFusion;
final fusionInfo = (coin == Coin.bitcoincash) final fusionInfo =
? ref.read(prefsChangeNotifierProvider).fusionServerInfoBch ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
: ref.read(prefsChangeNotifierProvider).fusionServerInfoXec;
try { try {
fusionWallet.uiState = ref.read( fusionWallet.uiState = ref.read(

View file

@ -8,6 +8,8 @@
* *
*/ */
import 'dart:async';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/db/hive/db.dart';
import 'package:stackwallet/services/event_bus/events/global/tor_status_changed_event.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 _setAmountUnits();
await _setMaxDecimals(); await _setMaxDecimals();
_useTor = await _getUseTor(); _useTor = await _getUseTor();
_fusionServerInfoBch = await _getFusionServerInfoBch(); _fusionServerInfo = await _getFusionServerInfo();
_fusionServerInfoXec = await _getFusionServerInfoXec();
_initialized = true; _initialized = true;
} }
@ -937,59 +938,84 @@ class Prefs extends ChangeNotifier {
// fusion server info // fusion server info
FusionInfo _fusionServerInfoBch = FusionInfo.BCH_DEFAULTS; final Map<Coin, FusionInfo> _fusionServerInfoDefaults = {
FusionInfo _fusionServerInfoXec = FusionInfo.XEC_DEFAULTS; Coin.bitcoincash: FusionInfo.BCH_DEFAULTS,
Coin.bitcoincashTestnet: FusionInfo.BCH_DEFAULTS,
Coin.eCash: FusionInfo.XEC_DEFAULTS,
};
FusionInfo get fusionServerInfoBch => _fusionServerInfoBch; Map<Coin, FusionInfo> _fusionServerInfo = {
FusionInfo get fusionServerInfoXec => _fusionServerInfoXec; 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<dynamic>( DB.instance.put<dynamic>(
boxName: DB.boxNamePrefs,
key: "fusionServerInfoMap",
value: _fusionServerInfo.map(
(key, value) => MapEntry(
key.name,
value.toJsonString(),
),
),
);
notifyListeners();
}
}
Future<Map<Coin, FusionInfo>> _getFusionServerInfo() async {
final map = await DB.instance.get<dynamic>(
boxName: DB.boxNamePrefs,
key: "fusionServerInfoMap",
) as Map?;
if (map == null) {
return _fusionServerInfo;
}
final actualMap = Map<String, String>.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<dynamic>(
boxName: DB.boxNamePrefs, boxName: DB.boxNamePrefs,
key: "fusionServerInfo", key: "fusionServerInfo",
value: fusionServerInfo.toJsonString(), ) as String?;
);
_fusionServerInfoBch = fusionServerInfo;
notifyListeners();
}
}
set fusionServerInfoXec(FusionInfo fusionServerInfo) { if (saved != null) {
if (fusionServerInfoXec != fusionServerInfo) { final bchInfo = FusionInfo.fromJsonString(saved);
actualMap[Coin.bitcoincash] = bchInfo;
actualMap[Coin.bitcoincashTestnet] = bchInfo;
unawaited(
DB.instance.put<dynamic>( DB.instance.put<dynamic>(
boxName: DB.boxNamePrefs, boxName: DB.boxNamePrefs,
key: "fusionServerInfoXec", key: "fusionServerInfoMap",
value: fusionServerInfo.toJsonString(), value: actualMap.map(
(key, value) => MapEntry(
key.name,
value.toJsonString(),
),
),
),
); );
_fusionServerInfoXec = fusionServerInfo;
notifyListeners();
} }
} }
Future<FusionInfo> _getFusionServerInfoBch() async { return actualMap;
final saved = await DB.instance.get<dynamic>(
boxName: DB.boxNamePrefs,
key: "fusionServerInfoBch",
) as String?;
try {
return FusionInfo.fromJsonString(saved!);
} catch (_) {
return FusionInfo.BCH_DEFAULTS;
}
}
Future<FusionInfo> _getFusionServerInfoXec() async {
final saved = await DB.instance.get<dynamic>(
boxName: DB.boxNamePrefs,
key: "fusionServerInfoXec",
) as String?;
try {
return FusionInfo.fromJsonString(saved!);
} catch (_) {
return FusionInfo.XEC_DEFAULTS;
}
} }
} }