dev
dart 73 lines 1.99 KB
Raw
1 import 'package:cw_core/transaction_priority.dart';
2 import 'package:flutter/foundation.dart';
3
4 class DecredTransactionPriority extends TransactionPriority {
5 const DecredTransactionPriority({required String title, required int raw})
6 : super(title: title, raw: raw);
7
8 static const List<DecredTransactionPriority> all = [fast, medium, slow];
9 static const DecredTransactionPriority slow = DecredTransactionPriority(title: 'Slow', raw: 0);
10 static const DecredTransactionPriority medium =
11 DecredTransactionPriority(title: 'Medium', raw: 1);
12 static const DecredTransactionPriority fast = DecredTransactionPriority(title: 'Fast', raw: 2);
13
14 static DecredTransactionPriority deserialize({required int raw}) {
15 switch (raw) {
16 case 0:
17 return slow;
18 case 1:
19 return medium;
20 case 2:
21 return fast;
22 default:
23 if (kDebugMode) {
24 throw Exception('Unexpected token: $raw for DecredTransactionPriority deserialize');
25 }
26 return medium;
27 }
28 }
29
30 String get units => 'atom';
31
32 @override
33 String toString() {
34 var label = '';
35
36 switch (this) {
37 case DecredTransactionPriority.slow:
38 label = 'Slow ~24hrs'; // '${S.current.transaction_priority_slow} ~24hrs';
39 break;
40 case DecredTransactionPriority.medium:
41 label = 'Medium'; // S.current.transaction_priority_medium;
42 break;
43 case DecredTransactionPriority.fast:
44 label = 'Fast'; // S.current.transaction_priority_fast;
45 break;
46 default:
47 break;
48 }
49
50 return label;
51 }
52
53 String labelWithRate(int rate) => '${toString()} ($rate ${units}/byte)';
54 }
55
56 class FeeCache {
57 int _feeRate;
58 DateTime stamp;
59 FeeCache(this._feeRate) : this.stamp = DateTime(0, 0, 0, 0, 0, 0, 0, 0);
60
61 bool isOld() {
62 return this.stamp.add(const Duration(minutes: 30)).isBefore(DateTime.now());
63 }
64
65 void update(int feeRate) {
66 this._feeRate = feeRate;
67 this.stamp = DateTime.now();
68 }
69
70 int feeRate() {
71 return this._feeRate;
72 }
73 }