Skip to content

Commit 9ebaf58

Browse files
committed
Refactor routine handling for the dashboard widgets
This makes sure that edits to the routine (such as changing the order of the days, etc.) are properly reflected in the dashboard.
1 parent 79bab32 commit 9ebaf58

39 files changed

+9766
-3692
lines changed

lib/providers/routines.dart

+21-32
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class RoutinesProvider with ChangeNotifier {
6363
static const _routineConfigRestTime = 'rest-config';
6464
static const _routineConfigMaxRestTime = 'max-rest-config';
6565

66-
Routine? _currentRoutine;
66+
Routine? activeRoutine;
6767
late ExercisesProvider _exerciseProvider;
6868
final WgerBaseProvider baseProvider;
6969
List<Routine> _routines = [];
@@ -97,7 +97,7 @@ class RoutinesProvider with ChangeNotifier {
9797

9898
/// Clears all lists
9999
void clear() {
100-
_currentRoutine = null;
100+
activeRoutine = null;
101101
_routines = [];
102102
_weightUnits = [];
103103
_repetitionUnits = [];
@@ -138,28 +138,14 @@ class RoutinesProvider with ChangeNotifier {
138138
return _routines.indexWhere((routine) => routine.id == id);
139139
}
140140

141-
/// Set the currently "active" workout plan
142-
void setCurrentPlan(int id) {
143-
_currentRoutine = findById(id);
144-
}
145-
146-
/// Returns the currently "active" workout plan
147-
Routine? get currentRoutine {
148-
return _currentRoutine;
149-
}
150-
151-
/// Reset the currently "active" workout plan to null
152-
void resetCurrentRoutine() {
153-
_currentRoutine = null;
154-
}
155-
156-
/// Returns the current active workout plan. At the moment this is just
157-
/// the latest, but this might change in the future.
158-
Routine? get activeRoutine {
141+
/// Sets the current active routine. At the moment this is just the latest,
142+
/// but this might change in the future.
143+
void setActiveRoutine() {
159144
if (_routines.isNotEmpty) {
160-
return _routines.first;
145+
activeRoutine = _routines.first;
146+
} else {
147+
activeRoutine = null;
161148
}
162-
return null;
163149
}
164150

165151
/*
@@ -179,12 +165,13 @@ class RoutinesProvider with ChangeNotifier {
179165
await fetchAndSetRoutineFull(entry['id']);
180166
}
181167

168+
setActiveRoutine();
182169
notifyListeners();
183170
}
184171

185-
/// Fetches all workout plan sparsely, i.e. only with the data on the plan
186-
/// object itself and no child attributes
187-
Future<void> fetchAndSetAllPlansSparse() async {
172+
/// Fetches all routines sparsely, i.e. only with the data on the object itself
173+
/// and no child attributes
174+
Future<void> fetchAndSetAllRoutinesSparse() async {
188175
final data = await baseProvider.fetch(
189176
baseProvider.makeUrl(_routinesUrlPath, query: {'limit': '1000', 'is_template': 'false'}),
190177
);
@@ -194,7 +181,7 @@ class RoutinesProvider with ChangeNotifier {
194181
_routines.add(plan);
195182
}
196183

197-
// _workoutPlans.sort((a, b) => b.created.compareTo(a.created));
184+
setActiveRoutine();
198185
notifyListeners();
199186
}
200187

@@ -216,18 +203,19 @@ class RoutinesProvider with ChangeNotifier {
216203
}
217204
}
218205

219-
/// Fetches a workout plan sparsely, i.e. only with the data on the plan
220-
/// object itself and no child attributes
206+
/// Fetches a routine sparsely, i.e. only with the data on the object itself
207+
/// and no child attributes
221208
Future<Routine> fetchAndSetRoutineSparse(int planId) async {
222209
final fullPlanData = await baseProvider.fetch(
223210
baseProvider.makeUrl(_routinesUrlPath, id: planId),
224211
);
225-
final plan = Routine.fromJson(fullPlanData);
226-
_routines.add(plan);
212+
final routine = Routine.fromJson(fullPlanData);
213+
_routines.add(routine);
227214
_routines.sort((a, b) => b.created.compareTo(a.created));
228215

216+
setActiveRoutine();
229217
notifyListeners();
230-
return plan;
218+
return routine;
231219
}
232220

233221
/// Fetches a workout plan fully, i.e. with all corresponding child attributes
@@ -306,7 +294,7 @@ class RoutinesProvider with ChangeNotifier {
306294
routine.dayDataGym = dayDataEntriesGym;
307295

308296
// Logs
309-
routine.sessions = List<WorkoutSessionApi>.from(sessionDataEntries);
297+
routine.sessions = List<WorkoutSessionApi>.of(sessionDataEntries);
310298
for (final session in routine.sessions) {
311299
for (final log in session.logs) {
312300
if (log.weightUnitId != null) {
@@ -327,6 +315,7 @@ class RoutinesProvider with ChangeNotifier {
327315
_routines.add(routine);
328316
}
329317

318+
setActiveRoutine();
330319
notifyListeners();
331320
return routine;
332321
}

lib/screens/dashboard.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import 'package:flutter/material.dart';
2020
import 'package:wger/l10n/generated/app_localizations.dart';
2121
import 'package:wger/widgets/core/app_bar.dart';
2222
import 'package:wger/widgets/dashboard/calendar.dart';
23-
import 'package:wger/widgets/dashboard/widgets.dart';
23+
import 'package:wger/widgets/dashboard/widgets/measurements.dart';
24+
import 'package:wger/widgets/dashboard/widgets/nutrition.dart';
25+
import 'package:wger/widgets/dashboard/widgets/routines.dart';
26+
import 'package:wger/widgets/dashboard/widgets/weight.dart';
2427

2528
class DashboardScreen extends StatelessWidget {
2629
const DashboardScreen();
@@ -35,7 +38,7 @@ class DashboardScreen extends StatelessWidget {
3538
padding: EdgeInsets.all(10),
3639
child: Column(
3740
children: [
38-
DashboardWorkoutWidget(),
41+
DashboardRoutineWidget(),
3942
DashboardNutritionWidget(),
4043
DashboardWeightWidget(),
4144
DashboardMeasurementWidget(),

lib/screens/home_tabs_screen.dart

+33-34
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
106106
await Future.wait([
107107
galleryProvider.fetchAndSetGallery(),
108108
nutritionPlansProvider.fetchAndSetAllPlansSparse(),
109-
routinesProvider.fetchAndSetAllPlansSparse(),
109+
routinesProvider.fetchAndSetAllRoutinesSparse(),
110110
// routinesProvider.fetchAndSetAllRoutinesFull(),
111111
weightProvider.fetchAndSetEntries(),
112112
measurementProvider.fetchAndSetAllCategoriesAndEntries(),
@@ -128,12 +128,11 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
128128
widget._logger.warning(e.toString());
129129
}
130130

131-
// Current workout plan
131+
// Current routine
132132
widget._logger.info('Loading current routine');
133133
if (routinesProvider.activeRoutine != null) {
134134
final planId = routinesProvider.activeRoutine!.id!;
135135
await routinesProvider.fetchAndSetRoutineFull(planId);
136-
routinesProvider.setCurrentPlan(planId);
137136
}
138137
}
139138

@@ -149,38 +148,38 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
149148
return const Scaffold(
150149
body: LoadingWidget(),
151150
);
152-
} else {
153-
return Scaffold(
154-
body: _screenList.elementAt(_selectedIndex),
155-
bottomNavigationBar: NavigationBar(
156-
destinations: [
157-
NavigationDestination(
158-
icon: const Icon(Icons.home),
159-
label: AppLocalizations.of(context).labelDashboard,
160-
),
161-
NavigationDestination(
162-
icon: const Icon(Icons.fitness_center),
163-
label: AppLocalizations.of(context).labelBottomNavWorkout,
164-
),
165-
NavigationDestination(
166-
icon: const Icon(Icons.restaurant),
167-
label: AppLocalizations.of(context).labelBottomNavNutrition,
168-
),
169-
NavigationDestination(
170-
icon: const FaIcon(FontAwesomeIcons.weightScale, size: 20),
171-
label: AppLocalizations.of(context).weight,
172-
),
173-
NavigationDestination(
174-
icon: const Icon(Icons.photo_library),
175-
label: AppLocalizations.of(context).gallery,
176-
),
177-
],
178-
onDestinationSelected: _onItemTapped,
179-
selectedIndex: _selectedIndex,
180-
labelBehavior: NavigationDestinationLabelBehavior.alwaysHide,
181-
),
182-
);
183151
}
152+
153+
return Scaffold(
154+
body: _screenList.elementAt(_selectedIndex),
155+
bottomNavigationBar: NavigationBar(
156+
destinations: [
157+
NavigationDestination(
158+
icon: const Icon(Icons.home),
159+
label: AppLocalizations.of(context).labelDashboard,
160+
),
161+
NavigationDestination(
162+
icon: const Icon(Icons.fitness_center),
163+
label: AppLocalizations.of(context).labelBottomNavWorkout,
164+
),
165+
NavigationDestination(
166+
icon: const Icon(Icons.restaurant),
167+
label: AppLocalizations.of(context).labelBottomNavNutrition,
168+
),
169+
NavigationDestination(
170+
icon: const FaIcon(FontAwesomeIcons.weightScale, size: 20),
171+
label: AppLocalizations.of(context).weight,
172+
),
173+
NavigationDestination(
174+
icon: const Icon(Icons.photo_library),
175+
label: AppLocalizations.of(context).gallery,
176+
),
177+
],
178+
onDestinationSelected: _onItemTapped,
179+
selectedIndex: _selectedIndex,
180+
labelBehavior: NavigationDestinationLabelBehavior.alwaysHide,
181+
),
182+
);
184183
},
185184
);
186185
}

0 commit comments

Comments
 (0)