slight improvement

This commit is contained in:
Serhii 2024-02-15 16:11:54 +02:00
parent cd69ce482b
commit a7837d2014
9 changed files with 141 additions and 38 deletions

View file

@ -15,7 +15,9 @@ import 'package:collection/collection.dart';
enum OnRamperPartner { enum OnRamperPartner {
guardarian, guardarian,
paybis, paybis,
utorg utorg,
alchemypay,
sardine
} }
extension Name on OnRamperPartner { extension Name on OnRamperPartner {
@ -27,6 +29,10 @@ extension Name on OnRamperPartner {
return 'Paybis'; return 'Paybis';
case OnRamperPartner.utorg: case OnRamperPartner.utorg:
return 'Utorg'; return 'Utorg';
case OnRamperPartner.alchemypay:
return 'Alchemy Pay';
case OnRamperPartner.sardine:
return 'Sardine';
} }
} }
} }
@ -138,7 +144,7 @@ class OnRamperBuyProvider extends BuyProvider {
final uri = requestOnramperUrl(context, isBuyAction); final uri = requestOnramperUrl(context, isBuyAction);
if (DeviceInfo.instance.isMobile) { if (DeviceInfo.instance.isMobile) {
Navigator.of(context) Navigator.of(context)
.pushNamed(Routes.webViewPage, arguments:[uri, providerType]); .pushNamed(Routes.webViewPage, arguments:[uri, providerType, isBuyAction]);
} else { } else {
await launchUrl(uri); await launchUrl(uri);
} }

View file

@ -39,6 +39,7 @@ class Order extends HiveObject {
final decoded = json.decode(jsonSource) as Map<String, dynamic>; final decoded = json.decode(jsonSource) as Map<String, dynamic>;
final providerRaw = decoded['providerRaw'] as int?; final providerRaw = decoded['providerRaw'] as int?;
final onramperPartnerRaw = decoded['onramperPartnerRaw'] as int?; final onramperPartnerRaw = decoded['onramperPartnerRaw'] as int?;
final stateRaw = decoded['stateRaw'] as String?;
return Order( return Order(
id: decoded['id'] as String, id: decoded['id'] as String,
@ -50,7 +51,7 @@ class Order extends HiveObject {
provider: providerRaw != null ? ProvidersHelper.deserialize(raw: providerRaw) : null, provider: providerRaw != null ? ProvidersHelper.deserialize(raw: providerRaw) : null,
onramperPartner: onramperPartner:
onramperPartnerRaw != null ? OnRamperBuyProvider.fromRaw(onramperPartnerRaw) : null, onramperPartnerRaw != null ? OnRamperBuyProvider.fromRaw(onramperPartnerRaw) : null,
state: TradeState.created, state: stateRaw != null ? TradeState.deserialize(raw: stateRaw) : null,
from: decoded['from'] as String?, from: decoded['from'] as String?,
to: decoded['to'] as String?, to: decoded['to'] as String?,
); );

View file

@ -814,7 +814,8 @@ Future<void> setup({
getIt.registerFactoryParam<WebViewPage,List<dynamic>, void>((args, _) { getIt.registerFactoryParam<WebViewPage,List<dynamic>, void>((args, _) {
final uri = args.first as Uri; final uri = args.first as Uri;
final type = args.length > 1 ? args[1] as ProviderType? : null; final type = args.length > 1 ? args[1] as ProviderType? : null;
return WebViewPage(uri, type, buyViewModel: getIt.get<BuyViewModel>()); final isBuyAction = args.length > 2 ? args[2] as bool? : null;
return WebViewPage(uri, type, isBuyAction, buyViewModel: getIt.get<BuyViewModel>());
}); });

View file

@ -1,5 +1,3 @@
import 'dart:async';
import 'package:cake_wallet/entities/provider_types.dart'; import 'package:cake_wallet/entities/provider_types.dart';
import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/screens/base_page.dart';
@ -11,12 +9,14 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
class WebViewPage extends BasePage { class WebViewPage extends BasePage {
WebViewPage(this._url, this._providerType, {required this.buyViewModel}) { WebViewPage(this._url, this._providerType, this.isBuyAction, {required this.buyViewModel}) {
buyViewModel.selectedProviderType = _providerType; buyViewModel.selectedProviderType = _providerType;
buyViewModel.isBuyAction = isBuyAction;
} }
final Uri _url; final Uri _url;
final ProviderType? _providerType; final ProviderType? _providerType;
final bool? isBuyAction;
final BuyViewModel buyViewModel; final BuyViewModel buyViewModel;
@override @override

View file

@ -134,8 +134,8 @@ class TransactionsPage extends StatelessWidget {
onTap: () => Navigator.of(context) onTap: () => Navigator.of(context)
.pushNamed(Routes.orderDetails, arguments: order), .pushNamed(Routes.orderDetails, arguments: order),
provider: ProvidersHelper.getProviderByType(order.provider!)!, provider: ProvidersHelper.getProviderByType(order.provider!)!,
from: order.from!, from: order.from ?? '-',
to: order.to!, to: order.to ?? '-',
createdAtFormattedDate: createdAtFormattedDate:
DateFormat('HH:mm').format(order.createdAt), DateFormat('HH:mm').format(order.createdAt),
formattedAmount: item.orderFormattedAmount, formattedAmount: item.orderFormattedAmount,

View file

@ -53,7 +53,7 @@ class OrderRow extends StatelessWidget {
color: Theme.of(context).extension<DashboardPageTheme>()!.textColor color: Theme.of(context).extension<DashboardPageTheme>()!.textColor
)), )),
formattedAmount != null formattedAmount != null
? Text(formattedAmount! + ' ' + to, ? Text(formattedAmount! + ' ' + from,
style: TextStyle( style: TextStyle(
fontSize: 16, fontSize: 16,
fontWeight: FontWeight.w500, fontWeight: FontWeight.w500,

View file

@ -30,28 +30,39 @@ abstract class BuyViewModelBase with Store {
ProviderType? selectedProviderType; ProviderType? selectedProviderType;
bool? isBuyAction;
WalletType get type => wallet.type; WalletType get type => wallet.type;
@computed @computed
FiatCurrency get fiatCurrency => settingsStore.fiatCurrency; FiatCurrency get fiatCurrency => settingsStore.fiatCurrency;
CryptoCurrency get cryptoCurrency => walletTypeToCryptoCurrency(type); Future<void> saveOrder(String orderId,
{int? onRamperPartnerRaw,
String? fiatCurrency,
String? cryptoCurrency,
String? fiatAmount,
String? cryptoAmount}) async {
bool isBuyAction = this.isBuyAction ?? true;
final formattedCryptoCurrency =
cryptoCurrency != null ? CryptoCurrency.fromString(cryptoCurrency) : null;
final orderData = {
'id': orderId,
'transferId': orderId,
'createdAt': DateTime.now().toIso8601String(),
'amount': isBuyAction ? fiatAmount ?? '' : cryptoAmount ?? '',
'receiveAddress': '',
'walletId': wallet.id,
'providerRaw': ProvidersHelper.serialize(selectedProviderType ?? ProviderType.askEachTime),
'onramperPartnerRaw': onRamperPartnerRaw,
'from': isBuyAction ? fiatCurrency : formattedCryptoCurrency?.title,
'to': isBuyAction ? formattedCryptoCurrency?.title : fiatCurrency,
};
Future<void> saveOrder(String orderId, {int? onRamperPartnerRaw}) async {
try { try {
final String jsonSource = json.encode({ final String jsonSource = json.encode(orderData).toString();
'id': orderId,
'transferId': orderId,
'createdAt': DateTime.now().toIso8601String(),
'amount': '0.0',
'receiveAddress': 'address123',
'walletId': wallet.id,
'providerRaw': ProvidersHelper.serialize(selectedProviderType ?? ProviderType.askEachTime),
'onramperPartnerRaw': onRamperPartnerRaw,
'stateRaw': 'created',
'from': fiatCurrency.title,
'to': cryptoCurrency.title,
}).toString();
final order = Order.fromJSON(jsonSource); final order = Order.fromJSON(jsonSource);
@ -65,18 +76,31 @@ abstract class BuyViewModelBase with Store {
void processProviderUrl({required String urlStr}) async { void processProviderUrl({required String urlStr}) async {
if (selectedProviderType == null) return; if (selectedProviderType == null) return;
final orderId = extractInfoFromUrl(urlStr, selectedProviderType!); final orderId = extractInfoFromUrl(
urlStr, selectedProviderType!, providerUrlOrderIdConfigs[selectedProviderType!]);
final onRamperPartner = determineOnRamperPartner(urlStr); final onRamperPartner = determineOnRamperPartner(urlStr);
final onRamperPartnerRaw = onRamperPartner != null ? onRamperPartner.index : null; final onRamperPartnerRaw = onRamperPartner != null ? onRamperPartner.index : null;
if (orderId != null && orderId.isNotEmpty && orderId != this.orderId) { if (orderId != null && orderId.isNotEmpty && orderId != this.orderId) {
final fiatCurrency = extractInfoFromUrl(
urlStr, selectedProviderType!, providerUrlFiatCurrencyConfigs[selectedProviderType!]);
final cryptoCurrency = extractInfoFromUrl(
urlStr, selectedProviderType!, providerUrlCryptoCurrencyConfigs[selectedProviderType!]);
final fiatAmount = extractInfoFromUrl(
urlStr, selectedProviderType!, providerUrlFiatAmountConfigs[selectedProviderType!]);
final cryptoAmount = extractInfoFromUrl(
urlStr, selectedProviderType!, providerUrlCryptoAmountConfigs[selectedProviderType!]);
this.orderId = orderId; this.orderId = orderId;
await saveOrder(orderId, onRamperPartnerRaw: onRamperPartnerRaw); await saveOrder(orderId,
onRamperPartnerRaw: onRamperPartnerRaw,
fiatCurrency: fiatCurrency,
cryptoCurrency: cryptoCurrency,
fiatAmount: fiatAmount,
cryptoAmount: cryptoAmount);
} }
} }
String? extractInfoFromUrl(String url, ProviderType providerType) { String? extractInfoFromUrl(String url, ProviderType providerType, ProviderUrlConfig? config) {
final config = providerUrlConfigs[providerType];
if (config == null) return null; if (config == null) return null;
for (var entry in config.parameterKeywords.entries) { for (var entry in config.parameterKeywords.entries) {
@ -105,11 +129,15 @@ abstract class BuyViewModelBase with Store {
return OnRamperPartner.paybis; return OnRamperPartner.paybis;
} else if (url.contains('utpay')) { } else if (url.contains('utpay')) {
return OnRamperPartner.utorg; return OnRamperPartner.utorg;
} else if (url.contains('alchemypay')) {
return OnRamperPartner.alchemypay;
} else if (url.contains('sardine')) {
return OnRamperPartner.sardine;
} }
return null; return null;
} }
final Map<ProviderType, ProviderUrlConfig> providerUrlConfigs = { final Map<ProviderType, ProviderUrlConfig> providerUrlOrderIdConfigs = {
ProviderType.onramper: ProviderUrlConfig( ProviderType.onramper: ProviderUrlConfig(
name: ProviderType.onramper.title, name: ProviderType.onramper.title,
parameterKeywords: { parameterKeywords: {
@ -125,6 +153,74 @@ abstract class BuyViewModelBase with Store {
'start': '/order/', 'start': '/order/',
'end': '/', 'end': '/',
}, },
'alchemypay': {
'start': 'merchantOrderNo=',
'end': '&',
},
'sardine': {
'start': 'client_token=',
'end': null,
},
},
),
};
final Map<ProviderType, ProviderUrlConfig> providerUrlFiatCurrencyConfigs = {
ProviderType.onramper: ProviderUrlConfig(
name: ProviderType.onramper.title,
parameterKeywords: {
'alchemypay': {
'start': 'fiat=',
'end': '&',
},
'sardine': {
'start': 'fixed_fiat_currency=',
'end': '&',
},
},
),
};
final Map<ProviderType, ProviderUrlConfig> providerUrlCryptoCurrencyConfigs = {
ProviderType.onramper: ProviderUrlConfig(
name: ProviderType.onramper.title,
parameterKeywords: {
'alchemypay': {
'start': 'crypto=',
'end': '&',
},
'sardine': {
'start': 'fixed_asset_type=',
'end': '&',
},
},
),
};
final Map<ProviderType, ProviderUrlConfig> providerUrlFiatAmountConfigs = {
ProviderType.onramper: ProviderUrlConfig(
name: ProviderType.onramper.title,
parameterKeywords: {
'alchemypay': {
'start': 'fiatAmount=',
'end': '&',
},
'sardine': {
'start': 'fixed_fiat_amount=',
'end': '&',
},
},
),
};
final Map<ProviderType, ProviderUrlConfig> providerUrlCryptoAmountConfigs = {
ProviderType.onramper: ProviderUrlConfig(
name: ProviderType.onramper.title,
parameterKeywords: {
'alchemypay': {
'start': 'cryptoAmount=',
'end': '&',
},
}, },
), ),
}; };

View file

@ -14,11 +14,9 @@ class OrderListItem extends ActionListItem {
BalanceDisplayMode get displayMode => settingsStore.balanceDisplayMode; BalanceDisplayMode get displayMode => settingsStore.balanceDisplayMode;
String get orderFormattedAmount { String get orderFormattedAmount {
return order.amount != null return displayMode == BalanceDisplayMode.hiddenBalance
? displayMode == BalanceDisplayMode.hiddenBalance
? '---' ? '---'
: order.amountFormatted() : order.amountFormatted();
: order.amount;
} }
@override @override

View file

@ -67,11 +67,12 @@ abstract class OrderDetailsViewModelBase with Store {
StandartListItem( StandartListItem(
title: 'Transfer ID', title: 'Transfer ID',
value: order.transferId), value: order.transferId),
if(order.state != null)
StandartListItem( StandartListItem(
title: S.current.trade_details_state, title: S.current.trade_details_state,
value: order.state != null value: order.state.toString())
? order.state.toString()
: S.current.trade_details_fetching),
]); ]);
items.add( items.add(
@ -112,7 +113,7 @@ abstract class OrderDetailsViewModelBase with Store {
items.add( items.add(
StandartListItem( StandartListItem(
title: S.current.trade_details_pair, title: S.current.trade_details_pair,
value: '${order.from}${order.to}') value: '${order.from ?? '-'}${order.to ?? '-'}')
); );
} }
} }