global.dart 5.5 KB

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