| 1 | import 'package:cake_wallet/buy/buy_provider_description.dart'; |
| 2 | import 'package:cake_wallet/exchange/trade_state.dart'; |
| 3 | import 'package:cw_core/format_amount.dart'; |
| 4 | import 'package:cw_core/hive_type_ids.dart'; |
| 5 | import 'package:hive/hive.dart'; |
| 6 | |
| 7 | import 'order_provider_description.dart'; |
| 8 | import 'order_source_description.dart'; |
| 9 | |
| 10 | part 'order.part.dart'; |
| 11 | |
| 12 | // @HiveType(typeId: Order.typeId) |
| 13 | class Order extends HiveObject { |
| 14 | Order({ |
| 15 | required this.id, |
| 16 | required this.transferId, |
| 17 | required this.createdAt, |
| 18 | required this.amount, |
| 19 | this.receiveAmount, |
| 20 | this.quantity, |
| 21 | required this.receiveAddress, |
| 22 | required this.walletId, |
| 23 | this.from, |
| 24 | this.to, |
| 25 | TradeState? state, |
| 26 | OrderSourceDescription source = OrderSourceDescription.buy, |
| 27 | BuyProviderDescription? buyProvider, |
| 28 | OrderProviderDescription? giftCardProvider, |
| 29 | }) { |
| 30 | sourceRaw = source.raw; |
| 31 | if (state != null) stateRaw = state.raw; |
| 32 | if (buyProvider != null) providerRaw = buyProvider.raw; |
| 33 | if (giftCardProvider != null) providerRaw = giftCardProvider.raw; |
| 34 | } |
| 35 | |
| 36 | static const typeId = ORDER_TYPE_ID; |
| 37 | static const boxName = 'Orders'; |
| 38 | static const boxKey = 'ordersBoxKey'; |
| 39 | |
| 40 | // @HiveField(0, defaultValue: '') |
| 41 | String id; |
| 42 | |
| 43 | // @HiveField(1, defaultValue: '') |
| 44 | String transferId; |
| 45 | |
| 46 | // @HiveField(2) |
| 47 | String? from; |
| 48 | |
| 49 | // @HiveField(3) |
| 50 | String? to; |
| 51 | |
| 52 | // @HiveField(4, defaultValue: '') |
| 53 | late String stateRaw; |
| 54 | |
| 55 | TradeState get state => TradeState.deserialize(raw: stateRaw); |
| 56 | |
| 57 | // @HiveField(5) |
| 58 | DateTime createdAt; |
| 59 | |
| 60 | // @HiveField(6, defaultValue: '') |
| 61 | String amount; |
| 62 | |
| 63 | // @HiveField(7, defaultValue: '') |
| 64 | String receiveAddress; |
| 65 | |
| 66 | // @HiveField(8, defaultValue: '') |
| 67 | String walletId; |
| 68 | |
| 69 | // @HiveField(9, defaultValue: 0) |
| 70 | late int providerRaw; |
| 71 | |
| 72 | // @HiveField(10, defaultValue: '') |
| 73 | String? receiveAmount; |
| 74 | |
| 75 | // @HiveField(11, defaultValue: 0) |
| 76 | late int? sourceRaw; |
| 77 | |
| 78 | // @HiveField(12, defaultValue: '') |
| 79 | String? quantity; |
| 80 | |
| 81 | OrderSourceDescription get source => OrderSourceDescription.deserialize(raw: sourceRaw ?? 0); |
| 82 | |
| 83 | BuyProviderDescription? get buyProvider => source == OrderSourceDescription.buy |
| 84 | ? BuyProviderDescription.deserialize(raw: providerRaw) |
| 85 | : null; |
| 86 | |
| 87 | OrderProviderDescription? get orderProvider => source == OrderSourceDescription.order |
| 88 | ? OrderProviderDescription.deserialize(raw: providerRaw) |
| 89 | : null; |
| 90 | |
| 91 | String get providerTitle { |
| 92 | if (source == OrderSourceDescription.buy) { |
| 93 | return buyProvider?.title ?? ''; |
| 94 | } else { |
| 95 | return orderProvider?.title ?? ''; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | String get providerIcon { |
| 100 | if (source == OrderSourceDescription.buy) { |
| 101 | return buyProvider?.image ?? ''; |
| 102 | } else { |
| 103 | return orderProvider?.image ?? ''; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | String amountFormatted() => formatAmount(amount) + ' ${from ?? ''}'; |
| 108 | } |