Commit 80b26c33ac47077463a82d66fa2292e0706cfad3

Authored by 权海
1 parent 733d5237

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) {
... ...
... ... @@ -74,7 +74,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);
}
... ...
... ... @@ -59,7 +59,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;
... ... @@ -106,14 +106,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(
... ... @@ -123,7 +123,7 @@ class AppleHealthRawDataCoreService {
final task = _calculateAndNotifyHealthDataUpdated();
_healthDataUpdatedCalculation = task;
try {
await task;
return await task;
} finally {
if (identical(_healthDataUpdatedCalculation, task)) {
_healthDataUpdatedCalculation = null;
... ... @@ -131,14 +131,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>[];
}
... ...
... ... @@ -119,11 +119,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', {
... ...