centralize exchange rate type parameter

This commit is contained in:
julian 2023-02-08 14:48:09 -06:00
parent 5c29a543a9
commit 15a44d9dff
4 changed files with 46 additions and 56 deletions

View file

@ -130,7 +130,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
}
Future<AggregateCurrency> _getAggregateCurrency(Currency currency) async {
final rateType = ref.read(prefsChangeNotifierProvider).exchangeRateType;
final rateType = ref.read(exchangeFormStateProvider).exchangeRateType;
final currencies = await ExchangeDataLoadingService.instance.isar.currencies
.filter()
.group((q) => rateType == ExchangeRateType.fixed
@ -159,7 +159,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
}
void selectSendCurrency() async {
final type = (ref.read(prefsChangeNotifierProvider).exchangeRateType);
final type = (ref.read(exchangeFormStateProvider).exchangeRateType);
final fromTicker = ref.read(exchangeFormStateProvider).fromTicker ?? "";
if (walletInitiated &&
@ -197,7 +197,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
willChange: ref.read(exchangeFormStateProvider).receiveCurrency?.ticker,
willChangeIsSend: false,
paired: ref.read(exchangeFormStateProvider).sendCurrency?.ticker,
isFixedRate: ref.read(prefsChangeNotifierProvider).exchangeRateType ==
isFixedRate: ref.read(exchangeFormStateProvider).exchangeRateType ==
ExchangeRateType.fixed,
);
@ -380,7 +380,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
}
void onExchangePressed() async {
final rateType = ref.read(prefsChangeNotifierProvider).exchangeRateType;
final rateType = ref.read(exchangeFormStateProvider).exchangeRateType;
final fromTicker = ref.read(exchangeFormStateProvider).fromTicker ?? "";
final toTicker = ref.read(exchangeFormStateProvider).toTicker ?? "";
final sendAmount = ref.read(exchangeFormStateProvider).sendAmount!;
@ -671,7 +671,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
debugPrint("BUILD: $runtimeType");
final rateType = ref.watch(
prefsChangeNotifierProvider.select((value) => value.exchangeRateType));
exchangeFormStateProvider.select((value) => value.exchangeRateType));
final isEstimated = rateType == ExchangeRateType.estimated;
@ -816,7 +816,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
background:
Theme.of(context).extension<StackColors>()!.textFieldDefaultBG,
onTap: () {
if (!(ref.read(prefsChangeNotifierProvider).exchangeRateType ==
if (!(ref.read(exchangeFormStateProvider).exchangeRateType ==
ExchangeRateType.estimated) &&
_receiveController.text == "-") {
_receiveController.text = "";

View file

@ -24,16 +24,12 @@ class RateTypeToggle extends ConsumerWidget {
return Toggle(
onValueChanged: (value) {
if (value) {
ref.read(prefsChangeNotifierProvider).exchangeRateType =
ExchangeRateType.fixed;
onChanged?.call(ExchangeRateType.fixed);
} else {
ref.read(prefsChangeNotifierProvider).exchangeRateType =
ExchangeRateType.estimated;
onChanged?.call(ExchangeRateType.estimated);
}
},
isOn: ref.watch(prefsChangeNotifierProvider
isOn: ref.watch(exchangeFormStateProvider
.select((value) => value.exchangeRateType)) ==
ExchangeRateType.fixed,
onColor: isDesktop

View file

@ -13,7 +13,6 @@ import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/stack_restoring_ui_state.dart';
import 'package:stackwallet/models/trade_wallet_lookup.dart';
import 'package:stackwallet/models/wallet_restore_state.dart';
import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
import 'package:stackwallet/services/address_book_service.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/coins/manager.dart';
@ -246,7 +245,6 @@ abstract class SWB {
final _prefs = Prefs.instance;
await _prefs.init();
prefs['currency'] = _prefs.currency;
prefs['exchangeRateType'] = _prefs.exchangeRateType.name;
prefs['useBiometrics'] = _prefs.useBiometrics;
prefs['hasPin'] = _prefs.hasPin;
prefs['language'] = _prefs.language;
@ -992,9 +990,6 @@ abstract class SWB {
final _prefs = Prefs.instance;
await _prefs.init();
_prefs.currency = prefs['currency'] as String;
_prefs.exchangeRateType = prefs['exchangeRateType'] == "estimated"
? ExchangeRateType.estimated
: ExchangeRateType.fixed;
// _prefs.useBiometrics = prefs['useBiometrics'] as bool;
// _prefs.hasPin = prefs['hasPin'] as bool;
_prefs.language = prefs['language'] as String;

View file

@ -1,6 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/enums/languages_enum.dart';
@ -18,7 +17,7 @@ class Prefs extends ChangeNotifier {
Future<void> init() async {
if (!_initialized) {
_currency = await _getPreferredCurrency();
_exchangeRateType = await _getExchangeRateType();
// _exchangeRateType = await _getExchangeRateType();
_useBiometrics = await _getUseBiometrics();
_hasPin = await _getHasPin();
_language = await _getPreferredLanguage();
@ -251,44 +250,44 @@ class Prefs extends ChangeNotifier {
// exchange rate type
ExchangeRateType _exchangeRateType = ExchangeRateType.estimated;
ExchangeRateType get exchangeRateType => _exchangeRateType;
set exchangeRateType(ExchangeRateType exchangeRateType) {
if (_exchangeRateType != exchangeRateType) {
switch (exchangeRateType) {
case ExchangeRateType.estimated:
DB.instance.put<dynamic>(
boxName: DB.boxNamePrefs,
key: "exchangeRateType",
value: "estimated");
break;
case ExchangeRateType.fixed:
DB.instance.put<dynamic>(
boxName: DB.boxNamePrefs,
key: "exchangeRateType",
value: "fixed");
break;
}
_exchangeRateType = exchangeRateType;
notifyListeners();
}
}
Future<ExchangeRateType> _getExchangeRateType() async {
String? rate = await DB.instance.get<dynamic>(
boxName: DB.boxNamePrefs, key: "exchangeRateType") as String?;
rate ??= "estimated";
switch (rate) {
case "estimated":
return ExchangeRateType.estimated;
case "fixed":
return ExchangeRateType.fixed;
default:
throw Exception("Invalid exchange rate type found in prefs!");
}
}
// ExchangeRateType _exchangeRateType = ExchangeRateType.estimated;
//
// ExchangeRateType get exchangeRateType => _exchangeRateType;
//
// set exchangeRateType(ExchangeRateType exchangeRateType) {
// if (_exchangeRateType != exchangeRateType) {
// switch (exchangeRateType) {
// case ExchangeRateType.estimated:
// DB.instance.put<dynamic>(
// boxName: DB.boxNamePrefs,
// key: "exchangeRateType",
// value: "estimated");
// break;
// case ExchangeRateType.fixed:
// DB.instance.put<dynamic>(
// boxName: DB.boxNamePrefs,
// key: "exchangeRateType",
// value: "fixed");
// break;
// }
// _exchangeRateType = exchangeRateType;
// notifyListeners();
// }
// }
//
// Future<ExchangeRateType> _getExchangeRateType() async {
// String? rate = await DB.instance.get<dynamic>(
// boxName: DB.boxNamePrefs, key: "exchangeRateType") as String?;
// rate ??= "estimated";
// switch (rate) {
// case "estimated":
// return ExchangeRateType.estimated;
// case "fixed":
// return ExchangeRateType.fixed;
// default:
// throw Exception("Invalid exchange rate type found in prefs!");
// }
// }
// use biometrics