mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 02:24:30 +01:00
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:
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user