dev
dart 181 lines 4.75 KB
Raw
1 import 'dart:async';
2
3 import 'package:cake_wallet/entities/bridge_transfer.dart';
4 import 'package:cake_wallet/evm/evm.dart';
5 import 'package:cake_wallet/generated/i18n.dart';
6 import 'package:cake_wallet/src/screens/transaction_details/address_list_item.dart';
7 import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
8 import 'package:cake_wallet/src/screens/trade_details/trade_details_status_item.dart';
9 import 'package:cake_wallet/src/screens/trade_details/track_trade_list_item.dart';
10 import 'package:cake_wallet/src/screens/transaction_details/transaction_details_list_item.dart';
11 import 'package:cake_wallet/store/bridge_transfers_store.dart';
12 import 'package:mobx/mobx.dart';
13 import 'package:url_launcher/url_launcher.dart';
14
15 part 'bridge_details_view_model.g.dart';
16
17 class BridgeDetailsViewModel = BridgeDetailsViewModelBase with _$BridgeDetailsViewModel;
18
19 abstract class BridgeDetailsViewModelBase with Store {
20 BridgeDetailsViewModelBase({
21 required BridgeTransfer transferForDetails,
22 required this.bridgeTransfersStore,
23 required this.walletId,
24 }) : items = ObservableList<TransactionDetailsListItem>(),
25 transfer = _findTransferInStore(
26 bridgeTransfersStore.bridgeTransfers, transferForDetails.id, walletId) ??
27 transferForDetails {
28 _updateItems();
29 _setupReaction();
30 }
31
32 static BridgeTransfer? _findTransferInStore(
33 List<BridgeTransfer> transfers,
34 String transferId,
35 String walletId,
36 ) {
37 try {
38 return transfers.firstWhere(
39 (t) => t.id == transferId && t.walletId == walletId,
40 );
41 } catch (_) {
42 return null;
43 }
44 }
45
46 final BridgeTransfersStore bridgeTransfersStore;
47 final String walletId;
48 ReactionDisposer? _reactionDisposer;
49
50 @observable
51 BridgeTransfer transfer;
52
53 @observable
54 ObservableList<TransactionDetailsListItem> items;
55
56 Timer? timer;
57
58 void _setupReaction() {
59 _reactionDisposer = reaction(
60 (_) => bridgeTransfersStore.bridgeTransfers,
61 (_) => updateTransfer(),
62 );
63 updateTransfer();
64 }
65
66 @action
67 void updateTransfer() {
68 final updatedTransfer = _findTransferInStore(
69 bridgeTransfersStore.bridgeTransfers,
70 transfer.id,
71 walletId,
72 );
73 if (updatedTransfer != null) {
74 transfer = updatedTransfer;
75 _updateItems();
76 }
77 }
78
79 void dispose() {
80 _reactionDisposer?.call();
81 timer?.cancel();
82 }
83
84 void _updateItems() {
85 items.clear();
86
87 final statusText = transfer.statusMessage?.isNotEmpty == true
88 ? '${_statusLabel(transfer.status)} · ${transfer.statusMessage}'
89 : _statusLabel(transfer.status);
90
91 items.add(
92 DetailsListStatusItem(
93 title: "Status",
94 value: statusText,
95 status: transfer.status,
96 ),
97 );
98
99 final sourceName =
100 evm?.getChainInfoByChainId(transfer.sourceChainId)?.name ?? '${transfer.sourceChainId}';
101 final destName = evm?.getChainInfoByChainId(transfer.destinationChainId)?.name ??
102 '${transfer.destinationChainId}';
103
104 items.add(
105 StandartListItem(
106 title: "Source chain",
107 value: sourceName,
108 ),
109 );
110
111 items.add(
112 StandartListItem(
113 title: "Destination chain",
114 value: destName,
115 ),
116 );
117
118 items.add(
119 StandartListItem(
120 title: "Amount",
121 value: '${transfer.amount} ${transfer.tokenSymbol}',
122 ),
123 );
124
125 items.add(
126 AddressListItem(
127 title: "Recipient",
128 value: transfer.recipientAddress,
129 ),
130 );
131
132 final sourceExplorerUrl = evm?.getExplorerUrlForChainId(transfer.sourceChainId);
133 final sourceTxUrl = sourceExplorerUrl != null && sourceExplorerUrl.isNotEmpty
134 ? '$sourceExplorerUrl/tx/${transfer.sourceTxHash}'
135 : null;
136
137 if (sourceTxUrl != null) {
138 final explorerDescription = S.current.view_transaction_on + Uri.parse(sourceTxUrl).host;
139 items.add(
140 TrackTradeListItem(
141 title: explorerDescription,
142 value: sourceTxUrl,
143 onTap: () => _launchUrl(sourceTxUrl),
144 ),
145 );
146 }
147
148 if (transfer.errorMessage != null && transfer.errorMessage!.isNotEmpty) {
149 items.add(
150 StandartListItem(
151 title: "Error",
152 value: transfer.errorMessage!,
153 ),
154 );
155 }
156 }
157
158 String _statusLabel(String status) {
159 switch (status) {
160 case 'submitted':
161 return "Submitted";
162 case 'confirming':
163 return "Confirming on source";
164 case 'initiated':
165 return "Bridge initiated";
166 case 'completed':
167 return "Completed";
168 case 'failed':
169 return "Failed";
170 default:
171 return status;
172 }
173 }
174
175 void _launchUrl(String url) {
176 final uri = Uri.parse(url);
177 try {
178 launchUrl(uri, mode: LaunchMode.externalApplication);
179 } catch (_) {}
180 }
181 }