From 13998ec3c0b38a2401e4eb46068323ab785be9d3 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 25 Jan 2025 20:07:19 +0100 Subject: [PATCH] Don't load the routine every time the log page are accessed --- lib/models/workouts/base_config.dart | 1 - lib/screens/routine_logs_screen.dart | 21 ++------------------- lib/widgets/routines/routines_list.dart | 16 ++++++++-------- lib/widgets/routines/workout_logs.dart | 2 +- test/workout/routine_logs_screen_test.dart | 9 +++++---- test/workout/slot_entry_model_test.dart | 14 ++------------ 6 files changed, 18 insertions(+), 45 deletions(-) diff --git a/lib/models/workouts/base_config.dart b/lib/models/workouts/base_config.dart index 90b6ce00..7807b443 100644 --- a/lib/models/workouts/base_config.dart +++ b/lib/models/workouts/base_config.dart @@ -58,7 +58,6 @@ class BaseConfig { required this.slotEntryId, required this.iteration, this.repeat = false, - // required this.trigger, required this.value, this.operation = 'r', this.step = 'abs', diff --git a/lib/screens/routine_logs_screen.dart b/lib/screens/routine_logs_screen.dart index 8d2dde2c..76379493 100644 --- a/lib/screens/routine_logs_screen.dart +++ b/lib/screens/routine_logs_screen.dart @@ -28,31 +28,14 @@ class WorkoutLogsScreen extends StatelessWidget { static const routeName = '/workout-logs'; - Future _loadFullWorkout(BuildContext context, int routineId) { - return Provider.of(context, listen: false).fetchAndSetRoutineFull(routineId); - } - @override Widget build(BuildContext context) { final routine = ModalRoute.of(context)!.settings.arguments as Routine; return Scaffold( appBar: RoutineDetailAppBar(routine), - body: FutureBuilder( - future: _loadFullWorkout(context, routine.id!), - builder: (context, AsyncSnapshot snapshot) => ListView( - children: [ - if (snapshot.connectionState == ConnectionState.waiting) - const SizedBox( - height: 200, - child: Center(child: CircularProgressIndicator()), - ) - else - Consumer( - builder: (context, value, child) => WorkoutLogs(routine), - ), - ], - ), + body: Consumer( + builder: (context, value, child) => WorkoutLogs(routine), ), ); } diff --git a/lib/widgets/routines/routines_list.dart b/lib/widgets/routines/routines_list.dart index 508ceb42..cee4f9b6 100644 --- a/lib/widgets/routines/routines_list.dart +++ b/lib/widgets/routines/routines_list.dart @@ -39,24 +39,24 @@ class RoutinesList extends StatelessWidget { padding: const EdgeInsets.all(10.0), itemCount: _routineProvider.items.length, itemBuilder: (context, index) { - final currentWorkout = _routineProvider.items[index]; + final currentRoutine = _routineProvider.items[index]; return Card( child: ListTile( onTap: () async { - _routineProvider.setCurrentPlan(currentWorkout.id!); + _routineProvider.setCurrentPlan(currentRoutine.id!); final routine = - await _routineProvider.fetchAndSetRoutineFull(currentWorkout.id!); + await _routineProvider.fetchAndSetRoutineFull(currentRoutine.id!); Navigator.of(context).pushNamed( RoutineScreen.routeName, arguments: routine, ); }, - title: Text(currentWorkout.name), + title: Text(currentRoutine.name), subtitle: Text( - '${DateFormat.yMd(Localizations.localeOf(context).languageCode).format(currentWorkout.start)}' - ' - ${DateFormat.yMd(Localizations.localeOf(context).languageCode).format(currentWorkout.end)}', + '${DateFormat.yMd(Localizations.localeOf(context).languageCode).format(currentRoutine.start)}' + ' - ${DateFormat.yMd(Localizations.localeOf(context).languageCode).format(currentRoutine.end)}', ), trailing: Row(mainAxisSize: MainAxisSize.min, children: [ const VerticalDivider(), @@ -70,7 +70,7 @@ class RoutinesList extends StatelessWidget { builder: (BuildContext contextDialog) { return AlertDialog( content: Text( - AppLocalizations.of(context).confirmDelete(currentWorkout.name), + AppLocalizations.of(context).confirmDelete(currentRoutine.name), ), actions: [ TextButton( @@ -91,7 +91,7 @@ class RoutinesList extends StatelessWidget { Provider.of( context, listen: false, - ).deleteRoutine(currentWorkout.id!); + ).deleteRoutine(currentRoutine.id!); // Close the popup Navigator.of(contextDialog).pop(); diff --git a/lib/widgets/routines/workout_logs.dart b/lib/widgets/routines/workout_logs.dart index 3be4e395..0fd63ca8 100644 --- a/lib/widgets/routines/workout_logs.dart +++ b/lib/widgets/routines/workout_logs.dart @@ -47,7 +47,7 @@ class _WorkoutLogsState extends State { @override Widget build(BuildContext context) { - return Column( + return ListView( children: [ Padding( padding: const EdgeInsets.symmetric(vertical: 10), diff --git a/test/workout/routine_logs_screen_test.dart b/test/workout/routine_logs_screen_test.dart index d7fc5640..9b45690a 100644 --- a/test/workout/routine_logs_screen_test.dart +++ b/test/workout/routine_logs_screen_test.dart @@ -23,8 +23,8 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:golden_toolkit/golden_toolkit.dart'; import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; import 'package:provider/provider.dart'; +import 'package:wger/models/workouts/routine.dart'; import 'package:wger/providers/routines.dart'; import 'package:wger/screens/routine_logs_screen.dart'; import 'package:wger/screens/routine_screen.dart'; @@ -34,11 +34,12 @@ import 'routine_logs_screen_test.mocks.dart'; @GenerateMocks([RoutinesProvider]) void main() { + late Routine routine; final mockRoutinesProvider = MockRoutinesProvider(); setUp(() { - when(mockRoutinesProvider.fetchAndSetRoutineFull(any)) - .thenAnswer((_) => Future.value(getTestRoutine())); + routine = getTestRoutine(); + routine.logs[0].date = DateTime.now(); }); Widget renderWidget({locale = 'en'}) { @@ -54,7 +55,7 @@ void main() { home: TextButton( onPressed: () => key.currentState!.push( MaterialPageRoute( - settings: RouteSettings(arguments: getTestRoutine()), + settings: RouteSettings(arguments: routine), builder: (_) => const WorkoutLogsScreen(), ), ), diff --git a/test/workout/slot_entry_model_test.dart b/test/workout/slot_entry_model_test.dart index 2e490603..8d932bce 100644 --- a/test/workout/slot_entry_model_test.dart +++ b/test/workout/slot_entry_model_test.dart @@ -57,18 +57,8 @@ void main() { test('Checks that an model with data correctly calculates hasProgressionRules', () { final slotEntry = SlotEntry.empty(); - slotEntry.weightConfigs.add(BaseConfig( - id: 1, - slotEntryId: 1, - iteration: 1, - value: 1, - )); - slotEntry.weightConfigs.add(BaseConfig( - id: 2, - slotEntryId: 1, - iteration: 1, - value: 1, - )); + slotEntry.weightConfigs.add(BaseConfig.firstIteration(3, 1)); + slotEntry.weightConfigs.add(BaseConfig.firstIteration(4, 1)); expect(slotEntry.hasProgressionRules, true); }); }