only show exchanges supported by selected aggregate currency pair based on exchange flow type

This commit is contained in:
julian 2023-02-08 14:06:58 -06:00
parent b1c8a56ba6
commit 4630d616cd
5 changed files with 543 additions and 432 deletions

View file

@ -19,8 +19,22 @@ class AggregateCurrency {
} }
String get ticker => _map.values.first!.ticker; String get ticker => _map.values.first!.ticker;
String get name => _map.values.first!.name; String get name => _map.values.first!.name;
String get image => _map.values.first!.image; String get image => _map.values.first!.image;
SupportedRateType get rateType => _map.values.first!.rateType; SupportedRateType get rateType => _map.values.first!.rateType;
bool get isStackCoin => _map.values.first!.isStackCoin; bool get isStackCoin => _map.values.first!.isStackCoin;
@override
String toString() {
String str = "AggregateCurrency: {";
for (final key in _map.keys) {
str += " $key: ${_map[key]},";
}
str += " }";
return str;
}
} }

View file

@ -3,7 +3,9 @@ import 'package:flutter/foundation.dart';
import 'package:stackwallet/models/exchange/aggregate_currency.dart'; import 'package:stackwallet/models/exchange/aggregate_currency.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart'; import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange.dart'; import 'package:stackwallet/services/exchange/exchange.dart';
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
class ExchangeFormState extends ChangeNotifier { class ExchangeFormState extends ChangeNotifier {
@ -323,6 +325,29 @@ class ExchangeFormState extends ChangeNotifier {
required bool shouldNotifyListeners, required bool shouldNotifyListeners,
}) async { }) async {
try { try {
switch (exchange.name) {
case ChangeNowExchange.exchangeName:
if (!_exchangeSupported(
exchangeName: exchange.name,
sendCurrency: sendCurrency,
receiveCurrency: receiveCurrency,
exchangeRateType: exchangeRateType,
)) {
_exchange = MajesticBankExchange.instance;
}
break;
case MajesticBankExchange.exchangeName:
if (!_exchangeSupported(
exchangeName: exchange.name,
sendCurrency: sendCurrency,
receiveCurrency: receiveCurrency,
exchangeRateType: exchangeRateType,
)) {
_exchange = ChangeNowExchange.instance;
}
break;
}
await _updateRanges(shouldNotifyListeners: false); await _updateRanges(shouldNotifyListeners: false);
await _updateEstimate(shouldNotifyListeners: false); await _updateEstimate(shouldNotifyListeners: false);
if (shouldNotifyListeners) { if (shouldNotifyListeners) {
@ -452,6 +477,25 @@ class ExchangeFormState extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
bool _exchangeSupported({
required String exchangeName,
required AggregateCurrency? sendCurrency,
required AggregateCurrency? receiveCurrency,
required ExchangeRateType exchangeRateType,
}) {
final send = sendCurrency?.forExchange(exchangeName);
if (send == null) return false;
final rcv = receiveCurrency?.forExchange(exchangeName);
if (rcv == null) return false;
if (exchangeRateType == ExchangeRateType.fixed) {
return send.supportsFixedRate && rcv.supportsFixedRate;
} else {
return send.supportsEstimatedRate && rcv.supportsEstimatedRate;
}
}
@override @override
String toString() { String toString() {
return "{" return "{"

View file

@ -44,6 +44,12 @@ class Currency {
@Index() @Index()
final bool isStackCoin; final bool isStackCoin;
@ignore
bool get supportsFixedRate => rateType == SupportedRateType.fixed || rateType == SupportedRateType.both;
@ignore
bool get supportsEstimatedRate => rateType == SupportedRateType.estimated || rateType == SupportedRateType.both;
Currency({ Currency({
required this.exchangeName, required this.exchangeName,
required this.ticker, required this.ticker,

View file

@ -862,10 +862,6 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
if (ref.watch(exchangeFormStateProvider).sendAmount != null && if (ref.watch(exchangeFormStateProvider).sendAmount != null &&
ref.watch(exchangeFormStateProvider).sendAmount != Decimal.zero) ref.watch(exchangeFormStateProvider).sendAmount != Decimal.zero)
ExchangeProviderOptions( ExchangeProviderOptions(
from: ref.watch(exchangeFormStateProvider).fromTicker,
to: ref.watch(exchangeFormStateProvider).toTicker,
fromAmount: ref.watch(exchangeFormStateProvider).sendAmount,
toAmount: ref.watch(exchangeFormStateProvider).receiveAmount,
fixedRate: rateType == ExchangeRateType.fixed, fixedRate: rateType == ExchangeRateType.fixed,
reversed: ref.watch( reversed: ref.watch(
exchangeFormStateProvider.select((value) => value.reversed)), exchangeFormStateProvider.select((value) => value.reversed)),

View file

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/exceptions/exchange/pair_unavailable_exception.dart'; import 'package:stackwallet/exceptions/exchange/pair_unavailable_exception.dart';
import 'package:stackwallet/models/exchange/aggregate_currency.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
@ -20,27 +21,62 @@ import 'package:stackwallet/widgets/animated_text.dart';
import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart';
class ExchangeProviderOptions extends ConsumerWidget { class ExchangeProviderOptions extends ConsumerStatefulWidget {
const ExchangeProviderOptions({ const ExchangeProviderOptions({
Key? key, Key? key,
required this.from,
required this.to,
required this.fromAmount,
required this.toAmount,
required this.fixedRate, required this.fixedRate,
required this.reversed, required this.reversed,
}) : super(key: key); }) : super(key: key);
final String? from;
final String? to;
final Decimal? fromAmount;
final Decimal? toAmount;
final bool fixedRate; final bool fixedRate;
final bool reversed; final bool reversed;
@override @override
Widget build(BuildContext context, WidgetRef ref) { ConsumerState<ExchangeProviderOptions> createState() =>
_ExchangeProviderOptionsState();
}
class _ExchangeProviderOptionsState
extends ConsumerState<ExchangeProviderOptions> {
final isDesktop = Util.isDesktop; final isDesktop = Util.isDesktop;
bool exchangeSupported({
required String exchangeName,
required AggregateCurrency? sendCurrency,
required AggregateCurrency? receiveCurrency,
}) {
final send = sendCurrency?.forExchange(exchangeName);
if (send == null) return false;
final rcv = receiveCurrency?.forExchange(exchangeName);
if (rcv == null) return false;
if (widget.fixedRate) {
return send.supportsFixedRate && rcv.supportsFixedRate;
} else {
return send.supportsEstimatedRate && rcv.supportsEstimatedRate;
}
}
@override
Widget build(BuildContext context) {
final sendCurrency = ref.watch(exchangeFormStateProvider).sendCurrency;
final receivingCurrency =
ref.watch(exchangeFormStateProvider).receiveCurrency;
final fromAmount = ref.watch(exchangeFormStateProvider).sendAmount;
final toAmount = ref.watch(exchangeFormStateProvider).receiveAmount;
final showChangeNow = exchangeSupported(
exchangeName: ChangeNowExchange.exchangeName,
sendCurrency: sendCurrency,
receiveCurrency: receivingCurrency,
);
final showMajesticBank = exchangeSupported(
exchangeName: MajesticBankExchange.exchangeName,
sendCurrency: sendCurrency,
receiveCurrency: receivingCurrency,
);
return RoundedWhiteContainer( return RoundedWhiteContainer(
padding: isDesktop ? const EdgeInsets.all(0) : const EdgeInsets.all(12), padding: isDesktop ? const EdgeInsets.all(0) : const EdgeInsets.all(12),
borderColor: isDesktop borderColor: isDesktop
@ -48,6 +84,7 @@ class ExchangeProviderOptions extends ConsumerWidget {
: null, : null,
child: Column( child: Column(
children: [ children: [
if (showChangeNow)
ConditionalParent( ConditionalParent(
condition: isDesktop, condition: isDesktop,
builder: (child) => MouseRegion( builder: (child) => MouseRegion(
@ -91,21 +128,23 @@ class ExchangeProviderOptions extends ConsumerWidget {
.watch(currentExchangeNameStateProvider.state) .watch(currentExchangeNameStateProvider.state)
.state, .state,
onChanged: (_) { onChanged: (_) {
// if (value is String) { if (ref
// ref .read(currentExchangeNameStateProvider
// .read( .state)
// currentExchangeNameStateProvider.state) .state !=
// .state = value; ChangeNowExchange.exchangeName) {
// ref ref
// .read(exchangeFormStateProvider(ref .read(currentExchangeNameStateProvider
// .read(prefsChangeNotifierProvider) .state)
// .exchangeRateType)) .state = ChangeNowExchange.exchangeName;
// .exchange = ref
// Exchange.fromName(ref .read(exchangeFormStateProvider)
// .read(currentExchangeNameStateProvider .updateExchange(
// .state) exchange: ref.read(exchangeProvider),
// .state); shouldUpdateData: true,
// } shouldNotifyListeners: true,
);
}
}, },
), ),
), ),
@ -136,25 +175,27 @@ class ExchangeProviderOptions extends ConsumerWidget {
children: [ children: [
Text( Text(
ChangeNowExchange.exchangeName, ChangeNowExchange.exchangeName,
style: STextStyles.titleBold12(context).copyWith( style:
STextStyles.titleBold12(context).copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
.textDark2, .textDark2,
), ),
), ),
if (from != null && if (sendCurrency != null &&
to != null && receivingCurrency != null &&
toAmount != null && toAmount != null &&
toAmount! > Decimal.zero && toAmount > Decimal.zero &&
fromAmount != null && fromAmount != null &&
fromAmount! > Decimal.zero) fromAmount > Decimal.zero)
FutureBuilder( FutureBuilder(
future: ChangeNowExchange.instance.getEstimate( future:
from!, ChangeNowExchange.instance.getEstimate(
to!, sendCurrency.ticker,
reversed ? toAmount! : fromAmount!, receivingCurrency.ticker,
fixedRate, widget.reversed ? toAmount : fromAmount,
reversed, widget.fixedRate,
widget.reversed,
), ),
builder: (context, builder: (context,
AsyncSnapshot<ExchangeResponse<Estimate>> AsyncSnapshot<ExchangeResponse<Estimate>>
@ -166,26 +207,26 @@ class ExchangeProviderOptions extends ConsumerWidget {
if (estimate != null) { if (estimate != null) {
Decimal rate; Decimal rate;
if (estimate.reversed) { if (estimate.reversed) {
rate = (toAmount! / rate = (toAmount /
estimate.estimatedAmount) estimate.estimatedAmount)
.toDecimal( .toDecimal(
scaleOnInfinitePrecision: 12); scaleOnInfinitePrecision: 12);
} else { } else {
rate = (estimate.estimatedAmount / rate = (estimate.estimatedAmount /
fromAmount!) fromAmount)
.toDecimal( .toDecimal(
scaleOnInfinitePrecision: 12); scaleOnInfinitePrecision: 12);
} }
Coin coin; Coin coin;
try { try {
coin = coin = coinFromTickerCaseInsensitive(
coinFromTickerCaseInsensitive(to!); receivingCurrency.ticker);
} catch (_) { } catch (_) {
coin = Coin.bitcoin; coin = Coin.bitcoin;
} }
return Text( return Text(
"1 ${from!.toUpperCase()} ~ ${Format.localizedStringAsFixed( "1 ${sendCurrency.ticker.toUpperCase()} ~ ${Format.localizedStringAsFixed(
value: rate, value: rate,
locale: ref.watch( locale: ref.watch(
localeServiceChangeNotifierProvider localeServiceChangeNotifierProvider
@ -195,9 +236,9 @@ class ExchangeProviderOptions extends ConsumerWidget {
decimalPlaces: decimalPlaces:
Constants.decimalPlacesForCoin( Constants.decimalPlacesForCoin(
coin), coin),
)} ${to!.toUpperCase()}", )} ${receivingCurrency.ticker.toUpperCase()}",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -208,8 +249,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
is PairUnavailableException) { is PairUnavailableException) {
return Text( return Text(
"Unsupported pair", "Unsupported pair",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -223,8 +264,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
); );
return Text( return Text(
"Failed to fetch rate", "Failed to fetch rate",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -240,7 +281,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
"Loading..", "Loading..",
"Loading...", "Loading...",
], ],
style: STextStyles.itemSubtitle12(context) style:
STextStyles.itemSubtitle12(context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -250,12 +292,12 @@ class ExchangeProviderOptions extends ConsumerWidget {
} }
}, },
), ),
if (!(from != null && if (!(sendCurrency != null &&
to != null && receivingCurrency != null &&
toAmount != null && toAmount != null &&
toAmount! > Decimal.zero && toAmount > Decimal.zero &&
fromAmount != null && fromAmount != null &&
fromAmount! > Decimal.zero)) fromAmount > Decimal.zero))
Text( Text(
"n/a", "n/a",
style: STextStyles.itemSubtitle12(context) style: STextStyles.itemSubtitle12(context)
@ -274,15 +316,19 @@ class ExchangeProviderOptions extends ConsumerWidget {
), ),
), ),
), ),
if (isDesktop)
Container( if (showChangeNow && showMajesticBank)
isDesktop
? Container(
height: 1, height: 1,
color: Theme.of(context).extension<StackColors>()!.background, color:
), Theme.of(context).extension<StackColors>()!.background,
if (!isDesktop) )
const SizedBox( : const SizedBox(
height: 16, height: 16,
), ),
if (showMajesticBank)
ConditionalParent( ConditionalParent(
condition: isDesktop, condition: isDesktop,
builder: (child) => MouseRegion( builder: (child) => MouseRegion(
@ -326,21 +372,24 @@ class ExchangeProviderOptions extends ConsumerWidget {
.watch(currentExchangeNameStateProvider.state) .watch(currentExchangeNameStateProvider.state)
.state, .state,
onChanged: (_) { onChanged: (_) {
// if (value is String) { if (ref
// ref .read(currentExchangeNameStateProvider
// .read( .state)
// currentExchangeNameStateProvider.state) .state !=
// .state = value; MajesticBankExchange.exchangeName) {
// ref ref
// .read(exchangeFormStateProvider(ref .read(currentExchangeNameStateProvider
// .read(prefsChangeNotifierProvider) .state)
// .exchangeRateType)) .state =
// .exchange = MajesticBankExchange.exchangeName;
// Exchange.fromName(ref ref
// .read(currentExchangeNameStateProvider .read(exchangeFormStateProvider)
// .state) .updateExchange(
// .state); exchange: ref.read(exchangeProvider),
// } shouldUpdateData: true,
shouldNotifyListeners: true,
);
}
}, },
), ),
), ),
@ -371,26 +420,27 @@ class ExchangeProviderOptions extends ConsumerWidget {
children: [ children: [
Text( Text(
MajesticBankExchange.exchangeName, MajesticBankExchange.exchangeName,
style: STextStyles.titleBold12(context).copyWith( style:
STextStyles.titleBold12(context).copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
.textDark2, .textDark2,
), ),
), ),
if (from != null && if (sendCurrency != null &&
to != null && receivingCurrency != null &&
toAmount != null && toAmount != null &&
toAmount! > Decimal.zero && toAmount > Decimal.zero &&
fromAmount != null && fromAmount != null &&
fromAmount! > Decimal.zero) fromAmount > Decimal.zero)
FutureBuilder( FutureBuilder(
future: future:
MajesticBankExchange.instance.getEstimate( MajesticBankExchange.instance.getEstimate(
from!, sendCurrency.ticker,
to!, receivingCurrency.ticker,
reversed ? toAmount! : fromAmount!, widget.reversed ? toAmount : fromAmount,
fixedRate, widget.fixedRate,
reversed, widget.reversed,
), ),
builder: (context, builder: (context,
AsyncSnapshot<ExchangeResponse<Estimate>> AsyncSnapshot<ExchangeResponse<Estimate>>
@ -402,26 +452,26 @@ class ExchangeProviderOptions extends ConsumerWidget {
if (estimate != null) { if (estimate != null) {
Decimal rate; Decimal rate;
if (estimate.reversed) { if (estimate.reversed) {
rate = (toAmount! / rate = (toAmount /
estimate.estimatedAmount) estimate.estimatedAmount)
.toDecimal( .toDecimal(
scaleOnInfinitePrecision: 12); scaleOnInfinitePrecision: 12);
} else { } else {
rate = (estimate.estimatedAmount / rate = (estimate.estimatedAmount /
fromAmount!) fromAmount)
.toDecimal( .toDecimal(
scaleOnInfinitePrecision: 12); scaleOnInfinitePrecision: 12);
} }
Coin coin; Coin coin;
try { try {
coin = coin = coinFromTickerCaseInsensitive(
coinFromTickerCaseInsensitive(to!); receivingCurrency.ticker);
} catch (_) { } catch (_) {
coin = Coin.bitcoin; coin = Coin.bitcoin;
} }
return Text( return Text(
"1 ${from!.toUpperCase()} ~ ${Format.localizedStringAsFixed( "1 ${sendCurrency.ticker.toUpperCase()} ~ ${Format.localizedStringAsFixed(
value: rate, value: rate,
locale: ref.watch( locale: ref.watch(
localeServiceChangeNotifierProvider localeServiceChangeNotifierProvider
@ -431,9 +481,9 @@ class ExchangeProviderOptions extends ConsumerWidget {
decimalPlaces: decimalPlaces:
Constants.decimalPlacesForCoin( Constants.decimalPlacesForCoin(
coin), coin),
)} ${to!.toUpperCase()}", )} ${receivingCurrency.ticker.toUpperCase()}",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -444,8 +494,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
is PairUnavailableException) { is PairUnavailableException) {
return Text( return Text(
"Unsupported pair", "Unsupported pair",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -459,8 +509,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
); );
return Text( return Text(
"Failed to fetch rate", "Failed to fetch rate",
style: style: STextStyles.itemSubtitle12(
STextStyles.itemSubtitle12(context) context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -476,7 +526,8 @@ class ExchangeProviderOptions extends ConsumerWidget {
"Loading..", "Loading..",
"Loading...", "Loading...",
], ],
style: STextStyles.itemSubtitle12(context) style:
STextStyles.itemSubtitle12(context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!
@ -486,12 +537,12 @@ class ExchangeProviderOptions extends ConsumerWidget {
} }
}, },
), ),
if (!(from != null && if (!(sendCurrency != null &&
to != null && receivingCurrency != null &&
toAmount != null && toAmount != null &&
toAmount! > Decimal.zero && toAmount > Decimal.zero &&
fromAmount != null && fromAmount != null &&
fromAmount! > Decimal.zero)) fromAmount > Decimal.zero))
Text( Text(
"n/a", "n/a",
style: STextStyles.itemSubtitle12(context) style: STextStyles.itemSubtitle12(context)
@ -510,11 +561,11 @@ class ExchangeProviderOptions extends ConsumerWidget {
), ),
), ),
), ),
if (isDesktop) // if (isDesktop)
Container( // Container(
height: 1, // height: 1,
color: Theme.of(context).extension<StackColors>()!.background, // color: Theme.of(context).extension<StackColors>()!.background,
), // ),
// if (!isDesktop) // if (!isDesktop)
// const SizedBox( // const SizedBox(
// height: 16, // height: 16,