| 1 | import 'dart:async'; |
| 2 | |
| 3 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 4 | import 'package:cake_wallet/generated/i18n.dart'; |
| 5 | import 'package:cake_wallet/src/screens/trade_details/trade_details_list_card.dart'; |
| 6 | import 'package:cake_wallet/src/screens/trade_details/trade_details_status_item.dart'; |
| 7 | import 'package:cake_wallet/src/screens/transaction_details/blockexplorer_list_item.dart'; |
| 8 | import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart'; |
| 9 | import 'package:cake_wallet/src/screens/transaction_details/transaction_details_list_item.dart'; |
| 10 | import 'package:cake_wallet/themes/core/theme_store.dart'; |
| 11 | import 'package:cake_wallet/utils/date_formatter.dart'; |
| 12 | import 'package:cw_core/payjoin_session.dart'; |
| 13 | import 'package:cw_core/transaction_info.dart'; |
| 14 | import 'package:flutter/widgets.dart'; |
| 15 | import 'package:hive_flutter/hive_flutter.dart'; |
| 16 | import 'package:mobx/mobx.dart'; |
| 17 | import 'package:url_launcher/url_launcher.dart'; |
| 18 | |
| 19 | part 'payjoin_details_view_model.g.dart'; |
| 20 | |
| 21 | class PayjoinDetailsViewModel = PayjoinDetailsViewModelBase with _$PayjoinDetailsViewModel; |
| 22 | |
| 23 | abstract class PayjoinDetailsViewModelBase with Store { |
| 24 | PayjoinDetailsViewModelBase( |
| 25 | this.payjoinSessionId, |
| 26 | this.transactionInfo, { |
| 27 | required this.payjoinSessionSource, |
| 28 | required this.themeStore, |
| 29 | }) : items = ObservableList<TransactionDetailsListItem>(), |
| 30 | payjoinSession = payjoinSessionSource.get(payjoinSessionId)! { |
| 31 | listener = payjoinSessionSource.watch().listen((e) { |
| 32 | if (e.key == payjoinSessionId) _updateItems(); |
| 33 | }); |
| 34 | _updateItems(); |
| 35 | } |
| 36 | |
| 37 | final Box<PayjoinSession> payjoinSessionSource; |
| 38 | final ThemeStore themeStore; |
| 39 | final String payjoinSessionId; |
| 40 | final TransactionInfo? transactionInfo; |
| 41 | |
| 42 | @observable |
| 43 | late PayjoinSession payjoinSession; |
| 44 | |
| 45 | final ObservableList<TransactionDetailsListItem> items; |
| 46 | |
| 47 | late final StreamSubscription<BoxEvent> listener; |
| 48 | |
| 49 | Timer? timer; |
| 50 | |
| 51 | @action |
| 52 | void _updateItems() { |
| 53 | final dateFormat = DateFormatter.withCurrentLocal(); |
| 54 | items.clear(); |
| 55 | items.addAll([ |
| 56 | DetailsListStatusItem( |
| 57 | title: S.current.status, |
| 58 | value: _getStatusString(), |
| 59 | status: payjoinSession.status, |
| 60 | ), |
| 61 | TradeDetailsListCardItem( |
| 62 | id: "${payjoinSession.isSenderSession ? S.current.outgoing : S.current.incoming} Payjoin", |
| 63 | createdAt: dateFormat.format(payjoinSession.inProgressSince!).toString(), |
| 64 | pair: |
| 65 | '${bitcoin!.formatterBitcoinAmountToString(amount: payjoinSession.amount.toInt())} BTC', |
| 66 | onTap: (_) {}, |
| 67 | ), |
| 68 | if (payjoinSession.error?.isNotEmpty == true) |
| 69 | StandartListItem( |
| 70 | title: S.current.error, |
| 71 | value: payjoinSession.error!, |
| 72 | ), |
| 73 | if (payjoinSession.txId?.isNotEmpty == true && transactionInfo != null) ...[ |
| 74 | StandartListItem( |
| 75 | title: S.current.transaction_details_transaction_id, |
| 76 | value: payjoinSession.txId!, |
| 77 | key: ValueKey('standard_list_item_transaction_details_id_key'), |
| 78 | ), |
| 79 | BlockExplorerListItem( |
| 80 | title: S.current.view_in_block_explorer, |
| 81 | value: '${S.current.view_transaction_on}mempool.space', |
| 82 | onTap: () async { |
| 83 | try { |
| 84 | final uri = Uri.parse('https://mempool.cakewallet.com/tx/${payjoinSession.txId!}'); |
| 85 | if (await canLaunchUrl(uri)) |
| 86 | await launchUrl(uri, mode: LaunchMode.externalApplication); |
| 87 | } catch (e) {} |
| 88 | }, |
| 89 | key: ValueKey('block_explorer_list_item_wallet_type_key'), |
| 90 | ) |
| 91 | ] |
| 92 | ]); |
| 93 | |
| 94 | if (transactionInfo != null) { |
| 95 | items.addAll([ |
| 96 | StandartListItem( |
| 97 | title: S.current.transaction_details_date, |
| 98 | value: dateFormat.format(transactionInfo!.date), |
| 99 | key: ValueKey('standard_list_item_transaction_details_date_key'), |
| 100 | ), |
| 101 | StandartListItem( |
| 102 | title: S.current.confirmations, |
| 103 | value: transactionInfo!.confirmations.toString(), |
| 104 | key: ValueKey('standard_list_item_transaction_confirmations_key'), |
| 105 | ), |
| 106 | StandartListItem( |
| 107 | title: S.current.transaction_details_height, |
| 108 | value: '${transactionInfo!.height}', |
| 109 | key: ValueKey('standard_list_item_transaction_details_height_key'), |
| 110 | ), |
| 111 | if (transactionInfo!.fee != null) |
| 112 | StandartListItem( |
| 113 | title: S.current.transaction_details_fee, |
| 114 | value: transactionInfo!.fee!.toStringWithSymbol(), |
| 115 | key: ValueKey('standard_list_item_transaction_details_fee_key'), |
| 116 | ), |
| 117 | ]); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | String _getStatusString() { |
| 122 | switch (payjoinSession.status) { |
| 123 | case 'success': |
| 124 | if (transactionInfo?.isPending == false) return S.current.successful; |
| 125 | return S.current.payjoin_request_awaiting_tx; |
| 126 | case 'inProgress': |
| 127 | return S.current.payjoin_request_in_progress; |
| 128 | case 'unrecoverable': |
| 129 | return S.current.error; |
| 130 | default: |
| 131 | return payjoinSession.status; |
| 132 | } |
| 133 | } |
| 134 | } |