Enable different currencies (#401)

Omar Hatem committed Jul 1, 2022 at 13:04 UTC 8321c9fbbe9769178115284dbb68bb2644e3130b
5 files changed +90 -54
lib/entities/fiat_currency.dart
+3
@@ -8,6 +8,9 @@ class FiatCurrency extends EnumerableItem<String> with Serializable<String> {
8
9 static List<FiatCurrency> get all => _all.values.toList();
10
11 + static List<FiatCurrency> get currenciesAvailableToBuyWith =>
12 + [aud, brl, cad, chf, czk, eur, dkk, gbp, hkd, ils, jpy, krw, mxn, myr, nok, nzd, pln, sek, sgd, thb, usd, zar];
13 +
14 static const aud = FiatCurrency(symbol: 'AUD', countryCode: "aus", fullName: "Australian Dollar");
15 static const bgn = FiatCurrency(symbol: 'BGN', countryCode: "bgr", fullName: "Bulgarian Lev");
16 static const brl = FiatCurrency(symbol: 'BRL', countryCode: "bra", fullName: "Brazilian Real");
lib/src/screens/buy/pre_order_page.dart
+67 -48
@@ -2,6 +2,8 @@ import 'dart:ui';
2 import 'package:cake_wallet/buy/buy_amount.dart';
3 import 'package:cake_wallet/buy/buy_provider.dart';
4 import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
5 +import 'package:cake_wallet/entities/fiat_currency.dart';
6 +import 'package:cake_wallet/src/widgets/picker.dart';
7 import 'package:cw_core/wallet_type.dart';
8 import 'package:cake_wallet/src/screens/buy/widgets/buy_list_item.dart';
9 import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
@@ -27,7 +29,6 @@ class PreOrderPage extends BasePage {
29 PreOrderPage({@required this.buyViewModel})
30 : _amountFocus = FocusNode(),
31 _amountController = TextEditingController() {
30 -
32 _amountController.addListener(() {
33 final amount = _amountController.text;
34
@@ -110,52 +111,70 @@ class PreOrderPage extends BasePage {
111 .decorationColor,
112 ], begin: Alignment.topLeft, end: Alignment.bottomRight),
113 ),
113 - child: Padding(
114 - padding: EdgeInsets.only(top: 100, bottom: 65),
115 - child: Center(
116 - child: Container(
117 - width: 210,
118 - child: BaseTextFormField(
119 - focusNode: _amountFocus,
120 - controller: _amountController,
121 - keyboardType:
122 - TextInputType.numberWithOptions(
123 - signed: false, decimal: true),
124 - inputFormatters: [
125 - FilteringTextInputFormatter
126 - .allow(RegExp(_amountPattern))
127 - ],
128 - prefixIcon: Padding(
129 - padding: EdgeInsets.only(top: 2),
130 - child:
131 - Text(buyViewModel.fiatCurrency.title + ': ',
132 - style: TextStyle(
133 - fontSize: 36,
134 - fontWeight: FontWeight.w600,
135 - color: Colors.white,
136 - )),
137 - ),
138 - hintText: '0.00',
139 - borderColor: Theme.of(context)
140 - .primaryTextTheme
141 - .body2
142 - .decorationColor,
143 - borderWidth: 0.5,
144 - textStyle: TextStyle(
114 + child: Padding(
115 + padding: EdgeInsets.only(top: 100, bottom: 65),
116 + child: Center(
117 + child: Container(
118 + width: 210,
119 + child: BaseTextFormField(
120 + focusNode: _amountFocus,
121 + controller: _amountController,
122 + keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true),
123 + inputFormatters: [FilteringTextInputFormatter.allow(RegExp(_amountPattern))],
124 + prefixIcon: GestureDetector(
125 + onTap: () {
126 + showPopUp<void>(
127 + context: context,
128 + builder: (_) => Picker(
129 + hintText: S.current.search_currency,
130 + items: FiatCurrency.currenciesAvailableToBuyWith,
131 + selectedAtIndex:
132 + FiatCurrency.currenciesAvailableToBuyWith.indexOf(buyViewModel.fiatCurrency),
133 + onItemSelected: (FiatCurrency selectedCurrency) {
134 + buyViewModel.buyAmountViewModel.fiatCurrency = selectedCurrency;
135 + },
136 + images: FiatCurrency.currenciesAvailableToBuyWith
137 + .map((e) => Image.asset("assets/images/flags/${e.countryCode}.png"))
138 + .toList(),
139 + isGridView: true,
140 + matchingCriteria: (FiatCurrency currency, String searchText) {
141 + return currency.title.toLowerCase().contains(searchText) ||
142 + currency.fullName.toLowerCase().contains(searchText);
143 + },
144 + ),
145 + );
146 + },
147 + child: Padding(
148 + padding: EdgeInsets.only(top: 2),
149 + child: Row(
150 + mainAxisSize: MainAxisSize.min,
151 + children: [
152 + Icon(Icons.keyboard_arrow_down, color: Colors.white),
153 + Text(
154 + buyViewModel.fiatCurrency.title + ': ',
155 + style: TextStyle(
156 fontSize: 36,
146 - fontWeight: FontWeight.w500,
147 - color: Colors.white),
148 - placeholderTextStyle: TextStyle(
149 - color: Theme.of(context)
150 - .primaryTextTheme
151 - .headline
152 - .decorationColor,
153 - fontWeight: FontWeight.w500,
154 - fontSize: 36),
155 - )
156 - )
157 - )
158 - )
157 + fontWeight: FontWeight.w600,
158 + color: Colors.white,
159 + ),
160 + ),
161 + ],
162 + ),
163 + ),
164 + ),
165 + hintText: '0.00',
166 + borderColor: Theme.of(context).primaryTextTheme.body2.decorationColor,
167 + borderWidth: 0.5,
168 + textStyle: TextStyle(fontSize: 36, fontWeight: FontWeight.w500, color: Colors.white),
169 + placeholderTextStyle: TextStyle(
170 + color: Theme.of(context).primaryTextTheme.headline.decorationColor,
171 + fontWeight: FontWeight.w500,
172 + fontSize: 36,
173 + ),
174 + ),
175 + ),
176 + ),
177 + ),
178 ),
179 if (buyViewModel.isShowProviderButtons) Padding(
180 padding: EdgeInsets.only(top: 38, bottom: 18),
@@ -273,7 +292,7 @@ class PreOrderPage extends BasePage {
292
293 buyViewModel.isRunning = true;
294 final url = await buyViewModel.fetchUrl();
276 -
295 +
296 if (url.isNotEmpty) {
297 if (buyViewModel.selectedProvider is MoonPayBuyProvider) {
298 if (await canLaunch(url)) await launch(url);
@@ -287,4 +306,4 @@ class PreOrderPage extends BasePage {
306 buyViewModel.reset();
307 buyViewModel.isRunning = false;
308 }
290 -}
\ No newline at end of file
309 +}
lib/view_model/buy/buy_amount_view_model.dart
+17 -3
@@ -1,3 +1,5 @@
1 +import 'package:cake_wallet/di.dart';
2 +import 'package:cake_wallet/store/settings_store.dart';
3 import 'package:mobx/mobx.dart';
4 import 'package:cake_wallet/entities/fiat_currency.dart';
5
@@ -6,12 +8,24 @@ part 'buy_amount_view_model.g.dart';
8 class BuyAmountViewModel = BuyAmountViewModelBase with _$BuyAmountViewModel;
9
10 abstract class BuyAmountViewModelBase with Store {
9 - BuyAmountViewModelBase() : amount = '';
11 + BuyAmountViewModelBase() {
12 + amount = '';
13 +
14 + int selectedIndex = FiatCurrency.currenciesAvailableToBuyWith
15 + .indexOf(getIt.get<SettingsStore>().fiatCurrency);
16 +
17 + if (selectedIndex == -1) {
18 + selectedIndex = FiatCurrency.currenciesAvailableToBuyWith
19 + .indexOf(FiatCurrency.usd);
20 + }
21 + fiatCurrency = FiatCurrency.currenciesAvailableToBuyWith[selectedIndex];
22 + }
23
24 @observable
25 String amount;
26
14 - FiatCurrency get fiatCurrency => FiatCurrency.usd;
27 + @observable
28 + FiatCurrency fiatCurrency;
29
30 @computed
31 double get doubleAmount {
@@ -25,4 +39,4 @@ abstract class BuyAmountViewModelBase with Store {
39
40 return _amount;
41 }
28 -}
\ No newline at end of file
42 +}
lib/view_model/buy/buy_view_model.dart
+1
@@ -54,6 +54,7 @@ abstract class BuyViewModelBase with Store {
54
55 double get doubleAmount => buyAmountViewModel.doubleAmount;
56
57 + @computed
58 FiatCurrency get fiatCurrency => buyAmountViewModel.fiatCurrency;
59
60 CryptoCurrency get cryptoCurrency => walletTypeToCryptoCurrency(type);
lib/view_model/settings/settings_view_model.dart
+2 -3
@@ -1,4 +1,3 @@
1 -import 'package:cake_wallet/di.dart';
1 import 'package:cake_wallet/entities/language_service.dart';
2 import 'package:cake_wallet/store/yat/yat_store.dart';
3 import 'package:cake_wallet/view_model/settings/choices_list_item.dart';
@@ -172,9 +171,9 @@ abstract class SettingsViewModelBase with Store {
171 displayItem: (dynamic code) {
172 return LanguageService.list[code];
173 },
175 - selectedItem: () => getIt.get<SettingsStore>().languageCode,
174 + selectedItem: () => _settingsStore.languageCode,
175 onItemSelected: (String code) {
177 - getIt.get<SettingsStore>().languageCode = code;
176 + _settingsStore.languageCode = code;
177 },
178 images: LanguageService.list.keys.map(
179 (e) => Image.asset("assets/images/flags/${LanguageService.localeCountryCode[e]}.png"))