2023-11-22 19:24:55 +00:00
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_exchange_provider.dart';
|
2023-11-22 18:54:34 +00:00
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_sell_provider.dart';
|
2023-04-14 04:39:08 +00:00
|
|
|
import 'package:cake_wallet/buy/onramper/onramper_buy_provider.dart';
|
2023-09-14 19:14:49 +00:00
|
|
|
import 'package:cake_wallet/buy/robinhood/robinhood_buy_provider.dart';
|
2023-04-14 04:39:08 +00:00
|
|
|
import 'package:cake_wallet/di.dart';
|
2023-09-14 19:14:49 +00:00
|
|
|
import 'package:cake_wallet/entities/buy_provider_types.dart';
|
2023-10-17 14:21:30 +00:00
|
|
|
import 'package:cake_wallet/entities/exchange_provider_types.dart';
|
2023-04-14 04:39:08 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/routes.dart';
|
|
|
|
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
|
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
|
|
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
|
|
|
import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
class MainActions {
|
|
|
|
final String Function(BuildContext context) name;
|
|
|
|
final String image;
|
|
|
|
|
|
|
|
final bool Function(DashboardViewModel viewModel)? isEnabled;
|
|
|
|
final bool Function(DashboardViewModel viewModel)? canShow;
|
2023-12-02 02:26:43 +00:00
|
|
|
final Future<void> Function(
|
|
|
|
BuildContext context, DashboardViewModel viewModel) onTap;
|
2023-04-14 04:39:08 +00:00
|
|
|
|
|
|
|
MainActions._({
|
|
|
|
required this.name,
|
|
|
|
required this.image,
|
|
|
|
this.isEnabled,
|
|
|
|
this.canShow,
|
|
|
|
required this.onTap,
|
|
|
|
});
|
|
|
|
|
|
|
|
static List<MainActions> all = [
|
|
|
|
buyAction,
|
|
|
|
receiveAction,
|
|
|
|
exchangeAction,
|
|
|
|
sendAction,
|
|
|
|
sellAction,
|
|
|
|
];
|
|
|
|
|
|
|
|
static MainActions buyAction = MainActions._(
|
|
|
|
name: (context) => S.of(context).buy,
|
|
|
|
image: 'assets/images/buy.png',
|
|
|
|
isEnabled: (viewModel) => viewModel.isEnabledBuyAction,
|
|
|
|
canShow: (viewModel) => viewModel.hasBuyAction,
|
|
|
|
onTap: (BuildContext context, DashboardViewModel viewModel) async {
|
2023-09-14 19:14:49 +00:00
|
|
|
final defaultBuyProvider = viewModel.defaultBuyProvider;
|
2023-04-14 04:39:08 +00:00
|
|
|
final walletType = viewModel.type;
|
|
|
|
|
2023-09-14 19:14:49 +00:00
|
|
|
if (!viewModel.isEnabledBuyAction) return;
|
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
switch (walletType) {
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
case WalletType.litecoin:
|
2023-08-04 17:01:49 +00:00
|
|
|
case WalletType.ethereum:
|
2023-12-02 02:26:43 +00:00
|
|
|
case WalletType.polygon:
|
2023-11-17 18:37:32 +00:00
|
|
|
case WalletType.bitcoinCash:
|
2023-09-14 19:14:49 +00:00
|
|
|
switch (defaultBuyProvider) {
|
|
|
|
case BuyProviderType.AskEachTime:
|
|
|
|
Navigator.pushNamed(context, Routes.buy);
|
|
|
|
break;
|
|
|
|
case BuyProviderType.Onramper:
|
|
|
|
await getIt.get<OnRamperBuyProvider>().launchProvider(context);
|
|
|
|
break;
|
|
|
|
case BuyProviderType.Robinhood:
|
|
|
|
await getIt.get<RobinhoodBuyProvider>().launchProvider(context);
|
|
|
|
break;
|
2023-04-14 04:39:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-10-13 11:49:00 +00:00
|
|
|
case WalletType.nano:
|
|
|
|
case WalletType.banano:
|
2023-09-14 19:14:49 +00:00
|
|
|
case WalletType.monero:
|
|
|
|
await getIt.get<OnRamperBuyProvider>().launchProvider(context);
|
|
|
|
break;
|
2023-04-14 04:39:08 +00:00
|
|
|
default:
|
|
|
|
await showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: S.of(context).buy,
|
2023-10-05 01:09:07 +00:00
|
|
|
alertContent: S.of(context).unsupported_asset,
|
2023-04-14 04:39:08 +00:00
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
static MainActions receiveAction = MainActions._(
|
|
|
|
name: (context) => S.of(context).receive,
|
|
|
|
image: 'assets/images/received.png',
|
|
|
|
onTap: (BuildContext context, DashboardViewModel viewModel) async {
|
|
|
|
Navigator.pushNamed(context, Routes.addressPage);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
static MainActions exchangeAction = MainActions._(
|
|
|
|
name: (context) => S.of(context).exchange,
|
|
|
|
image: 'assets/images/transfer.png',
|
|
|
|
isEnabled: (viewModel) => viewModel.isEnabledExchangeAction,
|
|
|
|
canShow: (viewModel) => viewModel.hasExchangeAction,
|
|
|
|
onTap: (BuildContext context, DashboardViewModel viewModel) async {
|
2023-10-17 14:21:30 +00:00
|
|
|
if (!viewModel.isEnabledExchangeAction) return;
|
|
|
|
final defaultExchangeProvider = viewModel.defaultExchangeProvider;
|
|
|
|
final walletType = viewModel.type;
|
2023-11-15 14:08:48 +00:00
|
|
|
|
2023-10-17 14:21:30 +00:00
|
|
|
switch (walletType) {
|
|
|
|
case WalletType.ethereum:
|
|
|
|
switch (defaultExchangeProvider) {
|
|
|
|
case ExchangeProviderType.AskEachTime:
|
|
|
|
Navigator.pushNamed(context, Routes.choose_exchange_provider);
|
|
|
|
break;
|
|
|
|
case ExchangeProviderType.MoonPay:
|
2023-10-18 15:25:26 +00:00
|
|
|
await getIt.get<MoonPayExchangeProvider>().launchProvider(context);
|
2023-10-17 14:21:30 +00:00
|
|
|
break;
|
|
|
|
case ExchangeProviderType.Normal:
|
|
|
|
await Navigator.of(context).pushNamed(Routes.exchange);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2023-11-15 14:20:37 +00:00
|
|
|
case WalletType.bitcoin:
|
|
|
|
case WalletType.litecoin:
|
|
|
|
case WalletType.bitcoinCash:
|
|
|
|
case WalletType.nano:
|
|
|
|
case WalletType.banano:
|
|
|
|
case WalletType.monero:
|
|
|
|
await Navigator.of(context).pushNamed(Routes.exchange);
|
|
|
|
break;
|
2023-10-17 14:21:30 +00:00
|
|
|
default:
|
|
|
|
await showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: S.of(context).exchange,
|
|
|
|
alertContent: S.of(context).unsupported_asset,
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
break;
|
2023-04-14 04:39:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
static MainActions sendAction = MainActions._(
|
|
|
|
name: (context) => S.of(context).send,
|
|
|
|
image: 'assets/images/upload.png',
|
|
|
|
onTap: (BuildContext context, DashboardViewModel viewModel) async {
|
|
|
|
Navigator.pushNamed(context, Routes.send);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
static MainActions sellAction = MainActions._(
|
|
|
|
name: (context) => S.of(context).sell,
|
|
|
|
image: 'assets/images/sell.png',
|
|
|
|
isEnabled: (viewModel) => viewModel.isEnabledSellAction,
|
|
|
|
canShow: (viewModel) => viewModel.hasSellAction,
|
|
|
|
onTap: (BuildContext context, DashboardViewModel viewModel) async {
|
|
|
|
final walletType = viewModel.type;
|
|
|
|
|
|
|
|
switch (walletType) {
|
|
|
|
case WalletType.bitcoin:
|
2023-06-13 23:15:10 +00:00
|
|
|
case WalletType.litecoin:
|
2023-08-04 17:01:49 +00:00
|
|
|
case WalletType.ethereum:
|
2023-12-02 02:26:43 +00:00
|
|
|
case WalletType.polygon:
|
2023-10-12 22:50:16 +00:00
|
|
|
case WalletType.bitcoinCash:
|
2023-05-15 12:26:56 +00:00
|
|
|
if (viewModel.isEnabledSellAction) {
|
|
|
|
final moonPaySellProvider = MoonPaySellProvider();
|
|
|
|
final uri = await moonPaySellProvider.requestUrl(
|
|
|
|
currency: viewModel.wallet.currency,
|
|
|
|
refundWalletAddress: viewModel.wallet.walletAddresses.address,
|
2023-06-13 23:15:10 +00:00
|
|
|
settingsStore: viewModel.settingsStore,
|
2023-05-15 12:26:56 +00:00
|
|
|
);
|
2023-06-13 23:15:10 +00:00
|
|
|
if (DeviceInfo.instance.isMobile) {
|
2023-10-17 14:21:30 +00:00
|
|
|
Navigator.of(context)
|
|
|
|
.pushNamed(Routes.webViewPage, arguments: [S.of(context).sell, uri]);
|
2023-06-13 23:15:10 +00:00
|
|
|
} else {
|
|
|
|
await launchUrl(uri);
|
|
|
|
}
|
2023-05-15 12:26:56 +00:00
|
|
|
}
|
2023-06-13 23:15:10 +00:00
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
await showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: S.of(context).sell,
|
2023-10-05 01:09:07 +00:00
|
|
|
alertContent: S.of(context).unsupported_asset,
|
2023-04-14 04:39:08 +00:00
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|