| 1 | import 'package:cake_wallet/buy/buy_provider.dart'; |
| 2 | import 'package:cake_wallet/buy/wyre/wyre_buy_provider.dart'; |
| 3 | import 'package:cw_core/crypto_currency.dart'; |
| 4 | import 'package:cake_wallet/entities/fiat_currency.dart'; |
| 5 | import 'package:cw_core/utils/print_verbose.dart'; |
| 6 | import 'package:cw_core/wallet_type.dart'; |
| 7 | import 'package:cake_wallet/store/settings_store.dart'; |
| 8 | import 'package:cake_wallet/view_model/buy/buy_item.dart'; |
| 9 | import 'package:hive/hive.dart'; |
| 10 | import 'package:cake_wallet/order/order.dart'; |
| 11 | import 'package:cake_wallet/store/dashboard/orders_store.dart'; |
| 12 | import 'package:mobx/mobx.dart'; |
| 13 | import 'package:cw_core/wallet_base.dart'; |
| 14 | import 'buy_amount_view_model.dart'; |
| 15 | |
| 16 | part 'buy_view_model.g.dart'; |
| 17 | |
| 18 | class BuyViewModel = BuyViewModelBase with _$BuyViewModel; |
| 19 | |
| 20 | abstract class BuyViewModelBase with Store { |
| 21 | BuyViewModelBase(this.ordersSource, this.ordersStore, this.settingsStore, this.buyAmountViewModel, |
| 22 | {required this.wallet}) |
| 23 | : isRunning = false, |
| 24 | isDisabled = true, |
| 25 | isShowProviderButtons = false, |
| 26 | items = <BuyItem>[] { |
| 27 | _fetchBuyItems(); |
| 28 | } |
| 29 | |
| 30 | final Box<Order> ordersSource; |
| 31 | final OrdersStore ordersStore; |
| 32 | final SettingsStore settingsStore; |
| 33 | final BuyAmountViewModel buyAmountViewModel; |
| 34 | final WalletBase wallet; |
| 35 | |
| 36 | @observable |
| 37 | BuyProvider? selectedProvider; |
| 38 | |
| 39 | @observable |
| 40 | List<BuyItem> items; |
| 41 | |
| 42 | @observable |
| 43 | bool isRunning; |
| 44 | |
| 45 | @observable |
| 46 | bool isDisabled; |
| 47 | |
| 48 | @observable |
| 49 | bool isShowProviderButtons; |
| 50 | |
| 51 | WalletType get type => wallet.type; |
| 52 | |
| 53 | double get doubleAmount => buyAmountViewModel.doubleAmount; |
| 54 | |
| 55 | @computed |
| 56 | FiatCurrency get fiatCurrency => buyAmountViewModel.fiatCurrency; |
| 57 | |
| 58 | CryptoCurrency get cryptoCurrency => wallet.currency; |
| 59 | |
| 60 | Future<String> fetchUrl() async { |
| 61 | String _url = ''; |
| 62 | |
| 63 | try { |
| 64 | _url = await selectedProvider!.requestUrl(doubleAmount.toString(), fiatCurrency.title); |
| 65 | } catch (e) { |
| 66 | printV(e.toString()); |
| 67 | } |
| 68 | |
| 69 | return _url; |
| 70 | } |
| 71 | |
| 72 | Future<void> saveOrder(String orderId) async { |
| 73 | try { |
| 74 | final order = await selectedProvider!.findOrderById(orderId); |
| 75 | order.from = fiatCurrency.title; |
| 76 | order.to = cryptoCurrency.title; |
| 77 | await ordersSource.add(order); |
| 78 | ordersStore.setOrder(order); |
| 79 | } catch (e) { |
| 80 | printV(e.toString()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void reset() { |
| 85 | buyAmountViewModel.amount = ''; |
| 86 | selectedProvider = null; |
| 87 | } |
| 88 | |
| 89 | Future<void> _fetchBuyItems() async { |
| 90 | final List<BuyProvider> _providerList = []; |
| 91 | |
| 92 | if (wallet.type == WalletType.bitcoin) { |
| 93 | _providerList.add(WyreBuyProvider(wallet: wallet)); |
| 94 | } |
| 95 | |
| 96 | items = _providerList |
| 97 | .map((provider) => BuyItem(provider: provider, buyAmountViewModel: buyAmountViewModel)) |
| 98 | .toList(); |
| 99 | } |
| 100 | } |