|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
|
|
import 'raw_data_service/health_raw_data_core_service.dart';
|
|
|
|
|
|
|
|
/// Receives native HealthKit work independently of the visible Flutter route.
|
|
|
|
abstract final class NativeHealthDataUpdateBridge {
|
|
|
|
static const _channel =
|
|
|
|
MethodChannel('doublefeel_flutter_health_update_channel');
|
|
|
|
static var _initialized = false;
|
|
|
|
|
|
|
|
static void initialize() {
|
|
|
|
if (_initialized) return;
|
|
|
|
_initialized = true;
|
|
|
|
_channel.setMethodCallHandler(_handleMethodCall);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<Object?> _handleMethodCall(MethodCall call) async {
|
|
|
|
if (call.method == 'healthDataEngineReady') return true;
|
|
|
|
if (call.method != 'healthDataUpdated') return null;
|
|
|
|
|
|
|
|
final dataTypes = _dataTypes(call.arguments);
|
|
|
|
debugPrint('[NativeHealthDataUpdateBridge] calculate dataTypes=$dataTypes');
|
|
|
|
await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(
|
|
|
|
dataTypes: dataTypes,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<int> _dataTypes(Object? arguments) {
|
|
|
|
final value = switch (arguments) {
|
|
|
|
{'dataTypes': final Object? dataTypes} => dataTypes,
|
|
|
|
{'data_types': final Object? dataTypes} => dataTypes,
|
|
|
|
_ => arguments,
|
|
|
|
};
|
|
|
|
if (value is! Iterable) return const <int>[];
|
|
|
|
return value.whereType<num>().map((value) => value.toInt()).toList();
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|