Replace the deprecated /api/v2/exercise/search endpoint

We can simply use the regular "exerciseinfo" endpoint, like we do for
ingredients. We still call fetchAndSetExercise for them, which is not
efficient, but
This commit is contained in:
Roland Geider
2026-04-14 13:40:26 +02:00
parent 6151957b0e
commit 2dbdf996e5
6 changed files with 38 additions and 947 deletions

View File

@@ -48,44 +48,3 @@ sealed class ExerciseApiData with _$ExerciseApiData {
factory ExerciseApiData.fromJson(Map<String, dynamic> json) => _$ExerciseApiDataFromJson(json);
}
/// Model for the search results returned from the /api/v2/exercise/search endpoint
///
@freezed
sealed class ExerciseSearchDetails with _$ExerciseSearchDetails {
factory ExerciseSearchDetails({
// ignore: invalid_annotation_target
@JsonKey(name: 'id') required int translationId,
// ignore: invalid_annotation_target
@JsonKey(name: 'base_id') required int exerciseId,
required String name,
required String category,
required String? image,
// ignore: invalid_annotation_target
@JsonKey(name: 'image_thumbnail') required String? imageThumbnail,
}) = _ExerciseSearchDetails;
factory ExerciseSearchDetails.fromJson(Map<String, dynamic> json) =>
_$ExerciseSearchDetailsFromJson(json);
}
@freezed
sealed class ExerciseSearchEntry with _$ExerciseSearchEntry {
factory ExerciseSearchEntry({
required String value,
required ExerciseSearchDetails data,
}) = _ExerciseSearchEntry;
factory ExerciseSearchEntry.fromJson(Map<String, dynamic> json) =>
_$ExerciseSearchEntryFromJson(json);
}
@freezed
sealed class ExerciseApiSearch with _$ExerciseApiSearch {
factory ExerciseApiSearch({
required List<ExerciseSearchEntry> suggestions,
}) = _ExerciseApiSearch;
factory ExerciseApiSearch.fromJson(Map<String, dynamic> json) =>
_$ExerciseApiSearchFromJson(json);
}

View File

@@ -386,825 +386,6 @@ as List<String>,
}
}
/// @nodoc
mixin _$ExerciseSearchDetails {
// ignore: invalid_annotation_target
@JsonKey(name: 'id') int get translationId;// ignore: invalid_annotation_target
@JsonKey(name: 'base_id') int get exerciseId; String get name; String get category; String? get image;// ignore: invalid_annotation_target
@JsonKey(name: 'image_thumbnail') String? get imageThumbnail;
/// Create a copy of ExerciseSearchDetails
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$ExerciseSearchDetailsCopyWith<ExerciseSearchDetails> get copyWith => _$ExerciseSearchDetailsCopyWithImpl<ExerciseSearchDetails>(this as ExerciseSearchDetails, _$identity);
/// Serializes this ExerciseSearchDetails to a JSON map.
Map<String, dynamic> toJson();
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is ExerciseSearchDetails&&(identical(other.translationId, translationId) || other.translationId == translationId)&&(identical(other.exerciseId, exerciseId) || other.exerciseId == exerciseId)&&(identical(other.name, name) || other.name == name)&&(identical(other.category, category) || other.category == category)&&(identical(other.image, image) || other.image == image)&&(identical(other.imageThumbnail, imageThumbnail) || other.imageThumbnail == imageThumbnail));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,translationId,exerciseId,name,category,image,imageThumbnail);
@override
String toString() {
return 'ExerciseSearchDetails(translationId: $translationId, exerciseId: $exerciseId, name: $name, category: $category, image: $image, imageThumbnail: $imageThumbnail)';
}
}
/// @nodoc
abstract mixin class $ExerciseSearchDetailsCopyWith<$Res> {
factory $ExerciseSearchDetailsCopyWith(ExerciseSearchDetails value, $Res Function(ExerciseSearchDetails) _then) = _$ExerciseSearchDetailsCopyWithImpl;
@useResult
$Res call({
@JsonKey(name: 'id') int translationId,@JsonKey(name: 'base_id') int exerciseId, String name, String category, String? image,@JsonKey(name: 'image_thumbnail') String? imageThumbnail
});
}
/// @nodoc
class _$ExerciseSearchDetailsCopyWithImpl<$Res>
implements $ExerciseSearchDetailsCopyWith<$Res> {
_$ExerciseSearchDetailsCopyWithImpl(this._self, this._then);
final ExerciseSearchDetails _self;
final $Res Function(ExerciseSearchDetails) _then;
/// Create a copy of ExerciseSearchDetails
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? translationId = null,Object? exerciseId = null,Object? name = null,Object? category = null,Object? image = freezed,Object? imageThumbnail = freezed,}) {
return _then(_self.copyWith(
translationId: null == translationId ? _self.translationId : translationId // ignore: cast_nullable_to_non_nullable
as int,exerciseId: null == exerciseId ? _self.exerciseId : exerciseId // ignore: cast_nullable_to_non_nullable
as int,name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
as String,category: null == category ? _self.category : category // ignore: cast_nullable_to_non_nullable
as String,image: freezed == image ? _self.image : image // ignore: cast_nullable_to_non_nullable
as String?,imageThumbnail: freezed == imageThumbnail ? _self.imageThumbnail : imageThumbnail // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
/// Adds pattern-matching-related methods to [ExerciseSearchDetails].
extension ExerciseSearchDetailsPatterns on ExerciseSearchDetails {
/// A variant of `map` that fallback to returning `orElse`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeMap<TResult extends Object?>(TResult Function( _ExerciseSearchDetails value)? $default,{required TResult orElse(),}){
final _that = this;
switch (_that) {
case _ExerciseSearchDetails() when $default != null:
return $default(_that);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// Callbacks receives the raw object, upcasted.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case final Subclass2 value:
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult map<TResult extends Object?>(TResult Function( _ExerciseSearchDetails value) $default,){
final _that = this;
switch (_that) {
case _ExerciseSearchDetails():
return $default(_that);}
}
/// A variant of `map` that fallback to returning `null`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(TResult? Function( _ExerciseSearchDetails value)? $default,){
final _that = this;
switch (_that) {
case _ExerciseSearchDetails() when $default != null:
return $default(_that);case _:
return null;
}
}
/// A variant of `when` that fallback to an `orElse` callback.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function(@JsonKey(name: 'id') int translationId, @JsonKey(name: 'base_id') int exerciseId, String name, String category, String? image, @JsonKey(name: 'image_thumbnail') String? imageThumbnail)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _ExerciseSearchDetails() when $default != null:
return $default(_that.translationId,_that.exerciseId,_that.name,_that.category,_that.image,_that.imageThumbnail);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// As opposed to `map`, this offers destructuring.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case Subclass2(:final field2):
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function(@JsonKey(name: 'id') int translationId, @JsonKey(name: 'base_id') int exerciseId, String name, String category, String? image, @JsonKey(name: 'image_thumbnail') String? imageThumbnail) $default,) {final _that = this;
switch (_that) {
case _ExerciseSearchDetails():
return $default(_that.translationId,_that.exerciseId,_that.name,_that.category,_that.image,_that.imageThumbnail);}
}
/// A variant of `when` that fallback to returning `null`
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function(@JsonKey(name: 'id') int translationId, @JsonKey(name: 'base_id') int exerciseId, String name, String category, String? image, @JsonKey(name: 'image_thumbnail') String? imageThumbnail)? $default,) {final _that = this;
switch (_that) {
case _ExerciseSearchDetails() when $default != null:
return $default(_that.translationId,_that.exerciseId,_that.name,_that.category,_that.image,_that.imageThumbnail);case _:
return null;
}
}
}
/// @nodoc
@JsonSerializable()
class _ExerciseSearchDetails implements ExerciseSearchDetails {
_ExerciseSearchDetails({@JsonKey(name: 'id') required this.translationId, @JsonKey(name: 'base_id') required this.exerciseId, required this.name, required this.category, required this.image, @JsonKey(name: 'image_thumbnail') required this.imageThumbnail});
factory _ExerciseSearchDetails.fromJson(Map<String, dynamic> json) => _$ExerciseSearchDetailsFromJson(json);
// ignore: invalid_annotation_target
@override@JsonKey(name: 'id') final int translationId;
// ignore: invalid_annotation_target
@override@JsonKey(name: 'base_id') final int exerciseId;
@override final String name;
@override final String category;
@override final String? image;
// ignore: invalid_annotation_target
@override@JsonKey(name: 'image_thumbnail') final String? imageThumbnail;
/// Create a copy of ExerciseSearchDetails
/// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
_$ExerciseSearchDetailsCopyWith<_ExerciseSearchDetails> get copyWith => __$ExerciseSearchDetailsCopyWithImpl<_ExerciseSearchDetails>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$ExerciseSearchDetailsToJson(this, );
}
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ExerciseSearchDetails&&(identical(other.translationId, translationId) || other.translationId == translationId)&&(identical(other.exerciseId, exerciseId) || other.exerciseId == exerciseId)&&(identical(other.name, name) || other.name == name)&&(identical(other.category, category) || other.category == category)&&(identical(other.image, image) || other.image == image)&&(identical(other.imageThumbnail, imageThumbnail) || other.imageThumbnail == imageThumbnail));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,translationId,exerciseId,name,category,image,imageThumbnail);
@override
String toString() {
return 'ExerciseSearchDetails(translationId: $translationId, exerciseId: $exerciseId, name: $name, category: $category, image: $image, imageThumbnail: $imageThumbnail)';
}
}
/// @nodoc
abstract mixin class _$ExerciseSearchDetailsCopyWith<$Res> implements $ExerciseSearchDetailsCopyWith<$Res> {
factory _$ExerciseSearchDetailsCopyWith(_ExerciseSearchDetails value, $Res Function(_ExerciseSearchDetails) _then) = __$ExerciseSearchDetailsCopyWithImpl;
@override @useResult
$Res call({
@JsonKey(name: 'id') int translationId,@JsonKey(name: 'base_id') int exerciseId, String name, String category, String? image,@JsonKey(name: 'image_thumbnail') String? imageThumbnail
});
}
/// @nodoc
class __$ExerciseSearchDetailsCopyWithImpl<$Res>
implements _$ExerciseSearchDetailsCopyWith<$Res> {
__$ExerciseSearchDetailsCopyWithImpl(this._self, this._then);
final _ExerciseSearchDetails _self;
final $Res Function(_ExerciseSearchDetails) _then;
/// Create a copy of ExerciseSearchDetails
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? translationId = null,Object? exerciseId = null,Object? name = null,Object? category = null,Object? image = freezed,Object? imageThumbnail = freezed,}) {
return _then(_ExerciseSearchDetails(
translationId: null == translationId ? _self.translationId : translationId // ignore: cast_nullable_to_non_nullable
as int,exerciseId: null == exerciseId ? _self.exerciseId : exerciseId // ignore: cast_nullable_to_non_nullable
as int,name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
as String,category: null == category ? _self.category : category // ignore: cast_nullable_to_non_nullable
as String,image: freezed == image ? _self.image : image // ignore: cast_nullable_to_non_nullable
as String?,imageThumbnail: freezed == imageThumbnail ? _self.imageThumbnail : imageThumbnail // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
/// @nodoc
mixin _$ExerciseSearchEntry {
String get value; ExerciseSearchDetails get data;
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$ExerciseSearchEntryCopyWith<ExerciseSearchEntry> get copyWith => _$ExerciseSearchEntryCopyWithImpl<ExerciseSearchEntry>(this as ExerciseSearchEntry, _$identity);
/// Serializes this ExerciseSearchEntry to a JSON map.
Map<String, dynamic> toJson();
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is ExerciseSearchEntry&&(identical(other.value, value) || other.value == value)&&(identical(other.data, data) || other.data == data));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,value,data);
@override
String toString() {
return 'ExerciseSearchEntry(value: $value, data: $data)';
}
}
/// @nodoc
abstract mixin class $ExerciseSearchEntryCopyWith<$Res> {
factory $ExerciseSearchEntryCopyWith(ExerciseSearchEntry value, $Res Function(ExerciseSearchEntry) _then) = _$ExerciseSearchEntryCopyWithImpl;
@useResult
$Res call({
String value, ExerciseSearchDetails data
});
$ExerciseSearchDetailsCopyWith<$Res> get data;
}
/// @nodoc
class _$ExerciseSearchEntryCopyWithImpl<$Res>
implements $ExerciseSearchEntryCopyWith<$Res> {
_$ExerciseSearchEntryCopyWithImpl(this._self, this._then);
final ExerciseSearchEntry _self;
final $Res Function(ExerciseSearchEntry) _then;
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? value = null,Object? data = null,}) {
return _then(_self.copyWith(
value: null == value ? _self.value : value // ignore: cast_nullable_to_non_nullable
as String,data: null == data ? _self.data : data // ignore: cast_nullable_to_non_nullable
as ExerciseSearchDetails,
));
}
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$ExerciseSearchDetailsCopyWith<$Res> get data {
return $ExerciseSearchDetailsCopyWith<$Res>(_self.data, (value) {
return _then(_self.copyWith(data: value));
});
}
}
/// Adds pattern-matching-related methods to [ExerciseSearchEntry].
extension ExerciseSearchEntryPatterns on ExerciseSearchEntry {
/// A variant of `map` that fallback to returning `orElse`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeMap<TResult extends Object?>(TResult Function( _ExerciseSearchEntry value)? $default,{required TResult orElse(),}){
final _that = this;
switch (_that) {
case _ExerciseSearchEntry() when $default != null:
return $default(_that);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// Callbacks receives the raw object, upcasted.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case final Subclass2 value:
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult map<TResult extends Object?>(TResult Function( _ExerciseSearchEntry value) $default,){
final _that = this;
switch (_that) {
case _ExerciseSearchEntry():
return $default(_that);}
}
/// A variant of `map` that fallback to returning `null`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(TResult? Function( _ExerciseSearchEntry value)? $default,){
final _that = this;
switch (_that) {
case _ExerciseSearchEntry() when $default != null:
return $default(_that);case _:
return null;
}
}
/// A variant of `when` that fallback to an `orElse` callback.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String value, ExerciseSearchDetails data)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _ExerciseSearchEntry() when $default != null:
return $default(_that.value,_that.data);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// As opposed to `map`, this offers destructuring.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case Subclass2(:final field2):
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String value, ExerciseSearchDetails data) $default,) {final _that = this;
switch (_that) {
case _ExerciseSearchEntry():
return $default(_that.value,_that.data);}
}
/// A variant of `when` that fallback to returning `null`
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String value, ExerciseSearchDetails data)? $default,) {final _that = this;
switch (_that) {
case _ExerciseSearchEntry() when $default != null:
return $default(_that.value,_that.data);case _:
return null;
}
}
}
/// @nodoc
@JsonSerializable()
class _ExerciseSearchEntry implements ExerciseSearchEntry {
_ExerciseSearchEntry({required this.value, required this.data});
factory _ExerciseSearchEntry.fromJson(Map<String, dynamic> json) => _$ExerciseSearchEntryFromJson(json);
@override final String value;
@override final ExerciseSearchDetails data;
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
_$ExerciseSearchEntryCopyWith<_ExerciseSearchEntry> get copyWith => __$ExerciseSearchEntryCopyWithImpl<_ExerciseSearchEntry>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$ExerciseSearchEntryToJson(this, );
}
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ExerciseSearchEntry&&(identical(other.value, value) || other.value == value)&&(identical(other.data, data) || other.data == data));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,value,data);
@override
String toString() {
return 'ExerciseSearchEntry(value: $value, data: $data)';
}
}
/// @nodoc
abstract mixin class _$ExerciseSearchEntryCopyWith<$Res> implements $ExerciseSearchEntryCopyWith<$Res> {
factory _$ExerciseSearchEntryCopyWith(_ExerciseSearchEntry value, $Res Function(_ExerciseSearchEntry) _then) = __$ExerciseSearchEntryCopyWithImpl;
@override @useResult
$Res call({
String value, ExerciseSearchDetails data
});
@override $ExerciseSearchDetailsCopyWith<$Res> get data;
}
/// @nodoc
class __$ExerciseSearchEntryCopyWithImpl<$Res>
implements _$ExerciseSearchEntryCopyWith<$Res> {
__$ExerciseSearchEntryCopyWithImpl(this._self, this._then);
final _ExerciseSearchEntry _self;
final $Res Function(_ExerciseSearchEntry) _then;
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? value = null,Object? data = null,}) {
return _then(_ExerciseSearchEntry(
value: null == value ? _self.value : value // ignore: cast_nullable_to_non_nullable
as String,data: null == data ? _self.data : data // ignore: cast_nullable_to_non_nullable
as ExerciseSearchDetails,
));
}
/// Create a copy of ExerciseSearchEntry
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$ExerciseSearchDetailsCopyWith<$Res> get data {
return $ExerciseSearchDetailsCopyWith<$Res>(_self.data, (value) {
return _then(_self.copyWith(data: value));
});
}
}
/// @nodoc
mixin _$ExerciseApiSearch {
List<ExerciseSearchEntry> get suggestions;
/// Create a copy of ExerciseApiSearch
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$ExerciseApiSearchCopyWith<ExerciseApiSearch> get copyWith => _$ExerciseApiSearchCopyWithImpl<ExerciseApiSearch>(this as ExerciseApiSearch, _$identity);
/// Serializes this ExerciseApiSearch to a JSON map.
Map<String, dynamic> toJson();
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is ExerciseApiSearch&&const DeepCollectionEquality().equals(other.suggestions, suggestions));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(suggestions));
@override
String toString() {
return 'ExerciseApiSearch(suggestions: $suggestions)';
}
}
/// @nodoc
abstract mixin class $ExerciseApiSearchCopyWith<$Res> {
factory $ExerciseApiSearchCopyWith(ExerciseApiSearch value, $Res Function(ExerciseApiSearch) _then) = _$ExerciseApiSearchCopyWithImpl;
@useResult
$Res call({
List<ExerciseSearchEntry> suggestions
});
}
/// @nodoc
class _$ExerciseApiSearchCopyWithImpl<$Res>
implements $ExerciseApiSearchCopyWith<$Res> {
_$ExerciseApiSearchCopyWithImpl(this._self, this._then);
final ExerciseApiSearch _self;
final $Res Function(ExerciseApiSearch) _then;
/// Create a copy of ExerciseApiSearch
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? suggestions = null,}) {
return _then(_self.copyWith(
suggestions: null == suggestions ? _self.suggestions : suggestions // ignore: cast_nullable_to_non_nullable
as List<ExerciseSearchEntry>,
));
}
}
/// Adds pattern-matching-related methods to [ExerciseApiSearch].
extension ExerciseApiSearchPatterns on ExerciseApiSearch {
/// A variant of `map` that fallback to returning `orElse`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeMap<TResult extends Object?>(TResult Function( _ExerciseApiSearch value)? $default,{required TResult orElse(),}){
final _that = this;
switch (_that) {
case _ExerciseApiSearch() when $default != null:
return $default(_that);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// Callbacks receives the raw object, upcasted.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case final Subclass2 value:
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult map<TResult extends Object?>(TResult Function( _ExerciseApiSearch value) $default,){
final _that = this;
switch (_that) {
case _ExerciseApiSearch():
return $default(_that);}
}
/// A variant of `map` that fallback to returning `null`.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case final Subclass value:
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(TResult? Function( _ExerciseApiSearch value)? $default,){
final _that = this;
switch (_that) {
case _ExerciseApiSearch() when $default != null:
return $default(_that);case _:
return null;
}
}
/// A variant of `when` that fallback to an `orElse` callback.
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return orElse();
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( List<ExerciseSearchEntry> suggestions)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _ExerciseApiSearch() when $default != null:
return $default(_that.suggestions);case _:
return orElse();
}
}
/// A `switch`-like method, using callbacks.
///
/// As opposed to `map`, this offers destructuring.
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case Subclass2(:final field2):
/// return ...;
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( List<ExerciseSearchEntry> suggestions) $default,) {final _that = this;
switch (_that) {
case _ExerciseApiSearch():
return $default(_that.suggestions);}
}
/// A variant of `when` that fallback to returning `null`
///
/// It is equivalent to doing:
/// ```dart
/// switch (sealedClass) {
/// case Subclass(:final field):
/// return ...;
/// case _:
/// return null;
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( List<ExerciseSearchEntry> suggestions)? $default,) {final _that = this;
switch (_that) {
case _ExerciseApiSearch() when $default != null:
return $default(_that.suggestions);case _:
return null;
}
}
}
/// @nodoc
@JsonSerializable()
class _ExerciseApiSearch implements ExerciseApiSearch {
_ExerciseApiSearch({required final List<ExerciseSearchEntry> suggestions}): _suggestions = suggestions;
factory _ExerciseApiSearch.fromJson(Map<String, dynamic> json) => _$ExerciseApiSearchFromJson(json);
final List<ExerciseSearchEntry> _suggestions;
@override List<ExerciseSearchEntry> get suggestions {
if (_suggestions is EqualUnmodifiableListView) return _suggestions;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_suggestions);
}
/// Create a copy of ExerciseApiSearch
/// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
_$ExerciseApiSearchCopyWith<_ExerciseApiSearch> get copyWith => __$ExerciseApiSearchCopyWithImpl<_ExerciseApiSearch>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$ExerciseApiSearchToJson(this, );
}
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ExerciseApiSearch&&const DeepCollectionEquality().equals(other._suggestions, _suggestions));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(_suggestions));
@override
String toString() {
return 'ExerciseApiSearch(suggestions: $suggestions)';
}
}
/// @nodoc
abstract mixin class _$ExerciseApiSearchCopyWith<$Res> implements $ExerciseApiSearchCopyWith<$Res> {
factory _$ExerciseApiSearchCopyWith(_ExerciseApiSearch value, $Res Function(_ExerciseApiSearch) _then) = __$ExerciseApiSearchCopyWithImpl;
@override @useResult
$Res call({
List<ExerciseSearchEntry> suggestions
});
}
/// @nodoc
class __$ExerciseApiSearchCopyWithImpl<$Res>
implements _$ExerciseApiSearchCopyWith<$Res> {
__$ExerciseApiSearchCopyWithImpl(this._self, this._then);
final _ExerciseApiSearch _self;
final $Res Function(_ExerciseApiSearch) _then;
/// Create a copy of ExerciseApiSearch
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? suggestions = null,}) {
return _then(_ExerciseApiSearch(
suggestions: null == suggestions ? _self._suggestions : suggestions // ignore: cast_nullable_to_non_nullable
as List<ExerciseSearchEntry>,
));
}
}
// dart format on

View File

@@ -57,47 +57,3 @@ Map<String, dynamic> _$ExerciseBaseDataToJson(_ExerciseBaseData instance) => <St
'author_history': instance.authors,
'total_authors_history': instance.authorsGlobal,
};
_ExerciseSearchDetails _$ExerciseSearchDetailsFromJson(
Map<String, dynamic> json,
) => _ExerciseSearchDetails(
translationId: (json['id'] as num).toInt(),
exerciseId: (json['base_id'] as num).toInt(),
name: json['name'] as String,
category: json['category'] as String,
image: json['image'] as String?,
imageThumbnail: json['image_thumbnail'] as String?,
);
Map<String, dynamic> _$ExerciseSearchDetailsToJson(
_ExerciseSearchDetails instance,
) => <String, dynamic>{
'id': instance.translationId,
'base_id': instance.exerciseId,
'name': instance.name,
'category': instance.category,
'image': instance.image,
'image_thumbnail': instance.imageThumbnail,
};
_ExerciseSearchEntry _$ExerciseSearchEntryFromJson(Map<String, dynamic> json) =>
_ExerciseSearchEntry(
value: json['value'] as String,
data: ExerciseSearchDetails.fromJson(
json['data'] as Map<String, dynamic>,
),
);
Map<String, dynamic> _$ExerciseSearchEntryToJson(
_ExerciseSearchEntry instance,
) => <String, dynamic>{'value': instance.value, 'data': instance.data};
_ExerciseApiSearch _$ExerciseApiSearchFromJson(Map<String, dynamic> json) => _ExerciseApiSearch(
suggestions: (json['suggestions'] as List<dynamic>)
.map((e) => ExerciseSearchEntry.fromJson(e as Map<String, dynamic>))
.toList(),
);
Map<String, dynamic> _$ExerciseApiSearchToJson(_ExerciseApiSearch instance) => <String, dynamic>{
'suggestions': instance.suggestions,
};

View File

@@ -49,7 +49,6 @@ class ExercisesProvider with ChangeNotifier {
static const exerciseUrlPath = 'exercise';
static const exerciseInfoUrlPath = 'exerciseinfo';
static const exerciseSearchPath = 'exercise/search';
static const categoriesUrlPath = 'exercisecategory';
static const musclesUrlPath = 'muscle';
@@ -676,6 +675,10 @@ 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,
@@ -693,24 +696,25 @@ class ExercisesProvider with ChangeNotifier {
// Send the request
final result = await baseProvider.fetch(
baseProvider.makeUrl(
exerciseSearchPath,
query: {'term': name, 'language': languages.join(',')},
exerciseInfoUrlPath,
query: {
'name__search': name,
'language__code': languages.join(','),
'limit': API_RESULTS_PAGE_SIZE,
},
),
);
// Load the exercises
final results = ExerciseApiSearch.fromJson(result);
final ids = (result['results'] as List).map<int>((data) => data['id'] as int).toList();
final List<Exercise> out = [];
for (final result in results.suggestions) {
final exercise = await fetchAndSetExercise(result.data.exerciseId);
for (final id in ids) {
final exercise = await fetchAndSetExercise(id);
if (exercise != null) {
out.add(exercise);
}
}
// return Future.wait(
// results.suggestions.map((e) => fetchAndSetExercise(e.data.exerciseId)),
// );
return out;
}

View File

@@ -48,7 +48,7 @@ void main() {
const String muscleUrl = 'muscle';
const String equipmentUrl = 'equipment';
const String languageUrl = 'language';
const String searchExerciseUrl = 'exercise/search';
const String exerciseSearchUrl = 'exerciseinfo';
final Uri tCategoryEntriesUri = Uri(
scheme: 'http',
@@ -83,7 +83,7 @@ void main() {
final Uri tSearchByNameUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$searchExerciseUrl/',
path: 'api/v2/$exerciseSearchUrl/',
);
const category1 = ExerciseCategory(id: 1, name: 'Arms');
@@ -364,22 +364,39 @@ void main() {
setUp(() {
const String tSearchTerm = 'press';
const String tSearchLanguage = 'en';
final Map<String, dynamic> query = {'term': tSearchTerm, 'language': tSearchLanguage};
final Map<String, dynamic> query = {
'name__search': tSearchTerm,
'language__code': tSearchLanguage,
'limit': '100',
};
tSearchByNameUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$searchExerciseUrl/',
path: 'api/v2/$exerciseSearchUrl/',
queryParameters: query,
);
final Map<String, dynamic> tSearchResponse = jsonDecode(
fixture('exercises/exercise_search_entries.json'),
final exerciseInfoEntry = jsonDecode(
fixture('exercises/exerciseinfo_response.json'),
);
final Map<String, dynamic> tSearchResponse = {
'count': 2,
'next': null,
'previous': null,
'results': [
{...exerciseInfoEntry, 'id': 1},
{...exerciseInfoEntry, 'id': 2},
],
};
// Mock exercise search
when(
mockBaseProvider.makeUrl(
searchExerciseUrl,
query: {'term': tSearchTerm, 'language': tSearchLanguage},
exerciseSearchUrl,
query: {
'name__search': tSearchTerm,
'language__code': tSearchLanguage,
'limit': '100',
},
),
).thenReturn(tSearchByNameUri);
when(mockBaseProvider.fetch(tSearchByNameUri)).thenAnswer((_) async => tSearchResponse);

View File

@@ -1,26 +0,0 @@
{
"suggestions": [
{
"value": "Bench Press Narrow Grip",
"data": {
"id": 1,
"base_id": 1,
"name": "test exercise 1",
"category": "Arms",
"image": "/media/exercise-images/76/Narrow-grip-bench-press-2.png",
"image_thumbnail": "/media/exercise-images/76/Narrow-grip-bench-press-2.png.30x30_q85_crop-smart.png"
}
},
{
"value": "Close-grip Bench Press",
"data": {
"id": 2,
"base_id": 2,
"name": "test exercise 2",
"category": "Arms",
"image": null,
"image_thumbnail": null
}
}
]
}