mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 01:41:05 +01:00
Merge pull request #1158 from wger-project/feature/group-sets
Group slots in the UI with the same exercise
This commit is contained in:
@@ -19,6 +19,31 @@ PODS:
|
||||
- shared_preferences_foundation (0.0.1):
|
||||
- Flutter
|
||||
- FlutterMacOS
|
||||
- sqlite3 (3.52.0):
|
||||
- sqlite3/common (= 3.52.0)
|
||||
- sqlite3/common (3.52.0)
|
||||
- sqlite3/dbstatvtab (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3/fts5 (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3/math (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3/perf-threadsafe (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3/rtree (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3/session (3.52.0):
|
||||
- sqlite3/common
|
||||
- sqlite3_flutter_libs (0.0.1):
|
||||
- Flutter
|
||||
- FlutterMacOS
|
||||
- sqlite3 (~> 3.52.0)
|
||||
- sqlite3/dbstatvtab
|
||||
- sqlite3/fts5
|
||||
- sqlite3/math
|
||||
- sqlite3/perf-threadsafe
|
||||
- sqlite3/rtree
|
||||
- sqlite3/session
|
||||
- url_launcher_ios (0.0.1):
|
||||
- Flutter
|
||||
- video_player_avfoundation (0.0.1):
|
||||
@@ -36,9 +61,14 @@ DEPENDENCIES:
|
||||
- pointer_interceptor_ios (from `.symlinks/plugins/pointer_interceptor_ios/ios`)
|
||||
- rive_common (from `.symlinks/plugins/rive_common/ios`)
|
||||
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
|
||||
- sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/darwin`)
|
||||
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
|
||||
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/darwin`)
|
||||
|
||||
SPEC REPOS:
|
||||
trunk:
|
||||
- sqlite3
|
||||
|
||||
EXTERNAL SOURCES:
|
||||
camera_avfoundation:
|
||||
:path: ".symlinks/plugins/camera_avfoundation/ios"
|
||||
@@ -60,6 +90,8 @@ EXTERNAL SOURCES:
|
||||
:path: ".symlinks/plugins/rive_common/ios"
|
||||
shared_preferences_foundation:
|
||||
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
|
||||
sqlite3_flutter_libs:
|
||||
:path: ".symlinks/plugins/sqlite3_flutter_libs/darwin"
|
||||
url_launcher_ios:
|
||||
:path: ".symlinks/plugins/url_launcher_ios/ios"
|
||||
video_player_avfoundation:
|
||||
@@ -76,6 +108,8 @@ SPEC CHECKSUMS:
|
||||
pointer_interceptor_ios: da06a662d5bfd329602b45b2ab41bc0fb5fdb0f0
|
||||
rive_common: dd421daaf9ae69f0125aa761dd96abd278399952
|
||||
shared_preferences_foundation: 7036424c3d8ec98dfe75ff1667cb0cd531ec82bb
|
||||
sqlite3: a51c07cf16e023d6c48abd5e5791a61a47354921
|
||||
sqlite3_flutter_libs: b3e120efe9a82017e5552a620f696589ed4f62ab
|
||||
url_launcher_ios: 7a95fa5b60cc718a708b8f2966718e93db0cef1b
|
||||
video_player_avfoundation: dd410b52df6d2466a42d28550e33e4146928280a
|
||||
|
||||
|
||||
@@ -228,6 +228,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"setNr": "Set {nr}",
|
||||
"@setNr": {
|
||||
"description": "Header in form indicating the number of the current set within a group of same exercises.",
|
||||
"type": "text",
|
||||
"placeholders": {
|
||||
"nr": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
},
|
||||
"sameRepetitions": "If you do the same repetitions and weight for all sets you can just fill in one row. For example for 4 sets just enter 10 for the repetitions, this automatically becomes \"4 x 10\".",
|
||||
"@sameRepetitions": {},
|
||||
"comment": "Comment",
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
* Copyright (c) 2020 - 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.
|
||||
*
|
||||
* wger Workout Manager is distributed in the hope that it will be useful,
|
||||
* 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.
|
||||
@@ -30,6 +30,39 @@ import 'package:wger/widgets/exercises/autocompleter.dart';
|
||||
import 'package:wger/widgets/routines/forms/slot_entry.dart';
|
||||
import 'package:wger/widgets/routines/slot.dart';
|
||||
|
||||
typedef SlotGroupInfo = ({int groupSize, int indexInGroup, String? exerciseName});
|
||||
|
||||
/// Groups consecutive single-entry slots with the same exerciseId.
|
||||
/// Returns a map from slot index to group metadata.
|
||||
Map<int, SlotGroupInfo> computeSlotGroups(List<Slot> slots, String languageCode) {
|
||||
final result = <int, SlotGroupInfo>{};
|
||||
int i = 0;
|
||||
while (i < slots.length) {
|
||||
final slot = slots[i];
|
||||
if (slot.entries.length != 1) {
|
||||
result[i] = (groupSize: 1, indexInGroup: 0, exerciseName: null);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
final exerciseId = slot.entries[0].exerciseId;
|
||||
int j = i + 1;
|
||||
while (j < slots.length &&
|
||||
slots[j].entries.length == 1 &&
|
||||
slots[j].entries[0].exerciseId == exerciseId) {
|
||||
j++;
|
||||
}
|
||||
final groupSize = j - i;
|
||||
final exerciseName = groupSize > 1
|
||||
? slot.entries[0].exerciseObj.getTranslation(languageCode).name
|
||||
: null;
|
||||
for (int k = i; k < j; k++) {
|
||||
result[k] = (groupSize: groupSize, indexInGroup: k - i, exerciseName: exerciseName);
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class SlotDetailWidget extends StatefulWidget {
|
||||
final Slot slot;
|
||||
final bool simpleMode;
|
||||
@@ -114,11 +147,66 @@ class _SlotFormWidgetStateNg extends State<ReorderableSlotList> {
|
||||
int? isDeletingSlot;
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
Future<void> _handleAddSet(Slot slot, int slotIndex) async {
|
||||
final provider = Provider.of<RoutinesProvider>(context, listen: false);
|
||||
if (slot.entries.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => isAddingSlot = true);
|
||||
try {
|
||||
final insertOrder = slotIndex + 2;
|
||||
|
||||
// Shift orders of subsequent slots
|
||||
final slotsToUpdate = <Slot>[];
|
||||
for (int k = slotIndex + 1; k < widget.slots.length; k++) {
|
||||
widget.slots[k].order = insertOrder + (k - slotIndex);
|
||||
slotsToUpdate.add(widget.slots[k]);
|
||||
}
|
||||
if (slotsToUpdate.isNotEmpty) {
|
||||
await provider.editSlots(slotsToUpdate, widget.day.routineId);
|
||||
}
|
||||
|
||||
// Create new slot after source
|
||||
final newSlot = await provider.addSlot(
|
||||
Slot.withData(day: widget.day.id, order: insertOrder),
|
||||
widget.day.routineId,
|
||||
);
|
||||
|
||||
// Create entry with same exercise
|
||||
final sourceEntry = slot.entries[0];
|
||||
await provider.addSlotEntry(
|
||||
SlotEntry.withData(
|
||||
slotId: newSlot.id!,
|
||||
exercise: sourceEntry.exerciseObj,
|
||||
order: 1,
|
||||
weightUnitId: sourceEntry.weightUnitId,
|
||||
),
|
||||
widget.day.routineId,
|
||||
);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
isAddingSlot = false;
|
||||
errorMessage = const SizedBox.shrink();
|
||||
});
|
||||
}
|
||||
} on WgerHttpException catch (error) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
isAddingSlot = false;
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
final provider = context.read<RoutinesProvider>();
|
||||
final languageCode = Localizations.localeOf(context).languageCode;
|
||||
final groupInfo = computeSlotGroups(widget.slots, languageCode);
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
@@ -141,70 +229,126 @@ class _SlotFormWidgetStateNg extends State<ReorderableSlotList> {
|
||||
itemBuilder: (context, index) {
|
||||
final slot = widget.slots[index];
|
||||
final isCurrentSlotSelected = slot.id == selectedSlotId;
|
||||
final info = groupInfo[index]!;
|
||||
final isGrouped = info.groupSize > 1;
|
||||
|
||||
// Title: "Set N" for grouped, "Superset N" or "Exercise N" otherwise
|
||||
final String titleText;
|
||||
if (isGrouped) {
|
||||
titleText = i18n.setNr((info.indexInGroup + 1).toString());
|
||||
} else if (slot.isSuperset) {
|
||||
titleText = i18n.supersetNr((index + 1).toString());
|
||||
} else {
|
||||
titleText = i18n.exerciseNr((index + 1).toString());
|
||||
}
|
||||
|
||||
// Subtitle: exercise name(s), or group header for first in group
|
||||
Widget? subtitleWidget;
|
||||
if (slot.entries.isEmpty) {
|
||||
subtitleWidget = Text(i18n.setHasNoExercises);
|
||||
} else if (isGrouped && info.indexInGroup == 0 && info.exerciseName != null) {
|
||||
subtitleWidget = Text(info.exerciseName!);
|
||||
} else if (!isGrouped) {
|
||||
subtitleWidget = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: slot.entries
|
||||
.map((e) => Text(e.exerciseObj.getTranslation(languageCode).name))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
// Group visual: flush cards with rounded outer corners only
|
||||
const double cardRadius = 12;
|
||||
final bool isFirst = info.indexInGroup == 0;
|
||||
final bool isLast = info.indexInGroup == info.groupSize - 1;
|
||||
final borderRadius = isGrouped
|
||||
? BorderRadius.only(
|
||||
topLeft: isFirst ? const Radius.circular(cardRadius) : Radius.zero,
|
||||
topRight: isFirst ? const Radius.circular(cardRadius) : Radius.zero,
|
||||
bottomLeft: isLast ? const Radius.circular(cardRadius) : Radius.zero,
|
||||
bottomRight: isLast ? const Radius.circular(cardRadius) : Radius.zero,
|
||||
)
|
||||
: BorderRadius.circular(cardRadius);
|
||||
|
||||
// Remove vertical gap between consecutive grouped cards
|
||||
final cardMargin = isGrouped
|
||||
? EdgeInsets.only(
|
||||
left: 4,
|
||||
right: 4,
|
||||
top: isFirst ? 4 : 0,
|
||||
bottom: isLast ? 4 : 0,
|
||||
)
|
||||
: const EdgeInsets.all(4);
|
||||
|
||||
final cardChild = Column(
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(titleText),
|
||||
tileColor: isCurrentSlotSelected ? Theme.of(context).highlightColor : null,
|
||||
leading: selectedSlotId == null
|
||||
? ReorderableDragStartListener(
|
||||
index: index,
|
||||
child: const Icon(Icons.drag_handle),
|
||||
)
|
||||
: const Icon(Icons.block),
|
||||
subtitle: subtitleWidget,
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (slot.entries.length == 1 && isLast)
|
||||
IconButton(
|
||||
tooltip: i18n.addSet,
|
||||
icon: isAddingSlot
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.content_copy),
|
||||
onPressed: isAddingSlot ? null : () => _handleAddSet(slot, index),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
if (selectedSlotId == slot.id) {
|
||||
selectedSlotId = null;
|
||||
} else {
|
||||
selectedSlotId = slot.id;
|
||||
}
|
||||
});
|
||||
},
|
||||
icon: isCurrentSlotSelected
|
||||
? const Icon(Icons.edit_off)
|
||||
: const Icon(Icons.edit),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: isDeletingSlot == index
|
||||
? null
|
||||
: () async {
|
||||
selectedSlotId = null;
|
||||
setState(() => isDeletingSlot = index);
|
||||
await provider.deleteSlot(slot.id!, widget.day.routineId);
|
||||
if (mounted) {
|
||||
setState(() => isDeletingSlot = null);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (isCurrentSlotSelected)
|
||||
SlotDetailWidget(slot, widget.day.routineId, simpleMode: simpleMode),
|
||||
],
|
||||
);
|
||||
|
||||
return Card(
|
||||
color: slot.entries.isEmpty ? Theme.of(context).colorScheme.inversePrimary : null,
|
||||
key: ValueKey(slot.id),
|
||||
child: Column(
|
||||
children: [
|
||||
ListTile(
|
||||
title: slot.isSuperset
|
||||
? Text(i18n.supersetNr((index + 1).toString()))
|
||||
: Text(i18n.exerciseNr((index + 1).toString())),
|
||||
tileColor: isCurrentSlotSelected ? Theme.of(context).highlightColor : null,
|
||||
leading: selectedSlotId == null
|
||||
? ReorderableDragStartListener(
|
||||
index: index,
|
||||
child: const Icon(Icons.drag_handle),
|
||||
)
|
||||
: const Icon(Icons.block),
|
||||
subtitle: slot.entries.isEmpty
|
||||
? Text(i18n.setHasNoExercises)
|
||||
: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
...slot.entries.map(
|
||||
(e) => Text(e.exerciseObj.getTranslation(languageCode).name),
|
||||
),
|
||||
],
|
||||
),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
if (selectedSlotId == slot.id) {
|
||||
selectedSlotId = null;
|
||||
} else {
|
||||
selectedSlotId = slot.id;
|
||||
}
|
||||
});
|
||||
},
|
||||
icon: isCurrentSlotSelected
|
||||
? const Icon(Icons.edit_off)
|
||||
: const Icon(Icons.edit),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: isDeletingSlot == index
|
||||
? null
|
||||
: () async {
|
||||
selectedSlotId = null;
|
||||
setState(() => isDeletingSlot = index);
|
||||
await provider.deleteSlot(slot.id!, widget.day.routineId);
|
||||
if (mounted) {
|
||||
setState(() => isDeletingSlot = null);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (isCurrentSlotSelected)
|
||||
SlotDetailWidget(slot, widget.day.routineId, simpleMode: simpleMode),
|
||||
],
|
||||
),
|
||||
margin: cardMargin,
|
||||
color: slot.entries.isEmpty ? Theme.of(context).colorScheme.inversePrimary : null,
|
||||
shape: RoundedRectangleBorder(borderRadius: borderRadius),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: cardChild,
|
||||
);
|
||||
},
|
||||
onReorder: (int oldIndex, int newIndex) {
|
||||
|
||||
344
test/routine/forms/slot_form_test.dart
Normal file
344
test/routine/forms/slot_form_test.dart
Normal file
@@ -0,0 +1,344 @@
|
||||
/*
|
||||
* 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_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/slot.dart';
|
||||
import 'package:wger/models/workouts/slot_entry.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/widgets/routines/forms/slot.dart';
|
||||
|
||||
import '../../../test_data/routines.dart';
|
||||
import '../slot_entry_form_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([RoutinesProvider])
|
||||
void main() {
|
||||
group('computeSlotGroups', () {
|
||||
final routine = getTestRoutine();
|
||||
final benchPress = routine.days[0].slots[0].entries[0].exerciseObj;
|
||||
final sideRaises = routine.days[0].slots[1].entries[0].exerciseObj;
|
||||
|
||||
SlotEntry makeEntry(int slotId, int exerciseId, {required exerciseObj}) {
|
||||
return SlotEntry(
|
||||
slotId: slotId,
|
||||
type: SlotEntryType.normal,
|
||||
order: 1,
|
||||
exerciseId: exerciseId,
|
||||
repetitionUnitId: 1,
|
||||
repetitionRounding: 1,
|
||||
weightUnitId: 1,
|
||||
weightRounding: 1,
|
||||
exercise: exerciseObj,
|
||||
);
|
||||
}
|
||||
|
||||
Slot makeSlot(int id, int order, SlotEntry entry) {
|
||||
final s = Slot.withData(id: id, day: 1, order: order);
|
||||
s.entries.add(entry);
|
||||
return s;
|
||||
}
|
||||
|
||||
test('empty list returns empty map', () {
|
||||
final groups = computeSlotGroups([], 'en');
|
||||
expect(groups, isEmpty);
|
||||
});
|
||||
|
||||
test('single slot is not grouped', () {
|
||||
final slots = [makeSlot(1, 1, makeEntry(1, benchPress.id!, exerciseObj: benchPress))];
|
||||
final groups = computeSlotGroups(slots, 'en');
|
||||
|
||||
expect(groups[0]!.groupSize, 1);
|
||||
expect(groups[0]!.indexInGroup, 0);
|
||||
expect(groups[0]!.exerciseName, isNull);
|
||||
});
|
||||
|
||||
test('two consecutive same-exercise slots form a group', () {
|
||||
final slots = [
|
||||
makeSlot(1, 1, makeEntry(1, benchPress.id!, exerciseObj: benchPress)),
|
||||
makeSlot(2, 2, makeEntry(2, benchPress.id!, exerciseObj: benchPress)),
|
||||
];
|
||||
final groups = computeSlotGroups(slots, 'en');
|
||||
|
||||
expect(groups[0]!.groupSize, 2);
|
||||
expect(groups[0]!.indexInGroup, 0);
|
||||
expect(groups[0]!.exerciseName, benchPress.getTranslation('en').name);
|
||||
|
||||
expect(groups[1]!.groupSize, 2);
|
||||
expect(groups[1]!.indexInGroup, 1);
|
||||
});
|
||||
|
||||
test('different exercises are not grouped', () {
|
||||
final slots = [
|
||||
makeSlot(1, 1, makeEntry(1, benchPress.id!, exerciseObj: benchPress)),
|
||||
makeSlot(2, 2, makeEntry(2, sideRaises.id!, exerciseObj: sideRaises)),
|
||||
];
|
||||
final groups = computeSlotGroups(slots, 'en');
|
||||
|
||||
expect(groups[0]!.groupSize, 1);
|
||||
expect(groups[1]!.groupSize, 1);
|
||||
});
|
||||
|
||||
test('only consecutive same-exercise slots are grouped', () {
|
||||
// Bench, Bench, Side raises, Bench → group[2], single, single
|
||||
final slots = [
|
||||
makeSlot(1, 1, makeEntry(1, benchPress.id!, exerciseObj: benchPress)),
|
||||
makeSlot(2, 2, makeEntry(2, benchPress.id!, exerciseObj: benchPress)),
|
||||
makeSlot(3, 3, makeEntry(3, sideRaises.id!, exerciseObj: sideRaises)),
|
||||
makeSlot(4, 4, makeEntry(4, benchPress.id!, exerciseObj: benchPress)),
|
||||
];
|
||||
final groups = computeSlotGroups(slots, 'en');
|
||||
|
||||
expect(groups[0]!.groupSize, 2);
|
||||
expect(groups[1]!.groupSize, 2);
|
||||
expect(groups[2]!.groupSize, 1);
|
||||
expect(groups[3]!.groupSize, 1);
|
||||
});
|
||||
|
||||
test('superset slots (multiple entries) are never grouped', () {
|
||||
final supersetSlot = Slot.withData(id: 1, day: 1, order: 1);
|
||||
supersetSlot.entries.add(makeEntry(1, benchPress.id!, exerciseObj: benchPress));
|
||||
supersetSlot.entries.add(makeEntry(1, sideRaises.id!, exerciseObj: sideRaises));
|
||||
|
||||
final slots = [
|
||||
supersetSlot,
|
||||
makeSlot(2, 2, makeEntry(2, benchPress.id!, exerciseObj: benchPress)),
|
||||
];
|
||||
final groups = computeSlotGroups(slots, 'en');
|
||||
|
||||
expect(groups[0]!.groupSize, 1);
|
||||
expect(groups[1]!.groupSize, 1);
|
||||
});
|
||||
});
|
||||
|
||||
group('ReorderableSlotList', () {
|
||||
late MockRoutinesProvider mockProvider;
|
||||
final routine = getTestRoutine();
|
||||
final day = routine.days[0]; // has 2 slots: bench press + side raises
|
||||
|
||||
setUp(() {
|
||||
mockProvider = MockRoutinesProvider();
|
||||
when(mockProvider.deleteSlot(any, any)).thenAnswer((_) async {});
|
||||
when(mockProvider.editSlots(any, any)).thenAnswer((_) async {});
|
||||
when(
|
||||
mockProvider.addSlot(any, any),
|
||||
).thenAnswer((_) async => Slot.withData(id: 99, day: day.id, order: 3));
|
||||
when(
|
||||
mockProvider.addSlotEntry(any, any),
|
||||
).thenAnswer((_) async => day.slots[0].entries[0]);
|
||||
});
|
||||
|
||||
Widget buildWidget(List<Slot> slots) {
|
||||
return ChangeNotifierProvider<RoutinesProvider>.value(
|
||||
value: mockProvider,
|
||||
child: MaterialApp(
|
||||
locale: const Locale('en'),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: ReorderableSlotList(slots, day),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('renders "Superset N" for superset slots', (tester) async {
|
||||
final supersetSlot = Slot.withData(id: 10, day: day.id, order: 1);
|
||||
supersetSlot.entries.add(day.slots[0].entries[0]);
|
||||
supersetSlot.addExerciseBase(day.slots[0].entries[0].exerciseObj);
|
||||
final entry2 = SlotEntry(
|
||||
slotId: 10,
|
||||
type: SlotEntryType.normal,
|
||||
order: 2,
|
||||
exerciseId: day.slots[1].entries[0].exerciseId,
|
||||
repetitionUnitId: 1,
|
||||
repetitionRounding: 1,
|
||||
weightUnitId: 1,
|
||||
weightRounding: 1,
|
||||
exercise: day.slots[1].entries[0].exerciseObj,
|
||||
);
|
||||
supersetSlot.entries.add(entry2);
|
||||
supersetSlot.addExerciseBase(day.slots[1].entries[0].exerciseObj);
|
||||
|
||||
await tester.pumpWidget(buildWidget([supersetSlot]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Superset 1'), findsOne);
|
||||
expect(find.text('Exercise 1'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('renders "Exercise N" for normal slots', (tester) async {
|
||||
await tester.pumpWidget(buildWidget(day.slots));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Exercise 1'), findsOne);
|
||||
expect(find.text('Exercise 2'), findsOne);
|
||||
});
|
||||
|
||||
testWidgets('renders "Set N" instead of "Exercise N" for grouped slots', (tester) async {
|
||||
// Two bench press slots in a row → should be grouped
|
||||
final benchEntry1 = day.slots[0].entries[0];
|
||||
final slot1 = Slot.withData(id: 1, day: day.id, order: 1);
|
||||
slot1.entries.add(benchEntry1);
|
||||
final slot2 = Slot.withData(id: 2, day: day.id, order: 2);
|
||||
slot2.entries.add(benchEntry1);
|
||||
|
||||
await tester.pumpWidget(buildWidget([slot1, slot2]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Set 1'), findsOne);
|
||||
expect(find.text('Set 2'), findsOne);
|
||||
expect(find.text('Exercise 1'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows exercise name as subtitle for first slot in a group', (tester) async {
|
||||
final benchEntry = day.slots[0].entries[0];
|
||||
final slot1 = Slot.withData(id: 1, day: day.id, order: 1);
|
||||
slot1.entries.add(benchEntry);
|
||||
final slot2 = Slot.withData(id: 2, day: day.id, order: 2);
|
||||
slot2.entries.add(benchEntry);
|
||||
|
||||
await tester.pumpWidget(buildWidget([slot1, slot2]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Exercise name should appear once (as group subtitle for Set 1)
|
||||
expect(find.text(benchEntry.exerciseObj.getTranslation('en').name), findsOne);
|
||||
});
|
||||
|
||||
testWidgets('exercise name is only shown for the first slot of a group', (tester) async {
|
||||
final benchEntry = day.slots[0].entries[0];
|
||||
final exerciseName = benchEntry.exerciseObj.getTranslation('en').name;
|
||||
final slot1 = Slot.withData(id: 1, day: day.id, order: 1);
|
||||
slot1.entries.add(benchEntry);
|
||||
final slot2 = Slot.withData(id: 2, day: day.id, order: 2);
|
||||
slot2.entries.add(benchEntry);
|
||||
final slot3 = Slot.withData(id: 3, day: day.id, order: 3);
|
||||
slot3.entries.add(benchEntry);
|
||||
|
||||
await tester.pumpWidget(buildWidget([slot1, slot2, slot3]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Name appears exactly once (subtitle of Set 1), not on Set 2 or Set 3
|
||||
expect(find.text(exerciseName), findsOne);
|
||||
});
|
||||
|
||||
testWidgets('shows "Add Set" button for single-entry slot', (tester) async {
|
||||
await tester.pumpWidget(buildWidget([day.slots[0]]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.content_copy), findsOne);
|
||||
});
|
||||
|
||||
testWidgets('does not show "Add Set" button for superset slot', (tester) async {
|
||||
final supersetSlot = Slot.withData(id: 10, day: day.id, order: 1);
|
||||
supersetSlot.entries.add(day.slots[0].entries[0]);
|
||||
// Add a second entry to make it a superset
|
||||
final entry2 = SlotEntry(
|
||||
slotId: 10,
|
||||
type: SlotEntryType.normal,
|
||||
order: 2,
|
||||
exerciseId: day.slots[1].entries[0].exerciseId,
|
||||
repetitionUnitId: 1,
|
||||
repetitionRounding: 1,
|
||||
weightUnitId: 1,
|
||||
weightRounding: 1,
|
||||
exercise: day.slots[1].entries[0].exerciseObj,
|
||||
);
|
||||
supersetSlot.entries.add(entry2);
|
||||
|
||||
await tester.pumpWidget(buildWidget([supersetSlot]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.content_copy), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('tapping "Add Set" calls addSlot and addSlotEntry', (tester) async {
|
||||
final slot = day.slots[0];
|
||||
await tester.pumpWidget(buildWidget([slot]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byIcon(Icons.content_copy));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
verify(
|
||||
mockProvider.addSlot(
|
||||
argThat(
|
||||
isA<Slot>().having((s) => s.day, 'day', day.id).having((s) => s.order, 'order', 2),
|
||||
),
|
||||
day.routineId,
|
||||
),
|
||||
).called(1);
|
||||
|
||||
verify(
|
||||
mockProvider.addSlotEntry(
|
||||
argThat(
|
||||
isA<SlotEntry>().having(
|
||||
(e) => e.exerciseId,
|
||||
'exerciseId',
|
||||
slot.entries[0].exerciseId,
|
||||
),
|
||||
),
|
||||
day.routineId,
|
||||
),
|
||||
).called(1);
|
||||
});
|
||||
|
||||
testWidgets('tapping "Add Set" calls editSlots to shift subsequent slots', (tester) async {
|
||||
final benchEntry = day.slots[0].entries[0];
|
||||
final slot1 = Slot.withData(id: 1, day: day.id, order: 1);
|
||||
slot1.entries.add(benchEntry);
|
||||
final slot2 = Slot.withData(id: 2, day: day.id, order: 2);
|
||||
slot2.entries.add(day.slots[1].entries[0]); // different exercise, comes after
|
||||
|
||||
await tester.pumpWidget(buildWidget([slot1, slot2]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byIcon(Icons.content_copy).first);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// slot2 must be shifted to order 3 to make room for the new slot at order 2
|
||||
verify(
|
||||
mockProvider.editSlots(
|
||||
argThat(contains(isA<Slot>().having((s) => s.order, 'order', 3))),
|
||||
day.routineId,
|
||||
),
|
||||
).called(1);
|
||||
});
|
||||
|
||||
testWidgets('"Add Set" button only appears on the last slot of a group', (tester) async {
|
||||
final benchEntry = day.slots[0].entries[0];
|
||||
final slot1 = Slot.withData(id: 1, day: day.id, order: 1);
|
||||
slot1.entries.add(benchEntry);
|
||||
final slot2 = Slot.withData(id: 2, day: day.id, order: 2);
|
||||
slot2.entries.add(benchEntry);
|
||||
final slot3 = Slot.withData(id: 3, day: day.id, order: 3);
|
||||
slot3.entries.add(benchEntry);
|
||||
|
||||
await tester.pumpWidget(buildWidget([slot1, slot2, slot3]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Only one copy button for the whole group (on the last slot)
|
||||
expect(find.byIcon(Icons.content_copy), findsOne);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user