2020-08-26 17:31:23 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/changenow/changenow_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/trade.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/store/dashboard/trades_store.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2020-08-27 19:42:28 +00:00
|
|
|
import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_item.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2020-08-26 17:31:23 +00:00
|
|
|
|
|
|
|
part 'exchange_trade_view_model.g.dart';
|
|
|
|
|
|
|
|
class ExchangeTradeViewModel = ExchangeTradeViewModelBase with _$ExchangeTradeViewModel;
|
|
|
|
|
|
|
|
abstract class ExchangeTradeViewModelBase with Store {
|
|
|
|
ExchangeTradeViewModelBase({this.wallet, this.trades, this.tradesStore}) {
|
|
|
|
trade = tradesStore.trade;
|
|
|
|
|
|
|
|
isSendable = trade.from == wallet.currency ||
|
|
|
|
trade.provider == ExchangeProviderDescription.xmrto;
|
|
|
|
|
|
|
|
switch (trade.provider) {
|
|
|
|
case ExchangeProviderDescription.xmrto:
|
|
|
|
_provider = XMRTOExchangeProvider();
|
|
|
|
break;
|
|
|
|
case ExchangeProviderDescription.changeNow:
|
|
|
|
_provider = ChangeNowExchangeProvider();
|
|
|
|
break;
|
|
|
|
case ExchangeProviderDescription.morphToken:
|
|
|
|
_provider = MorphTokenExchangeProvider(trades: trades);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-28 20:04:48 +00:00
|
|
|
items = ObservableList<ExchangeTradeItem>();
|
|
|
|
items.addAll([
|
2020-08-27 19:42:28 +00:00
|
|
|
ExchangeTradeItem(
|
|
|
|
title: S.current.id,
|
|
|
|
data: '${trade.id}',
|
|
|
|
isCopied: true),
|
|
|
|
ExchangeTradeItem(
|
|
|
|
title: S.current.amount,
|
|
|
|
data: '${trade.amount}',
|
|
|
|
isCopied: false),
|
|
|
|
ExchangeTradeItem(
|
|
|
|
title: S.current.status,
|
|
|
|
data: '${trade.state}',
|
|
|
|
isCopied: false),
|
|
|
|
ExchangeTradeItem(
|
|
|
|
title: S.current.widgets_address + ':',
|
|
|
|
data: trade.inputAddress,
|
|
|
|
isCopied: true),
|
|
|
|
]);
|
|
|
|
|
2020-08-26 17:31:23 +00:00
|
|
|
_updateTrade();
|
|
|
|
_timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade());
|
|
|
|
}
|
|
|
|
|
|
|
|
final WalletBase wallet;
|
|
|
|
final Box<Trade> trades;
|
|
|
|
final TradesStore tradesStore;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
Trade trade;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool isSendable;
|
|
|
|
|
2020-08-27 19:42:28 +00:00
|
|
|
@observable
|
2020-08-28 20:04:48 +00:00
|
|
|
ObservableList<ExchangeTradeItem> items;
|
2020-08-27 19:42:28 +00:00
|
|
|
|
2020-08-26 17:31:23 +00:00
|
|
|
ExchangeProvider _provider;
|
|
|
|
|
|
|
|
Timer _timer;
|
|
|
|
|
|
|
|
@action
|
|
|
|
Future<void> _updateTrade() async {
|
|
|
|
try {
|
|
|
|
final updatedTrade = await _provider.findTradeById(id: trade.id);
|
|
|
|
|
|
|
|
if (updatedTrade.createdAt == null && trade.createdAt != null) {
|
|
|
|
updatedTrade.createdAt = trade.createdAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
trade = updatedTrade;
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|