CAKE-278 | added isReceiveAmountEditable and isFixedRateMode to exchange_view_model.dart; fixed changeReceiveAmount() in the exchange_view_model.dart; applied StandardCheckbox to exchange_page.dart for checking fixed rate mode; applied alert dialog to exchange_page.dart when receive amount has focus and fixed rate mode is not checked; added reaction for isFixedRateMode in the exchange_page.dart; added key and changeValue() to standard_checkbox.dart

OleksandrSobol committed Feb 16, 2021 at 21:06 UTC 317ef8f3e4414749569b74f83f4925b80d9c3400
3 files changed +71 -7
lib/src/screens/exchange/exchange_page.dart
+53 -4
@@ -1,6 +1,8 @@
1 import 'dart:ui';
2 import 'package:cake_wallet/entities/sync_status.dart';
3 import 'package:cake_wallet/entities/wallet_type.dart';
4 +import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart';
5 +import 'package:cake_wallet/src/widgets/standard_checkbox.dart';
6 import 'package:dotted_border/dotted_border.dart';
7 import 'package:flutter/cupertino.dart';
8 import 'package:flutter/material.dart';
@@ -37,6 +39,7 @@ class ExchangePage extends BasePage {
39 final ExchangeViewModel exchangeViewModel;
40 final depositKey = GlobalKey<ExchangeCardState>();
41 final receiveKey = GlobalKey<ExchangeCardState>();
42 + final checkBoxKey = GlobalKey<StandardCheckboxState>();
43 final _formKey = GlobalKey<FormState>();
44 final _depositAmountFocus = FocusNode();
45 final _receiveAmountFocus = FocusNode();
@@ -240,9 +243,7 @@ class ExchangePage extends BasePage {
243 ? exchangeViewModel.wallet.address
244 : exchangeViewModel.receiveAddress,
245 initialIsAmountEditable: exchangeViewModel
243 - .provider is XMRTOExchangeProvider
244 - ? true
245 - : false,
246 + .isReceiveAmountEditable,
247 initialIsAddressEditable:
248 exchangeViewModel
249 .isReceiveAddressEnabled,
@@ -271,6 +272,21 @@ class ExchangePage extends BasePage {
272 ],
273 ),
274 ),
275 + Padding(
276 + padding: EdgeInsets.only(top: 12, left: 24),
277 + child: Row(
278 + mainAxisAlignment: MainAxisAlignment.start,
279 + children: [
280 + StandardCheckbox(
281 + key: checkBoxKey,
282 + value: exchangeViewModel.isFixedRateMode,
283 + caption: 'Fixed rate', // FIXME
284 + onChanged: (value) =>
285 + exchangeViewModel.isFixedRateMode = value,
286 + ),
287 + ],
288 + )
289 + ),
290 Padding(
291 padding: EdgeInsets.only(top: 30, left: 24, bottom: 24),
292 child: Row(
@@ -567,7 +583,7 @@ class ExchangePage extends BasePage {
583 });
584
585 reaction((_) => exchangeViewModel.provider, (ExchangeProvider provider) {
570 - provider is XMRTOExchangeProvider
586 + provider is ChangeNowExchangeProvider
587 ? receiveKey.currentState.isAmountEditable(isEditable: true)
588 : receiveKey.currentState.isAmountEditable(isEditable: false);
589 });
@@ -647,6 +663,39 @@ class ExchangePage extends BasePage {
663 }
664 });
665
666 + _receiveAmountFocus.addListener(() {
667 + if (_receiveAmountFocus.hasFocus && !exchangeViewModel.isFixedRateMode) {
668 + showPopUp<void>(
669 + context: context,
670 + builder: (BuildContext context) {
671 + return AlertWithTwoActions(
672 + alertTitle: S.of(context).exchange,
673 + alertContent: 'You will be able to enter receive amount when fixed rate is checked. Do you want to check fixed rate?', //FIXME
674 + leftButtonText: S.of(context).cancel,
675 + rightButtonText: S.of(context).ok,
676 + actionLeftButton: () {
677 + FocusScope.of(context).unfocus();
678 + receiveAmountController.text = '';
679 + Navigator.of(context).pop();
680 + },
681 + actionRightButton: () {
682 + exchangeViewModel.isFixedRateMode = true;
683 + checkBoxKey.currentState
684 + .changeValue(exchangeViewModel.isFixedRateMode);
685 + Navigator.of(context).pop();
686 + });
687 + });
688 + }
689 + });
690 +
691 + reaction((_) => exchangeViewModel.isFixedRateMode, (bool isFixedRateMode) {
692 + if ((_receiveAmountFocus.hasFocus ||
693 + exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) {
694 + FocusScope.of(context).unfocus();
695 + receiveAmountController.text = '';
696 + }
697 + });
698 +
699 _isReactionsSet = true;
700 }
701
lib/src/widgets/standard_checkbox.dart
+6 -1
@@ -4,9 +4,10 @@ import 'package:flutter/material.dart';
4
5 class StandardCheckbox extends StatefulWidget {
6 StandardCheckbox({
7 + Key key,
8 @required this.value,
9 this.caption = '',
9 - @required this.onChanged});
10 + @required this.onChanged}) : super(key: key);
11
12 final bool value;
13 final String caption;
@@ -24,6 +25,10 @@ class StandardCheckboxState extends State<StandardCheckbox> {
25 String caption;
26 Function(bool) onChanged;
27
28 + void changeValue(bool newValue) {
29 + setState(() => value = newValue);
30 + }
31 +
32 @override
33 Widget build(BuildContext context) {
34 return GestureDetector(
lib/view_model/exchange/exchange_view_model.dart
+12 -2
@@ -58,6 +58,9 @@ abstract class ExchangeViewModelBase with Store {
58 }
59 });
60
61 + isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
62 + isFixedRateMode = false;
63 +
64 isReceiveAmountEntered = false;
65 loadLimits();
66 }
@@ -106,6 +109,12 @@ abstract class ExchangeViewModelBase with Store {
109 @observable
110 bool isReceiveAmountEntered;
111
112 + @observable
113 + bool isReceiveAmountEditable;
114 +
115 + @observable
116 + bool isFixedRateMode;
117 +
118 @computed
119 SyncStatus get status => wallet.syncStatus;
120
@@ -127,6 +136,7 @@ abstract class ExchangeViewModelBase with Store {
136 this.provider = provider;
137 depositAmount = '';
138 receiveAmount = '';
139 + isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
140 loadLimits();
141 }
142
@@ -160,8 +170,8 @@ abstract class ExchangeViewModelBase with Store {
170
171 provider
172 .calculateAmount(
163 - from: depositCurrency,
164 - to: receiveCurrency,
173 + from: receiveCurrency,
174 + to: depositCurrency,
175 amount: _amount,
176 isReceiveAmount: true)
177 .then((amount) => _cryptoNumberFormat