From db04964f2038d22a8449abe74eebf4412147ead5 Mon Sep 17 00:00:00 2001 From: Arun Muralidharan Date: Sun, 27 Jun 2021 23:36:53 +0200 Subject: [PATCH 1/3] Reorder Sets Progress PR for Issue #41. Currently you can edit the order (long press to reorder) in the workout plan screen. The order change is only local; it persists only for the current session. However, I have not figured out how to reflect the changed order in the DB using the API. Does Day even store the order of its Sets in the DB? Or is it dynamically populated into the model in the app? --- lib/widgets/workouts/day.dart | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/widgets/workouts/day.dart b/lib/widgets/workouts/day.dart index f8a25a9d5..90dc749c5 100644 --- a/lib/widgets/workouts/day.dart +++ b/lib/widgets/workouts/day.dart @@ -94,6 +94,13 @@ class WorkoutDayWidget extends StatefulWidget { class _WorkoutDayWidgetState extends State { bool _expanded = false; + late List _sets; + + void initState() { + super.initState(); + _sets = widget._day.sets; + } + void _toggleExpanded() { setState(() { _expanded = !_expanded; @@ -102,6 +109,7 @@ class _WorkoutDayWidgetState extends State { Widget getSetRow(Set set) { return Row( + key: ValueKey(set.id), crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [ @@ -198,11 +206,23 @@ class _WorkoutDayWidgetState extends State { ], ), Divider(), - ...widget._day.sets - .map( - (set) => getSetRow(set), - ) - .toList(), + ReorderableListView( + physics: NeverScrollableScrollPhysics(), + shrinkWrap: true, + onReorder: (oldIndex, newIndex) { + setState(() { + if (oldIndex < newIndex) { + newIndex -= 1; + } + final Set _set = _sets.removeAt(oldIndex); + _sets.insert(newIndex, _set); + }); + }, + children: [ + for (final _set in _sets) + getSetRow(_set), + ], + ), OutlinedButton( child: Text(AppLocalizations.of(context).addSet), onPressed: () { From 22993a0151a0761eb02a5f2cb9ef587a8f3fd76e Mon Sep 17 00:00:00 2001 From: Arun Muralidharan Date: Mon, 28 Jun 2021 14:59:43 +0200 Subject: [PATCH 2/3] Reorder List Change in order now reflects in DB as well. --- lib/providers/workout_plans.dart | 16 ++++++++++++++++ lib/widgets/workouts/day.dart | 20 +++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/providers/workout_plans.dart b/lib/providers/workout_plans.dart index 4078c9723..ffdc95411 100644 --- a/lib/providers/workout_plans.dart +++ b/lib/providers/workout_plans.dart @@ -403,6 +403,22 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier { return set; } + Future editSet(Set workoutSet) async { + await patch(workoutSet.toJson(), makeUrl(_setsUrlPath, id: workoutSet.id)); + notifyListeners(); + } + + // Sets the order field for the given list of sets, starting from startIndex. + // Better than calling editSet for each set after reordering as it will notify + // for every element, rebuilding for each notification. + Future reorderSets(List sets, int startIndex) async { + for (int i = startIndex; i < sets.length; i++) { + sets[i].order = i; + await patch(sets[i].toJson(), makeUrl(_setsUrlPath, id: sets[i].id)); + } + notifyListeners(); + } + Future fetchComputedSettings(Set workoutSet) async { final data = await fetch(makeUrl( _setsUrlPath, diff --git a/lib/widgets/workouts/day.dart b/lib/widgets/workouts/day.dart index 90dc749c5..58a26baaa 100644 --- a/lib/widgets/workouts/day.dart +++ b/lib/widgets/workouts/day.dart @@ -145,6 +145,7 @@ class _WorkoutDayWidgetState extends State { @override Widget build(BuildContext context) { + _sets.sort((a, b) => a.order!.compareTo(b.order!)); return Padding( padding: const EdgeInsets.only(left: 8, right: 8, bottom: 12), child: Card( @@ -209,17 +210,22 @@ class _WorkoutDayWidgetState extends State { ReorderableListView( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, - onReorder: (oldIndex, newIndex) { + onReorder: (_oldIndex, _newIndex) async { + int _startIndex = 0; + if (_oldIndex < _newIndex) { + _newIndex -= 1; + _startIndex = _oldIndex; + } else { + _startIndex = _newIndex; + } setState(() { - if (oldIndex < newIndex) { - newIndex -= 1; - } - final Set _set = _sets.removeAt(oldIndex); - _sets.insert(newIndex, _set); + final Set _set = _sets.removeAt(_oldIndex); + _sets.insert(_newIndex, _set); }); + Provider.of(context, listen: false).reorderSets(_sets, _startIndex); }, children: [ - for (final _set in _sets) + for (final _set in widget._day.sets) getSetRow(_set), ], ), From 7eb26da24310ef3df4ca1f4d1be746629f197fb7 Mon Sep 17 00:00:00 2001 From: Arun Muralidharan Date: Mon, 28 Jun 2021 21:56:25 +0200 Subject: [PATCH 3/3] Reorder Sets Now updates order upon deletion as well. Order will be set to length of sets upon adding a new one. --- lib/providers/workout_plans.dart | 6 ++---- lib/widgets/workouts/day.dart | 16 ++++++++++------ lib/widgets/workouts/forms.dart | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/providers/workout_plans.dart b/lib/providers/workout_plans.dart index ffdc95411..a58084ce3 100644 --- a/lib/providers/workout_plans.dart +++ b/lib/providers/workout_plans.dart @@ -408,15 +408,13 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier { notifyListeners(); } - // Sets the order field for the given list of sets, starting from startIndex. - // Better than calling editSet for each set after reordering as it will notify - // for every element, rebuilding for each notification. - Future reorderSets(List sets, int startIndex) async { + Future> reorderSets(List sets, int startIndex) async { for (int i = startIndex; i < sets.length; i++) { sets[i].order = i; await patch(sets[i].toJson(), makeUrl(_setsUrlPath, id: sets[i].id)); } notifyListeners(); + return sets; } Future fetchComputedSettings(Set workoutSet) async { diff --git a/lib/widgets/workouts/day.dart b/lib/widgets/workouts/day.dart index 58a26baaa..c29507a7a 100644 --- a/lib/widgets/workouts/day.dart +++ b/lib/widgets/workouts/day.dart @@ -99,6 +99,7 @@ class _WorkoutDayWidgetState extends State { void initState() { super.initState(); _sets = widget._day.sets; + _sets.sort((a, b) => a.order!.compareTo(b.order!)); } void _toggleExpanded() { @@ -135,7 +136,12 @@ class _WorkoutDayWidgetState extends State { visualDensity: VisualDensity.compact, icon: Icon(Icons.delete), iconSize: ICON_SIZE_SMALL, - onPressed: () { + onPressed: () async { + int _startIndex = _sets.indexOf(set); + setState(() { + _sets.remove(set); + }); + _sets = await Provider.of(context, listen: false).reorderSets(_sets, _startIndex); Provider.of(context, listen: false).deleteSet(set); }, ), @@ -145,7 +151,6 @@ class _WorkoutDayWidgetState extends State { @override Widget build(BuildContext context) { - _sets.sort((a, b) => a.order!.compareTo(b.order!)); return Padding( padding: const EdgeInsets.only(left: 8, right: 8, bottom: 12), child: Card( @@ -219,13 +224,12 @@ class _WorkoutDayWidgetState extends State { _startIndex = _newIndex; } setState(() { - final Set _set = _sets.removeAt(_oldIndex); - _sets.insert(_newIndex, _set); + _sets.insert(_newIndex, _sets.removeAt(_oldIndex)); }); - Provider.of(context, listen: false).reorderSets(_sets, _startIndex); + _sets = await Provider.of(context, listen: false).reorderSets(_sets, _startIndex); }, children: [ - for (final _set in widget._day.sets) + for (final _set in _sets) getSetRow(_set), ], ), diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart index b6baff1fd..03e8099e7 100644 --- a/lib/widgets/workouts/forms.dart +++ b/lib/widgets/workouts/forms.dart @@ -259,7 +259,7 @@ class SetFormWidget extends StatefulWidget { late Set _set; SetFormWidget(this._day, [Set? set]) { - this._set = set ?? Set.withData(day: _day.id, sets: 4); + this._set = set ?? Set.withData(day: _day.id, order: _day.sets.length, sets: 4); } @override