123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import 'package:fis_common/index.dart';
- import 'package:fis_common/web/web_asset_bundle.dart';
- import 'package:fis_i18n/i18n.dart';
- import 'package:fis_ui/utils/sizer/sizer.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/consts/devices.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/js_platform/listener.dart';
- import 'package:vitalapp/managers/interfaces/excelData.dart';
- import 'package:vitalapp/pages/admin/usb_state.dart';
- import 'package:vnote_device_plugin/events/global_event_handler.dart';
- import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
- import 'package:vitalapp/architecture/network_connectivity.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:vitalapp/managers/index.dart';
- import 'package:vitalapp/managers/interfaces/permission.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'package:fis_measure/process/workspace/rpc_bridge.dart';
- import 'package:fis_common/env/env.dart';
- import 'package:universal_html/js.dart' as js;
- import 'architecture/app_parameters.dart';
- import 'architecture/services/index.dart';
- import 'architecture/storage/shared_storage.dart';
- import 'architecture/utils/advance_debounce.dart';
- import 'architecture/utils/web_parameter_helpler.dart';
- import 'managers/interfaces/account.dart';
- import 'managers/interfaces/template.dart';
- /// 【全局】 是否在线
- bool get kIsOnline => netChecker.isOnline;
- FPlatformEnum get platform => FPlatform.current;
- abstract class Global {
- static AssetBundle? _assetBundle;
- /// 资源包
- static AssetBundle get assetBundle => _assetBundle!;
- static Future<void> init() async {
- // 日志
- if (kReleaseMode) {
- // 生产环境
- Get.isLogEnable = false;
- } else {
- // debug/profile 环境
- logger.setMinimumLevel(LogLevel.Degbug);
- }
- await logger.init();
- logger.i("Global init start...");
- // 资源包
- _initAssetBundle();
- // 锁定横屏
- await SystemChrome.setPreferredOrientations([
- DeviceOrientation.landscapeLeft,
- // DeviceOrientation.landscapeRight,
- ]);
- try {
- UsbState usbState = UsbState();
- usbState.startListening();
- } catch (e) {
- logger.e("Global init UsbState error.", e);
- }
- // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
- SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
- SystemChrome.setSystemUIChangeCallback((systemOverlaysAreVisible) async {
- advanceDebounce(() {
- Future.delayed(const Duration(seconds: 2), () {
- SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
- });
- }, "setSystemUIChangeCallback");
- });
- SystemChrome.setSystemUIOverlayStyle(
- const SystemUiOverlayStyle(
- statusBarColor: Colors.transparent,
- statusBarIconBrightness: Brightness.light,
- ),
- );
- // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
- // Loading工具
- PromptBox.init();
- /// 共享存储
- await Get.putAsync<SharedStorageService>(() => SharedStorageService.init());
- /// 预设配置
- await AppParameters.load();
- // 状态
- await Store.init();
- // 开始检测网络状态
- netChecker.run();
- if (platform == FPlatformEnum.android) {
- await db.init();
- /// 设备测量插件初始化
- await _initDevices();
- }
- ///获取启动参数
- if (ParameterHelper.parasmeters.isEmpty)
- ParameterHelper.getAppStartParameters();
- if (kDebugMode && Store.user.token.isNullOrEmpty) {
- Store.user.setToken("94642923B8D240FF81F7744D3B8EDEBF");
- }
- _initRpc();
- /// 全局管理器中心
- ManagerCenter.load();
- //_checkIsLogin();
- /// 初始化动态权限
- _initPermission();
- var bookOptions = [
- FTrKeybookOption.ChineseOption,
- ];
- final curLocale = CHINESE_LOCALE;
- await FI18n.init((locale) => Get.updateLocale(locale), bookOptions);
- await FI18n.load(curLocale);
- Get.put(FI18n.ins);
- Sizer.init();
- // 依赖注入
- await initServices();
- // // 主题
- // final theme = AppTheme(themeMode: ThemeMode.system);
- // await FTheme.init(theme);
- // Get.put(theme);
- await _loadDefaultFont();
- final templateController = Get.find<ITemplateManager>();
- await templateController.saveTemplate();
- //Get.put(RPCBridge(rpc, Store.user.token ?? ''));
- //JsPlatformListener().run();
- logger.i("Global init finish...");
- }
- /// 初始化资源包
- static void _initAssetBundle() {
- if (kIsWeb) {
- final jsVersion = js.context["serviceWorkerVersion"];
- String version = jsVersion?.toString() ??
- "debug_${DateTime.now().millisecondsSinceEpoch}";
- _assetBundle = WebPlatformAssetBundle(
- rootPath: js.context["STATIC_ROOT"]!.toString(),
- version: version,
- );
- } else {
- _assetBundle = rootBundle;
- }
- }
- static Future<void> _loadDefaultFont() async {
- try {
- final loader = FontLoader("NotoSansSC");
- loader.addFont(rootBundle.load("assets/fonts/NotoSansSC-R-fixed.otf"));
- await loader.load();
- } catch (e) {
- logger.e("Global load default font error.", e);
- }
- }
- static void _initRpc() {
- RpcSettingCache.setRpcHostPath(Store.app.serverUrl);
- }
- static Future<void> _initPermission() async {
- IPermissionManager permissionManager = Get.find<IPermissionManager>();
- await permissionManager.requestPermissions([
- 'Location',
- 'BluetoothConnect',
- 'BluetoothScan',
- 'BluetoothAdvertise',
- 'Camera',
- 'InstallPackages',
- ]);
- }
- static Future<void> _checkIsLogin() async {
- if (kIsOnline) {
- final accountController = Get.find<IAccountManager>();
- await accountController.getOperationPermission();
- }
- }
- static Future<void> _initDevices() async {
- // TODO: 后续改成按后门配置开启
- await VnoteDevicePluginPlatform.instance.init(false);
- final List<_DeviceStatusTipsHandler> statusHandlers = [];
- for (int status in DeviceConsts.STATUS_DESC_MAP.keys) {
- final message = DeviceConsts.STATUS_DESC_MAP[status]!;
- statusHandlers.add(_DeviceStatusTipsHandler(status, message));
- }
- GlobalDeviceEventHandler.setup(statusHandlers: statusHandlers);
- }
- }
- class _DeviceStatusTipsHandler extends DeviceEventStatusHandler {
- late final String _msg;
- _DeviceStatusTipsHandler(int status, String msg) : super(status) {
- _msg = msg;
- }
- String get message => _msg;
- @override
- void handle(Map<String, dynamic>? data) {
- // PromptBox.toast(message);
- Get.snackbar("提示", message);
- }
- }
|