ix_sp.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'dart:convert';
  4. import '../../../config/translations/localization_service.dart';
  5. import '../../../utils/crypto.dart';
  6. import '../../../utils/log/logger.dart';
  7. import '../../constants/keys.dart';
  8. import '../models/disconnect_domain.dart';
  9. import '../models/launch/launch.dart';
  10. import '../models/launch/user.dart';
  11. class IXSP {
  12. // prevent making instance
  13. IXSP._();
  14. // get storage
  15. static late SharedPreferences _sharedPreferences;
  16. // STORING KEYS
  17. /// Key for storing the device ID
  18. static const String _deviceIdKey = 'iv_device_id';
  19. static const String _fcmTokenKey = 'fcm_token';
  20. static const String _currentLocalKey = 'current_local';
  21. static const String _lightThemeKey = 'is_theme_light';
  22. static const String _launchGame = 'is_launch_game';
  23. static const String _ignoreVersionKey = 'ignore_version';
  24. static const String _isNewInstallKey = 'is_new_install';
  25. static const String _userUuidKey = 'user_uuid';
  26. static const String _launchDataKey = 'launch_data';
  27. //记录网络报错的域名信息
  28. static const String _disconnectDomainsKey = 'disconnect_domains';
  29. //记录最后一次是否是地区禁用
  30. static const String _isRegionDisabledKey = 'is_region_disabled';
  31. //记录最后一次是否是用户禁用
  32. static const String _isUserDisabledKey = 'is_user_disabled';
  33. //记录最后一次是否是设备禁用
  34. static const String _isDeviceDisabledKey = 'is_device_disabled';
  35. //记录最后一次上传的性能日志boostSessionId
  36. static const String _lastMetricsLogKey = 'last_metrics_log';
  37. //记录最后一次上传的es日志boostSessionId
  38. static const String _lastESLogKey = 'last_es_log';
  39. //记录是否开启debug log
  40. static const String _enableDebugLogKey = 'enable_debug_log';
  41. //记录是否开启ping 0默认 1开启 2关闭
  42. static const String _enablePingModeKey = 'enable_ping_mode';
  43. /// init get storage services
  44. static Future<void> init() async {
  45. _sharedPreferences = await SharedPreferences.getInstance();
  46. }
  47. static setStorage(SharedPreferences sharedPreferences) {
  48. _sharedPreferences = sharedPreferences;
  49. }
  50. /// set theme current type as light theme
  51. static Future<void> setThemeIsLight(bool lightTheme) =>
  52. _sharedPreferences.setBool(_lightThemeKey, lightTheme);
  53. /// get if the current theme type is light
  54. static bool getThemeIsLight() =>
  55. _sharedPreferences.getBool(_lightThemeKey) ??
  56. true; // todo set the default theme (true for light, false for dark)
  57. /// save current locale
  58. static Future<void> setCurrentLanguage(String languageCode) =>
  59. _sharedPreferences.setString(_currentLocalKey, languageCode);
  60. /// get current locale
  61. static Locale getCurrentLocal() {
  62. String? langCode = _sharedPreferences.getString(_currentLocalKey);
  63. // default language is english
  64. if (langCode == null) {
  65. return LocalizationService.defaultLanguage;
  66. }
  67. return LocalizationService.supportedLanguages[langCode]!;
  68. }
  69. /// save generated fcm token
  70. static Future<void> setFcmToken(String token) =>
  71. _sharedPreferences.setString(_fcmTokenKey, token);
  72. /// get authorization token
  73. static String getFcmToken() =>
  74. _sharedPreferences.getString(_fcmTokenKey) ?? '';
  75. /// set launch game
  76. static Future<void> setLaunchGame(bool isLaunch) =>
  77. _sharedPreferences.setBool(_launchGame, isLaunch);
  78. /// get launch game
  79. static bool getLaunchGame() =>
  80. _sharedPreferences.getBool(_launchGame) ?? false;
  81. /// clear all data from shared pref
  82. static Future<void> clear() async => await _sharedPreferences.clear();
  83. /// Save unique ID to shared preferences
  84. static Future<void> setDeviceId(String deviceId) =>
  85. _sharedPreferences.setString(_deviceIdKey, deviceId);
  86. /// Get unique ID from shared preferences
  87. static String? getDeviceId() => _sharedPreferences.getString(_deviceIdKey);
  88. /// Save ignore version to shared preferences
  89. static Future<void> setIgnoreVersion(int version) =>
  90. _sharedPreferences.setInt(_ignoreVersionKey, version);
  91. /// Get ignore version from shared preferences
  92. static int getIgnoreVersion() =>
  93. _sharedPreferences.getInt(_ignoreVersionKey) ?? 0;
  94. /// Save is new install to shared preferences
  95. static Future<void> setIsNewInstall(bool isNewInstall) =>
  96. _sharedPreferences.setBool(_isNewInstallKey, isNewInstall);
  97. /// Get is new install from shared preferences
  98. static bool getIsNewInstall() =>
  99. _sharedPreferences.getBool(_isNewInstallKey) ?? true;
  100. /// Save user uuid to shared preferences
  101. static Future<void> setUserUuid(String userUuid) =>
  102. _sharedPreferences.setString(_userUuidKey, userUuid);
  103. /// Get user uuid from shared preferences
  104. static String? getUserUuid() => _sharedPreferences.getString(_userUuidKey);
  105. /// 保存 DisconnectDomain 列表,合并 domain+status 相同的数据
  106. static Future<void> addDisconnectDomain(DisconnectDomain newItem) async {
  107. try {
  108. final prefs = _sharedPreferences;
  109. // 读取已存在的数据
  110. final oldJson = prefs.getString(_disconnectDomainsKey);
  111. List<DisconnectDomain> allList = [];
  112. if (oldJson != null && oldJson.isNotEmpty) {
  113. final List<dynamic> oldList = jsonDecode(oldJson);
  114. allList = oldList.map((e) => DisconnectDomain.fromJson(e)).toList();
  115. }
  116. // 合并新旧数据
  117. allList.add(newItem);
  118. // 按 domain+status 分组合并
  119. final Map<String, DisconnectDomain> merged = {};
  120. for (var item in allList) {
  121. final key = '${item.domain}_${item.status}';
  122. if (!merged.containsKey(key)) {
  123. merged[key] = DisconnectDomain(
  124. domain: item.domain,
  125. status: item.status,
  126. msg: item.msg,
  127. startTime: item.startTime,
  128. endTime: item.endTime,
  129. count: item.count,
  130. );
  131. } else {
  132. final exist = merged[key]!;
  133. exist.startTime = exist.startTime < item.startTime
  134. ? exist.startTime
  135. : item.startTime;
  136. exist.endTime = exist.endTime > item.endTime
  137. ? exist.endTime
  138. : item.endTime;
  139. exist.count += item.count;
  140. // msg 以最新的为准
  141. exist.msg = item.msg;
  142. }
  143. }
  144. // 保存合并后的数据
  145. final mergedList = merged.values.toList();
  146. await prefs.setString(
  147. _disconnectDomainsKey,
  148. jsonEncode(mergedList.map((e) => e.toJson()).toList()),
  149. );
  150. } catch (e) {
  151. log('IVSP', 'addDisconnectDomain error: $e');
  152. }
  153. }
  154. /// 读取 DisconnectDomain 列表
  155. static List<DisconnectDomain> getDisconnectDomains() {
  156. final jsonStr = _sharedPreferences.getString(_disconnectDomainsKey);
  157. if (jsonStr == null || jsonStr.isEmpty) return [];
  158. final List<dynamic> list = jsonDecode(jsonStr);
  159. return list.map((e) => DisconnectDomain.fromJson(e)).toList();
  160. }
  161. /// 清空 DisconnectDomain 列表
  162. static Future<void> clearDisconnectDomains() async {
  163. await _sharedPreferences.remove(_disconnectDomainsKey);
  164. }
  165. // 记录最后一次是否是用户禁用
  166. static Future<void> setLastIsUserDisabled(bool isUserDisabled) async {
  167. await _sharedPreferences.setBool(_isUserDisabledKey, isUserDisabled);
  168. }
  169. // 获取最后一次是否是用户禁用
  170. static bool getLastIsUserDisabled() {
  171. return _sharedPreferences.getBool(_isUserDisabledKey) ?? false;
  172. }
  173. // 记录最后一次是否是地区禁用
  174. static Future<void> setLastIsRegionDisabled(bool isRegionDisabled) async {
  175. await _sharedPreferences.setBool(_isRegionDisabledKey, isRegionDisabled);
  176. }
  177. // 获取最后一次是否是地区禁用
  178. static bool getLastIsRegionDisabled() {
  179. return _sharedPreferences.getBool(_isRegionDisabledKey) ?? false;
  180. }
  181. // 记录最后一次是否是设备禁用
  182. static Future<void> setLastIsDeviceDisabled(bool isDeviceDisabled) async {
  183. await _sharedPreferences.setBool(_isDeviceDisabledKey, isDeviceDisabled);
  184. }
  185. // 获取最后一次是否是设备禁用
  186. static bool getLastIsDeviceDisabled() {
  187. return _sharedPreferences.getBool(_isDeviceDisabledKey) ?? false;
  188. }
  189. /// Save last metrics log
  190. static Future<void> setLastMetricsLog(String log) async {
  191. await _sharedPreferences.setString(_lastMetricsLogKey, log);
  192. }
  193. /// Get last metrics log
  194. static String getLastMetricsLog() =>
  195. _sharedPreferences.getString(_lastMetricsLogKey) ?? '';
  196. /// Save last es log
  197. static Future<void> setLastESLog(String log) async {
  198. await _sharedPreferences.setString(_lastESLogKey, log);
  199. }
  200. /// Get last es log
  201. static String getLastESLog() =>
  202. _sharedPreferences.getString(_lastESLogKey) ?? '';
  203. /// Save enable debug log
  204. static Future<void> setEnableDebugLog(bool enable) async {
  205. await _sharedPreferences.setBool(_enableDebugLogKey, enable);
  206. }
  207. /// Get enable debug log
  208. static bool getEnableDebugLog() =>
  209. _sharedPreferences.getBool(_enableDebugLogKey) ?? false;
  210. /// Save enable ping
  211. static Future<void> setEnablePingMode(int model) async {
  212. await _sharedPreferences.setInt(_enablePingModeKey, model);
  213. }
  214. /// Get enable ping
  215. static int getEnablePingMode() =>
  216. _sharedPreferences.getInt(_enablePingModeKey) ?? 0;
  217. /// 保存 Launch 数据
  218. static Future<bool> saveLaunch(Launch launch) async {
  219. try {
  220. final jsonData = jsonEncode(launch.toJson());
  221. // 保存数据
  222. await _sharedPreferences.setString(
  223. _launchDataKey,
  224. Crytpo.encrypt(jsonData, Keys.aesKey, Keys.aesIv),
  225. );
  226. // 保存保存时间
  227. log('IXSP', 'Launch data saved successfully');
  228. return true;
  229. } catch (e) {
  230. log('IXSP', 'Error saving launch data: $e');
  231. return false;
  232. }
  233. }
  234. /// 获取 Launch 数据
  235. static Launch? getLaunch() {
  236. try {
  237. final jsonData = _sharedPreferences.getString(_launchDataKey);
  238. if (jsonData == null) {
  239. return null;
  240. }
  241. final Map<String, dynamic> map = jsonDecode(
  242. Crytpo.decrypt(jsonData, Keys.aesKey, Keys.aesIv),
  243. );
  244. return Launch.fromJson(map);
  245. } catch (e) {
  246. log('IXSP', 'Error getting launch data: $e');
  247. return null;
  248. }
  249. }
  250. /// 获取用户信息
  251. static User? getUser() {
  252. final launch = getLaunch();
  253. return launch?.userConfig;
  254. }
  255. /// 保存用户信息
  256. static Future<void> saveUser(User user) async {
  257. final launch = getLaunch();
  258. final newLaunch = launch!.copyWith(userConfig: user);
  259. await saveLaunch(newLaunch);
  260. }
  261. /// 清除 Launch 数据
  262. static Future<bool> clearLaunchData() async {
  263. try {
  264. await _sharedPreferences.remove(_launchDataKey);
  265. log('IXSP', 'Launch data cleared');
  266. return true;
  267. } catch (e) {
  268. log('IXSP', 'Error clearing launch data: $e');
  269. return false;
  270. }
  271. }
  272. }