global.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import 'package:fis_common/web/web_asset_bundle.dart';
  2. import 'package:fis_i18n/i18n.dart';
  3. import 'package:fis_ui/utils/sizer/sizer.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:get/get.dart';
  8. import 'package:vitalapp/consts/devices.dart';
  9. import 'package:vitalapp/database/db.dart';
  10. import 'package:vitalapp/js_platform/listener.dart';
  11. import 'package:vitalapp/pages/admin/usb_state.dart';
  12. import 'package:vnote_device_plugin/events/global_event_handler.dart';
  13. import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
  14. import 'package:vitalapp/architecture/network_connectivity.dart';
  15. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  16. import 'package:vitalapp/managers/index.dart';
  17. import 'package:vitalapp/managers/interfaces/permission.dart';
  18. import 'package:vitalapp/rpc.dart';
  19. import 'package:vitalapp/store/store.dart';
  20. import 'package:fis_common/logger/logger.dart';
  21. import 'package:fis_measure/process/workspace/rpc_bridge.dart';
  22. import 'package:fis_common/env/env.dart';
  23. import 'package:universal_html/js.dart' as js;
  24. import 'architecture/app_parameters.dart';
  25. import 'architecture/services/index.dart';
  26. import 'architecture/storage/shared_storage.dart';
  27. import 'architecture/utils/advance_debounce.dart';
  28. import 'managers/interfaces/account.dart';
  29. /// 【全局】 是否在线
  30. bool get kIsOnline => netChecker.isOnline;
  31. FPlatformEnum get platform => FPlatform.current;
  32. abstract class Global {
  33. static AssetBundle? _assetBundle;
  34. /// 资源包
  35. static AssetBundle get assetBundle => _assetBundle!;
  36. static Future<void> init() async {
  37. // 日志
  38. if (kReleaseMode) {
  39. // 生产环境
  40. Get.isLogEnable = false;
  41. } else {
  42. // debug/profile 环境
  43. logger.setMinimumLevel(LogLevel.Degbug);
  44. }
  45. await logger.init();
  46. logger.i("Global init start...");
  47. // 资源包
  48. _initAssetBundle();
  49. // 锁定横屏
  50. await SystemChrome.setPreferredOrientations([
  51. DeviceOrientation.landscapeLeft,
  52. // DeviceOrientation.landscapeRight,
  53. ]);
  54. try {
  55. UsbState usbState = UsbState();
  56. usbState.startListening();
  57. } catch (e) {
  58. logger.e("Global init UsbState error.", e);
  59. }
  60. // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  61. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  62. SystemChrome.setSystemUIChangeCallback((systemOverlaysAreVisible) async {
  63. advanceDebounce(() {
  64. Future.delayed(const Duration(seconds: 2), () {
  65. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  66. });
  67. }, "setSystemUIChangeCallback");
  68. });
  69. SystemChrome.setSystemUIOverlayStyle(
  70. const SystemUiOverlayStyle(
  71. statusBarColor: Colors.transparent,
  72. statusBarIconBrightness: Brightness.light,
  73. ),
  74. );
  75. // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  76. // Loading工具
  77. PromptBox.init();
  78. /// 共享存储
  79. await Get.putAsync<SharedStorageService>(() => SharedStorageService.init());
  80. /// 预设配置
  81. await AppParameters.load();
  82. // 状态
  83. await Store.init();
  84. // 开始检测网络状态
  85. netChecker.run();
  86. if (platform == FPlatformEnum.android) {
  87. await db.init();
  88. /// 设备测量插件初始化
  89. await _initDevices();
  90. }
  91. _initRpc();
  92. /// 全局管理器中心
  93. ManagerCenter.load();
  94. _checkIsLogin();
  95. /// 初始化动态权限
  96. _initPermission();
  97. var bookOptions = [
  98. FTrKeybookOption.ChineseOption,
  99. FTrKeybookOption.EnglishOption,
  100. FTrKeybookOption.RussianOption,
  101. FTrKeybookOption.SpanishOption,
  102. ];
  103. final curLocale = CHINESE_LOCALE;
  104. await FI18n.init((locale) => Get.updateLocale(locale), bookOptions);
  105. await FI18n.load(curLocale);
  106. Get.put(FI18n.ins);
  107. Sizer.init();
  108. // 依赖注入
  109. await initServices();
  110. // // 主题
  111. // final theme = AppTheme(themeMode: ThemeMode.system);
  112. // await FTheme.init(theme);
  113. // Get.put(theme);
  114. await _loadDefaultFont();
  115. Get.put(RPCBridge(rpc, Store.user.token ?? ''));
  116. JsPlatformListener().run();
  117. logger.i("Global init finish...");
  118. }
  119. /// 初始化资源包
  120. static void _initAssetBundle() {
  121. if (kIsWeb) {
  122. final jsVersion = js.context["serviceWorkerVersion"];
  123. String version = jsVersion?.toString() ??
  124. "debug_${DateTime.now().millisecondsSinceEpoch}";
  125. _assetBundle = WebPlatformAssetBundle(
  126. rootPath: js.context["STATIC_ROOT"]!.toString(),
  127. version: version,
  128. );
  129. } else {
  130. _assetBundle = rootBundle;
  131. }
  132. }
  133. static Future<void> _loadDefaultFont() async {
  134. try {
  135. final loader = FontLoader("NotoSansSC");
  136. loader.addFont(rootBundle.load("assets/fonts/NotoSansSC-R-fixed.otf"));
  137. await loader.load();
  138. } catch (e) {
  139. logger.e("Global load default font error.", e);
  140. }
  141. }
  142. static void _initRpc() {
  143. RpcSettingCache.setRpcHostPath(Store.app.serverUrl);
  144. }
  145. static Future<void> _initPermission() async {
  146. IPermissionManager permissionManager = Get.find<IPermissionManager>();
  147. await permissionManager.requestPermissions([
  148. 'Location',
  149. 'BluetoothConnect',
  150. 'BluetoothScan',
  151. 'BluetoothAdvertise',
  152. 'Camera',
  153. 'InstallPackages',
  154. ]);
  155. }
  156. static Future<void> _checkIsLogin() async {
  157. if (kIsOnline) {
  158. final accountController = Get.find<IAccountManager>();
  159. await accountController.getOperationPermission();
  160. }
  161. }
  162. static Future<void> _initDevices() async {
  163. // TODO: 后续改成按后门配置开启
  164. await VnoteDevicePluginPlatform.instance.init(false);
  165. final List<_DeviceStatusTipsHandler> statusHandlers = [];
  166. for (int status in DeviceConsts.STATUS_DESC_MAP.keys) {
  167. final message = DeviceConsts.STATUS_DESC_MAP[status]!;
  168. statusHandlers.add(_DeviceStatusTipsHandler(status, message));
  169. }
  170. GlobalDeviceEventHandler.setup(statusHandlers: statusHandlers);
  171. }
  172. }
  173. class _DeviceStatusTipsHandler extends DeviceEventStatusHandler {
  174. late final String _msg;
  175. _DeviceStatusTipsHandler(int status, String msg) : super(status) {
  176. _msg = msg;
  177. }
  178. String get message => _msg;
  179. @override
  180. void handle(Map<String, dynamic>? data) {
  181. // PromptBox.toast(message);
  182. Get.snackbar("提示", message);
  183. }
  184. }