Directly return the exercises found by the search

While these do not land in the local cache, this is still faster and
better, and works like the ingredientSearch method.
This commit is contained in:
Roland Geider
2026-04-14 13:53:30 +02:00
parent 2dbdf996e5
commit f9f106f5ed
5 changed files with 26 additions and 30 deletions

View File

@@ -162,8 +162,8 @@ class Exercise extends Equatable {
lastUpdate = exerciseData.lastUpdate;
lastUpdateGlobal = exerciseData.lastUpdateGlobal;
musclesSecondary = exerciseData.muscles;
muscles = exerciseData.muscles;
musclesSecondary = exerciseData.musclesSecondary;
equipment = exerciseData.equipment;
category = exerciseData.category;
translations = exerciseData.translations.map((e) {

View File

@@ -675,10 +675,6 @@ class ExercisesProvider with ChangeNotifier {
///
/// We could do this locally, but the server has better text searching capabilities
/// with postgresql.
///
/// TODO: currently we still only use the results to extract the IDs and then
/// fetch the exercises one by one (and hope they are in the local db),
/// which is not ideal.
Future<List<Exercise>> searchExercise(
String name, {
String languageCode = LANGUAGE_SHORT_ENGLISH,
@@ -705,18 +701,9 @@ class ExercisesProvider with ChangeNotifier {
),
);
// Load the exercises
final ids = (result['results'] as List).map<int>((data) => data['id'] as int).toList();
final List<Exercise> out = [];
for (final id in ids) {
final exercise = await fetchAndSetExercise(id);
if (exercise != null) {
out.add(exercise);
}
}
return out;
return (result['results'] as List)
.map((e) => Exercise.fromApiDataJson(e as Map<String, dynamic>, _languages))
.toList();
}
}

View File

@@ -104,9 +104,8 @@ void main() {
'Obliquus externus abdominis',
]);
expect(exercise.musclesSecondary.map((e) => e.name), [
'Biceps femoris',
'Brachialis',
'Obliquus externus abdominis',
'Anterior deltoid',
'Trapezius',
]);
expect(exercise.images.map((e) => e.uuid), [
'1f5d2b2f-d4ea-4eeb-9377-56176465e08d',

View File

@@ -375,16 +375,29 @@ void main() {
path: 'api/v2/$exerciseSearchUrl/',
queryParameters: query,
);
final exerciseInfoEntry = jsonDecode(
fixture('exercises/exerciseinfo_response.json'),
);
) as Map<String, dynamic>;
// Build a paginated exerciseinfo response with two results that
// have the categories used by the test exercises so that the
// category filter test below works correctly.
final Map<String, dynamic> tSearchResponse = {
'count': 2,
'next': null,
'previous': null,
'results': [
{...exerciseInfoEntry, 'id': 1},
{...exerciseInfoEntry, 'id': 2},
{
...exerciseInfoEntry,
'id': 1,
'category': {'id': 1, 'name': 'Arms'},
},
{
...exerciseInfoEntry,
'id': 2,
'category': {'id': 2, 'name': 'Legs'},
},
],
};
@@ -411,10 +424,8 @@ void main() {
// assert
verify(provider.baseProvider.fetch(tSearchByNameUri)).called(1);
expect(
provider.filteredExercises,
[data.getTestExercises()[0], data.getTestExercises()[1]],
);
expect(provider.filteredExercises.length, 2);
expect(provider.filteredExercises.map((e) => e.id).toList(), [1, 2]);
});
test('Should find items from selection but should filter them by search term', () async {
// arrange

View File

@@ -49,9 +49,8 @@ void main() {
'Obliquus externus abdominis',
]);
expect(exercise.musclesSecondary.map((e) => e.name), [
'Biceps femoris',
'Brachialis',
'Obliquus externus abdominis',
'Anterior deltoid',
'Trapezius',
]);
expect(exercise.images.map((e) => e.uuid), [
'1f5d2b2f-d4ea-4eeb-9377-56176465e08d',