global.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/consts/devices.dart';
  6. import 'package:vitalapp/database/db.dart';
  7. import 'package:vnote_device_plugin/consts/status.dart';
  8. import 'package:vnote_device_plugin/events/global_event_handler.dart';
  9. import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
  10. import 'package:vitalapp/architecture/network_connectivity.dart';
  11. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  12. import 'package:vitalapp/managers/index.dart';
  13. import 'package:vitalapp/managers/interfaces/permission.dart';
  14. import 'package:vitalapp/rpc.dart';
  15. import 'package:vitalapp/store/store.dart';
  16. import 'package:fis_common/logger/logger.dart';
  17. import 'architecture/app_parameters.dart';
  18. import 'managers/interfaces/account.dart';
  19. /// 【全局】 是否在线
  20. bool get kIsOnline => netChecker.isOnline;
  21. abstract class Global {
  22. static Future<void> init() async {
  23. // 日志
  24. if (kReleaseMode) {
  25. // 生产环境
  26. Get.isLogEnable = false;
  27. } else {
  28. // debug/profile 环境
  29. logger.setMinimumLevel(LogLevel.Degbug);
  30. }
  31. await logger.init();
  32. logger.i("Global init start...");
  33. // 锁定横屏
  34. await SystemChrome.setPreferredOrientations([
  35. DeviceOrientation.landscapeLeft,
  36. DeviceOrientation.landscapeRight,
  37. ]);
  38. // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  39. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  40. SystemChrome.setSystemUIChangeCallback((systemOverlaysAreVisible) async {
  41. await Future.delayed(const Duration(seconds: 1));
  42. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  43. });
  44. SystemChrome.setSystemUIOverlayStyle(
  45. const SystemUiOverlayStyle(
  46. statusBarColor: Colors.transparent,
  47. statusBarIconBrightness: Brightness.light,
  48. ),
  49. );
  50. // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  51. // Loading工具
  52. PromptBox.init();
  53. /// 预设配置
  54. await AppParameters.load();
  55. // 状态
  56. await Store.init();
  57. // 开始检测网络状态
  58. netChecker.run();
  59. await db.init();
  60. _initRpc();
  61. /// 全局管理器中心
  62. ManagerCenter.load();
  63. _checkIsLogin();
  64. /// 初始化动态权限
  65. _initPermission();
  66. /// 设备测量插件初始化
  67. await _initDevices();
  68. logger.i("Global init finish...");
  69. }
  70. static void _initRpc() {
  71. RpcSettingCache.setRpcHostPath(Store.app.serverUrl);
  72. }
  73. static Future<void> _initPermission() async {
  74. IPermissionManager permissionManager = Get.find<IPermissionManager>();
  75. await permissionManager.requestPermissions([
  76. 'Location',
  77. 'BluetoothConnect',
  78. 'BluetoothScan',
  79. 'BluetoothAdvertise',
  80. 'InstallPackages',
  81. 'Camera',
  82. ]);
  83. // await permissionManager.requestBluetoothConnectPermission();
  84. // await permissionManager.requestBluetoothAdvertisePermission();
  85. // await permissionManager.requestBluetoothScanPermission();
  86. // await permissionManager.requestInstallPackagesPermission();
  87. }
  88. static Future<void> _checkIsLogin() async {
  89. if (kIsOnline) {
  90. final accountController = Get.find<IAccountManager>();
  91. await accountController.getOperationPermission();
  92. }
  93. }
  94. static Future<void> _initDevices() async {
  95. // TODO: 后续改成按后门配置开启
  96. await VnoteDevicePluginPlatform.instance.init(true);
  97. final List<_DeviceStatusTipsHandler> statusHandlers = [];
  98. for (int status in DeviceConsts.STATUS_DESC_MAP.keys) {
  99. final message = DeviceConsts.STATUS_DESC_MAP[status]!;
  100. statusHandlers.add(_DeviceStatusTipsHandler(status, message));
  101. }
  102. GlobalDeviceEventHandler.setup(statusHandlers: statusHandlers);
  103. }
  104. }
  105. class _DeviceStatusTipsHandler extends DeviceEventStatusHandler {
  106. late final String _msg;
  107. _DeviceStatusTipsHandler(int status, String msg) : super(status) {
  108. _msg = msg;
  109. }
  110. String get message => _msg;
  111. @override
  112. void handle(Map<String, dynamic>? data) {
  113. // PromptBox.toast(message);
  114. Get.snackbar("提示", message);
  115. }
  116. }