dev
dart 69 lines 1.72 KB
Raw
1 import 'package:cw_core/cake_hive.dart';
2 import 'package:cw_core/nano_account.dart';
3 import 'package:mobx/mobx.dart';
4 import 'package:hive/hive.dart';
5
6 part 'nano_account_list.g.dart';
7
8 class NanoAccountList = NanoAccountListBase with _$NanoAccountList;
9
10 abstract class NanoAccountListBase with Store {
11 NanoAccountListBase(this.address)
12 : accounts = ObservableList<NanoAccount>(),
13 _isRefreshing = false,
14 _isUpdating = false {
15 refresh();
16 }
17
18 @observable
19 ObservableList<NanoAccount> accounts;
20 bool _isRefreshing;
21 bool _isUpdating;
22
23 String address;
24
25 Future<void> update(String? address) async {
26 if (_isUpdating) {
27 return;
28 }
29
30 try {
31 _isUpdating = true;
32
33 final accounts = await getAll(address: address ?? this.address);
34
35 if (accounts.isNotEmpty) {
36 this.accounts.clear();
37 this.accounts.addAll(accounts);
38 }
39
40 _isUpdating = false;
41 } catch (e) {
42 _isUpdating = false;
43 rethrow;
44 }
45 }
46
47 Future<List<NanoAccount>> getAll({String? address}) async {
48 final box = await CakeHive.openBox<NanoAccount>(address ?? this.address);
49
50 // get all accounts in box:
51 return box.values.toList();
52 }
53
54 Future<void> addAccount({required String label}) async {
55 final box = await CakeHive.openBox<NanoAccount>(address);
56 final account = NanoAccount(id: box.length, label: label, balance: "0.00", isSelected: false);
57 await box.add(account);
58 await account.save();
59 }
60
61 Future<void> setLabelAccount({required int accountIndex, required String label}) async {
62 final box = await CakeHive.openBox<NanoAccount>(address);
63 final account = box.getAt(accountIndex);
64 account!.label = label;
65 await account.save();
66 }
67
68 void refresh() {}
69 }