Commit 80b26c33ac47077463a82d66fa2292e0706cfad3

Authored by 权海
1 parent 733d5237

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

@@ -18,14 +18,26 @@ abstract final class NativeHealthDataUpdateBridge { @@ -18,14 +18,26 @@ abstract final class NativeHealthDataUpdateBridge {
18 18
19 static Future<Object?> _handleMethodCall(MethodCall call) async { 19 static Future<Object?> _handleMethodCall(MethodCall call) async {
20 if (call.method == 'healthDataEngineReady') return true; 20 if (call.method == 'healthDataEngineReady') return true;
21 - if (call.method != 'healthDataUpdated') return null; 21 + if (call.method != 'healthDataUpdated' &&
  22 + call.method != 'calculateHealthDataUpdate') {
  23 + return null;
  24 + }
22 25
23 final dataTypes = _dataTypes(call.arguments); 26 final dataTypes = _dataTypes(call.arguments);
24 debugPrint('[NativeHealthDataUpdateBridge] calculate dataTypes=$dataTypes'); 27 debugPrint('[NativeHealthDataUpdateBridge] calculate dataTypes=$dataTypes');
25 - await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(  
26 - dataTypes: dataTypes,  
27 - );  
28 - return true; 28 + try {
  29 + final hasNewResult =
  30 + await Get.find<HealthRawDataCoreService>().onHealthDataUpdated(
  31 + dataTypes: dataTypes,
  32 + );
  33 + return call.method == 'calculateHealthDataUpdate' ? hasNewResult : true;
  34 + } catch (error, stackTrace) {
  35 + debugPrint(
  36 + '[NativeHealthDataUpdateBridge] calculate.failed '
  37 + 'error=$error stack=$stackTrace',
  38 + );
  39 + return false;
  40 + }
29 } 41 }
30 42
31 static List<int> _dataTypes(Object? arguments) { 43 static List<int> _dataTypes(Object? arguments) {
@@ -74,7 +74,7 @@ class HealthRawDataCoreService { @@ -74,7 +74,7 @@ class HealthRawDataCoreService {
74 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream => 74 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
75 _currentService.healthDataUpdatedStream; 75 _currentService.healthDataUpdatedStream;
76 76
77 - Future<void> onHealthDataUpdated({List<int>? dataTypes}) { 77 + Future<bool> onHealthDataUpdated({List<int>? dataTypes}) {
78 return _currentService.onHealthDataUpdated(dataTypes: dataTypes); 78 return _currentService.onHealthDataUpdated(dataTypes: dataTypes);
79 } 79 }
80 80
@@ -59,7 +59,7 @@ class AppleHealthRawDataCoreService { @@ -59,7 +59,7 @@ class AppleHealthRawDataCoreService {
59 final StreamController<HealthRawDataUpdatedEvent> 59 final StreamController<HealthRawDataUpdatedEvent>
60 _healthDataUpdatedController = 60 _healthDataUpdatedController =
61 StreamController<HealthRawDataUpdatedEvent>.broadcast(); 61 StreamController<HealthRawDataUpdatedEvent>.broadcast();
62 - Future<void>? _healthDataUpdatedCalculation; 62 + Future<bool>? _healthDataUpdatedCalculation;
63 Future<HealthRawStressCalculationResult>? _coreCalculation; 63 Future<HealthRawStressCalculationResult>? _coreCalculation;
64 List<int> _pendingHealthDataUpdatedTypes = const <int>[]; 64 List<int> _pendingHealthDataUpdatedTypes = const <int>[];
65 bool _isUploadingHrvResults = false; 65 bool _isUploadingHrvResults = false;
@@ -106,14 +106,14 @@ class AppleHealthRawDataCoreService { @@ -106,14 +106,14 @@ class AppleHealthRawDataCoreService {
106 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream => 106 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
107 _healthDataUpdatedController.stream; 107 _healthDataUpdatedController.stream;
108 108
109 - Future<void> onHealthDataUpdated({List<int>? dataTypes}) async { 109 + Future<bool> onHealthDataUpdated({List<int>? dataTypes}) async {
110 await _saveNativeCallFlutterChange(dataTypes); 110 await _saveNativeCallFlutterChange(dataTypes);
111 if (_healthDataUpdatedCalculation != null) { 111 if (_healthDataUpdatedCalculation != null) {
112 _pendingHealthDataUpdatedTypes = _mergeHealthDataTypes( 112 _pendingHealthDataUpdatedTypes = _mergeHealthDataTypes(
113 _pendingHealthDataUpdatedTypes, 113 _pendingHealthDataUpdatedTypes,
114 dataTypes, 114 dataTypes,
115 ); 115 );
116 - return _healthDataUpdatedCalculation; 116 + return _healthDataUpdatedCalculation!;
117 } 117 }
118 118
119 _pendingHealthDataUpdatedTypes = _mergeHealthDataTypes( 119 _pendingHealthDataUpdatedTypes = _mergeHealthDataTypes(
@@ -123,7 +123,7 @@ class AppleHealthRawDataCoreService { @@ -123,7 +123,7 @@ class AppleHealthRawDataCoreService {
123 final task = _calculateAndNotifyHealthDataUpdated(); 123 final task = _calculateAndNotifyHealthDataUpdated();
124 _healthDataUpdatedCalculation = task; 124 _healthDataUpdatedCalculation = task;
125 try { 125 try {
126 - await task; 126 + return await task;
127 } finally { 127 } finally {
128 if (identical(_healthDataUpdatedCalculation, task)) { 128 if (identical(_healthDataUpdatedCalculation, task)) {
129 _healthDataUpdatedCalculation = null; 129 _healthDataUpdatedCalculation = null;
@@ -131,14 +131,18 @@ class AppleHealthRawDataCoreService { @@ -131,14 +131,18 @@ class AppleHealthRawDataCoreService {
131 } 131 }
132 } 132 }
133 133
134 - Future<void> _calculateAndNotifyHealthDataUpdated() async { 134 + Future<bool> _calculateAndNotifyHealthDataUpdated() async {
135 try { 135 try {
136 - await startCoreCaculate(); 136 + final result = await startCoreCaculate();
137 final completedDataTypes = _pendingHealthDataUpdatedTypes; 137 final completedDataTypes = _pendingHealthDataUpdatedTypes;
138 await _saveFlutterCaculateFinished(completedDataTypes); 138 await _saveFlutterCaculateFinished(completedDataTypes);
139 _healthDataUpdatedController.add( 139 _healthDataUpdatedController.add(
140 HealthRawDataUpdatedEvent(dataTypes: completedDataTypes), 140 HealthRawDataUpdatedEvent(dataTypes: completedDataTypes),
141 ); 141 );
  142 + return result.hrvStressPoints.isNotEmpty ||
  143 + result.realtimeStressPoints.isNotEmpty ||
  144 + result.dailyStressPoints.isNotEmpty ||
  145 + result.sleepResults.isNotEmpty;
142 } finally { 146 } finally {
143 _pendingHealthDataUpdatedTypes = const <int>[]; 147 _pendingHealthDataUpdatedTypes = const <int>[];
144 } 148 }
@@ -119,11 +119,15 @@ class OHOSHealthRawDataCoreService { @@ -119,11 +119,15 @@ class OHOSHealthRawDataCoreService {
119 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream => 119 Stream<HealthRawDataUpdatedEvent> get healthDataUpdatedStream =>
120 _healthDataUpdatedController.stream; 120 _healthDataUpdatedController.stream;
121 121
122 - Future<void> onHealthDataUpdated({List<int>? dataTypes}) async {  
123 - await startCoreCaculate(); 122 + Future<bool> onHealthDataUpdated({List<int>? dataTypes}) async {
  123 + final result = await startCoreCaculate();
124 _healthDataUpdatedController.add( 124 _healthDataUpdatedController.add(
125 HealthRawDataUpdatedEvent(dataTypes: dataTypes ?? const <int>[]), 125 HealthRawDataUpdatedEvent(dataTypes: dataTypes ?? const <int>[]),
126 ); 126 );
  127 + return result.hrvStressPoints.isNotEmpty ||
  128 + result.realtimeStressPoints.isNotEmpty ||
  129 + result.dailyStressPoints.isNotEmpty ||
  130 + result.sleepResults.isNotEmpty;
127 } 131 }
128 132
129 Future<String> databaseFilePath() { 133 Future<String> databaseFilePath() {
@@ -9,6 +9,7 @@ import 'package:intl/date_symbol_data_local.dart'; @@ -9,6 +9,7 @@ import 'package:intl/date_symbol_data_local.dart';
9 import 'app/bootstrap/app_bootstrap.dart'; 9 import 'app/bootstrap/app_bootstrap.dart';
10 import 'app/double_feel_app.dart'; 10 import 'app/double_feel_app.dart';
11 import 'core/services/raw_data_service/health_raw_data_core_service.dart'; 11 import 'core/services/raw_data_service/health_raw_data_core_service.dart';
  12 +import 'core/services/raw_data_service/health_raw_models.dart';
12 import 'core/theme/app_theme.dart'; 13 import 'core/theme/app_theme.dart';
13 import 'data/local/user_preferences_storage.dart'; 14 import 'data/local/user_preferences_storage.dart';
14 15
@@ -105,12 +106,18 @@ Future<void> _runPendingHealthTasks( @@ -105,12 +106,18 @@ Future<void> _runPendingHealthTasks(
105 106
106 try { 107 try {
107 await _healthBackgroundLog(channel, 'calculate.start'); 108 await _healthBackgroundLog(channel, 'calculate.start');
108 - await Get.find<HealthRawDataCoreService>().startCoreCaculate();  
109 - await _healthBackgroundLog(channel, 'calculate.success'); 109 + final calculation =
  110 + await Get.find<HealthRawDataCoreService>().startCoreCaculate();
  111 + final hasNewResult = _hasNewHealthCalculationResult(calculation);
  112 + await _healthBackgroundLog(
  113 + channel,
  114 + 'calculate.success hasNewResult=$hasNewResult',
  115 + );
110 for (final task in tasks) { 116 for (final task in tasks) {
111 await channel.invokeMethod('healthBackgroundTaskFinished', { 117 await channel.invokeMethod('healthBackgroundTaskFinished', {
112 'taskId': task.taskId, 118 'taskId': task.taskId,
113 'success': true, 119 'success': true,
  120 + 'calculated': hasNewResult,
114 }); 121 });
115 } 122 }
116 } catch (error) { 123 } catch (error) {
@@ -126,6 +133,13 @@ Future<void> _runPendingHealthTasks( @@ -126,6 +133,13 @@ Future<void> _runPendingHealthTasks(
126 } 133 }
127 } 134 }
128 135
  136 +bool _hasNewHealthCalculationResult(HealthRawStressCalculationResult result) {
  137 + return result.hrvStressPoints.isNotEmpty ||
  138 + result.realtimeStressPoints.isNotEmpty ||
  139 + result.dailyStressPoints.isNotEmpty ||
  140 + result.sleepResults.isNotEmpty;
  141 +}
  142 +
129 Future<void> _healthBackgroundLog(MethodChannel channel, String message) async { 143 Future<void> _healthBackgroundLog(MethodChannel channel, String message) async {
130 try { 144 try {
131 await channel.invokeMethod('healthBackgroundLog', { 145 await channel.invokeMethod('healthBackgroundLog', {