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 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<CashFusionView> {
);
// 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<CashFusionView> {
.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();

View file

@ -43,7 +43,7 @@ class FusionProgressView extends ConsumerStatefulWidget {
}
class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
Coin? coin;
late final Coin coin;
Future<bool> _requestAndProcessCancel() async {
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
Widget build(BuildContext context) {
final bool _succeeded =
@ -100,12 +110,6 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
.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<FusionProgressView> {
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(

View file

@ -93,11 +93,8 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
);
// 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<DesktopCashFusionView> {
.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();

View file

@ -40,7 +40,7 @@ class FusionDialogView extends ConsumerStatefulWidget {
}
class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
Coin? coin;
late final Coin coin;
Future<bool> _requestAndProcessCancel() async {
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
Widget build(BuildContext context) {
final bool _succeeded =
@ -154,12 +164,6 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
.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<FusionDialogView> {
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(

View file

@ -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<Coin, FusionInfo> _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<Coin, FusionInfo> _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<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,
key: "fusionServerInfo",
value: fusionServerInfo.toJsonString(),
);
_fusionServerInfoBch = fusionServerInfo;
notifyListeners();
}
}
) as String?;
set fusionServerInfoXec(FusionInfo fusionServerInfo) {
if (fusionServerInfoXec != fusionServerInfo) {
if (saved != null) {
final bchInfo = FusionInfo.fromJsonString(saved);
actualMap[Coin.bitcoincash] = bchInfo;
actualMap[Coin.bitcoincashTestnet] = bchInfo;
unawaited(
DB.instance.put<dynamic>(
boxName: DB.boxNamePrefs,
key: "fusionServerInfoXec",
value: fusionServerInfo.toJsonString(),
key: "fusionServerInfoMap",
value: actualMap.map(
(key, value) => MapEntry(
key.name,
value.toJsonString(),
),
),
),
);
_fusionServerInfoXec = fusionServerInfo;
notifyListeners();
}
}
Future<FusionInfo> _getFusionServerInfoBch() async {
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;
}
return actualMap;
}
}