2021-03-12 19:04:37 +00:00
|
|
|
import 'dart:async';
|
2021-04-13 18:40:44 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
|
|
|
import 'package:cake_wallet/buy/buy_provider_description.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/buy/order.dart';
|
2021-03-12 19:04:37 +00:00
|
|
|
import 'package:cake_wallet/utils/date_formatter.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/trade_details/track_trade_list_item.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2024-01-01 13:05:37 +00:00
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_provider.dart';
|
2021-04-13 18:40:44 +00:00
|
|
|
import 'package:cake_wallet/buy/wyre/wyre_buy_provider.dart';
|
2021-03-12 19:04:37 +00:00
|
|
|
|
|
|
|
part 'order_details_view_model.g.dart';
|
|
|
|
|
|
|
|
class OrderDetailsViewModel = OrderDetailsViewModelBase
|
|
|
|
with _$OrderDetailsViewModel;
|
|
|
|
|
|
|
|
abstract class OrderDetailsViewModelBase with Store {
|
2022-10-12 17:09:57 +00:00
|
|
|
OrderDetailsViewModelBase({required WalletBase wallet, required Order orderForDetails})
|
2024-05-23 11:00:52 +00:00
|
|
|
: items = ObservableList<StandartListItem>(),
|
2022-10-12 17:09:57 +00:00
|
|
|
order = orderForDetails {
|
2021-04-13 18:40:44 +00:00
|
|
|
if (order.provider != null) {
|
|
|
|
switch (order.provider) {
|
|
|
|
case BuyProviderDescription.wyre:
|
|
|
|
_provider = WyreBuyProvider(wallet: wallet);
|
|
|
|
break;
|
|
|
|
case BuyProviderDescription.moonPay:
|
2024-03-28 13:30:41 +00:00
|
|
|
// _provider = MoonPayProvider(wallet: wallet);// TODO: CW-521
|
2021-04-13 18:40:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:04:37 +00:00
|
|
|
_updateItems();
|
|
|
|
_updateOrder();
|
2021-04-13 18:40:44 +00:00
|
|
|
timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateOrder());
|
2021-03-12 19:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@observable
|
|
|
|
Order order;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<StandartListItem> items;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
BuyProvider? _provider;
|
2021-03-23 17:19:01 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Timer? timer;
|
2021-03-12 19:04:37 +00:00
|
|
|
|
|
|
|
@action
|
|
|
|
Future<void> _updateOrder() async {
|
|
|
|
try {
|
2024-03-28 13:30:41 +00:00
|
|
|
if (_provider != null && (_provider is MoonPayProvider || _provider is WyreBuyProvider)) {
|
|
|
|
final updatedOrder = _provider is MoonPayProvider
|
|
|
|
? await (_provider as MoonPayProvider).findOrderById(order.id)
|
2023-12-28 19:20:59 +00:00
|
|
|
: await (_provider as WyreBuyProvider).findOrderById(order.id);
|
2021-04-30 10:12:00 +00:00
|
|
|
updatedOrder.from = order.from;
|
|
|
|
updatedOrder.to = order.to;
|
2021-04-13 18:40:44 +00:00
|
|
|
updatedOrder.receiveAddress = order.receiveAddress;
|
|
|
|
updatedOrder.walletId = order.walletId;
|
2021-04-14 18:23:10 +00:00
|
|
|
if (order.provider != null) {
|
|
|
|
updatedOrder.providerRaw = order.provider.raw;
|
|
|
|
}
|
2021-04-13 18:40:44 +00:00
|
|
|
order = updatedOrder;
|
|
|
|
_updateItems();
|
|
|
|
}
|
2021-03-12 19:04:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateItems() {
|
|
|
|
final dateFormat = DateFormatter.withCurrentLocal();
|
2022-10-12 17:09:57 +00:00
|
|
|
items.clear();
|
2021-03-12 19:04:37 +00:00
|
|
|
items.addAll([
|
|
|
|
StandartListItem(
|
|
|
|
title: 'Transfer ID',
|
|
|
|
value: order.transferId),
|
|
|
|
StandartListItem(
|
|
|
|
title: S.current.trade_details_state,
|
|
|
|
value: order.state != null
|
|
|
|
? order.state.toString()
|
|
|
|
: S.current.trade_details_fetching),
|
|
|
|
]);
|
2021-04-13 18:40:44 +00:00
|
|
|
|
2023-03-31 19:14:22 +00:00
|
|
|
items.add(
|
|
|
|
StandartListItem(
|
|
|
|
title: 'Buy provider',
|
|
|
|
value: order.provider.title)
|
|
|
|
);
|
2021-04-13 18:40:44 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
if (_provider != null && (_provider is MoonPayProvider || _provider is WyreBuyProvider)) {
|
2023-12-28 19:20:59 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
final trackUrl = _provider is MoonPayProvider
|
|
|
|
? (_provider as MoonPayProvider).trackUrl
|
2023-12-28 19:20:59 +00:00
|
|
|
: (_provider as WyreBuyProvider).trackUrl;
|
|
|
|
|
|
|
|
if (trackUrl.isNotEmpty ?? false) {
|
|
|
|
final buildURL = trackUrl + '${order.transferId}';
|
|
|
|
items.add(
|
|
|
|
TrackTradeListItem(
|
2024-03-28 12:41:11 +00:00
|
|
|
title: S.current.track,
|
2023-12-28 19:20:59 +00:00
|
|
|
value: buildURL,
|
2024-05-23 11:00:52 +00:00
|
|
|
onTap: () async {
|
2023-12-28 19:20:59 +00:00
|
|
|
try {
|
2024-05-23 11:00:52 +00:00
|
|
|
final uri = Uri.parse(buildURL);
|
|
|
|
if (await canLaunchUrl(uri))
|
|
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
2023-12-28 19:20:59 +00:00
|
|
|
} catch (e) {}
|
2024-05-23 11:00:52 +00:00
|
|
|
}));
|
2023-12-28 19:20:59 +00:00
|
|
|
}
|
2021-04-13 18:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
items.add(
|
|
|
|
StandartListItem(
|
|
|
|
title: S.current.trade_details_created_at,
|
|
|
|
value: dateFormat.format(order.createdAt).toString())
|
|
|
|
);
|
|
|
|
|
|
|
|
items.add(
|
|
|
|
StandartListItem(
|
|
|
|
title: S.current.trade_details_pair,
|
|
|
|
value: '${order.from} → ${order.to}')
|
|
|
|
);
|
2021-03-12 19:04:37 +00:00
|
|
|
}
|
|
|
|
}
|