Commit c187bd363e7f071cd42faed6329a8ad2504ca5ee

Authored by 权海
1 parent 67c9e06c

feat(ui):修改同步数据流程逻辑

... ... @@ -921,9 +921,13 @@ class TodayController extends GetxController with WidgetsBindingObserver {
// changeDate(today);
checkAddFriendVisible();
checkHrvAdBannerVisible();
final isSelectedToday = DateUtils.isSameDay(
DateUtils.dateOnly(selectedDate.value),
DateUtils.dateOnly(DateTime.now()),
);
checkHealthDataAuthCardVisible(
isPullToRefresh: isPullToRefresh,
shouldCaculateAndUpload: isOhos(),
shouldCaculateAndUpload: isOhos() && isSelectedToday,
);
checkNotificationAuthorizationBannerVisible();
// await loadDataForDate(selectedDate.value);
... ...
... ... @@ -210,17 +210,6 @@ class OHOSHealthRawDataCoreService {
}
final userId = _userId;
final hasAuthorization = await _hasHealthReadAuthorization();
if (!hasAuthorization) {
_logInfo('skip calculate without OHOS health privacy authorization');
return HealthRawStressCalculationResult(
userId: userId,
hrvStressPoints: const <HealthRawHrvStressPoint>[],
realtimeStressPoints: const <HealthRawRealtimeStressPoint>[],
dailyStressPoints: const <HealthRawDailyStressPoint>[],
sleepResults: const <HealthRawSleepResult>[],
);
}
await _localStore.ensureReadable(userId);
final effectiveEndTime =
endTime ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;
... ... @@ -233,10 +222,19 @@ class OHOSHealthRawDataCoreService {
if (effectiveEndTime < requestedStartTime) {
throw ArgumentError.value(endTime, 'endTime');
}
final syncResults = await _syncCalculationRawDataIfNeeded(
startTime: requestedStartTime,
endTime: effectiveEndTime,
);
final hasAuthorization = await _hasHealthReadAuthorization();
final syncResults = hasAuthorization
? await _syncCalculationRawDataIfNeeded(
startTime: requestedStartTime,
endTime: effectiveEndTime,
)
: const <OhosHealthRawDataSyncResult>[];
if (!hasAuthorization) {
_logInfo(
'skip OHOS raw data sync without health privacy authorization; '
'continue calculate and upload from local database',
);
}
_logInfo(
'calculate_sync_finish startTime=$requestedStartTime '
'endTime=$effectiveEndTime syncResults=$syncResults',
... ... @@ -495,9 +493,6 @@ class OHOSHealthRawDataCoreService {
required int endTime,
int readChunkDays = defaultReadChunkDays,
}) async {
if (!await _hasHealthReadAuthorization()) {
return const <HealthKitRawDataPoint>[];
}
return _fetchRawDataInChunks(
dataType,
startTime,
... ... @@ -511,9 +506,6 @@ class OHOSHealthRawDataCoreService {
required int endTime,
int readChunkDays = defaultReadChunkDays,
}) async {
if (!await _hasHealthReadAuthorization()) {
return const <HealthKitRawDataPoint>[];
}
return _fetchSleepIntervalsInChunks(
startTime,
endTime,
... ... @@ -525,9 +517,6 @@ class OHOSHealthRawDataCoreService {
required int startTime,
required int endTime,
}) async {
if (!await _hasHealthReadAuthorization()) {
return const <HealthKitRawActivityDataPoint>[];
}
final points = await _rawDataSource.getRawActivityData(startTime, endTime);
return points
.where((e) => e.endTime >= startTime && e.endTime <= endTime)
... ... @@ -581,6 +570,7 @@ class OHOSHealthRawDataCoreService {
if (rawDataSource is! OhosHealthRawDataSource) {
return const <OhosHealthRawDataSyncResult>[];
}
await rawDataSource.getActivityGoal(refresh: true);
return rawDataSource.syncCalculationRawData(
startTime: startTime,
endTime: endTime,
... ...
import 'dart:io';
import 'package:doublefeel_flutter/core/services/raw_data_service/health_raw_data_core_service.dart';
import 'package:doublefeel_flutter/core/services/raw_data_service/health_raw_data_source.dart';
import 'package:doublefeel_flutter/core/services/raw_data_service/platform_ios/apple_health_raw_data_core_service.dart';
import 'package:doublefeel_flutter/core/services/raw_data_service/platform_ohos/ohos_health_raw_data_core_service.dart';
import 'package:doublefeel_flutter/data/models/health/health_v2_models.dart';
import 'package:doublefeel_flutter/pigeon/health_kit_raw_data_api.g.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
... ... @@ -57,6 +60,29 @@ void main() {
'${tempDir.path}/ohos_hrv_result_$userId.sqlite',
);
});
test('OHOS local raw queries do not check remote authorization', () async {
var authCheckCount = 0;
final service = OHOSHealthRawDataCoreService(
rawDataSource: _FakeHealthRawDataSource(),
userIdProvider: () => 1,
uploadResultsAfterCalculation: false,
healthReadAuthorizationChecker: () async {
authCheckCount += 1;
return false;
},
);
final points = await service.queryRawDataPoints(
dataType: 2,
startTime: 100,
endTime: 200,
);
expect(authCheckCount, 0);
expect(points, hasLength(1));
expect(points.single.endTime, 123);
});
}
class _FakeAppleHealthRawDataCoreService extends AppleHealthRawDataCoreService {
... ... @@ -98,3 +124,57 @@ class _FakeOhosHealthRawDataCoreService extends OHOSHealthRawDataCoreService {
return false;
}
}
class _FakeHealthRawDataSource implements HealthRawDataSource {
@override
Future<V2ActivityTarget?> getActivityGoal({bool refresh = true}) async {
return null;
}
@override
Future<List<HealthKitRawActivityDataPoint>> getRawActivityData(
int startTime,
int endTime,
) async {
return [
HealthKitRawActivityDataPoint(endTime: 123, activeEnergyBurned: 10),
];
}
@override
Future<List<HealthKitRawDataPoint>> getRawData(
int dataType,
int startTime,
int endTime,
) async {
return [
HealthKitRawDataPoint(
dataType: dataType,
startTime: 123,
endTime: 123,
value: 72,
),
];
}
@override
Future<List<HealthKitRawSleepDataPoint>> getRawSleepData(
int startTime,
int endTime,
) async {
return const <HealthKitRawSleepDataPoint>[];
}
@override
Future<List<HealthKitRawWorkoutDataPoint>> getRawWorkoutData(
int startTime,
int endTime,
) async {
return const <HealthKitRawWorkoutDataPoint>[];
}
@override
Future<bool> hasHealthData() async {
return true;
}
}
... ...