2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
2024-01-01 13:05:37 +00:00
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_provider.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/buy/wyre/wyre_buy_provider.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2021-06-01 18:03:35 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/view_model/buy/buy_item.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:cake_wallet/buy/order.dart';
|
|
|
|
import 'package:cake_wallet/store/dashboard/orders_store.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'buy_amount_view_model.dart';
|
|
|
|
|
|
|
|
part 'buy_view_model.g.dart';
|
|
|
|
|
|
|
|
class BuyViewModel = BuyViewModelBase with _$BuyViewModel;
|
|
|
|
|
|
|
|
abstract class BuyViewModelBase with Store {
|
2021-06-01 18:03:35 +00:00
|
|
|
BuyViewModelBase(this.ordersSource, this.ordersStore, this.settingsStore,
|
2022-10-12 17:09:57 +00:00
|
|
|
this.buyAmountViewModel, {required this.wallet})
|
|
|
|
: isRunning = false,
|
|
|
|
isDisabled = true,
|
|
|
|
isShowProviderButtons = false,
|
|
|
|
items = <BuyItem>[] {
|
2021-06-07 13:40:25 +00:00
|
|
|
_fetchBuyItems();
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final Box<Order> ordersSource;
|
|
|
|
final OrdersStore ordersStore;
|
2021-06-01 18:03:35 +00:00
|
|
|
final SettingsStore settingsStore;
|
2021-04-12 18:22:22 +00:00
|
|
|
final BuyAmountViewModel buyAmountViewModel;
|
|
|
|
final WalletBase wallet;
|
|
|
|
|
|
|
|
@observable
|
2022-10-12 17:09:57 +00:00
|
|
|
BuyProvider? selectedProvider;
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
List<BuyItem> items;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool isRunning;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool isDisabled;
|
|
|
|
|
2021-04-15 17:10:23 +00:00
|
|
|
@observable
|
|
|
|
bool isShowProviderButtons;
|
|
|
|
|
2021-04-12 18:22:22 +00:00
|
|
|
WalletType get type => wallet.type;
|
|
|
|
|
|
|
|
double get doubleAmount => buyAmountViewModel.doubleAmount;
|
|
|
|
|
2022-07-01 11:04:00 +00:00
|
|
|
@computed
|
2021-04-12 18:22:22 +00:00
|
|
|
FiatCurrency get fiatCurrency => buyAmountViewModel.fiatCurrency;
|
|
|
|
|
|
|
|
CryptoCurrency get cryptoCurrency => walletTypeToCryptoCurrency(type);
|
|
|
|
|
2021-04-13 18:40:44 +00:00
|
|
|
Future <String> fetchUrl() async {
|
|
|
|
String _url = '';
|
|
|
|
|
2021-04-12 18:22:22 +00:00
|
|
|
try {
|
2023-12-28 19:20:59 +00:00
|
|
|
_url = await selectedProvider!.requestUrl(doubleAmount.toString(), fiatCurrency.title);
|
2021-04-12 18:22:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
2021-04-13 18:40:44 +00:00
|
|
|
|
|
|
|
return _url;
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> saveOrder(String orderId) async {
|
|
|
|
try {
|
2022-10-12 17:09:57 +00:00
|
|
|
final order = await selectedProvider!.findOrderById(orderId);
|
2021-04-30 08:08:25 +00:00
|
|
|
order.from = fiatCurrency.title;
|
|
|
|
order.to = cryptoCurrency.title;
|
2021-04-12 18:22:22 +00:00
|
|
|
await ordersSource.add(order);
|
|
|
|
ordersStore.setOrder(order);
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 18:40:44 +00:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
buyAmountViewModel.amount = '';
|
|
|
|
selectedProvider = null;
|
|
|
|
}
|
2021-06-07 13:40:25 +00:00
|
|
|
|
|
|
|
Future<void> _fetchBuyItems() async {
|
2021-06-08 20:06:22 +00:00
|
|
|
final List<BuyProvider> _providerList = [];
|
|
|
|
|
|
|
|
if (wallet.type == WalletType.bitcoin) {
|
|
|
|
_providerList.add(WyreBuyProvider(wallet: wallet));
|
|
|
|
}
|
2021-06-07 13:40:25 +00:00
|
|
|
|
|
|
|
items = _providerList.map((provider) =>
|
|
|
|
BuyItem(provider: provider, buyAmountViewModel: buyAmountViewModel))
|
|
|
|
.toList();
|
|
|
|
}
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|