|
|
|
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
|
+} |