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 name => _map.values.first!.name;
String get image => _map.values.first!.image;
SupportedRateType get rateType => _map.values.first!.rateType;
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/response_objects/estimate.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/majestic_bank/majestic_bank_exchange.dart';
import 'package:stackwallet/utilities/logger.dart';
class ExchangeFormState extends ChangeNotifier {
@ -323,6 +325,29 @@ class ExchangeFormState extends ChangeNotifier {
required bool shouldNotifyListeners,
}) async {
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 _updateEstimate(shouldNotifyListeners: false);
if (shouldNotifyListeners) {
@ -452,6 +477,25 @@ class ExchangeFormState extends ChangeNotifier {
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
String toString() {
return "{"

View file

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

View file

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