Commit 83c5279fb26fd355a3130b94b434174365cf243c

Authored by 权海
1 parent a3a0c7ad

feat(ui):startCaculate 增加返回值,原生根据返回值决定是否启用hardness engine,防止后台isolate无法执行

... ... @@ -18,14 +18,26 @@ abstract final class NativeHealthDataUpdateBridge {
static Future<Object?> _handleMethodCall(MethodCall call) async {
if (call.method == 'healthDataEngineReady') return true;
if (call.method != 'healthDataUpdated') return null;
if (call.method != 'healthDataUpdated' &&
call.method != 'calculateHealthDataUpdate') {
return null;
}
final dataTypes = _dataTypes(call.arguments);
debugPrint('[NativeHealthDataUpdateBridge] calculate dataTypes=$dataTypes');
await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(
dataTypes: dataTypes,
);
return true;
try {
final hasNewResult =
await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(
dataTypes: dataTypes,
);
return call.method == 'calculateHealthDataUpdate' ? hasNewResult : true;
} catch (error, stackTrace) {
debugPrint(
'[NativeHealthDataUpdateBridge] calculate.failed '
'error=$error stack=$stackTrace',
);
return false;
}
}
static List<int> _dataTypes(Object? arguments) {
... ...
... ... @@ -71,7 +71,7 @@ class HealthRawDataCoreService {
Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
_currentService.healthDataUpdatedStream;
Future<void> onHealthDataUpdated({List<int>? dataTypes}) {
Future<bool> onHealthDataUpdated({List<int>? dataTypes}) {
return _currentService.onHealthDataUpdated(dataTypes: dataTypes);
}
... ...
... ... @@ -60,7 +60,7 @@ class AppleHealthRawDataCoreService {
final StreamController<HealthRawDataUpdatedEvent>
_healthDataUpdatedController =
StreamController<HealthRawDataUpdatedEvent>.broadcast();
Future<void>? _healthDataUpdatedCalculation;
Future<bool>? _healthDataUpdatedCalculation;
Future<HealthRawStressCalculationResult>? _coreCalculation;
List<int> _pendingHealthDataUpdatedTypes = const <int>[];
bool _isUploadingHrvResults = false;
... ... @@ -107,14 +107,14 @@ class AppleHealthRawDataCoreService {
Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
_healthDataUpdatedController.stream;
Future<void> onHealthDataUpdated({List<int>? dataTypes}) async {
Future<bool> onHealthDataUpdated({List<int>? dataTypes}) async {
await _saveNativeCallFlutterChange(dataTypes);
if (_healthDataUpdatedCalculation != null) {
_pendingHealthDataUpdatedTypes = _mergeHealthDataTypes(
_pendingHealthDataUpdatedTypes,
dataTypes,
);
return _healthDataUpdatedCalculation;
return _healthDataUpdatedCalculation!;
}
_pendingHealthDataUpdatedTypes = _mergeHealthDataTypes(
... ... @@ -124,7 +124,7 @@ class AppleHealthRawDataCoreService {
final task = _calculateAndNotifyHealthDataUpdated();
_healthDataUpdatedCalculation = task;
try {
await task;
return await task;
} finally {
if (identical(_healthDataUpdatedCalculation, task)) {
_healthDataUpdatedCalculation = null;
... ... @@ -132,14 +132,18 @@ class AppleHealthRawDataCoreService {
}
}
Future<void> _calculateAndNotifyHealthDataUpdated() async {
Future<bool> _calculateAndNotifyHealthDataUpdated() async {
try {
await startCoreCaculate();
final result = await startCoreCaculate();
final completedDataTypes = _pendingHealthDataUpdatedTypes;
await _saveFlutterCaculateFinished(completedDataTypes);
_healthDataUpdatedController.add(
HealthRawDataUpdatedEvent(dataTypes: completedDataTypes),
);
return result.hrvStressPoints.isNotEmpty ||
result.realtimeStressPoints.isNotEmpty ||
result.dailyStressPoints.isNotEmpty ||
result.sleepResults.isNotEmpty;
} finally {
_pendingHealthDataUpdatedTypes = const <int>[];
}
... ...
... ... @@ -82,11 +82,15 @@ class OHOSHealthRawDataCoreService {
Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
_healthDataUpdatedController.stream;
Future<void> onHealthDataUpdated({List<int>? dataTypes}) async {
await startCoreCaculate();
Future<bool> onHealthDataUpdated({List<int>? dataTypes}) async {
final result = await startCoreCaculate();
_healthDataUpdatedController.add(
HealthRawDataUpdatedEvent(dataTypes: dataTypes ?? const <int>[]),
);
return result.hrvStressPoints.isNotEmpty ||
result.realtimeStressPoints.isNotEmpty ||
result.dailyStressPoints.isNotEmpty ||
result.sleepResults.isNotEmpty;
}
Future<String> databaseFilePath() {
... ...
... ... @@ -9,6 +9,7 @@ import 'package:intl/date_symbol_data_local.dart';
import 'app/bootstrap/app_bootstrap.dart';
import 'app/double_feel_app.dart';
import 'core/services/raw_data_service/health_raw_data_core_service.dart';
import 'core/services/raw_data_service/health_raw_models.dart';
import 'core/theme/app_theme.dart';
import 'data/local/user_preferences_storage.dart';
... ... @@ -105,12 +106,18 @@ Future<void> _runPendingHealthTasks(
try {
await _healthBackgroundLog(channel, 'calculate.start');
await Get.find<HealthRawDataCoreService>().startCoreCaculate();
await _healthBackgroundLog(channel, 'calculate.success');
final calculation =
await Get.find<HealthRawDataCoreService>().startCoreCaculate();
final hasNewResult = _hasNewHealthCalculationResult(calculation);
await _healthBackgroundLog(
channel,
'calculate.success hasNewResult=$hasNewResult',
);
for (final task in tasks) {
await channel.invokeMethod('healthBackgroundTaskFinished', {
'taskId': task.taskId,
'success': true,
'calculated': hasNewResult,
});
}
} catch (error) {
... ... @@ -126,6 +133,13 @@ Future<void> _runPendingHealthTasks(
}
}
bool _hasNewHealthCalculationResult(HealthRawStressCalculationResult result) {
return result.hrvStressPoints.isNotEmpty ||
result.realtimeStressPoints.isNotEmpty ||
result.dailyStressPoints.isNotEmpty ||
result.sleepResults.isNotEmpty;
}
Future<void> _healthBackgroundLog(MethodChannel channel, String message) async {
try {
await channel.invokeMethod('healthBackgroundLog', {
... ...
... ... @@ -567,13 +567,14 @@ void main() {
);
final eventFuture = service.healthDataUpdatedStream.first;
await service.onHealthDataUpdated(
final hasNewResult = await service.onHealthDataUpdated(
dataTypes: [
HealthDataUploadType.hrv.type,
HealthDataUploadType.heartRate.type,
],
);
expect(hasNewResult, isTrue);
final event = await eventFuture;
expect(
event.dataTypes,
... ... @@ -581,6 +582,24 @@ void main() {
);
});
test('onHealthDataUpdated returns false after an empty calculation',
() async {
final api = _FakeHealthKitRawDataHostApi();
final service = AppleHealthRawDataCoreService(
healthApi: _FakeHealthKitHostApi(status: 0),
rawDataApi: api,
localStore: _MemoryHealthRawStressLocalStore(),
userIdProvider: () => 42,
uploadResultsAfterCalculation: false,
);
final hasNewResult = await service.onHealthDataUpdated(
dataTypes: [HealthDataUploadType.hrv.type],
);
expect(hasNewResult, isFalse);
});
test('startCoreCaculate calculates and stores sleep results', () async {
final now = DateTime.now();
final day = DateTime(now.year, now.month, now.day);
... ...