Keep the measurement screens on screen while a range loads

This commit is contained in:
Roland Geider
2026-08-05 22:15:18 +02:00
parent 1fd6b505a8
commit 449beec791
3 changed files with 120 additions and 79 deletions

View File

@@ -53,6 +53,13 @@ class _MeasurementEntriesScreenState extends ConsumerState<MeasurementEntriesScr
/// that reads the entries, not only the span the chart draws
ChartRange _range = ChartRange.last3Months;
/// The category as it was last read.
///
/// Picking another range watches a different provider, which starts out
/// loading: without this the screen would drop everything it is showing for
/// a frame and come back, which is what the range switch used to look like.
MeasurementCategory? _lastCategory;
@override
void didChangeDependencies() {
super.didChangeDependencies();
@@ -70,91 +77,100 @@ class _MeasurementEntriesScreenState extends ConsumerState<MeasurementEntriesScr
measurementCategorySinceProvider(_categoryId, _range.readCutoff),
);
return categoryValue.when(
loading: () => const BoxedProgressIndicator(),
error: (error, _) => StreamErrorIndicator(error.toString()),
data: (category) {
// Category was deleted (locally or via PowerSync from another device).
// Leave this now-stale screen.
if (category == null) {
return objectGoneRedirect(context);
}
// Category was deleted (locally or via PowerSync from another device).
// Leave this now-stale screen. Asked of the value that just arrived, not
// of the cache, which still holds the category as it was.
if (categoryValue.hasValue && categoryValue.value == null) {
return objectGoneRedirect(context);
}
return Scaffold(
appBar: AppBar(
title: Text(category.displayName(context)),
actions: [
PopupMenuButton<MeasurementOptions>(
icon: const Icon(Icons.more_vert),
onSelected: (value) {
switch (value) {
case MeasurementOptions.edit:
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
MeasurementCategoryForm(category),
),
);
break;
final category = categoryValue.value ?? _lastCategory;
if (categoryValue.value != null) {
_lastCategory = categoryValue.value;
}
case MeasurementOptions.delete:
showConfirmDeleteDialog(
context,
itemName: category.displayName(context),
onConfirm: () =>
ref.read(measurementProvider.notifier).deleteCategory(category.id!),
// Exit the detail screen once the category is gone.
onDeleted: () => Navigator.of(context).pop(),
);
break;
}
},
itemBuilder: (context) {
return [
if (category.isEditable)
PopupMenuItem<MeasurementOptions>(
value: MeasurementOptions.edit,
child: Text(AppLocalizations.of(context).edit),
// The scaffold is built whether or not there is data: a bare indicator in
// its place is a screen without a background, i.e. a black flash
return Scaffold(
appBar: AppBar(
title: Text(category?.displayName(context) ?? ''),
actions: [
if (category != null)
PopupMenuButton<MeasurementOptions>(
icon: const Icon(Icons.more_vert),
onSelected: (value) {
switch (value) {
case MeasurementOptions.edit:
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
MeasurementCategoryForm(category),
),
);
break;
case MeasurementOptions.delete:
showConfirmDeleteDialog(
context,
itemName: category.displayName(context),
onConfirm: () =>
ref.read(measurementProvider.notifier).deleteCategory(category.id!),
// Exit the detail screen once the category is gone.
onDeleted: () => Navigator.of(context).pop(),
);
break;
}
},
itemBuilder: (context) {
return [
if (category.isEditable)
PopupMenuItem<MeasurementOptions>(
value: MeasurementOptions.delete,
child: Text(AppLocalizations.of(context).delete),
value: MeasurementOptions.edit,
child: Text(AppLocalizations.of(context).edit),
),
];
},
),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add, color: Colors.white),
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newEntry,
// A group holds no entries itself: one reading is a value per
// component, entered in one go
category.isGroup
? GroupMeasurementEntryForm(category)
: MeasurementEntryForm(_categoryId),
),
);
},
),
body: WidescreenWrapper(
child: SingleChildScrollView(
child: EntriesList(
category,
range: _range,
onRangeChanged: (range) => setState(() => _range = range),
),
PopupMenuItem<MeasurementOptions>(
value: MeasurementOptions.delete,
child: Text(AppLocalizations.of(context).delete),
),
];
},
),
],
),
floatingActionButton: category == null
? null
: FloatingActionButton(
child: const Icon(Icons.add, color: Colors.white),
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newEntry,
// A group holds no entries itself: one reading is a value
// per component, entered in one go
category.isGroup
? GroupMeasurementEntryForm(category)
: MeasurementEntryForm(_categoryId),
),
);
},
),
body: WidescreenWrapper(
child: switch ((category, categoryValue)) {
(final MeasurementCategory category, _) => SingleChildScrollView(
child: EntriesList(
category,
range: _range,
onRangeChanged: (range) => setState(() => _range = range),
),
),
);
},
(_, AsyncError(:final error)) => StreamErrorIndicator(error.toString()),
_ => const BoxedProgressIndicator(),
},
),
);
}
}

View File

@@ -23,6 +23,7 @@ import 'package:wger/core/wide_screen_wrapper.dart';
import 'package:wger/core/widgets/app_bar.dart';
import 'package:wger/core/widgets/progress_indicator.dart';
import 'package:wger/features/account/providers/user_profile_notifier.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/unit_conversion.dart';
import 'package:wger/features/measurements/providers/body_weight_provider.dart';
import 'package:wger/features/measurements/widgets/chart_range_selector.dart';
@@ -52,12 +53,21 @@ class _WeightScreenState extends ConsumerState<WeightScreen> {
/// that reads the entries, not only the span the chart draws
ChartRange _range = ChartRange.last3Months;
/// The category as it was last read, kept so picking another range does not
/// replace the chart and the list with a spinner, see
/// MeasurementEntriesScreen
MeasurementCategory? _lastCategory;
@override
Widget build(BuildContext context) {
final i18n = AppLocalizations.of(context);
// New entries need the official category, which the server creates and the
// initial sync delivers; hide the FAB until it is there.
final category = ref.watch(bodyWeightCategorySinceProvider(_range.readCutoff)).value;
final read = ref.watch(bodyWeightCategorySinceProvider(_range.readCutoff)).value;
if (read != null) {
_lastCategory = read;
}
final category = read ?? _lastCategory;
// The profile decides the display unit, so nothing can be drawn without it
final profile = ref.watch(userProfileProvider).value;

View File

@@ -87,6 +87,21 @@ void main() {
expect(find.text('30 %'), findsNWidgets(1));
});
testWidgets('switching the range keeps the screen it already drew', (tester) async {
await tester.pumpWidget(createEntriesScreen());
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
// The new range watches a different provider, which starts out loading:
// this is the frame in which the screen used to drop everything it had
await tester.tap(find.text('1 month'));
await tester.pump();
expect(find.text('Body fat'), findsOneWidget);
expect(find.text('30 %'), findsNWidgets(1));
expect(find.byType(CircularProgressIndicator), findsNothing);
});
testWidgets('Tests the localization of dates - EN', (WidgetTester tester) async {
await tester.pumpWidget(createEntriesScreen());
await tester.tap(find.byType(TextButton));