localStorage.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. class LocalStorage {
  6. final accountsKey = 'wallet_account_list';
  7. final currentAccountKey = 'wallet_current_account';
  8. final contactsKey = 'wallet_contact_list';
  9. final seedKey = 'wallet_seed';
  10. final customKVKey = 'wallet_kv';
  11. final storage = _LocalStorage();
  12. Future<void> addAccount(Map<String, dynamic> acc) async {
  13. return storage.addItemToList(accountsKey, acc);
  14. }
  15. Future<void> removeAccount(String? pubKey) async {
  16. return storage.removeItemFromList(accountsKey, 'pubKey', pubKey);
  17. }
  18. Future<List<Map<String, dynamic>>> getAccountList() async {
  19. return storage.getList(accountsKey);
  20. }
  21. Future<bool> setCurrentAccount(String pubKey) async {
  22. return storage.setKV(currentAccountKey, pubKey);
  23. }
  24. Future<String?> getCurrentAccount() async {
  25. return storage.getKV(currentAccountKey);
  26. }
  27. Future<void> addContact(Map<String, dynamic> con) async {
  28. return storage.addItemToList(contactsKey, con);
  29. }
  30. Future<void> removeContact(String address) async {
  31. return storage.removeItemFromList(contactsKey, 'address', address);
  32. }
  33. Future<void> updateContact(Map<String, dynamic> con) async {
  34. return storage.updateItemInList(
  35. contactsKey, 'address', con['address'], con);
  36. }
  37. Future<List<Map<String, dynamic>>> getContactList() async {
  38. return storage.getList(contactsKey);
  39. }
  40. Future<bool> setSeeds(String seedType, Map value) async {
  41. return storage.setKV('${seedKey}_$seedType', jsonEncode(value));
  42. }
  43. Future<Map> getSeeds(String? seedType) async {
  44. String? value = await storage.getKV('${seedKey}_$seedType');
  45. if (value != null) {
  46. return jsonDecode(value);
  47. }
  48. return {};
  49. }
  50. Future<bool> setObject(String key, Object value) async {
  51. String str = await compute(jsonEncode, value);
  52. return storage.setKV('${customKVKey}_$key', str);
  53. }
  54. Future<Object?> getObject(String key) async {
  55. String? value = await storage.getKV('${customKVKey}_$key');
  56. if (value != null) {
  57. Object data = await compute(
  58. jsonDecode as FutureOr<Object> Function(dynamic), value);
  59. return data;
  60. }
  61. return null;
  62. }
  63. Future<void> setAccountCache(
  64. String accPubKey, String key, Object value) async {
  65. Map? data = await (getObject(key) as FutureOr<Map<dynamic, dynamic>?>);
  66. if (data == null) {
  67. data = {};
  68. }
  69. data[accPubKey] = value;
  70. setObject(key, data);
  71. }
  72. Future<Object?> getAccountCache(String accPubKey, String key) async {
  73. Map? data = await (getObject(key) as FutureOr<Map<dynamic, dynamic>?>);
  74. if (data == null) {
  75. return null;
  76. }
  77. return data[accPubKey];
  78. }
  79. // cache timeout 10 minutes
  80. static const int customCacheTimeLength = 10 * 60 * 1000;
  81. static bool checkCacheTimeout(int cacheTime) {
  82. return DateTime.now().millisecondsSinceEpoch - customCacheTimeLength >
  83. cacheTime;
  84. }
  85. }
  86. class _LocalStorage {
  87. Future<String?> getKV(String key) async {
  88. final prefs = await SharedPreferences.getInstance();
  89. return prefs.getString(key);
  90. }
  91. Future<bool> setKV(String key, value) async {
  92. final prefs = await SharedPreferences.getInstance();
  93. return prefs.setString(key, value);
  94. }
  95. Future<void> addItemToList(String storeKey, Map<String, dynamic> acc) async {
  96. List<Map<String, dynamic>> ls = [];
  97. String? str = await getKV(storeKey);
  98. if (str != null) {
  99. Iterable l = jsonDecode(str);
  100. ls = l.map((i) => Map<String, dynamic>.from(i)).toList();
  101. }
  102. ls.add(acc);
  103. setKV(storeKey, jsonEncode(ls));
  104. }
  105. Future<void> removeItemFromList(
  106. String storeKey, String itemKey, String? itemValue) async {
  107. var ls = await getList(storeKey);
  108. ls.removeWhere((item) => item[itemKey] == itemValue);
  109. setKV(storeKey, jsonEncode(ls));
  110. }
  111. Future<void> updateItemInList(String storeKey, String itemKey,
  112. String? itemValue, Map<String, dynamic> itemNew) async {
  113. var ls = await getList(storeKey);
  114. ls.removeWhere((item) => item[itemKey] == itemValue);
  115. ls.add(itemNew);
  116. setKV(storeKey, jsonEncode(ls));
  117. }
  118. Future<List<Map<String, dynamic>>> getList(String storeKey) async {
  119. List<Map<String, dynamic>> res = [];
  120. String? str = await getKV(storeKey);
  121. if (str != null) {
  122. Iterable l = jsonDecode(str);
  123. res = l.map((i) => Map<String, dynamic>.from(i)).toList();
  124. }
  125. return res;
  126. }
  127. }