Cw 55 allow saving templateswith fiat amount (#343)

* added xhv logo * Added prefix icon widget * Added sending fiat template * template for sending selected fiat currency * fix issues

Serhii committed May 3, 2022 at 13:44 UTC c08f2c1667ac4ad2556edd250a67c5feedfedcae
9 files changed +135 -39
assets/images/xhv_logo.png
Binary files /dev/null and b/assets/images/xhv_logo.png differ
lib/di.dart
+2 -1
@@ -343,7 +343,8 @@ Future setup(
343 _transactionDescriptionBox));
344
345 getIt.registerFactory(
346 - () => SendPage(sendViewModel: getIt.get<SendViewModel>()));
346 + () => SendPage(sendViewModel: getIt.get<SendViewModel>(),
347 + settingsViewModel: getIt.get<SettingsViewModel>()));
348
349 getIt.registerFactory(() => SendTemplatePage(
350 sendTemplateViewModel: getIt.get<SendTemplateViewModel>()));
lib/entities/template.dart
+12 -2
@@ -4,7 +4,7 @@ part 'template.g.dart';
4
5 @HiveType(typeId: Template.typeId)
6 class Template extends HiveObject {
7 - Template({this.name, this.address, this.cryptoCurrency, this.amount});
7 + Template({this.name,this.isCurrencySelected, this.address, this.cryptoCurrency, this.amount, this.fiatCurrency, this.amountFiat});
8
9 static const typeId = 6;
10 static const boxName = 'Template';
@@ -20,4 +20,14 @@ class Template extends HiveObject {
20
21 @HiveField(3)
22 String amount;
23 -}
\ No newline at end of file
23 +
24 + @HiveField(4)
25 + String fiatCurrency;
26 +
27 + @HiveField(5)
28 + bool isCurrencySelected;
29 +
30 + @HiveField(6)
31 + String amountFiat;
32 +}
33 +
lib/src/screens/exchange/widgets/currency_utils.dart
+2
@@ -48,6 +48,8 @@ class CurrencyUtils {
48 return 'assets/images/xlm_icon.png';
49 case CryptoCurrency.xrp:
50 return 'assets/images/xrp_icon.png';
51 + case CryptoCurrency.xhv:
52 + return 'assets/images/xhv_logo.png';
53 default:
54 return null;
55 }
lib/src/screens/send/send_page.dart
+20 -4
@@ -1,10 +1,12 @@
1 import 'dart:ui';
2 +import 'package:cake_wallet/entities/fiat_currency.dart';
3 import 'package:cake_wallet/src/screens/dashboard/widgets/sync_indicator_icon.dart';
4 import 'package:cake_wallet/src/screens/send/widgets/send_card.dart';
5 import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
6 import 'package:cake_wallet/src/widgets/picker.dart';
7 import 'package:cake_wallet/src/widgets/template_tile.dart';
8 import 'package:cake_wallet/view_model/send/output.dart';
9 +import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
10 import 'package:flutter/cupertino.dart';
11 import 'package:flutter/material.dart';
12 import 'package:flutter_mobx/flutter_mobx.dart';
@@ -26,11 +28,13 @@ import 'package:smooth_page_indicator/smooth_page_indicator.dart';
28 import 'package:cw_core/crypto_currency.dart';
29
30 class SendPage extends BasePage {
29 - SendPage({@required this.sendViewModel}) : _formKey = GlobalKey<FormState>();
31 + SendPage({@required this.sendViewModel,@required this.settingsViewModel }) : _formKey = GlobalKey<FormState>(),fiatFromSettings = settingsViewModel.fiatCurrency;
32
33 final SendViewModel sendViewModel;
34 + final SettingsViewModel settingsViewModel;
35 final GlobalKey<FormState> _formKey;
36 final controller = PageController(initialPage: 0);
37 + final FiatCurrency fiatFromSettings ;
38
39 bool _effectsInstalled = false;
40
@@ -49,6 +53,12 @@ class SendPage extends BasePage {
53 @override
54 AppBarStyle get appBarStyle => AppBarStyle.transparent;
55
56 + @override
57 + void onClose(BuildContext context) {
58 + settingsViewModel.setFiatCurrency(fiatFromSettings);
59 + Navigator.of(context).pop();
60 + }
61 +
62 @override
63 Widget middle(BuildContext context) => Row(
64 mainAxisAlignment: MainAxisAlignment.center,
@@ -212,12 +222,18 @@ class SendPage extends BasePage {
222 return TemplateTile(
223 key: UniqueKey(),
224 to: template.name,
215 - amount: template.amount,
216 - from: template.cryptoCurrency,
225 + amount: template.isCurrencySelected ? template.amount : template.amountFiat,
226 + from: template.isCurrencySelected ? template.cryptoCurrency : template.fiatCurrency,
227 onTap: () async {
228 + final fiatFromTemplate = FiatCurrency.all.singleWhere((element) => element.title == template.fiatCurrency);
229 final output = _defineCurrentOutput();
230 output.address = template.address;
220 - output.setCryptoAmount(template.amount);
231 + if(template.isCurrencySelected){
232 + output.setCryptoAmount(template.amount);
233 + }else{
234 + settingsViewModel.setFiatCurrency(fiatFromTemplate);
235 + output.setFiatAmount(template.amountFiat);
236 + }
237 output.resetParsedAddress();
238 await output.fetchParsedAddress(context);
239 },
lib/src/screens/send/send_template_page.dart
+38 -27
@@ -1,4 +1,5 @@
1 import 'package:cake_wallet/utils/payment_request.dart';
2 +import 'package:flutter_mobx/flutter_mobx.dart';
3 import 'package:mobx/mobx.dart';
4 import 'package:flutter/cupertino.dart';
5 import 'package:flutter/material.dart';
@@ -12,6 +13,7 @@ import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
13 import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
14 import 'package:cake_wallet/src/widgets/primary_button.dart';
15 import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
16 +import 'package:cake_wallet/src/screens/send/widgets/prefix_currency_icon_widget.dart';
17
18 class SendTemplatePage extends BasePage {
19 SendTemplatePage({@required this.sendTemplateViewModel}) {
@@ -142,7 +144,13 @@ class SendTemplatePage extends BasePage {
144 ),
145 Padding(
146 padding: const EdgeInsets.only(top: 20),
145 - child: BaseTextFormField(
147 + child: Focus(
148 + onFocusChange: (hasFocus) {
149 + if (hasFocus) {
150 + sendTemplateViewModel.selectCurrency();
151 + }
152 + },
153 + child: BaseTextFormField(
154 focusNode: _cryptoAmountFocus,
155 controller: _cryptoAmountController,
156 keyboardType: TextInputType.numberWithOptions(
@@ -151,17 +159,14 @@ class SendTemplatePage extends BasePage {
159 FilteringTextInputFormatter.deny(
160 RegExp('[\\-|\\ ]'))
161 ],
154 - prefixIcon: Padding(
155 - padding: EdgeInsets.only(top: 9),
156 - child: Text(
157 - sendTemplateViewModel.currency.title +
158 - ':',
159 - style: TextStyle(
160 - fontSize: 16,
161 - fontWeight: FontWeight.w600,
162 - color: Colors.white,
163 - )),
164 - ),
162 + prefixIcon: Observer(
163 + builder: (_) => PrefixCurrencyIcon(
164 + title: sendTemplateViewModel
165 + .currency.title,
166 + isSelected:
167 + sendTemplateViewModel
168 + .isCurrencySelected,
169 + )),
170 hintText: '0.0000',
171 borderColor: Theme.of(context)
172 .primaryTextTheme
@@ -179,10 +184,16 @@ class SendTemplatePage extends BasePage {
184 fontWeight: FontWeight.w500,
185 fontSize: 14),
186 validator:
182 - sendTemplateViewModel.amountValidator)),
187 + sendTemplateViewModel.amountValidator))),
188 Padding(
189 padding: const EdgeInsets.only(top: 20),
185 - child: BaseTextFormField(
190 + child: Focus(
191 + onFocusChange: (hasFocus) {
192 + if (hasFocus) {
193 + sendTemplateViewModel.selectFiat();
194 + }
195 + },
196 + child: BaseTextFormField(
197 focusNode: _fiatAmountFocus,
198 controller: _fiatAmountController,
199 keyboardType: TextInputType.numberWithOptions(
@@ -191,16 +202,13 @@ class SendTemplatePage extends BasePage {
202 FilteringTextInputFormatter.deny(
203 RegExp('[\\-|\\ ]'))
204 ],
194 - prefixIcon: Padding(
195 - padding: EdgeInsets.only(top: 9),
196 - child: Text(
197 - sendTemplateViewModel.fiat.title + ':',
198 - style: TextStyle(
199 - fontSize: 16,
200 - fontWeight: FontWeight.w600,
201 - color: Colors.white,
202 - )),
203 - ),
205 + prefixIcon: Observer(
206 + builder: (_) => PrefixCurrencyIcon(
207 + title: sendTemplateViewModel
208 + .fiat.title,
209 + isSelected: sendTemplateViewModel
210 + .isFiatSelected,
211 + )),
212 hintText: '0.00',
213 borderColor: Theme.of(context)
214 .primaryTextTheme
@@ -217,7 +225,7 @@ class SendTemplatePage extends BasePage {
225 .decorationColor,
226 fontWeight: FontWeight.w500,
227 fontSize: 14),
220 - )),
228 + ))),
229 ],
230 ),
231 )
@@ -231,10 +239,13 @@ class SendTemplatePage extends BasePage {
239 onPressed: () {
240 if (_formKey.currentState.validate()) {
241 sendTemplateViewModel.addTemplate(
242 + isCurrencySelected: sendTemplateViewModel.isCurrencySelected,
243 name: _nameController.text,
244 address: _addressController.text,
236 - cryptoCurrency: sendTemplateViewModel.currency.title,
237 - amount: _cryptoAmountController.text);
245 + cryptoCurrency:sendTemplateViewModel.currency.title,
246 + fiatCurrency: sendTemplateViewModel.fiat.title,
247 + amount: _cryptoAmountController.text,
248 + amountFiat: _fiatAmountController.text);
249 Navigator.of(context).pop();
250 }
251 },
lib/src/screens/send/widgets/prefix_currency_icon_widget.dart new
+32
@@ -0,0 +1,32 @@
1 +import 'package:flutter/material.dart';
2 +
3 +class PrefixCurrencyIcon extends StatelessWidget {
4 + PrefixCurrencyIcon({
5 + @required this.isSelected,
6 + @required this.title,
7 + });
8 +
9 + final bool isSelected;
10 + final String title;
11 +
12 + @override
13 + Widget build(BuildContext context) {
14 + return Padding(
15 + padding: EdgeInsets.fromLTRB(0, 6.0, 8.0, 0),
16 + child: Column(children: [
17 + Container(
18 + padding: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
19 + decoration: BoxDecoration(
20 + borderRadius: BorderRadius.circular(26),
21 + color: isSelected ? Colors.green : Colors.transparent,
22 + ),
23 + child: Text(title + ':',
24 + style: TextStyle(
25 + fontSize: 16,
26 + fontWeight: FontWeight.w600,
27 + color: Colors.white,
28 + )),
29 + )
30 + ]));
31 + }
32 +}
lib/store/templates/send_template_store.dart
+3 -3
@@ -23,9 +23,9 @@ abstract class SendTemplateBase with Store {
23 templates.replaceRange(0, templates.length, templateSource.values.toList());
24
25 @action
26 - Future addTemplate({String name, String address, String cryptoCurrency, String amount}) async {
27 - final template = Template(name: name, address: address,
28 - cryptoCurrency: cryptoCurrency, amount: amount);
26 + Future addTemplate({String name,bool isCurrencySelected, String address, String cryptoCurrency, String fiatCurrency, String amount,String amountFiat}) async {
27 + final template = Template(name: name,isCurrencySelected: isCurrencySelected, address: address,
28 + cryptoCurrency: cryptoCurrency, fiatCurrency: fiatCurrency, amount: amount, amountFiat: amountFiat);
29 await templateSource.add(template);
30 }
31
lib/view_model/send/send_template_view_model.dart
+26 -2
@@ -36,6 +36,24 @@ abstract class SendTemplateViewModelBase with Store {
36
37 FiatCurrency get fiat => _settingsStore.fiatCurrency;
38
39 + @observable
40 + bool isCurrencySelected = true;
41 +
42 + @observable
43 + bool isFiatSelected = false;
44 +
45 + @action
46 + void selectCurrency () {
47 + isCurrencySelected = true;
48 + isFiatSelected = false;
49 + }
50 +
51 + @action
52 + void selectFiat () {
53 + isFiatSelected = true;
54 + isCurrencySelected = false;
55 + }
56 +
57 @computed
58 ObservableList<Template> get templates => _sendTemplateStore.templates;
59
@@ -48,14 +66,20 @@ abstract class SendTemplateViewModelBase with Store {
66
67 void addTemplate(
68 {String name,
69 + bool isCurrencySelected,
70 String address,
71 String cryptoCurrency,
53 - String amount}) {
72 + String fiatCurrency,
73 + String amount,
74 + String amountFiat}) {
75 _sendTemplateStore.addTemplate(
76 name: name,
77 + isCurrencySelected: isCurrencySelected,
78 address: address,
79 cryptoCurrency: cryptoCurrency,
58 - amount: amount);
80 + fiatCurrency: fiatCurrency,
81 + amount: amount,
82 + amountFiat: amountFiat);
83 updateTemplate();
84 }
85