2020-12-18 12:42:53 +00:00
|
|
|
import 'dart:async';
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/changenow_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/exolix_exchange_provider.dart';
|
2024-05-14 02:07:16 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/quantex_exchange_provider.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/sideshift_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/simpleswap_exchange_provider.dart';
|
2024-03-28 12:41:11 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
|
2020-12-18 12:42:53 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/trade_details/track_trade_list_item.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/trade_details/trade_details_list_card.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/trade_details/trade_details_status_item.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/trade_details/trade_provider_unsupported_item.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
|
2022-09-02 13:10:54 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2020-12-18 12:42:53 +00:00
|
|
|
import 'package:cake_wallet/utils/date_formatter.dart';
|
2022-09-02 13:10:54 +00:00
|
|
|
import 'package:cake_wallet/utils/show_bar.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2022-09-02 13:10:54 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2020-12-18 12:42:53 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2021-02-02 01:36:36 +00:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2023-03-31 19:14:22 +00:00
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
part 'trade_details_view_model.g.dart';
|
|
|
|
|
2023-02-07 17:39:39 +00:00
|
|
|
class TradeDetailsViewModel = TradeDetailsViewModelBase with _$TradeDetailsViewModel;
|
2020-12-18 12:42:53 +00:00
|
|
|
|
|
|
|
abstract class TradeDetailsViewModelBase with Store {
|
2022-10-12 17:09:57 +00:00
|
|
|
TradeDetailsViewModelBase({
|
|
|
|
required Trade tradeForDetails,
|
|
|
|
required this.trades,
|
2023-02-07 17:39:39 +00:00
|
|
|
required this.settingsStore,
|
|
|
|
}) : items = ObservableList<StandartListItem>(),
|
2023-06-14 20:55:49 +00:00
|
|
|
trade = trades.values.firstWhereOrNull((element) => element.id == tradeForDetails.id) ??
|
|
|
|
tradeForDetails {
|
2020-12-18 12:42:53 +00:00
|
|
|
switch (trade.provider) {
|
|
|
|
case ExchangeProviderDescription.changeNow:
|
2023-07-08 02:30:05 +00:00
|
|
|
_provider = ChangeNowExchangeProvider(settingsStore: settingsStore);
|
2020-12-18 12:42:53 +00:00
|
|
|
break;
|
2022-04-13 13:28:21 +00:00
|
|
|
case ExchangeProviderDescription.sideShift:
|
|
|
|
_provider = SideShiftExchangeProvider();
|
|
|
|
break;
|
2023-02-07 17:39:39 +00:00
|
|
|
case ExchangeProviderDescription.simpleSwap:
|
2022-08-31 15:34:07 +00:00
|
|
|
_provider = SimpleSwapExchangeProvider();
|
|
|
|
break;
|
2023-02-06 19:20:43 +00:00
|
|
|
case ExchangeProviderDescription.trocador:
|
|
|
|
_provider = TrocadorExchangeProvider();
|
|
|
|
break;
|
2023-10-12 00:20:19 +00:00
|
|
|
case ExchangeProviderDescription.exolix:
|
|
|
|
_provider = ExolixExchangeProvider();
|
|
|
|
break;
|
2024-03-28 12:41:11 +00:00
|
|
|
case ExchangeProviderDescription.thorChain:
|
|
|
|
_provider = ThorChainExchangeProvider(tradesStore: trades);
|
|
|
|
break;
|
2024-05-14 02:07:16 +00:00
|
|
|
case ExchangeProviderDescription.quantex:
|
|
|
|
_provider = QuantexExchangeProvider();
|
|
|
|
break;
|
2020-12-18 12:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateItems();
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (_provider != null) {
|
|
|
|
_updateTrade();
|
|
|
|
timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade());
|
|
|
|
}
|
2020-12-18 12:42:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 12:41:11 +00:00
|
|
|
static String? getTrackUrl(ExchangeProviderDescription provider, Trade trade) {
|
|
|
|
switch (provider) {
|
|
|
|
case ExchangeProviderDescription.changeNow:
|
|
|
|
return 'https://changenow.io/exchange/txs/${trade.id}';
|
|
|
|
case ExchangeProviderDescription.sideShift:
|
|
|
|
return 'https://sideshift.ai/orders/${trade.id}';
|
|
|
|
case ExchangeProviderDescription.simpleSwap:
|
|
|
|
return 'https://simpleswap.io/exchange?id=${trade.id}';
|
|
|
|
case ExchangeProviderDescription.trocador:
|
|
|
|
return 'https://trocador.app/en/checkout/${trade.id}';
|
|
|
|
case ExchangeProviderDescription.exolix:
|
|
|
|
return 'https://exolix.com/transaction/${trade.id}';
|
|
|
|
case ExchangeProviderDescription.thorChain:
|
|
|
|
return 'https://track.ninerealms.com/${trade.id}';
|
2024-05-14 02:07:16 +00:00
|
|
|
case ExchangeProviderDescription.quantex:
|
|
|
|
return 'https://myquantex.com/send/${trade.id}';
|
2024-03-28 12:41:11 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
final Box<Trade> trades;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
Trade trade;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<StandartListItem> items;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
ExchangeProvider? _provider;
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Timer? timer;
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2022-09-02 13:10:54 +00:00
|
|
|
final SettingsStore settingsStore;
|
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
@action
|
|
|
|
Future<void> _updateTrade() async {
|
|
|
|
try {
|
2022-10-12 17:09:57 +00:00
|
|
|
final updatedTrade = await _provider!.findTradeById(id: trade.id);
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (updatedTrade.createdAt == null && trade.createdAt != null)
|
2020-12-18 12:42:53 +00:00
|
|
|
updatedTrade.createdAt = trade.createdAt;
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2023-06-14 20:55:49 +00:00
|
|
|
Trade? foundElement = trades.values.firstWhereOrNull((element) => element.id == trade.id);
|
|
|
|
if (foundElement != null) {
|
|
|
|
final editedTrade = trades.get(foundElement.key);
|
|
|
|
editedTrade?.stateRaw = updatedTrade.stateRaw;
|
|
|
|
editedTrade?.save();
|
|
|
|
}
|
2020-12-18 12:42:53 +00:00
|
|
|
|
|
|
|
trade = updatedTrade;
|
|
|
|
|
|
|
|
_updateItems();
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateItems() {
|
2022-09-02 13:10:54 +00:00
|
|
|
final dateFormat = DateFormatter.withCurrentLocal(reverse: true);
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
items.clear();
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (_provider == null)
|
|
|
|
items.add(TradeProviderUnsupportedItem(
|
|
|
|
error: S.current.exchange_provider_unsupported(trade.provider.title)));
|
|
|
|
|
2022-09-02 13:10:54 +00:00
|
|
|
items.add(
|
2023-02-07 17:39:39 +00:00
|
|
|
DetailsListStatusItem(title: S.current.trade_details_state, value: trade.state.toString()));
|
2022-09-02 13:10:54 +00:00
|
|
|
|
|
|
|
items.add(TradeDetailsListCardItem.tradeDetails(
|
|
|
|
id: trade.id,
|
2022-10-12 17:09:57 +00:00
|
|
|
createdAt: trade.createdAt != null ? dateFormat.format(trade.createdAt!) : '',
|
2022-09-02 13:10:54 +00:00
|
|
|
from: trade.from,
|
|
|
|
to: trade.to,
|
|
|
|
onTap: (BuildContext context) {
|
|
|
|
Clipboard.setData(ClipboardData(text: '${trade.id}'));
|
|
|
|
showBar<void>(context, S.of(context).copied_to_clipboard);
|
|
|
|
},
|
|
|
|
));
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2023-02-07 17:39:39 +00:00
|
|
|
items.add(StandartListItem(
|
|
|
|
title: S.current.trade_details_provider, value: trade.provider.toString()));
|
2020-12-18 12:42:53 +00:00
|
|
|
|
2024-03-28 12:41:11 +00:00
|
|
|
final trackUrl = TradeDetailsViewModelBase.getTrackUrl(trade.provider, trade);
|
|
|
|
if (trackUrl != null) {
|
2021-02-02 01:36:36 +00:00
|
|
|
items.add(TrackTradeListItem(
|
2024-03-28 12:41:11 +00:00
|
|
|
title: S.current.track, value: trackUrl, onTap: () => _launchUrl(trackUrl)));
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 12:41:11 +00:00
|
|
|
if (trade.isRefund == true) {
|
|
|
|
items.add(StandartListItem(
|
|
|
|
title: 'Refund', value: trade.refundAddress ?? ''));
|
2023-02-07 17:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trade.provider == ExchangeProviderDescription.trocador) {
|
|
|
|
items.add(StandartListItem(
|
|
|
|
title: '${trade.providerName} ${S.current.id.toUpperCase()}',
|
|
|
|
value: trade.providerId ?? ''));
|
|
|
|
|
2024-03-28 12:41:11 +00:00
|
|
|
if (trade.password != null && trade.password!.isNotEmpty) {
|
2023-02-07 17:39:39 +00:00
|
|
|
items.add(StandartListItem(
|
|
|
|
title: '${trade.providerName} ${S.current.password}', value: trade.password ?? ''));
|
2024-03-28 12:41:11 +00:00
|
|
|
}
|
2023-10-12 00:20:19 +00:00
|
|
|
}
|
2020-12-18 12:42:53 +00:00
|
|
|
}
|
2023-03-31 19:14:22 +00:00
|
|
|
|
|
|
|
void _launchUrl(String url) {
|
2023-06-14 20:55:49 +00:00
|
|
|
final uri = Uri.parse(url);
|
2023-03-31 19:14:22 +00:00
|
|
|
try {
|
2023-06-14 20:55:49 +00:00
|
|
|
launchUrl(uri);
|
2023-03-31 19:14:22 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
2021-02-02 01:36:36 +00:00
|
|
|
}
|