mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 01:18:35 +01:00
Notify the user with a snackbar when health permissions are denied
This commit is contained in:
@@ -71,10 +71,18 @@ class _HealthSyncSettingsTileState extends ConsumerState<HealthSyncSettingsTile>
|
||||
: (enabled) async {
|
||||
final notifier = ref.read(healthSyncProvider.notifier);
|
||||
if (enabled) {
|
||||
// null means the platform permissions were not granted
|
||||
final count = await notifier.enableSync();
|
||||
// Imported entries land in the local Drift DB and surface through
|
||||
// the measurement stream automatically, so no manual refresh is needed.
|
||||
if (context.mounted && count > 0) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
if (count == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(i18n.healthSyncPermissionDenied)),
|
||||
);
|
||||
} else if (count > 0) {
|
||||
// Imported entries land in the local Drift DB and surface through
|
||||
// the measurement stream automatically, so no manual refresh is needed.
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(i18n.healthSyncSuccess(count))),
|
||||
);
|
||||
|
||||
@@ -85,10 +85,13 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
|
||||
Future<bool> isAvailable() => _health.isAvailable();
|
||||
|
||||
/// Requests permissions, persists the preference, and runs an initial import.
|
||||
Future<int> enableSync() async {
|
||||
///
|
||||
/// Returns the number of imported entries, or `null` when the platform
|
||||
/// permissions were not granted (sync stays disabled).
|
||||
Future<int?> enableSync() async {
|
||||
_logger.info('Enabling health sync');
|
||||
if (!await _health.ensureAuthorized(_types)) {
|
||||
return 0;
|
||||
return null;
|
||||
}
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(true);
|
||||
state = state.copyWith(isEnabled: true);
|
||||
|
||||
@@ -1512,6 +1512,10 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"healthSyncPermissionDenied": "Access to health data was not granted",
|
||||
"@healthSyncPermissionDenied": {
|
||||
"description": "Snackbar shown when enabling health sync fails because the platform permissions were denied"
|
||||
},
|
||||
"health": "Health",
|
||||
"@health": {
|
||||
"description": "Section header for health-related settings"
|
||||
|
||||
86
test/features/account/widgets/settings/health_sync_test.dart
Normal file
86
test/features/account/widgets/settings/health_sync_test.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wger/features/account/widgets/settings/health_sync.dart';
|
||||
import 'package:wger/features/health/providers/health_sync.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
|
||||
/// Reports the platform as available and returns a canned result from
|
||||
/// [enableSync] without touching the real health/preferences stack.
|
||||
class _FakeHealthSyncNotifier extends HealthSyncNotifier {
|
||||
final int? enableSyncResult;
|
||||
|
||||
_FakeHealthSyncNotifier(this.enableSyncResult);
|
||||
|
||||
@override
|
||||
HealthSyncState build() => const HealthSyncState();
|
||||
|
||||
@override
|
||||
Future<bool> isAvailable() async => true;
|
||||
|
||||
@override
|
||||
Future<int?> enableSync() async => enableSyncResult;
|
||||
}
|
||||
|
||||
void main() {
|
||||
Widget createTile(int? enableSyncResult) {
|
||||
return ProviderScope(
|
||||
overrides: [
|
||||
healthSyncProvider.overrideWith(() => _FakeHealthSyncNotifier(enableSyncResult)),
|
||||
],
|
||||
child: const MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(body: HealthSyncSettingsTile()),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('shows a snackbar when the permissions are denied', (tester) async {
|
||||
await tester.pumpWidget(createTile(null));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byType(SwitchListTile));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Access to health data was not granted'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows the import count after a successful sync', (tester) async {
|
||||
await tester.pumpWidget(createTile(5));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byType(SwitchListTile));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Imported 5 measurements from Health'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows no snackbar when nothing was imported', (tester) async {
|
||||
await tester.pumpWidget(createTile(0));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byType(SwitchListTile));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(SnackBar), findsNothing);
|
||||
});
|
||||
}
|
||||
@@ -35,6 +35,42 @@ import 'health_sync_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([HealthRepository, MeasurementRepository])
|
||||
void main() {
|
||||
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');
|
||||
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);
|
||||
}
|
||||
|
||||
group('HealthSyncState', () {
|
||||
test('default state has sync disabled', () {
|
||||
const state = HealthSyncState();
|
||||
@@ -52,43 +88,40 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('syncOnAppOpen', () {
|
||||
late MockHealthRepository health;
|
||||
late MockMeasurementRepository measurements;
|
||||
group('enableSync', () {
|
||||
test('returns null and stays disabled when permissions are denied', () async {
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(false);
|
||||
when(health.ensureAuthorized(any)).thenAnswer((_) async => false);
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(true);
|
||||
final count = await createNotifier().enableSync();
|
||||
|
||||
health = MockHealthRepository();
|
||||
measurements = MockMeasurementRepository();
|
||||
|
||||
when(health.ensureAuthorized(any)).thenAnswer((_) async => true);
|
||||
when(health.sourceName).thenReturn('apple');
|
||||
when(measurements.addLocalDrift(any)).thenAnswer((_) async {});
|
||||
when(measurements.addLocalDriftCategory(any)).thenAnswer((_) async {});
|
||||
expect(count, isNull);
|
||||
expect(await PreferenceHelper.instance.getHealthSyncEnabled(), isFalse);
|
||||
verifyNever(
|
||||
health.read(types: anyNamed('types'), start: anyNamed('start'), end: anyNamed('end')),
|
||||
);
|
||||
});
|
||||
|
||||
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'),
|
||||
test('persists the preference and runs an initial import', () async {
|
||||
await PreferenceHelper.instance.setHealthSyncEnabled(false);
|
||||
when(measurements.getAllOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
|
||||
stubReadings([
|
||||
HealthReading(
|
||||
type: HealthDataType.HEIGHT,
|
||||
value: 1.8,
|
||||
date: DateTime(2026, 1, 2),
|
||||
externalId: 'h-1',
|
||||
),
|
||||
).thenAnswer((_) async => readings);
|
||||
}
|
||||
]);
|
||||
|
||||
final count = await createNotifier().enableSync();
|
||||
|
||||
expect(count, 1);
|
||||
expect(await PreferenceHelper.instance.getHealthSyncEnabled(), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('syncOnAppOpen', () {
|
||||
test('imports enabled metrics into new categories with converted values', () async {
|
||||
when(measurements.getAllOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
|
||||
stubReadings([
|
||||
|
||||
Reference in New Issue
Block a user