store.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // ignore_for_file: constant_identifier_names
  2. import 'dart:convert';
  3. import 'package:fis_common/helpers/encrypt.dart';
  4. import 'package:fis_common/index.dart';
  5. import 'package:fis_common/logger/logger.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:get/get.dart';
  8. import 'package:vitalapp/store/modules/resident.dart';
  9. import 'package:vitalapp/store/modules/user.dart';
  10. import '../architecture/storage/text_storage.dart';
  11. import 'defines.dart';
  12. import 'modules/app.dart';
  13. class Store implements PersistentalbeState {
  14. /// 已注册模块名称
  15. static final Set<String> _registeredModuleNames = {};
  16. /// 文本存储文件名
  17. static const _PERSISTENCE_FILENAME = "states.dat";
  18. /// 文本存储文件名
  19. static const _PERSISTENCE_FILENAME_DEBUG = "states_debug.dat";
  20. /// 应用状态
  21. static AppState get app => _findModule<AppState>();
  22. /// 用户状态
  23. static UserState get user => _findModule<UserState>();
  24. /// 居民检测状态
  25. static ResidentState get resident => _findModule<ResidentState>();
  26. /// 全局单例
  27. static Store get ins => Get.find();
  28. /// 文本存储
  29. late final TextStorage _persistenceStorage;
  30. Store._() {
  31. const fileName =
  32. kDebugMode ? _PERSISTENCE_FILENAME_DEBUG : _PERSISTENCE_FILENAME;
  33. if (FPlatform.isWindows) {
  34. _persistenceStorage = TextStorageShellWeb(fileName);
  35. return;
  36. }
  37. if (FPlatform.isPureWeb) {
  38. _persistenceStorage = TextStoragePureWeb(fileName);
  39. return;
  40. } else {
  41. _persistenceStorage = TextStorage(fileName: fileName);
  42. }
  43. }
  44. /// 初始化单例
  45. static Future<Store> init() async {
  46. if (Get.isRegistered<Store>()) return ins;
  47. logger.i("Store init start...");
  48. // 创建实例
  49. final instance = Store._();
  50. Get.put(instance);
  51. await Future.delayed(const Duration(milliseconds: 500));
  52. // 注册状态模块
  53. _regModuleSync(AppState());
  54. _regModuleSync(UserState());
  55. _regModuleSync(ResidentState());
  56. // 加载持久化缓存
  57. await instance._loadPersistenceData();
  58. logger.i("Store init done!");
  59. return instance;
  60. }
  61. /// 持久化
  62. static Future<bool> persistent() => ins._persistent();
  63. @override
  64. Future<void> acceptPersistenceJson(Map<String, dynamic> map) async {
  65. for (var key in map.keys) {
  66. var value = map[key];
  67. if (value != null) {
  68. logger.i('start acceptPersistenceJson by name:$key');
  69. final module = _findModuleByName(key);
  70. await module.acceptPersistenceJson(map[key]);
  71. logger.i('end acceptPersistenceJson by name:$key');
  72. }
  73. }
  74. }
  75. @override
  76. Map<String, dynamic> toPersistenceJson() {
  77. Map<String, dynamic> map = {};
  78. for (final name in _registeredModuleNames) {
  79. final module = _findModuleByName(name);
  80. map[name] = module.toPersistenceJson();
  81. }
  82. return map;
  83. }
  84. Future<void> _loadPersistenceData() async {
  85. try {
  86. var text = await _persistenceStorage.read();
  87. if (text == null || text.isEmpty) return;
  88. if (!kDebugMode && text.length > 2 && !text.startsWith('{')) {
  89. try {
  90. text = FEncryptHelper.decrypAES(text);
  91. } catch (e) {
  92. return logger.e("States load persistence data: decrypt fail.", e);
  93. }
  94. }
  95. logger.i("Get store json:{$text}");
  96. final map = jsonDecode(text);
  97. await acceptPersistenceJson(map);
  98. } catch (e) {
  99. logger.e("States load persistence data error.", e);
  100. }
  101. }
  102. Future<bool> _persistent() async {
  103. try {
  104. final map = toPersistenceJson();
  105. var json = jsonEncode(map);
  106. if (!kDebugMode) {
  107. // 非debug模式需要加密
  108. json = FEncryptHelper.encrypAES(json);
  109. }
  110. final result = await _persistenceStorage.save(json);
  111. return result;
  112. } catch (e) {
  113. logger.e("States persistence error.", e);
  114. }
  115. return false;
  116. }
  117. static T _findModule<T extends StateModuleBase>() {
  118. final tag = T.toString();
  119. return _findModuleByName<T>(tag);
  120. }
  121. static T _findModuleByName<T extends StateModuleBase>(String name) {
  122. final module = Get.find<StateModuleBase>(tag: name) as T;
  123. return module;
  124. }
  125. static void _regModuleSync<T extends StateModuleBase>(T module) {
  126. final tag = T.toString();
  127. _registeredModuleNames.add(tag);
  128. Get.put<StateModuleBase>(module, tag: tag);
  129. }
  130. }