2023-05-03 20:03:31 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
2022-10-04 17:06:14 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-05-03 20:03:31 +00:00
|
|
|
import 'package:stackwallet/models/exchange/active_pair.dart';
|
|
|
|
import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
|
|
|
|
import 'package:stackwallet/models/exchange/response_objects/range.dart';
|
|
|
|
import 'package:stackwallet/services/exchange/exchange.dart';
|
2023-05-04 18:19:50 +00:00
|
|
|
import 'package:stackwallet/services/exchange/exchange_response.dart';
|
2023-05-03 20:03:31 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
2022-10-04 17:06:14 +00:00
|
|
|
|
2023-05-04 18:19:50 +00:00
|
|
|
final efEstimatesListProvider = StateProvider.family<
|
|
|
|
Tuple2<ExchangeResponse<List<Estimate>>, Range?>?,
|
|
|
|
String>((ref, exchangeName) => null);
|
2023-05-03 20:03:31 +00:00
|
|
|
|
|
|
|
final efRateTypeProvider =
|
|
|
|
StateProvider<ExchangeRateType>((ref) => ExchangeRateType.estimated);
|
|
|
|
|
|
|
|
final efExchangeProvider =
|
|
|
|
StateProvider<Exchange>((ref) => Exchange.defaultExchange);
|
|
|
|
final efExchangeProviderNameProvider =
|
|
|
|
StateProvider<String>((ref) => Exchange.defaultExchange.name);
|
|
|
|
|
|
|
|
final currentCombinedExchangeIdProvider = Provider<String>((ref) {
|
|
|
|
return "${ref.watch(efExchangeProvider).name}"
|
|
|
|
" (${ref.watch(efExchangeProviderNameProvider)})";
|
|
|
|
});
|
|
|
|
|
|
|
|
final efSendAmountProvider = StateProvider<Decimal?>((ref) => null);
|
|
|
|
final efReceiveAmountProvider = StateProvider<Decimal?>((ref) => null);
|
|
|
|
|
|
|
|
final efSendAmountStringProvider = StateProvider<String>((ref) {
|
2023-05-04 14:58:50 +00:00
|
|
|
final refreshing = ref.watch(efRefreshingProvider);
|
|
|
|
final reversed = ref.watch(efReversedProvider);
|
|
|
|
if (refreshing && reversed) {
|
|
|
|
return "-";
|
|
|
|
} else {
|
2023-05-05 14:20:37 +00:00
|
|
|
return ref.watch(efSendAmountProvider)?.toString() ?? "";
|
2023-05-04 14:58:50 +00:00
|
|
|
}
|
2023-05-03 20:03:31 +00:00
|
|
|
});
|
|
|
|
final efReceiveAmountStringProvider = StateProvider<String>((ref) {
|
2023-05-04 14:58:50 +00:00
|
|
|
final refreshing = ref.watch(efRefreshingProvider);
|
|
|
|
final reversed = ref.watch(efReversedProvider);
|
|
|
|
|
|
|
|
if (refreshing && reversed == false) {
|
|
|
|
return "-";
|
|
|
|
} else {
|
2023-05-05 14:20:37 +00:00
|
|
|
return ref.watch(efReceiveAmountProvider)?.toString() ?? "";
|
2023-05-04 14:58:50 +00:00
|
|
|
}
|
2023-05-03 20:03:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
final efReversedProvider = StateProvider<bool>((ref) => false);
|
|
|
|
|
|
|
|
final efCurrencyPairProvider = ChangeNotifierProvider<ActivePair>(
|
|
|
|
(ref) => ActivePair(),
|
|
|
|
);
|
|
|
|
|
|
|
|
final efEstimateProvider = StateProvider<Estimate?>((ref) {
|
|
|
|
final exchange = ref.watch(efExchangeProvider);
|
|
|
|
final provider = ref.watch(efExchangeProviderNameProvider);
|
|
|
|
final reversed = ref.watch(efReversedProvider);
|
|
|
|
final fixedRate = ref.watch(efRateTypeProvider) == ExchangeRateType.fixed;
|
|
|
|
|
2023-05-04 18:19:50 +00:00
|
|
|
final matches = ref
|
|
|
|
.watch(efEstimatesListProvider(exchange.name))
|
|
|
|
?.item1
|
|
|
|
.value
|
|
|
|
?.where((e) {
|
2023-05-03 20:03:31 +00:00
|
|
|
return e.exchangeProvider == provider &&
|
|
|
|
e.fixedRate == fixedRate &&
|
|
|
|
e.reversed == reversed;
|
|
|
|
});
|
|
|
|
|
|
|
|
Estimate? result;
|
|
|
|
|
|
|
|
if (matches != null && matches.isNotEmpty) {
|
|
|
|
result = matches.first;
|
|
|
|
} else {
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
final efCanExchangeProvider = StateProvider<bool>((ref) {
|
|
|
|
final Estimate? estimate = ref.watch(efEstimateProvider);
|
2023-05-03 21:47:12 +00:00
|
|
|
final refreshing = ref.watch(efRefreshingProvider);
|
2023-05-03 20:03:31 +00:00
|
|
|
|
2023-05-03 21:47:12 +00:00
|
|
|
return !refreshing && estimate != null;
|
2023-05-03 20:03:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
final efRefreshingProvider = StateProvider<bool>((ref) => false);
|