diff --git a/lib/di.dart b/lib/di.dart index f924bbae0..a37f6fabe 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -28,6 +28,7 @@ import 'package:cake_wallet/src/screens/send/send_template_page.dart'; import 'package:cake_wallet/src/screens/settings/change_language.dart'; import 'package:cake_wallet/src/screens/settings/settings.dart'; import 'package:cake_wallet/src/screens/setup_pin_code/setup_pin_code.dart'; +import 'package:cake_wallet/src/screens/trade_details/trade_details_page.dart'; import 'package:cake_wallet/src/screens/transaction_details/transaction_details_page.dart'; import 'package:cake_wallet/src/screens/wallet_keys/wallet_keys_page.dart'; import 'package:cake_wallet/src/screens/exchange/exchange_page.dart'; @@ -56,6 +57,7 @@ import 'package:cake_wallet/view_model/node_list/node_create_or_edit_view_model. import 'package:cake_wallet/view_model/rescan_view_model.dart'; import 'package:cake_wallet/view_model/setup_pin_code_view_model.dart'; import 'package:cake_wallet/view_model/transaction_details_view_model.dart'; +import 'package:cake_wallet/view_model/trade_details_view_model.dart'; import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart'; import 'package:cake_wallet/view_model/auth_view_model.dart'; import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; @@ -436,4 +438,10 @@ Future setup( getIt.registerFactoryParam( (WalletType type, _) => PreSeedPage(type)); + + getIt.registerFactoryParam((trade, _) => + TradeDetailsViewModel(tradeForDetails: trade, trades: tradesSource)); + + getIt.registerFactoryParam((Trade trade, _) => + TradeDetailsPage(getIt.get(param1: trade))); } diff --git a/lib/router.dart b/lib/router.dart index 93b56071b..69cfb9eff 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -279,7 +279,8 @@ Route createRoute(RouteSettings settings) { case Routes.tradeDetails: return MaterialPageRoute( - builder: (_) => TradeDetailsPage(settings.arguments as Trade)); + builder: (_) => + getIt.get(param1: settings.arguments as Trade)); case Routes.restoreWalletFromSeedDetails: final args = settings.arguments as List; diff --git a/lib/src/screens/exchange_trade/exchange_trade_page.dart b/lib/src/screens/exchange_trade/exchange_trade_page.dart index e43b8d8f1..81449fb0e 100644 --- a/lib/src/screens/exchange_trade/exchange_trade_page.dart +++ b/lib/src/screens/exchange_trade/exchange_trade_page.dart @@ -6,8 +6,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/core/execution_state.dart'; -import 'package:cake_wallet/entities/crypto_currency.dart'; -import 'package:cake_wallet/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/src/screens/exchange_trade/information_page.dart'; import 'package:cake_wallet/src/screens/send/widgets/confirm_sending_alert.dart'; import 'package:cake_wallet/src/widgets/standart_list_row.dart'; @@ -172,36 +170,14 @@ class ExchangeTradeState extends State { ), itemBuilder: (context, index) { final item = widget.exchangeTradeViewModel.items[index]; - String value; + final value = item.data ?? fetchingLabel; - final content = Observer(builder: (_) { - switch (index) { - case 0: - value = - '${widget.exchangeTradeViewModel.trade.id ?? fetchingLabel}'; - break; - case 1: - value = - '${widget.exchangeTradeViewModel.trade.amount ?? fetchingLabel}'; - break; - case 2: - value = - '${widget.exchangeTradeViewModel.trade.state ?? fetchingLabel}'; - break; - case 3: - value = widget.exchangeTradeViewModel.trade - .inputAddress ?? - fetchingLabel; - break; - } - - return StandartListRow( - title: item.title, - value: value, - valueFontSize: 14, - image: item.isCopied ? copyImage : null, - ); - }); + final content = StandartListRow( + title: item.title, + value: value, + valueFontSize: 14, + image: item.isCopied ? copyImage : null, + ); return item.isCopied ? Builder( diff --git a/lib/src/screens/trade_details/trade_details_page.dart b/lib/src/screens/trade_details/trade_details_page.dart index 30b29d115..f108e85bd 100644 --- a/lib/src/screens/trade_details/trade_details_page.dart +++ b/lib/src/screens/trade_details/trade_details_page.dart @@ -1,51 +1,20 @@ import 'package:cake_wallet/utils/show_bar.dart'; +import 'package:cake_wallet/view_model/trade_details_view_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; -import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/generated/i18n.dart'; -import 'package:cake_wallet/utils/date_formatter.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; -import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart'; import 'package:cake_wallet/src/widgets/standart_list_row.dart'; class TradeDetailsPage extends BasePage { - TradeDetailsPage(this.trade) : _items = [] { - final dateFormat = DateFormatter.withCurrentLocal(); - _items.addAll([ - StandartListItem(title: S.current.trade_details_id, value: trade.id), - StandartListItem( - title: S.current.trade_details_state, - value: trade.state != null - ? trade.state.toString() - : S.current.trade_details_fetching) - ]); - - if (trade.provider != null) { - _items.add(StandartListItem( - title: S.current.trade_details_provider, - value: trade.provider.toString())); - } - - if (trade.createdAt != null) { - _items.add(StandartListItem( - title: S.current.trade_details_created_at, - value: dateFormat.format(trade.createdAt).toString())); - } - - if (trade.from != null && trade.to != null) { - _items.add(StandartListItem( - title: S.current.trade_details_pair, - value: '${trade.from.toString()} → ${trade.to.toString()}')); - } - } + TradeDetailsPage(this.tradeDetailsViewModel); @override String get title => S.current.trade_details_title; - final Trade trade; - final List _items; + final TradeDetailsViewModel tradeDetailsViewModel; @override Widget body(BuildContext context) { @@ -61,10 +30,11 @@ class TradeDetailsPage extends BasePage { .primaryTextTheme .title .backgroundColor)), - itemCount: _items.length, + itemCount: tradeDetailsViewModel.items.length, itemBuilder: (BuildContext context, int index) { - final item = _items[index]; - final isDrawBottom = index == _items.length - 1 ? true : false; + final item = tradeDetailsViewModel.items[index]; + final isDrawBottom = + index == tradeDetailsViewModel.items.length - 1 ? true : false; return GestureDetector( onTap: () { diff --git a/lib/view_model/exchange/exchange_trade_view_model.dart b/lib/view_model/exchange/exchange_trade_view_model.dart index dedb2c559..100445bc0 100644 --- a/lib/view_model/exchange/exchange_trade_view_model.dart +++ b/lib/view_model/exchange/exchange_trade_view_model.dart @@ -39,20 +39,11 @@ abstract class ExchangeTradeViewModelBase with Store { } items = ObservableList(); - items.addAll([ - 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), - ]); + + _updateItems(); _updateTrade(); + _timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade()); } @@ -95,8 +86,27 @@ abstract class ExchangeTradeViewModelBase with Store { } trade = updatedTrade; + + _updateItems(); } catch (e) { print(e.toString()); } } + + void _updateItems() { + items?.clear(); + + items.addAll([ + 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), + ]); + } } diff --git a/lib/view_model/trade_details_view_model.dart b/lib/view_model/trade_details_view_model.dart new file mode 100644 index 000000000..63cb5f323 --- /dev/null +++ b/lib/view_model/trade_details_view_model.dart @@ -0,0 +1,105 @@ +import 'dart:async'; +import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart'; +import 'package:cake_wallet/exchange/exchange_provider.dart'; +import 'package:cake_wallet/exchange/exchange_provider_description.dart'; +import 'package:cake_wallet/exchange/morphtoken/morphtoken_exchange_provider.dart'; +import 'package:cake_wallet/exchange/trade.dart'; +import 'package:cake_wallet/exchange/xmrto/xmrto_exchange_provider.dart'; +import 'package:cake_wallet/utils/date_formatter.dart'; +import 'package:hive/hive.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'; + +part 'trade_details_view_model.g.dart'; + +class TradeDetailsViewModel = TradeDetailsViewModelBase + with _$TradeDetailsViewModel; + +abstract class TradeDetailsViewModelBase with Store { + TradeDetailsViewModelBase({Trade tradeForDetails, this.trades}) { + trade = tradeForDetails; + + switch (trade.provider) { + case ExchangeProviderDescription.xmrto: + _provider = XMRTOExchangeProvider(); + break; + case ExchangeProviderDescription.changeNow: + _provider = ChangeNowExchangeProvider(); + break; + case ExchangeProviderDescription.morphToken: + _provider = MorphTokenExchangeProvider(trades: trades); + break; + } + + items = ObservableList(); + + _updateItems(); + + _updateTrade(); + + _timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade()); + } + + final Box trades; + + @observable + Trade trade; + + @observable + ObservableList items; + + ExchangeProvider _provider; + + Timer _timer; + + @action + Future _updateTrade() async { + try { + final updatedTrade = await _provider.findTradeById(id: trade.id); + + if (updatedTrade.createdAt == null && trade.createdAt != null) { + updatedTrade.createdAt = trade.createdAt; + } + + trade = updatedTrade; + + _updateItems(); + } catch (e) { + print(e.toString()); + } + } + + void _updateItems() { + final dateFormat = DateFormatter.withCurrentLocal(); + + items?.clear(); + + items.addAll([ + StandartListItem(title: S.current.trade_details_id, value: trade.id), + StandartListItem( + title: S.current.trade_details_state, + value: trade.state != null + ? trade.state.toString() + : S.current.trade_details_fetching) + ]); + + if (trade.provider != null) { + items.add(StandartListItem( + title: S.current.trade_details_provider, + value: trade.provider.toString())); + } + + if (trade.createdAt != null) { + items.add(StandartListItem( + title: S.current.trade_details_created_at, + value: dateFormat.format(trade.createdAt).toString())); + } + + if (trade.from != null && trade.to != null) { + items.add(StandartListItem( + title: S.current.trade_details_pair, + value: '${trade.from.toString()} → ${trade.to.toString()}')); + } + } +} \ No newline at end of file