mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-06 07:54:02 +01:00
Add health repository
This follows the convention in the rest of the application and allows us to easily mock it for tests.
This commit is contained in:
1 parent
18b745f365
commit
01c13b1ad6
6 files changed
+518
-81
No files matched your search
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2026 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:health/health.dart';
|
||||
|
||||
/// A single reading pulled from a health platform, reduced to the fields the
|
||||
/// importer needs. Keeps the `health` package's [HealthDataPoint] out of the
|
||||
/// notifier so the sync logic can be tested with plain values.
|
||||
class HealthReading {
|
||||
const HealthReading({
|
||||
required this.type,
|
||||
required this.value,
|
||||
required this.date,
|
||||
this.externalId,
|
||||
});
|
||||
|
||||
/// The platform type this reading belongs to (matched against
|
||||
/// `HealthMetric.dataType`).
|
||||
final HealthDataType type;
|
||||
|
||||
/// Numeric value in the platform's native unit.
|
||||
final double value;
|
||||
|
||||
/// Start of the reading.
|
||||
final DateTime date;
|
||||
|
||||
/// Platform record UUID for deduplication; `null` when the platform gives none.
|
||||
final String? externalId;
|
||||
|
||||
/// Builds a reading from a platform data point, or `null` for a non-numeric
|
||||
/// point (which the importer cannot store).
|
||||
static HealthReading? fromDataPoint(HealthDataPoint point) {
|
||||
final value = point.value;
|
||||
if (value is! NumericHealthValue) {
|
||||
return null;
|
||||
}
|
||||
return HealthReading(
|
||||
type: point.type,
|
||||
value: value.numericValue.toDouble(),
|
||||
date: point.dateFrom,
|
||||
externalId: point.uuid.isEmpty ? null : point.uuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2026 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:health/health.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:wger/features/health/models/health_reading.dart';
|
||||
|
||||
final healthRepositoryProvider = Provider<HealthRepository>((ref) {
|
||||
return HealthRepository();
|
||||
});
|
||||
|
||||
/// Wraps the `health` plugin so the rest of the app talks to a small, mockable
|
||||
/// surface instead of Apple Health / Health Connect directly.
|
||||
class HealthRepository {
|
||||
HealthRepository([Health? health]) : _health = health ?? Health();
|
||||
|
||||
final Health _health;
|
||||
final _logger = Logger('HealthRepository');
|
||||
|
||||
/// Identifies which platform a reading came from.
|
||||
String get sourceName => Platform.isIOS ? 'apple_health' : 'health_connect';
|
||||
|
||||
/// Whether a health platform is usable on this device.
|
||||
Future<bool> isAvailable() async {
|
||||
if (Platform.isAndroid) {
|
||||
await _health.configure();
|
||||
final status = await _health.getHealthConnectSdkStatus();
|
||||
return status == HealthConnectSdkStatus.sdkAvailable;
|
||||
}
|
||||
return Platform.isIOS;
|
||||
}
|
||||
|
||||
/// Ensures READ access to [types] (requesting it if needed) and, on Android,
|
||||
/// access to historical data. Returns whether access is granted.
|
||||
Future<bool> ensureAuthorized(List<HealthDataType> types) async {
|
||||
await _health.configure();
|
||||
final access = List.filled(types.length, HealthDataAccess.READ);
|
||||
|
||||
final hasPerms = await _health.hasPermissions(types, permissions: access);
|
||||
if (hasPerms != true) {
|
||||
final granted = await _health.requestAuthorization(types, permissions: access);
|
||||
if (!granted) {
|
||||
_logger.warning('Health permissions not granted');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
await _health.requestHealthDataHistoryAuthorization();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Reads all [types] between [start] and [end], platform-deduplicated and
|
||||
/// reduced to numeric [HealthReading]s.
|
||||
Future<List<HealthReading>> read({
|
||||
required List<HealthDataType> types,
|
||||
required DateTime start,
|
||||
required DateTime end,
|
||||
}) async {
|
||||
final points = _health.removeDuplicates(
|
||||
await _health.getHealthDataFromTypes(types: types, startTime: start, endTime: end),
|
||||
);
|
||||
return points.map(HealthReading.fromDataPoint).whereType<HealthReading>().toList();
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:health/health.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
@@ -25,6 +23,7 @@ import 'package:powersync/powersync.dart' as ps;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:wger/core/shared_preferences.dart';
|
||||
import 'package:wger/features/health/models/health_metric.dart';
|
||||
import 'package:wger/features/health/providers/health_repository.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_category.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_entry.dart';
|
||||
import 'package:wger/features/measurements/providers/measurement_repository.dart';
|
||||
@@ -52,25 +51,22 @@ class HealthSyncState {
|
||||
}
|
||||
|
||||
/// Imports body metrics from Apple Health / Health Connect into measurement
|
||||
/// categories. Read-only: reads the platform, writes to the local Drift DB, and
|
||||
/// lets PowerSync push the rows up. Re-imports are deduplicated via each
|
||||
/// measurement's [MeasurementEntry.externalId] (the platform record UUID).
|
||||
/// categories. Read-only: reads the platform (via [HealthRepository]), writes to
|
||||
/// the local Drift DB, and lets PowerSync push the rows up. Re-imports are
|
||||
/// deduplicated via each measurement's [MeasurementEntry.externalId] (the
|
||||
/// platform record UUID).
|
||||
@Riverpod(keepAlive: true)
|
||||
class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
final _logger = Logger('HealthSyncNotifier');
|
||||
late final Health _health;
|
||||
late final MeasurementRepository _repo;
|
||||
late final HealthRepository _health;
|
||||
late final MeasurementRepository _measurements;
|
||||
|
||||
List<HealthDataType> get _types => enabledHealthMetrics.map((m) => m.dataType).toList();
|
||||
|
||||
List<HealthDataAccess> get _readAccess => List.filled(_types.length, HealthDataAccess.READ);
|
||||
|
||||
String get _sourceName => Platform.isIOS ? 'apple_health' : 'health_connect';
|
||||
|
||||
@override
|
||||
HealthSyncState build() {
|
||||
_health = Health();
|
||||
_repo = ref.read(measurementRepositoryProvider);
|
||||
_health = ref.read(healthRepositoryProvider);
|
||||
_measurements = ref.read(measurementRepositoryProvider);
|
||||
_loadPersistedState();
|
||||
return const HealthSyncState();
|
||||
}
|
||||
@@ -82,33 +78,16 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
}
|
||||
|
||||
/// Whether a health platform is available on this device.
|
||||
Future<bool> isAvailable() async {
|
||||
if (Platform.isAndroid) {
|
||||
await _health.configure();
|
||||
final status = await _health.getHealthConnectSdkStatus();
|
||||
return status == HealthConnectSdkStatus.sdkAvailable;
|
||||
}
|
||||
return Platform.isIOS;
|
||||
}
|
||||
Future<bool> isAvailable() => _health.isAvailable();
|
||||
|
||||
/// Requests permissions, persists the preference, and runs an initial import.
|
||||
Future<int> enableSync() async {
|
||||
_logger.info('Enabling health sync');
|
||||
await _health.configure();
|
||||
|
||||
final authorized = await _health.requestAuthorization(_types, permissions: _readAccess);
|
||||
if (!authorized) {
|
||||
_logger.warning('Health permissions not granted');
|
||||
if (!await _health.ensureAuthorized(_types)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
await _health.requestHealthDataHistoryAuthorization();
|
||||
}
|
||||
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(true);
|
||||
state = state.copyWith(isEnabled: true);
|
||||
|
||||
return syncOnAppOpen();
|
||||
}
|
||||
|
||||
@@ -133,16 +112,10 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
state = state.copyWith(isEnabled: true, isSyncing: true);
|
||||
|
||||
try {
|
||||
await _health.configure();
|
||||
|
||||
final hasPerms = await _health.hasPermissions(_types, permissions: _readAccess);
|
||||
if (hasPerms != true) {
|
||||
final authorized = await _health.requestAuthorization(_types, permissions: _readAccess);
|
||||
if (!authorized) {
|
||||
_logger.warning('Health permissions not granted during sync');
|
||||
state = state.copyWith(isSyncing: false);
|
||||
return 0;
|
||||
}
|
||||
if (!await _health.ensureAuthorized(_types)) {
|
||||
_logger.warning('Health permissions not granted during sync');
|
||||
state = state.copyWith(isSyncing: false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
final lastSyncStr = await prefs.getLastHealthSyncTimestamp();
|
||||
@@ -150,25 +123,21 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
final endTime = DateTime.now();
|
||||
_logger.info('Syncing health data from $startTime to $endTime');
|
||||
|
||||
var points = await _health.getHealthDataFromTypes(
|
||||
types: _types,
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
);
|
||||
points = _health.removeDuplicates(points);
|
||||
if (points.isEmpty) {
|
||||
final readings = await _health.read(types: _types, start: startTime, end: endTime);
|
||||
if (readings.isEmpty) {
|
||||
_logger.info('No new health data');
|
||||
state = state.copyWith(isSyncing: false, lastSyncCount: 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
final categories = await _repo.getAllOnce();
|
||||
final categories = await _measurements.getAllOnce();
|
||||
final source = _health.sourceName;
|
||||
var synced = 0;
|
||||
DateTime? latest;
|
||||
|
||||
for (final metric in enabledHealthMetrics) {
|
||||
final metricPoints = points.where((p) => p.type == metric.dataType);
|
||||
if (metricPoints.isEmpty) {
|
||||
final metricReadings = readings.where((r) => r.type == metric.dataType);
|
||||
if (metricReadings.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -178,33 +147,29 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
if (e.externalId != null) e.externalId!,
|
||||
};
|
||||
|
||||
for (final point in metricPoints) {
|
||||
final value = point.value;
|
||||
if (value is! NumericHealthValue) {
|
||||
continue;
|
||||
}
|
||||
final uuid = point.uuid;
|
||||
if (uuid.isNotEmpty && seen.contains(uuid)) {
|
||||
for (final reading in metricReadings) {
|
||||
final uuid = reading.externalId;
|
||||
if (uuid != null && seen.contains(uuid)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await _repo.addLocalDrift(
|
||||
await _measurements.addLocalDrift(
|
||||
MeasurementEntry(
|
||||
categoryId: category.id!,
|
||||
date: point.dateFrom,
|
||||
value: metric.toCategoryValue(value.numericValue.toDouble()),
|
||||
date: reading.date,
|
||||
value: metric.toCategoryValue(reading.value),
|
||||
notes: '',
|
||||
source: _sourceName,
|
||||
externalId: uuid.isEmpty ? null : uuid,
|
||||
source: source,
|
||||
externalId: uuid,
|
||||
),
|
||||
);
|
||||
|
||||
if (uuid.isNotEmpty) {
|
||||
if (uuid != null) {
|
||||
seen.add(uuid);
|
||||
}
|
||||
synced++;
|
||||
if (latest == null || point.dateFrom.isAfter(latest)) {
|
||||
latest = point.dateFrom;
|
||||
if (latest == null || reading.date.isAfter(latest)) {
|
||||
latest = reading.date;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -243,7 +208,7 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
unit: metric.unit,
|
||||
metricType: metric.metricType,
|
||||
);
|
||||
await _repo.addLocalDriftCategory(category);
|
||||
await _measurements.addLocalDriftCategory(category);
|
||||
categories.add(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@@ -9,23 +9,26 @@ part of 'health_sync.dart';
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
/// Imports body metrics from Apple Health / Health Connect into measurement
|
||||
/// categories. Read-only: reads the platform, writes to the local Drift DB, and
|
||||
/// lets PowerSync push the rows up. Re-imports are deduplicated via each
|
||||
/// measurement's [MeasurementEntry.externalId] (the platform record UUID).
|
||||
/// categories. Read-only: reads the platform (via [HealthRepository]), writes to
|
||||
/// the local Drift DB, and lets PowerSync push the rows up. Re-imports are
|
||||
/// deduplicated via each measurement's [MeasurementEntry.externalId] (the
|
||||
/// platform record UUID).
|
||||
|
||||
@ProviderFor(HealthSyncNotifier)
|
||||
final healthSyncProvider = HealthSyncNotifierProvider._();
|
||||
|
||||
/// Imports body metrics from Apple Health / Health Connect into measurement
|
||||
/// categories. Read-only: reads the platform, writes to the local Drift DB, and
|
||||
/// lets PowerSync push the rows up. Re-imports are deduplicated via each
|
||||
/// measurement's [MeasurementEntry.externalId] (the platform record UUID).
|
||||
/// categories. Read-only: reads the platform (via [HealthRepository]), writes to
|
||||
/// the local Drift DB, and lets PowerSync push the rows up. Re-imports are
|
||||
/// deduplicated via each measurement's [MeasurementEntry.externalId] (the
|
||||
/// platform record UUID).
|
||||
final class HealthSyncNotifierProvider
|
||||
extends $NotifierProvider<HealthSyncNotifier, HealthSyncState> {
|
||||
/// Imports body metrics from Apple Health / Health Connect into measurement
|
||||
/// categories. Read-only: reads the platform, writes to the local Drift DB, and
|
||||
/// lets PowerSync push the rows up. Re-imports are deduplicated via each
|
||||
/// measurement's [MeasurementEntry.externalId] (the platform record UUID).
|
||||
/// categories. Read-only: reads the platform (via [HealthRepository]), writes to
|
||||
/// the local Drift DB, and lets PowerSync push the rows up. Re-imports are
|
||||
/// deduplicated via each measurement's [MeasurementEntry.externalId] (the
|
||||
/// platform record UUID).
|
||||
HealthSyncNotifierProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
@@ -53,12 +56,13 @@ final class HealthSyncNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$healthSyncNotifierHash() => r'9efca0080b2dd104a3e409f1c81a9976ec0e635c';
|
||||
String _$healthSyncNotifierHash() => r'9c7a678e7740bfa32db7638bd227d64da3bfef09';
|
||||
|
||||
/// Imports body metrics from Apple Health / Health Connect into measurement
|
||||
/// categories. Read-only: reads the platform, writes to the local Drift DB, and
|
||||
/// lets PowerSync push the rows up. Re-imports are deduplicated via each
|
||||
/// measurement's [MeasurementEntry.externalId] (the platform record UUID).
|
||||
/// categories. Read-only: reads the platform (via [HealthRepository]), writes to
|
||||
/// the local Drift DB, and lets PowerSync push the rows up. Re-imports are
|
||||
/// deduplicated via each measurement's [MeasurementEntry.externalId] (the
|
||||
/// platform record UUID).
|
||||
|
||||
abstract class _$HealthSyncNotifier extends $Notifier<HealthSyncState> {
|
||||
HealthSyncState build();
|
||||
|
||||
@@ -16,9 +16,24 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:health/health.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
import 'package:wger/core/shared_preferences.dart';
|
||||
import 'package:wger/features/health/models/health_reading.dart';
|
||||
import 'package:wger/features/health/providers/health_repository.dart';
|
||||
import 'package:wger/features/health/providers/health_sync.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_category.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_entry.dart';
|
||||
import 'package:wger/features/measurements/providers/measurement_repository.dart';
|
||||
|
||||
import 'health_sync_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([HealthRepository, MeasurementRepository])
|
||||
void main() {
|
||||
group('HealthSyncState', () {
|
||||
test('default state has sync disabled', () {
|
||||
@@ -36,4 +51,137 @@ void main() {
|
||||
expect(updated.lastSyncCount, 5);
|
||||
});
|
||||
});
|
||||
|
||||
group('syncOnAppOpen', () {
|
||||
late MockHealthRepository health;
|
||||
late MockMeasurementRepository measurements;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(true);
|
||||
|
||||
health = MockHealthRepository();
|
||||
measurements = MockMeasurementRepository();
|
||||
|
||||
when(health.ensureAuthorized(any)).thenAnswer((_) async => true);
|
||||
when(health.sourceName).thenReturn('apple_health');
|
||||
when(measurements.addLocalDrift(any)).thenAnswer((_) async {});
|
||||
when(measurements.addLocalDriftCategory(any)).thenAnswer((_) async {});
|
||||
});
|
||||
|
||||
HealthSyncNotifier createNotifier() {
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [
|
||||
healthRepositoryProvider.overrideWithValue(health),
|
||||
measurementRepositoryProvider.overrideWithValue(measurements),
|
||||
],
|
||||
);
|
||||
return container.read(healthSyncProvider.notifier);
|
||||
}
|
||||
|
||||
void stubReadings(List<HealthReading> readings) {
|
||||
when(
|
||||
health.read(
|
||||
types: anyNamed('types'),
|
||||
start: anyNamed('start'),
|
||||
end: anyNamed('end'),
|
||||
),
|
||||
).thenAnswer((_) async => readings);
|
||||
}
|
||||
|
||||
test('imports enabled metrics into new categories with converted values', () async {
|
||||
when(measurements.getAllOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
|
||||
stubReadings([
|
||||
HealthReading(
|
||||
type: HealthDataType.BODY_FAT_PERCENTAGE,
|
||||
value: 0.2,
|
||||
date: DateTime(2026, 1, 1),
|
||||
externalId: 'bf-1',
|
||||
),
|
||||
HealthReading(
|
||||
type: HealthDataType.HEIGHT,
|
||||
value: 1.8,
|
||||
date: DateTime(2026, 1, 2),
|
||||
externalId: 'h-1',
|
||||
),
|
||||
]);
|
||||
|
||||
final count = await createNotifier().syncOnAppOpen();
|
||||
expect(count, 2);
|
||||
|
||||
final createdCategories = verify(
|
||||
measurements.addLocalDriftCategory(captureAny),
|
||||
).captured.cast<MeasurementCategory>();
|
||||
expect(createdCategories.map((c) => c.metricType), containsAll(['body_fat', 'height']));
|
||||
|
||||
final entries = verify(
|
||||
measurements.addLocalDrift(captureAny),
|
||||
).captured.cast<MeasurementEntry>();
|
||||
|
||||
final bodyFat = entries.firstWhere((e) => e.externalId == 'bf-1');
|
||||
expect(bodyFat.value, closeTo(20, 0.001)); // fraction -> percent
|
||||
expect(bodyFat.source, 'apple_health');
|
||||
|
||||
final height = entries.firstWhere((e) => e.externalId == 'h-1');
|
||||
expect(height.value, closeTo(180, 0.001)); // meters -> cm
|
||||
});
|
||||
|
||||
test('skips readings already imported (dedup by externalId)', () async {
|
||||
final existing = MeasurementCategory(
|
||||
id: 'cat-bf',
|
||||
name: 'Body fat',
|
||||
unit: '%',
|
||||
metricType: 'body_fat',
|
||||
entries: [
|
||||
MeasurementEntry(
|
||||
id: 'e1',
|
||||
categoryId: 'cat-bf',
|
||||
date: DateTime(2026, 1, 1),
|
||||
value: 20,
|
||||
notes: '',
|
||||
source: 'apple_health',
|
||||
externalId: 'bf-1',
|
||||
),
|
||||
],
|
||||
);
|
||||
when(measurements.getAllOnce()).thenAnswer((_) async => [existing]);
|
||||
stubReadings([
|
||||
HealthReading(
|
||||
type: HealthDataType.BODY_FAT_PERCENTAGE,
|
||||
value: 0.2,
|
||||
date: DateTime(2026, 1, 1),
|
||||
externalId: 'bf-1', // already imported
|
||||
),
|
||||
HealthReading(
|
||||
type: HealthDataType.BODY_FAT_PERCENTAGE,
|
||||
value: 0.22,
|
||||
date: DateTime(2026, 1, 3),
|
||||
externalId: 'bf-2', // new
|
||||
),
|
||||
]);
|
||||
|
||||
final count = await createNotifier().syncOnAppOpen();
|
||||
expect(count, 1);
|
||||
verifyNever(measurements.addLocalDriftCategory(any));
|
||||
|
||||
final entries = verify(
|
||||
measurements.addLocalDrift(captureAny),
|
||||
).captured.cast<MeasurementEntry>();
|
||||
expect(entries.single.externalId, 'bf-2');
|
||||
});
|
||||
|
||||
test('does nothing when sync is disabled', () async {
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(false);
|
||||
|
||||
final count = await createNotifier().syncOnAppOpen();
|
||||
expect(count, 0);
|
||||
verifyNever(
|
||||
health.read(
|
||||
types: anyNamed('types'),
|
||||
start: anyNamed('start'),
|
||||
end: anyNamed('end'),
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
// Mocks generated by Mockito 5.4.6 from annotations
|
||||
// in wger/test/features/health/providers/health_sync_test.dart.
|
||||
// Do not manually edit this file.
|
||||
|
||||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'dart:async' as _i4;
|
||||
|
||||
import 'package:health/health.dart' as _i5;
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:mockito/src/dummies.dart' as _i3;
|
||||
import 'package:wger/features/health/models/health_reading.dart' as _i6;
|
||||
import 'package:wger/features/health/providers/health_repository.dart' as _i2;
|
||||
import 'package:wger/features/measurements/models/measurement_category.dart' as _i8;
|
||||
import 'package:wger/features/measurements/models/measurement_entry.dart' as _i9;
|
||||
import 'package:wger/features/measurements/providers/measurement_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
// ignore_for_file: avoid_setters_without_getters
|
||||
// ignore_for_file: comment_references
|
||||
// ignore_for_file: deprecated_member_use
|
||||
// ignore_for_file: deprecated_member_use_from_same_package
|
||||
// ignore_for_file: implementation_imports
|
||||
// ignore_for_file: invalid_use_of_visible_for_testing_member
|
||||
// ignore_for_file: must_be_immutable
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
// ignore_for_file: unnecessary_parenthesis
|
||||
// ignore_for_file: camel_case_types
|
||||
// ignore_for_file: subtype_of_sealed_class
|
||||
// ignore_for_file: invalid_use_of_internal_member
|
||||
|
||||
/// A class which mocks [HealthRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockHealthRepository extends _i1.Mock implements _i2.HealthRepository {
|
||||
MockHealthRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
String get sourceName =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.getter(#sourceName),
|
||||
returnValue: _i3.dummyValue<String>(
|
||||
this,
|
||||
Invocation.getter(#sourceName),
|
||||
),
|
||||
)
|
||||
as String);
|
||||
|
||||
@override
|
||||
_i4.Future<bool> isAvailable() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#isAvailable, []),
|
||||
returnValue: _i4.Future<bool>.value(false),
|
||||
)
|
||||
as _i4.Future<bool>);
|
||||
|
||||
@override
|
||||
_i4.Future<bool> ensureAuthorized(List<_i5.HealthDataType>? types) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#ensureAuthorized, [types]),
|
||||
returnValue: _i4.Future<bool>.value(false),
|
||||
)
|
||||
as _i4.Future<bool>);
|
||||
|
||||
@override
|
||||
_i4.Future<List<_i6.HealthReading>> read({
|
||||
required List<_i5.HealthDataType>? types,
|
||||
required DateTime? start,
|
||||
required DateTime? end,
|
||||
}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#read, [], {
|
||||
#types: types,
|
||||
#start: start,
|
||||
#end: end,
|
||||
}),
|
||||
returnValue: _i4.Future<List<_i6.HealthReading>>.value(
|
||||
<_i6.HealthReading>[],
|
||||
),
|
||||
)
|
||||
as _i4.Future<List<_i6.HealthReading>>);
|
||||
}
|
||||
|
||||
/// A class which mocks [MeasurementRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockMeasurementRepository extends _i1.Mock implements _i7.MeasurementRepository {
|
||||
MockMeasurementRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i4.Stream<List<_i8.MeasurementCategory>> watchAll() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAll, []),
|
||||
returnValue: _i4.Stream<List<_i8.MeasurementCategory>>.empty(),
|
||||
)
|
||||
as _i4.Stream<List<_i8.MeasurementCategory>>);
|
||||
|
||||
@override
|
||||
_i4.Stream<_i8.MeasurementCategory?> watchLocalDriftCategoryById(
|
||||
String? id,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchLocalDriftCategoryById, [id]),
|
||||
returnValue: _i4.Stream<_i8.MeasurementCategory?>.empty(),
|
||||
)
|
||||
as _i4.Stream<_i8.MeasurementCategory?>);
|
||||
|
||||
@override
|
||||
_i4.Future<List<_i8.MeasurementCategory>> getAllOnce() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#getAllOnce, []),
|
||||
returnValue: _i4.Future<List<_i8.MeasurementCategory>>.value(
|
||||
<_i8.MeasurementCategory>[],
|
||||
),
|
||||
)
|
||||
as _i4.Future<List<_i8.MeasurementCategory>>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> deleteLocalDrift(String? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDrift, [id]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> updateLocalDrift(_i9.MeasurementEntry? entry) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#updateLocalDrift, [entry]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> addLocalDrift(_i9.MeasurementEntry? entry) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addLocalDrift, [entry]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> deleteLocalDriftCategory(String? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDriftCategory, [id]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> updateLocalDriftCategory(
|
||||
_i8.MeasurementCategory? category,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#updateLocalDriftCategory, [category]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
|
||||
@override
|
||||
_i4.Future<void> addLocalDriftCategory(_i8.MeasurementCategory? category) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addLocalDriftCategory, [category]),
|
||||
returnValue: _i4.Future<void>.value(),
|
||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
}
|
||||
Reference in new issue
Block a user