From 5c29a543a98aea6671c1587069518e62522a780f Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 8 Feb 2023 14:22:22 -0600 Subject: [PATCH] reduce unneeded providers --- lib/pages/exchange_view/exchange_form.dart | 5 +- .../exchange_step_views/step_2_view.dart | 6 +-- .../exchange_step_views/step_3_view.dart | 12 ++--- .../exchange_step_views/step_4_view.dart | 6 ++- .../exchange_provider_options.dart | 47 +++++++------------ .../exchange_steps/step_scaffold.dart | 5 +- .../subwidgets/desktop_step_1.dart | 3 +- .../subwidgets/desktop_step_3.dart | 5 +- .../subwidgets/desktop_step_4.dart | 3 +- .../current_exchange_name_state_provider.dart | 6 --- lib/providers/exchange/exchange_provider.dart | 9 ---- 11 files changed, 42 insertions(+), 65 deletions(-) delete mode 100644 lib/providers/exchange/current_exchange_name_state_provider.dart delete mode 100644 lib/providers/exchange/exchange_provider.dart diff --git a/lib/pages/exchange_view/exchange_form.dart b/lib/pages/exchange_view/exchange_form.dart index 885cc1d62..88497534b 100644 --- a/lib/pages/exchange_view/exchange_form.dart +++ b/lib/pages/exchange_view/exchange_form.dart @@ -386,7 +386,7 @@ class _ExchangeFormState extends ConsumerState { final sendAmount = ref.read(exchangeFormStateProvider).sendAmount!; final estimate = ref.read(exchangeFormStateProvider).estimate!; - final exchangeName = ref.read(currentExchangeNameStateProvider.state).state; + final exchangeName = ref.read(exchangeFormStateProvider).exchange.name; String rate; @@ -828,7 +828,8 @@ class _ExchangeFormState extends ConsumerState { currency: ref.watch(exchangeFormStateProvider .select((value) => value.receiveCurrency)), readOnly: (rateType) == ExchangeRateType.estimated && - ref.watch(exchangeProvider).name == + ref.watch(exchangeFormStateProvider + .select((value) => value.exchange.name)) == ChangeNowExchange.exchangeName, ), if (ref diff --git a/lib/pages/exchange_view/exchange_step_views/step_2_view.dart b/lib/pages/exchange_view/exchange_step_views/step_2_view.dart index af2567d9a..18950b851 100644 --- a/lib/pages/exchange_view/exchange_step_views/step_2_view.dart +++ b/lib/pages/exchange_view/exchange_step_views/step_2_view.dart @@ -124,9 +124,9 @@ class _Step2ViewState extends ConsumerState { @override Widget build(BuildContext context) { - final supportsRefund = - ref.watch(currentExchangeNameStateProvider.state).state != - MajesticBankExchange.exchangeName; + final supportsRefund = ref.watch( + exchangeFormStateProvider.select((value) => value.exchange.name)) != + MajesticBankExchange.exchangeName; return Background( child: Scaffold( diff --git a/lib/pages/exchange_view/exchange_step_views/step_3_view.dart b/lib/pages/exchange_view/exchange_step_views/step_3_view.dart index 8b08f7961..e09ad9a5c 100644 --- a/lib/pages/exchange_view/exchange_step_views/step_3_view.dart +++ b/lib/pages/exchange_view/exchange_step_views/step_3_view.dart @@ -7,9 +7,8 @@ import 'package:stackwallet/models/exchange/response_objects/trade.dart'; import 'package:stackwallet/pages/exchange_view/exchange_step_views/step_4_view.dart'; import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart'; import 'package:stackwallet/pages/exchange_view/sub_widgets/step_row.dart'; -import 'package:stackwallet/providers/exchange/current_exchange_name_state_provider.dart'; -import 'package:stackwallet/providers/exchange/exchange_provider.dart'; import 'package:stackwallet/providers/global/trades_service_provider.dart'; +import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart'; import 'package:stackwallet/services/notifications_api.dart'; @@ -53,9 +52,9 @@ class _Step3ViewState extends ConsumerState { @override Widget build(BuildContext context) { - final supportsRefund = - ref.watch(currentExchangeNameStateProvider.state).state != - MajesticBankExchange.exchangeName; + final supportsRefund = ref.watch( + exchangeFormStateProvider.select((value) => value.exchange.name)) != + MajesticBankExchange.exchangeName; return Background( child: Scaffold( @@ -255,7 +254,8 @@ class _Step3ViewState extends ConsumerState { final ExchangeResponse response = await ref - .read(exchangeProvider) + .read(exchangeFormStateProvider) + .exchange .createTrade( from: model.sendTicker, to: model.receiveTicker, diff --git a/lib/pages/exchange_view/exchange_step_views/step_4_view.dart b/lib/pages/exchange_view/exchange_step_views/step_4_view.dart index d5f1339d0..1278d1ddb 100644 --- a/lib/pages/exchange_view/exchange_step_views/step_4_view.dart +++ b/lib/pages/exchange_view/exchange_step_views/step_4_view.dart @@ -65,8 +65,10 @@ class _Step4ViewState extends ConsumerState { } Future _updateStatus() async { - final statusResponse = - await ref.read(exchangeProvider).updateTrade(model.trade!); + final statusResponse = await ref + .read(exchangeFormStateProvider) + .exchange + .updateTrade(model.trade!); String status = "Waiting"; if (statusResponse.value != null) { status = statusResponse.value!.status; diff --git a/lib/pages/exchange_view/sub_widgets/exchange_provider_options.dart b/lib/pages/exchange_view/sub_widgets/exchange_provider_options.dart index ac021cea1..c0f4312d0 100644 --- a/lib/pages/exchange_view/sub_widgets/exchange_provider_options.dart +++ b/lib/pages/exchange_view/sub_widgets/exchange_provider_options.dart @@ -93,12 +93,10 @@ class _ExchangeProviderOptionsState ), child: GestureDetector( onTap: () { - if (ref.read(currentExchangeNameStateProvider.state).state != + if (ref.read(exchangeFormStateProvider).exchange.name != ChangeNowExchange.exchangeName) { - ref.read(currentExchangeNameStateProvider.state).state = - ChangeNowExchange.exchangeName; ref.read(exchangeFormStateProvider).updateExchange( - exchange: ref.read(exchangeProvider), + exchange: ChangeNowExchange.instance, shouldUpdateData: true, shouldNotifyListeners: true, ); @@ -124,23 +122,18 @@ class _ExchangeProviderOptionsState .extension()! .radioButtonIconEnabled, value: ChangeNowExchange.exchangeName, - groupValue: ref - .watch(currentExchangeNameStateProvider.state) - .state, + groupValue: ref.watch(exchangeFormStateProvider + .select((value) => value.exchange.name)), onChanged: (_) { if (ref - .read(currentExchangeNameStateProvider - .state) - .state != + .read(exchangeFormStateProvider) + .exchange + .name != ChangeNowExchange.exchangeName) { - ref - .read(currentExchangeNameStateProvider - .state) - .state = ChangeNowExchange.exchangeName; ref .read(exchangeFormStateProvider) .updateExchange( - exchange: ref.read(exchangeProvider), + exchange: ChangeNowExchange.instance, shouldUpdateData: true, shouldNotifyListeners: true, ); @@ -337,12 +330,10 @@ class _ExchangeProviderOptionsState ), child: GestureDetector( onTap: () { - if (ref.read(currentExchangeNameStateProvider.state).state != + if (ref.read(exchangeFormStateProvider).exchange.name != MajesticBankExchange.exchangeName) { - ref.read(currentExchangeNameStateProvider.state).state = - MajesticBankExchange.exchangeName; ref.read(exchangeFormStateProvider).updateExchange( - exchange: ref.read(exchangeProvider), + exchange: MajesticBankExchange.instance, shouldUpdateData: true, shouldNotifyListeners: true, ); @@ -368,24 +359,18 @@ class _ExchangeProviderOptionsState .extension()! .radioButtonIconEnabled, value: MajesticBankExchange.exchangeName, - groupValue: ref - .watch(currentExchangeNameStateProvider.state) - .state, + groupValue: ref.watch(exchangeFormStateProvider + .select((value) => value.exchange.name)), onChanged: (_) { if (ref - .read(currentExchangeNameStateProvider - .state) - .state != + .read(exchangeFormStateProvider) + .exchange + .name != MajesticBankExchange.exchangeName) { - ref - .read(currentExchangeNameStateProvider - .state) - .state = - MajesticBankExchange.exchangeName; ref .read(exchangeFormStateProvider) .updateExchange( - exchange: ref.read(exchangeProvider), + exchange: MajesticBankExchange.instance, shouldUpdateData: true, shouldNotifyListeners: true, ); diff --git a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart index 88a8c34ff..2276ce112 100644 --- a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart +++ b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart @@ -13,7 +13,7 @@ import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_ste import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_3.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_4.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_exchange/subwidgets/desktop_exchange_steps_indicator.dart'; -import 'package:stackwallet/providers/exchange/exchange_provider.dart'; +import 'package:stackwallet/providers/exchange/exchange_form_state_provider.dart'; import 'package:stackwallet/providers/global/trades_service_provider.dart'; import 'package:stackwallet/route_generator.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; @@ -83,7 +83,8 @@ class _StepScaffoldState extends ConsumerState { ); final ExchangeResponse response = await ref - .read(exchangeProvider) + .read(exchangeFormStateProvider) + .exchange .createTrade( from: ref.read(desktopExchangeModelProvider)!.sendTicker, to: ref.read(desktopExchangeModelProvider)!.receiveTicker, diff --git a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_1.dart b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_1.dart index 4ec96f95d..1267db967 100644 --- a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_1.dart +++ b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_1.dart @@ -38,7 +38,8 @@ class DesktopStep1 extends ConsumerWidget { children: [ DesktopStepItem( label: "Exchange", - value: ref.watch(currentExchangeNameStateProvider.state).state, + value: ref.watch(exchangeFormStateProvider + .select((value) => value.exchange.name)), ), Container( height: 1, diff --git a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_3.dart b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_3.dart index 714cee971..57464c859 100644 --- a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_3.dart +++ b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_3.dart @@ -3,7 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_item.dart'; -import 'package:stackwallet/providers/exchange/current_exchange_name_state_provider.dart'; +import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; @@ -36,7 +36,8 @@ class _DesktopStep3State extends ConsumerState { children: [ DesktopStepItem( label: "Exchange", - value: ref.watch(currentExchangeNameStateProvider.state).state, + value: ref.watch(exchangeFormStateProvider + .select((value) => value.exchange.name)), ), Container( height: 1, diff --git a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_4.dart b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_4.dart index 4943ed442..2654c2cea 100644 --- a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_4.dart +++ b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_4.dart @@ -45,7 +45,8 @@ class _DesktopStep4State extends ConsumerState { return; } - final statusResponse = await ref.read(exchangeProvider).updateTrade(trade); + final statusResponse = + await ref.read(exchangeFormStateProvider).exchange.updateTrade(trade); String status = "Waiting"; if (statusResponse.value != null) { status = statusResponse.value!.status; diff --git a/lib/providers/exchange/current_exchange_name_state_provider.dart b/lib/providers/exchange/current_exchange_name_state_provider.dart deleted file mode 100644 index 3dc481c8f..000000000 --- a/lib/providers/exchange/current_exchange_name_state_provider.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/services/exchange/exchange.dart'; - -final currentExchangeNameStateProvider = StateProvider( - (ref) => Exchange.defaultExchange.name, -); diff --git a/lib/providers/exchange/exchange_provider.dart b/lib/providers/exchange/exchange_provider.dart deleted file mode 100644 index e65847a18..000000000 --- a/lib/providers/exchange/exchange_provider.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/providers/exchange/current_exchange_name_state_provider.dart'; -import 'package:stackwallet/services/exchange/exchange.dart'; - -final exchangeProvider = Provider( - (ref) => Exchange.fromName( - ref.watch(currentExchangeNameStateProvider.state).state, - ), -);