| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/src/screens/transaction_details/blockexplorer_list_item.dart'; |
| 3 | import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart'; |
| 4 | import 'package:cake_wallet/src/screens/transaction_details/textfield_list_item.dart'; |
| 5 | import 'package:cake_wallet/src/screens/transaction_details/transaction_details_list_item.dart'; |
| 6 | import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_item.dart'; |
| 7 | import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_model.dart'; |
| 8 | import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_switch_item.dart'; |
| 9 | import 'package:cw_core/wallet_type.dart'; |
| 10 | import 'package:mobx/mobx.dart'; |
| 11 | import 'package:url_launcher/url_launcher.dart'; |
| 12 | |
| 13 | part 'unspent_coins_details_view_model.g.dart'; |
| 14 | |
| 15 | class UnspentCoinsDetailsViewModel = UnspentCoinsDetailsViewModelBase |
| 16 | with _$UnspentCoinsDetailsViewModel; |
| 17 | |
| 18 | abstract class UnspentCoinsDetailsViewModelBase with Store { |
| 19 | UnspentCoinsDetailsViewModelBase( |
| 20 | {required this.unspentCoinsItem, required this.unspentCoinsListViewModel}) |
| 21 | : items = <TransactionDetailsListItem>[], |
| 22 | _type = unspentCoinsListViewModel.wallet.type, |
| 23 | isFrozen = unspentCoinsItem.isFrozen, |
| 24 | note = unspentCoinsItem.note { |
| 25 | items = [ |
| 26 | StandartListItem(title: S.current.transaction_details_amount, value: unspentCoinsItem.amount), |
| 27 | StandartListItem( |
| 28 | title: S.current.transaction_details_transaction_id, value: unspentCoinsItem.hash), |
| 29 | StandartListItem(title: S.current.widgets_address, value: formattedAddress), |
| 30 | TextFieldListItem( |
| 31 | title: S.current.note_tap_to_change, |
| 32 | value: note, |
| 33 | onSubmitted: (value) { |
| 34 | unspentCoinsItem.note = value; |
| 35 | unspentCoinsListViewModel.saveUnspentCoinInfo(unspentCoinsItem); |
| 36 | }), |
| 37 | UnspentCoinsSwitchItem( |
| 38 | title: S.current.freeze, |
| 39 | value: '', |
| 40 | switchValue: () => isFrozen, |
| 41 | onSwitchValueChange: (value) async { |
| 42 | isFrozen = value; |
| 43 | unspentCoinsItem.isFrozen = value; |
| 44 | if (value) unspentCoinsItem.isSending = !value; |
| 45 | await unspentCoinsListViewModel.saveUnspentCoinInfo(unspentCoinsItem); |
| 46 | }) |
| 47 | ]; |
| 48 | |
| 49 | if ([WalletType.bitcoin, WalletType.litecoin, WalletType.bitcoinCash, WalletType.dogecoin] |
| 50 | .contains(_type)) { |
| 51 | items.add(BlockExplorerListItem( |
| 52 | title: S.current.view_in_block_explorer, |
| 53 | value: _explorerDescription(_type), |
| 54 | onTap: () { |
| 55 | try { |
| 56 | final url = Uri.parse(_explorerUrl(_type, unspentCoinsItem.hash)); |
| 57 | return launchUrl(url, mode: LaunchMode.externalApplication); |
| 58 | } catch (e) {} |
| 59 | }, |
| 60 | )); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | String _explorerUrl(WalletType type, String txId) { |
| 65 | switch (type) { |
| 66 | case WalletType.bitcoin: |
| 67 | return 'https://ordinals.com/tx/${txId}'; |
| 68 | case WalletType.litecoin: |
| 69 | return 'https://litecoin.earlyordies.com/tx/${txId}'; |
| 70 | case WalletType.bitcoinCash: |
| 71 | return 'https://blockchair.com/bitcoin-cash/transaction/${txId}'; |
| 72 | case WalletType.dogecoin: |
| 73 | return 'https://dogechain.info/tx/${txId}'; |
| 74 | default: |
| 75 | return ''; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | String _explorerDescription(WalletType type) { |
| 80 | switch (type) { |
| 81 | case WalletType.bitcoin: |
| 82 | return '${S.current.view_transaction_on}Ordinals.com'; |
| 83 | case WalletType.litecoin: |
| 84 | return '${S.current.view_transaction_on}Earlyordies.com'; |
| 85 | case WalletType.bitcoinCash: |
| 86 | return '${S.current.view_transaction_on}Blockchair.com'; |
| 87 | case WalletType.dogecoin: |
| 88 | return '${S.current.view_transaction_on}Dogechain.info'; |
| 89 | default: |
| 90 | return ''; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @observable |
| 95 | bool isFrozen; |
| 96 | |
| 97 | @observable |
| 98 | String note; |
| 99 | |
| 100 | final UnspentCoinsItem unspentCoinsItem; |
| 101 | final UnspentCoinsListViewModel unspentCoinsListViewModel; |
| 102 | final WalletType _type; |
| 103 | List<TransactionDetailsListItem> items; |
| 104 | |
| 105 | String get formattedAddress => unspentCoinsItem.address; |
| 106 | } |