Commit ac583fa70635d88d8930b31c9b9042ffd0f1e28a

Authored by 权海
1 parent 8d7571c7

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].
@@ -40,6 +41,8 @@ abstract final class AppBootstrap { @@ -40,6 +41,8 @@ abstract final class AppBootstrap {
40 localStorage: local, 41 localStorage: local,
41 ).dependencies(); 42 ).dependencies();
42 43
  44 + NativeHealthDataUpdateBridge.initialize();
  45 +
43 LoadingService.init(); 46 LoadingService.init();
44 47
45 if (local.termsAgreed) { 48 if (local.termsAgreed) {
  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 +}