Commit 62536c66070595227ca6b8e5eee6acb6b9b8d3b0

Authored by 权海
1 parent 33a7b6b5

feat(ui):增加原生检测flutter引擎是否存活

@@ -12,6 +12,7 @@ import '../../data/local/local_storage.dart'; @@ -12,6 +12,7 @@ import '../../data/local/local_storage.dart';
12 import '../../data/local/user_account_storage.dart'; 12 import '../../data/local/user_account_storage.dart';
13 import '../../data/local/user_preferences_storage.dart'; 13 import '../../data/local/user_preferences_storage.dart';
14 import '../../core/services/loading_service.dart'; 14 import '../../core/services/loading_service.dart';
  15 +import '../../core/services/native_health_data_update_bridge.dart';
15 import '../bindings/initial_binding.dart'; 16 import '../bindings/initial_binding.dart';
16 17
17 /// Application startup before [runApp]. 18 /// Application startup before [runApp].
@@ -41,6 +42,7 @@ abstract final class AppBootstrap { @@ -41,6 +42,7 @@ abstract final class AppBootstrap {
41 ).dependencies(); 42 ).dependencies();
42 43
43 await userPreferencesStorage.syncLoginInfoToNative(); 44 await userPreferencesStorage.syncLoginInfoToNative();
  45 + NativeHealthDataUpdateBridge.initialize();
44 46
45 LoadingService.init(); 47 LoadingService.init();
46 48
  1 +import 'package:flutter/foundation.dart';
  2 +import 'package:flutter/services.dart';
  3 +import 'package:get/get.dart';
  4 +
  5 +import 'raw_data_service/health_raw_data_core_service.dart';
  6 +
  7 +/// Receives native HealthKit work independently of the visible Flutter route.
  8 +abstract final class NativeHealthDataUpdateBridge {
  9 + static const _channel =
  10 + MethodChannel('doublefeel_flutter_health_update_channel');
  11 + static var _initialized = false;
  12 +
  13 + static void initialize() {
  14 + if (_initialized) return;
  15 + _initialized = true;
  16 + _channel.setMethodCallHandler(_handleMethodCall);
  17 + }
  18 +
  19 + static Future<Object?> _handleMethodCall(MethodCall call) async {
  20 + if (call.method == 'healthDataEngineReady') return true;
  21 + if (call.method != 'healthDataUpdated') return null;
  22 +
  23 + final dataTypes = _dataTypes(call.arguments);
  24 + debugPrint('[NativeHealthDataUpdateBridge] calculate dataTypes=$dataTypes');
  25 + await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(
  26 + dataTypes: dataTypes,
  27 + );
  28 + return true;
  29 + }
  30 +
  31 + static List<int> _dataTypes(Object? arguments) {
  32 + final value = switch (arguments) {
  33 + {'dataTypes': final Object? dataTypes} => dataTypes,
  34 + {'data_types': final Object? dataTypes} => dataTypes,
  35 + _ => arguments,
  36 + };
  37 + if (value is! Iterable) return const <int>[];
  38 + return value.whereType<num>().map((value) => value.toInt()).toList();
  39 + }
  40 +}