| 1 | import 'package:cake_wallet/entities/template.dart'; |
| 2 | import 'package:cake_wallet/store/app_store.dart'; |
| 3 | import 'package:cake_wallet/view_model/send/output.dart'; |
| 4 | import 'package:mobx/mobx.dart'; |
| 5 | import 'package:cw_core/wallet_base.dart'; |
| 6 | import 'package:cw_core/crypto_currency.dart'; |
| 7 | import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; |
| 8 | |
| 9 | part 'template_view_model.g.dart'; |
| 10 | |
| 11 | class TemplateViewModel = TemplateViewModelBase with _$TemplateViewModel; |
| 12 | |
| 13 | abstract class TemplateViewModelBase with Store { |
| 14 | final WalletBase _wallet; |
| 15 | final AppStore _appStore; |
| 16 | final FiatConversionStore _fiatConversationStore; |
| 17 | |
| 18 | TemplateViewModelBase({ |
| 19 | required WalletBase wallet, |
| 20 | required AppStore appStore, |
| 21 | required FiatConversionStore fiatConversationStore, |
| 22 | }) : _wallet = wallet, |
| 23 | _appStore = appStore, |
| 24 | _fiatConversationStore = fiatConversationStore, |
| 25 | _currency = wallet.currency, |
| 26 | output = Output(wallet, appStore, fiatConversationStore, ([_]) => wallet.currency) { |
| 27 | output = Output(_wallet, _appStore, _fiatConversationStore, _outputCryptoCurrencyHandler); |
| 28 | } |
| 29 | |
| 30 | @observable |
| 31 | Output output; |
| 32 | |
| 33 | @observable |
| 34 | String name = ''; |
| 35 | |
| 36 | @observable |
| 37 | String address = ''; |
| 38 | |
| 39 | @observable |
| 40 | CryptoCurrency _currency; |
| 41 | |
| 42 | @observable |
| 43 | bool isCryptoSelected = true; |
| 44 | |
| 45 | @action |
| 46 | void setCryptoCurrency(bool value) => isCryptoSelected = value; |
| 47 | |
| 48 | @action |
| 49 | void reset() { |
| 50 | name = ''; |
| 51 | address = ''; |
| 52 | isCryptoSelected = true; |
| 53 | output.reset(); |
| 54 | } |
| 55 | |
| 56 | Template toTemplate({required String cryptoCurrency, required String fiatCurrency}) => Template( |
| 57 | isCurrencySelectedRaw: isCryptoSelected, |
| 58 | nameRaw: name, |
| 59 | addressRaw: address, |
| 60 | cryptoCurrencyRaw: cryptoCurrency, |
| 61 | fiatCurrencyRaw: fiatCurrency, |
| 62 | amountRaw: output.cryptoAmount, |
| 63 | amountFiatRaw: output.fiatAmount, |
| 64 | ); |
| 65 | |
| 66 | CryptoCurrency _outputCryptoCurrencyHandler([CryptoCurrency? override]) { |
| 67 | if (override != null && _currency != override) changeSelectedCurrency(override); |
| 68 | |
| 69 | return _currency; |
| 70 | } |
| 71 | |
| 72 | @action |
| 73 | void changeSelectedCurrency(CryptoCurrency currency) { |
| 74 | isCryptoSelected = true; |
| 75 | _currency = currency; |
| 76 | } |
| 77 | |
| 78 | @computed |
| 79 | CryptoCurrency get selectedCurrency => _currency; |
| 80 | } |