Update the online connectivity gates

Some of these models can now be edited completely locally, others only partly
(e.g. the gallery entries can only be created online)
This commit is contained in:
Roland Geider
2026-05-20 14:14:00 +02:00
parent d5acd004c4
commit cbf6884a30
17 changed files with 417 additions and 214 deletions

View File

@@ -21,7 +21,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg_icons/flutter_svg_icons.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/nutrition/nutritional_plan.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/providers/nutrition_notifier.dart';
import 'package:wger/screens/form_screen.dart';
import 'package:wger/screens/log_meals_screen.dart';
@@ -55,7 +54,6 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
@override
Widget build(BuildContext context) {
const appBarForeground = Colors.white;
final isOnline = ref.watch(networkStatusProvider);
// Resolve the freshest plan from state so meal/diary edits reflect
// immediately. Falls back to the route argument if state hasn't streamed
// yet (e.g. cold open, deep link).
@@ -70,20 +68,17 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
FloatingActionButton(
heroTag: null,
tooltip: AppLocalizations.of(context).logIngredient,
backgroundColor: isOnline ? null : Colors.grey,
onPressed: isOnline
? () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).logIngredient,
getIngredientLogForm(_nutritionalPlan),
hasListView: true,
),
);
}
: null,
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).logIngredient,
getIngredientLogForm(_nutritionalPlan),
hasListView: true,
),
);
},
child: const SvgIcon(
icon: SvgIconData('assets/icons/ingredient-diary.svg'),
color: Colors.white,
@@ -93,15 +88,12 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
FloatingActionButton(
heroTag: null,
tooltip: AppLocalizations.of(context).logMeal,
backgroundColor: isOnline ? null : Colors.grey,
onPressed: isOnline
? () {
Navigator.of(context).pushNamed(
LogMealsScreen.routeName,
arguments: _nutritionalPlan,
);
}
: null,
onPressed: () {
Navigator.of(context).pushNamed(
LogMealsScreen.routeName,
arguments: _nutritionalPlan,
);
},
child: const SvgIcon(
icon: SvgIconData('assets/icons/meal-diary.svg'),
color: Colors.white,
@@ -121,18 +113,16 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
icon: const SvgIcon(
icon: SvgIconData('assets/icons/meal-add.svg'),
),
onPressed: isOnline
? () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).addMeal,
MealForm(_nutritionalPlan.id!),
),
);
}
: null,
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).addMeal,
MealForm(_nutritionalPlan.id!),
),
);
},
),
PopupMenuButton<NutritionalPlanOptions>(
icon: const Icon(Icons.more_vert, color: appBarForeground),
@@ -159,7 +149,6 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
return [
PopupMenuItem<NutritionalPlanOptions>(
value: NutritionalPlanOptions.edit,
enabled: isOnline,
child: ListTile(
leading: const Icon(Icons.edit),
title: Text(AppLocalizations.of(context).edit),
@@ -168,7 +157,6 @@ class _NutritionalPlanScreenState extends ConsumerState<NutritionalPlanScreen> {
const PopupMenuDivider(),
PopupMenuItem<NutritionalPlanOptions>(
value: NutritionalPlanOptions.delete,
enabled: isOnline,
child: ListTile(
leading: const Icon(Icons.delete),
title: Text(AppLocalizations.of(context).delete),

View File

@@ -20,7 +20,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wger/core/wide_screen_wrapper.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/screens/form_screen.dart';
import 'package:wger/widgets/core/app_bar.dart';
import 'package:wger/widgets/nutrition/forms.dart';
@@ -33,25 +32,20 @@ class NutritionalPlansScreen extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final isOnline = ref.watch(networkStatusProvider);
return Scaffold(
appBar: EmptyAppBar(AppLocalizations.of(context).nutritionalPlans),
floatingActionButton: FloatingActionButton(
backgroundColor: isOnline ? null : Colors.grey,
onPressed: isOnline
? () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newNutritionalPlan,
hasListView: true,
PlanForm(),
),
);
}
: null,
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newNutritionalPlan,
hasListView: true,
PlanForm(),
),
);
},
child: const Icon(Icons.add, color: Colors.white),
),
body: const WidescreenWrapper(child: NutritionalPlansList()),

View File

@@ -103,6 +103,10 @@ class _ImageFormState extends ConsumerState<ImageForm> {
Widget build(BuildContext context) {
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
final isOnline = ref.watch(networkStatusProvider);
// Creating an image or replacing its photo is a binary REST upload and
// needs connectivity. A metadata-only edit syncs through PowerSync and
// works offline.
final requiresUpload = widget._image.id == null || _file != null;
if (dateController.text.isEmpty) {
dateController.text = dateFormat.format(widget._image.date);
@@ -199,9 +203,9 @@ class _ImageFormState extends ConsumerState<ImageForm> {
),
ElevatedButton(
key: const Key(SUBMIT_BUTTON_KEY_NAME),
child: Text(AppLocalizations.of(context).save),
onPressed: isOnline
? () async {
onPressed: (requiresUpload && !isOnline)
? null
: () async {
// Validate and save
final isValid = _form.currentState!.validate();
if (!isValid) {
@@ -216,8 +220,8 @@ class _ImageFormState extends ConsumerState<ImageForm> {
notifier.editImage(widget._image, _file);
}
Navigator.of(context).pop();
}
: null,
},
child: Text(AppLocalizations.of(context).save),
),
],
),

View File

@@ -24,7 +24,6 @@ import 'package:wger/helpers/platform.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/gallery/image.dart';
import 'package:wger/providers/gallery_notifier.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/screens/form_screen.dart';
import 'package:wger/widgets/core/text_prompt.dart';
import 'package:wger/widgets/core/wger_image.dart';
@@ -79,7 +78,6 @@ class ImageDetail extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final isOnline = ref.watch(networkStatusProvider);
return Container(
key: Key('image-${image.id!}-detail'),
padding: const EdgeInsets.all(10),
@@ -104,29 +102,25 @@ class ImageDetail extends ConsumerWidget {
children: [
IconButton(
icon: const Icon(Icons.delete),
onPressed: isOnline
? () {
ref.read(galleryProvider.notifier).deleteImage(image);
Navigator.of(context).pop();
}
: null,
onPressed: () {
ref.read(galleryProvider.notifier).deleteImage(image);
Navigator.of(context).pop();
},
),
if (!isDesktop)
IconButton(
icon: const Icon(Icons.edit),
onPressed: isOnline
? () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
ImageForm(image),
hasListView: true,
),
);
}
: null,
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
ImageForm(image),
hasListView: true,
),
);
},
),
],
),

View File

@@ -23,7 +23,6 @@ import 'package:wger/helpers/consts.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/nutrition/meal.dart';
import 'package:wger/models/nutrition/meal_item.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/providers/nutrition_notifier.dart';
import 'package:wger/screens/form_screen.dart';
import 'package:wger/screens/log_meal_screen.dart';
@@ -87,7 +86,6 @@ class _MealWidgetState extends ConsumerState<MealWidget> {
@override
Widget build(BuildContext context) {
final isOnline = ref.watch(networkStatusProvider);
return Container(
padding: const EdgeInsets.all(3),
child: Card(
@@ -112,19 +110,17 @@ class _MealWidgetState extends ConsumerState<MealWidget> {
TextButton.icon(
icon: const Icon(Icons.add),
label: Text(AppLocalizations.of(context).addIngredient),
onPressed: isOnline
? () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).addIngredient,
getMealItemForm(widget._meal, widget._recentMealItems),
hasListView: true,
),
);
}
: null,
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).addIngredient,
getMealItemForm(widget._meal, widget._recentMealItems),
hasListView: true,
),
);
},
),
TextButton.icon(
label: Text(AppLocalizations.of(context).edit),

View File

@@ -22,7 +22,6 @@ import 'package:intl/intl.dart';
import 'package:wger/helpers/measurements.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/body_weight_notifier.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/providers/nutrition_notifier.dart';
import 'package:wger/providers/user_profile_notifier.dart';
import 'package:wger/screens/nutritional_plan_screen.dart';
@@ -100,7 +99,6 @@ class NutritionalPlansList extends riverpod.ConsumerWidget {
Widget build(BuildContext context, riverpod.WidgetRef ref) {
final plansAsync = ref.watch(nutritionProvider);
final notifier = ref.read(nutritionProvider.notifier);
final isOnline = ref.watch(networkStatusProvider);
return AsyncValueWidget<NutritionState>(
value: plansAsync,
@@ -153,56 +151,54 @@ class NutritionalPlansList extends riverpod.ConsumerWidget {
IconButton(
icon: const Icon(Icons.delete),
tooltip: AppLocalizations.of(context).delete,
onPressed: isOnline
? () async {
// Delete the plan from DB
await showDialog(
context: context,
builder: (BuildContext contextDialog) {
return AlertDialog(
content: Text(
AppLocalizations.of(
context,
).confirmDelete(currentPlan.description),
onPressed: () async {
// Delete the plan from DB
await showDialog(
context: context,
builder: (BuildContext contextDialog) {
return AlertDialog(
content: Text(
AppLocalizations.of(
context,
).confirmDelete(currentPlan.description),
),
actions: [
TextButton(
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
),
onPressed: () => Navigator.of(contextDialog).pop(),
),
TextButton(
child: Text(
AppLocalizations.of(context).delete,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
actions: [
TextButton(
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
),
onPressed: () => Navigator.of(contextDialog).pop(),
),
TextButton(
child: Text(
AppLocalizations.of(context).delete,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
onPressed: () {
// Confirmed, delete the plan
notifier.deletePlan(currentPlan.id!);
),
onPressed: () {
// Confirmed, delete the plan
notifier.deletePlan(currentPlan.id!);
// Close the popup
Navigator.of(contextDialog).pop();
// Close the popup
Navigator.of(contextDialog).pop();
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
},
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
],
);
},
);
}
: null,
);
},
),
],
);
},
);
},
),
],
),

View File

@@ -108,7 +108,6 @@ class RoutineDetailAppBar extends ConsumerWidget implements PreferredSizeWidget
),
PopupMenuItem<_RoutineDetailBarOptions>(
value: _RoutineDetailBarOptions.delete,
enabled: isOnline,
child: Text(i18n.delete),
),
];

View File

@@ -219,9 +219,11 @@ class _RoutineFormState extends ConsumerState<RoutineForm> {
},
),
const SizedBox(height: 5),
// Creating a routine needs the server to assign an integer PK; editing an
// existing one syncs through PowerSync and works offline.
ElevatedButton(
key: const Key(SUBMIT_BUTTON_KEY_NAME),
onPressed: isSaving || !isOnline
onPressed: isSaving || (widget._routine.id == null && !isOnline)
? null
: () async {
// Validate and save

View File

@@ -20,7 +20,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/providers/routines_notifier.dart';
import 'package:wger/screens/routine_screen.dart';
import 'package:wger/widgets/core/async_value_widget.dart';
@@ -38,7 +37,6 @@ class _RoutinesListState extends ConsumerState<RoutinesList> {
@override
Widget build(BuildContext context) {
final isOnline = ref.watch(networkStatusProvider);
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
final routineProvider = ref.read(routinesRiverpodProvider.notifier);
final routinesAsync = ref.watch(routinesRiverpodProvider);
@@ -96,58 +94,56 @@ class _RoutinesListState extends ConsumerState<RoutinesList> {
IconButton(
icon: const Icon(Icons.delete),
tooltip: AppLocalizations.of(context).delete,
onPressed: isOnline
? () async {
// Delete workout from DB
await showDialog(
context: context,
builder: (BuildContext contextDialog) {
return AlertDialog(
content: Text(
AppLocalizations.of(
context,
).confirmDelete(currentRoutine.name),
onPressed: () async {
// Delete workout from DB
await showDialog(
context: context,
builder: (BuildContext contextDialog) {
return AlertDialog(
content: Text(
AppLocalizations.of(
context,
).confirmDelete(currentRoutine.name),
),
actions: [
TextButton(
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
),
onPressed: () => Navigator.of(contextDialog).pop(),
),
TextButton(
child: Text(
AppLocalizations.of(context).delete,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
actions: [
TextButton(
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
),
onPressed: () => Navigator.of(contextDialog).pop(),
),
TextButton(
child: Text(
AppLocalizations.of(context).delete,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
onPressed: () {
// Confirmed, delete the workout
routineProvider.deleteRoutine(currentRoutine.id!);
),
onPressed: () {
// Confirmed, delete the workout
routineProvider.deleteRoutine(currentRoutine.id!);
// Close the popup
Navigator.of(contextDialog).pop();
// Close the popup
Navigator.of(contextDialog).pop();
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(
context,
).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
},
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(
context,
).successfullyDeleted,
textAlign: TextAlign.center,
),
),
],
);
},
);
}
: null,
);
},
),
],
);
},
);
},
),
],
),

View File

@@ -26,6 +26,7 @@ import 'package:wger/helpers/consts.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/gallery/image.dart';
import 'package:wger/providers/gallery_repository.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/widgets/gallery/forms.dart';
import '../../test_data/gallery.dart';
@@ -43,9 +44,10 @@ void main() {
when(mockGalleryRepository.editLocalDrift(any)).thenAnswer((_) async {});
});
Widget createScreen({useImage = true, locale = 'en'}) {
Widget createScreen({useImage = true, locale = 'en', bool isOnline = true}) {
return ProviderScope(
overrides: [
networkStatusProvider.overrideWithValue(isOnline),
galleryRepositoryProvider.overrideWithValue(mockGalleryRepository),
],
child: MaterialApp(
@@ -85,4 +87,33 @@ void main() {
expect(find.text('Please select an image'), findsOneWidget);
expect(find.byIcon(Icons.photo_camera), findsOneWidget);
});
testWidgets('Submit stays enabled offline for a metadata-only edit', (
WidgetTester tester,
) async {
// Editing an existing image without replacing the photo syncs through
// PowerSync, so it works offline.
await mockNetworkImagesFor(
() => tester.pumpWidget(createScreen(useImage: true, isOnline: false)),
);
await tester.pumpAndSettle();
final button = tester.widget<ElevatedButton>(
find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)),
);
expect(button.onPressed, isNotNull);
});
testWidgets('Submit is disabled offline for a new image', (WidgetTester tester) async {
// Creating an image is a binary REST upload and needs connectivity.
await mockNetworkImagesFor(
() => tester.pumpWidget(createScreen(useImage: false, isOnline: false)),
);
await tester.pumpAndSettle();
final button = tester.widget<ElevatedButton>(
find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)),
);
expect(button.onPressed, isNull);
});
}

View File

@@ -25,6 +25,7 @@ import 'package:mockito/mockito.dart';
import 'package:network_image_mock/network_image_mock.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/gallery_repository.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/widgets/gallery/overview.dart';
import '../../test_data/gallery.dart';
@@ -55,9 +56,10 @@ void main() {
);
}
Widget renderDetail({locale = 'en'}) {
Widget renderDetail({locale = 'en', bool isOnline = true}) {
return ProviderScope(
overrides: [
networkStatusProvider.overrideWithValue(isOnline),
galleryRepositoryProvider.overrideWithValue(mockGalleryRepository),
],
child: MaterialApp(
@@ -92,4 +94,15 @@ void main() {
expect(find.text('30.5.2021'), findsOneWidget);
});
testWidgets('Delete stays enabled when offline', (WidgetTester tester) async {
// Image deletion syncs through PowerSync, so it works offline.
await mockNetworkImagesFor(() => tester.pumpWidget(renderDetail(isOnline: false)));
await tester.pumpAndSettle();
final deleteButton = tester.widget<IconButton>(
find.widgetWithIcon(IconButton, Icons.delete),
);
expect(deleteButton.onPressed, isNotNull);
});
}

View File

@@ -0,0 +1,71 @@
/*
* 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/l10n/generated/app_localizations.dart';
import 'package:wger/models/nutrition/meal.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/widgets/nutrition/meal.dart';
void main() {
Meal buildMeal() => Meal(
id: 'aa000000-0000-4000-8000-000000000001',
plan: 'bb000000-0000-4000-8000-000000000001',
name: 'Breakfast',
time: const TimeOfDay(hour: 8, minute: 0),
);
Widget renderMeal({bool isOnline = true}) {
return ProviderScope(
// The meal widget does not gate its actions on connectivity. Pinning the
// network status to offline makes a re-introduced connectivity gate fail
// this test.
overrides: [networkStatusProvider.overrideWithValue(isOnline)],
child: MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: SingleChildScrollView(
child: MealWidget(buildMeal(), const [], false, false),
),
),
),
);
}
testWidgets('Add-ingredient button stays enabled when offline', (
WidgetTester tester,
) async {
await tester.pumpWidget(renderMeal(isOnline: false));
await tester.pumpAndSettle();
// Reveal the meal action buttons by entering editing mode.
await tester.tap(find.byIcon(Icons.edit));
await tester.pumpAndSettle();
final addButton = tester.widget<TextButton>(
find.ancestor(
of: find.byIcon(Icons.add),
matching: find.byType(TextButton),
),
);
expect(addButton.onPressed, isNotNull);
});
}

View File

@@ -29,6 +29,7 @@ import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/nutrition/ingredient.dart';
import 'package:wger/providers/body_weight_repository.dart';
import 'package:wger/providers/ingredient_repository.dart';
import 'package:wger/providers/network_provider.dart';
import 'package:wger/providers/nutrition_repository.dart';
import 'package:wger/screens/nutritional_plan_screen.dart';
@@ -82,9 +83,10 @@ void main() {
when(mockIngredientRepo.watchById(any)).thenAnswer((_) => Stream.value(fetchedIngredient));
});
ProviderContainer makeContainer() {
ProviderContainer makeContainer({bool isOnline = true}) {
final container = ProviderContainer(
overrides: [
networkStatusProvider.overrideWithValue(isOnline),
bodyWeightRepositoryProvider.overrideWithValue(mockBodyWeightRepository),
nutritionRepositoryProvider.overrideWithValue(mockNutritionRepo),
ingredientRepositoryProvider.overrideWithValue(mockIngredientRepo),
@@ -196,6 +198,40 @@ void main() {
tags: ['golden'],
);
testWidgets('Logging and meal actions stay enabled when offline', (tester) async {
tester.view.physicalSize = const Size(500, 1000);
tester.view.devicePixelRatio = 1.0;
addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});
final container = makeContainer(isOnline: false);
await tester.pumpWidget(createNutritionalPlan(container: container));
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
// Diary logging and meal management all sync through PowerSync.
final fabs = tester.widgetList<FloatingActionButton>(
find.byType(FloatingActionButton),
);
expect(fabs, isNotEmpty);
for (final fab in fabs) {
expect(fab.onPressed, isNotNull);
}
// The plan edit/delete actions in the app bar menu.
await tester.tap(find.byIcon(Icons.more_vert));
await tester.pumpAndSettle();
final menuItems = tester.widgetList<PopupMenuItem<NutritionalPlanOptions>>(
find.byType(PopupMenuItem<NutritionalPlanOptions>),
);
expect(menuItems, hasLength(2));
for (final item in menuItems) {
expect(item.enabled, isTrue);
}
});
testWidgets('Tests the localization of times - EN', (WidgetTester tester) async {
final container = makeContainer();
await tester.pumpWidget(createNutritionalPlan(container: container));

View File

@@ -150,19 +150,36 @@ void main() {
expect(find.byType(PlanForm), findsOneWidget);
});
testWidgets('Add-plan FAB is disabled when offline', (WidgetTester tester) async {
// Plan creation still goes through REST, so the FAB must not let users
// navigate into a form they can't submit.
testWidgets('Add-plan FAB stays enabled when offline', (WidgetTester tester) async {
// Plan creation goes through PowerSync, so it works offline.
await tester.pumpWidget(createHomeScreen(isOnline: false));
await tester.pumpAndSettle();
final fab = tester.widget<FloatingActionButton>(find.byType(FloatingActionButton));
expect(fab.onPressed, isNull);
expect(fab.onPressed, isNotNull);
// Tapping must not push the form route.
await tester.tap(find.byType(FloatingActionButton), warnIfMissed: false);
// Tapping opens the form.
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
expect(find.byType(PlanForm), findsNothing);
expect(find.byType(PlanForm), findsOneWidget);
});
testWidgets('Delete button stays enabled when offline', (WidgetTester tester) async {
// Plan deletion syncs through PowerSync, so it works offline.
await tester.pumpWidget(createHomeScreen(isOnline: false));
await tester.pumpAndSettle();
final deleteButton = tester.widget<IconButton>(
find.widgetWithIcon(IconButton, Icons.delete).first,
);
expect(deleteButton.onPressed, isNotNull);
await tester.tap(find.byIcon(Icons.delete).first);
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
await tester.tap(find.text('Delete'));
await tester.pumpAndSettle();
verify(mockNutritionRepo.deleteLocalDrift(any)).called(1);
});
testWidgets('Tests the localization of dates - EN', (WidgetTester tester) async {

View File

@@ -253,18 +253,35 @@ void main() {
expect(find.text('The name is not valid'), findsOneWidget, reason: 'Error message is shown');
});
testWidgets('Submit button is disabled when offline', (WidgetTester tester) async {
testWidgets('Submit stays enabled offline when editing a routine', (WidgetTester tester) async {
// Editing a routine syncs through PowerSync and works offline.
await tester.pumpWidget(renderWidget(existingRoutine, isOnline: false));
await tester.pumpAndSettle();
final button = tester.widget<ElevatedButton>(
find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)),
);
expect(button.onPressed, isNotNull);
await tester.enterText(find.byKey(const Key('field-name')), 'New name');
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
await tester.pump();
verify(mockRoutinesRepository.editLocalDrift(any));
});
testWidgets('Submit is disabled offline when creating a routine', (WidgetTester tester) async {
// Creating a routine needs the server to assign the integer PK.
await tester.pumpWidget(renderWidget(newRoutine, isOnline: false));
await tester.pumpAndSettle();
final button = tester.widget<ElevatedButton>(
find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)),
);
expect(button.onPressed, isNull);
// And tapping it must not trigger the edit request.
// And tapping it must not trigger the create request.
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)), warnIfMissed: false);
await tester.pump();
verifyNever(mockRoutinesRepository.editLocalDrift(any));
verifyNever(mockRoutinesRepository.addRoutineServer(any));
});
}

View File

@@ -38,12 +38,12 @@ class _StubRoutinesRiverpod extends RoutinesRiverpod {
}
void main() {
Widget renderWidget({locale = 'en'}) {
Widget renderWidget({locale = 'en', bool isOnline = true}) {
final key = GlobalKey<NavigatorState>();
final container = ProviderContainer.test(
overrides: [
networkStatusProvider.overrideWithValue(true),
networkStatusProvider.overrideWithValue(isOnline),
routinesRiverpodProvider.overrideWith(
() => _StubRoutinesRiverpod([getTestRoutine()]),
),
@@ -101,4 +101,45 @@ void main() {
},
tags: ['golden'],
);
testWidgets('App bar menu: delete works offline, edit needs online', (
WidgetTester tester,
) async {
tester.view.physicalSize = const Size(500, 1000);
tester.view.devicePixelRatio = 1.0;
addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});
await tester.pumpWidget(renderWidget(isOnline: false));
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
// Open the routine detail app bar menu.
await tester.tap(
find.descendant(
of: find.byType(AppBar),
matching: find.byWidgetPredicate((w) => w is PopupMenuButton),
),
);
await tester.pumpAndSettle();
// itemBuilder order: workout logs, edit, delete.
final items = tester
.widgetList(find.byWidgetPredicate((w) => w is PopupMenuItem))
.cast<PopupMenuItem>()
.toList();
expect(items, hasLength(3));
expect(
items[1].enabled,
isFalse,
reason: 'Edit opens the structure editor (REST), needs connectivity',
);
expect(
items[2].enabled,
isTrue,
reason: 'Delete syncs through PowerSync and works offline',
);
});
}

View File

@@ -115,14 +115,22 @@ void main() {
await tester.pumpAndSettle();
});
testWidgets('Handle offline status', (WidgetTester tester) async {
testWidgets('Delete button stays enabled when offline', (WidgetTester tester) async {
// Routine deletion syncs through PowerSync, so it must work offline.
await tester.pumpWidget(renderWidget(isOnline: false));
await tester.pumpAndSettle();
final deleteButton = tester.widget<IconButton>(
find.widgetWithIcon(IconButton, Icons.delete).first,
);
expect(deleteButton.onPressed, isNull);
expect(deleteButton.onPressed, isNotNull);
// The confirmation dialog opens and the delete flow runs through.
await tester.tap(find.byIcon(Icons.delete).first);
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
await tester.tap(find.text('Delete'));
await tester.pumpAndSettle();
});
testWidgets('Test the form on the workout plan screen', (WidgetTester tester) async {