forked from eatvio/eatvio_app
refactor: profile model
This commit is contained in:
@@ -8,6 +8,10 @@ class RecipesCubit extends Cubit<RecipesState> {
|
||||
final FeedRepository feedRepository;
|
||||
final RecipeRepository recipeRepository;
|
||||
Set<int> _selectedTags = {};
|
||||
int currentPage = 1;
|
||||
late int lastPage;
|
||||
bool isLoading = false;
|
||||
String? _query;
|
||||
|
||||
RecipesCubit({
|
||||
required this.feedRepository,
|
||||
@@ -18,6 +22,7 @@ class RecipesCubit extends Cubit<RecipesState> {
|
||||
String? query,
|
||||
}) async {
|
||||
try {
|
||||
_query = query;
|
||||
final recipesList = await feedRepository.getRecipes(
|
||||
tags: _selectedTags.toList(),
|
||||
query: query,
|
||||
@@ -28,12 +33,42 @@ class RecipesCubit extends Cubit<RecipesState> {
|
||||
recipesTags: recipesTags,
|
||||
selectedTags: _selectedTags,
|
||||
selectedTagsCounter: _selectedTags.length,
|
||||
isLoading: isLoading,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(RecipesState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
loadNextPage() async {
|
||||
if (currentPage < lastPage && !isLoading) {
|
||||
isLoading = true;
|
||||
|
||||
emit((state as RecipesStateLoaded).copyWith(isLoading: isLoading));
|
||||
|
||||
try {
|
||||
if (currentPage < lastPage) {
|
||||
currentPage++;
|
||||
final recipesList = await feedRepository.getRecipes(
|
||||
page: currentPage,
|
||||
tags: _selectedTags.toList(),
|
||||
query: _query,
|
||||
);
|
||||
isLoading = false;
|
||||
emit((state as RecipesStateLoaded).copyWith(
|
||||
recipesList: [
|
||||
...(state as RecipesStateLoaded).recipesList,
|
||||
...recipesList,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipesState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateTags(RecipeTag tag) {
|
||||
_selectedTags.contains(tag.id) ? _selectedTags.remove(tag.id) : _selectedTags.add(tag.id);
|
||||
emit((state as RecipesStateLoaded).copyWith(
|
||||
|
||||
@@ -15,6 +15,7 @@ abstract class RecipesState with _$RecipesState {
|
||||
required List<RecipeTag> recipesTags,
|
||||
required Set<int> selectedTags,
|
||||
required int selectedTagsCounter,
|
||||
required bool isLoading,
|
||||
}) = RecipesStateLoaded;
|
||||
|
||||
const factory RecipesState.error({
|
||||
|
||||
@@ -24,7 +24,8 @@ mixin _$RecipesState {
|
||||
List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter)
|
||||
int selectedTagsCounter,
|
||||
bool isLoading)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) =>
|
||||
@@ -34,7 +35,7 @@ mixin _$RecipesState {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) =>
|
||||
@@ -44,7 +45,7 @@ mixin _$RecipesState {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? loading,
|
||||
TResult Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult Function(String errorMessage)? error,
|
||||
required TResult orElse(),
|
||||
@@ -93,6 +94,9 @@ class _$RecipesStateCopyWithImpl<$Res, $Val extends RecipesState>
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@@ -109,6 +113,9 @@ class __$$RecipesStateImplCopyWithImpl<$Res>
|
||||
__$$RecipesStateImplCopyWithImpl(
|
||||
_$RecipesStateImpl _value, $Res Function(_$RecipesStateImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@@ -139,7 +146,8 @@ class _$RecipesStateImpl implements _RecipesState {
|
||||
List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter)
|
||||
int selectedTagsCounter,
|
||||
bool isLoading)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) {
|
||||
@@ -152,7 +160,7 @@ class _$RecipesStateImpl implements _RecipesState {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) {
|
||||
@@ -165,7 +173,7 @@ class _$RecipesStateImpl implements _RecipesState {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? loading,
|
||||
TResult Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult Function(String errorMessage)? error,
|
||||
required TResult orElse(),
|
||||
@@ -232,6 +240,9 @@ class __$$RecipesStateLoadingImplCopyWithImpl<$Res>
|
||||
__$$RecipesStateLoadingImplCopyWithImpl(_$RecipesStateLoadingImpl _value,
|
||||
$Res Function(_$RecipesStateLoadingImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@@ -263,7 +274,8 @@ class _$RecipesStateLoadingImpl implements RecipesStateLoading {
|
||||
List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter)
|
||||
int selectedTagsCounter,
|
||||
bool isLoading)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) {
|
||||
@@ -276,7 +288,7 @@ class _$RecipesStateLoadingImpl implements RecipesStateLoading {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) {
|
||||
@@ -289,7 +301,7 @@ class _$RecipesStateLoadingImpl implements RecipesStateLoading {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? loading,
|
||||
TResult Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult Function(String errorMessage)? error,
|
||||
required TResult orElse(),
|
||||
@@ -352,7 +364,8 @@ abstract class _$$RecipesStateLoadedImplCopyWith<$Res> {
|
||||
{List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter});
|
||||
int selectedTagsCounter,
|
||||
bool isLoading});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@@ -363,6 +376,8 @@ class __$$RecipesStateLoadedImplCopyWithImpl<$Res>
|
||||
$Res Function(_$RecipesStateLoadedImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
@@ -370,6 +385,7 @@ class __$$RecipesStateLoadedImplCopyWithImpl<$Res>
|
||||
Object? recipesTags = null,
|
||||
Object? selectedTags = null,
|
||||
Object? selectedTagsCounter = null,
|
||||
Object? isLoading = null,
|
||||
}) {
|
||||
return _then(_$RecipesStateLoadedImpl(
|
||||
recipesList: null == recipesList
|
||||
@@ -388,6 +404,10 @@ class __$$RecipesStateLoadedImplCopyWithImpl<$Res>
|
||||
? _value.selectedTagsCounter
|
||||
: selectedTagsCounter // ignore: cast_nullable_to_non_nullable
|
||||
as int,
|
||||
isLoading: null == isLoading
|
||||
? _value.isLoading
|
||||
: isLoading // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -399,7 +419,8 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
{required final List<Recipes> recipesList,
|
||||
required final List<RecipeTag> recipesTags,
|
||||
required final Set<int> selectedTags,
|
||||
required this.selectedTagsCounter})
|
||||
required this.selectedTagsCounter,
|
||||
required this.isLoading})
|
||||
: _recipesList = recipesList,
|
||||
_recipesTags = recipesTags,
|
||||
_selectedTags = selectedTags;
|
||||
@@ -430,10 +451,12 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
|
||||
@override
|
||||
final int selectedTagsCounter;
|
||||
@override
|
||||
final bool isLoading;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'RecipesState.loaded(recipesList: $recipesList, recipesTags: $recipesTags, selectedTags: $selectedTags, selectedTagsCounter: $selectedTagsCounter)';
|
||||
return 'RecipesState.loaded(recipesList: $recipesList, recipesTags: $recipesTags, selectedTags: $selectedTags, selectedTagsCounter: $selectedTagsCounter, isLoading: $isLoading)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -448,7 +471,9 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
const DeepCollectionEquality()
|
||||
.equals(other._selectedTags, _selectedTags) &&
|
||||
(identical(other.selectedTagsCounter, selectedTagsCounter) ||
|
||||
other.selectedTagsCounter == selectedTagsCounter));
|
||||
other.selectedTagsCounter == selectedTagsCounter) &&
|
||||
(identical(other.isLoading, isLoading) ||
|
||||
other.isLoading == isLoading));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -457,9 +482,12 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
const DeepCollectionEquality().hash(_recipesList),
|
||||
const DeepCollectionEquality().hash(_recipesTags),
|
||||
const DeepCollectionEquality().hash(_selectedTags),
|
||||
selectedTagsCounter);
|
||||
selectedTagsCounter,
|
||||
isLoading);
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$RecipesStateLoadedImplCopyWith<_$RecipesStateLoadedImpl> get copyWith =>
|
||||
@@ -475,11 +503,13 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter)
|
||||
int selectedTagsCounter,
|
||||
bool isLoading)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) {
|
||||
return loaded(recipesList, recipesTags, selectedTags, selectedTagsCounter);
|
||||
return loaded(
|
||||
recipesList, recipesTags, selectedTags, selectedTagsCounter, isLoading);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -488,12 +518,12 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) {
|
||||
return loaded?.call(
|
||||
recipesList, recipesTags, selectedTags, selectedTagsCounter);
|
||||
recipesList, recipesTags, selectedTags, selectedTagsCounter, isLoading);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -502,14 +532,14 @@ class _$RecipesStateLoadedImpl implements RecipesStateLoaded {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? loading,
|
||||
TResult Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult Function(String errorMessage)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
if (loaded != null) {
|
||||
return loaded(
|
||||
recipesList, recipesTags, selectedTags, selectedTagsCounter);
|
||||
return loaded(recipesList, recipesTags, selectedTags, selectedTagsCounter,
|
||||
isLoading);
|
||||
}
|
||||
return orElse();
|
||||
}
|
||||
@@ -557,13 +587,18 @@ abstract class RecipesStateLoaded implements RecipesState {
|
||||
{required final List<Recipes> recipesList,
|
||||
required final List<RecipeTag> recipesTags,
|
||||
required final Set<int> selectedTags,
|
||||
required final int selectedTagsCounter}) = _$RecipesStateLoadedImpl;
|
||||
required final int selectedTagsCounter,
|
||||
required final bool isLoading}) = _$RecipesStateLoadedImpl;
|
||||
|
||||
List<Recipes> get recipesList;
|
||||
List<RecipeTag> get recipesTags;
|
||||
Set<int> get selectedTags;
|
||||
int get selectedTagsCounter;
|
||||
@JsonKey(ignore: true)
|
||||
bool get isLoading;
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$RecipesStateLoadedImplCopyWith<_$RecipesStateLoadedImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
@@ -585,6 +620,8 @@ class __$$RecipesStateErrorImplCopyWithImpl<$Res>
|
||||
$Res Function(_$RecipesStateErrorImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
@@ -624,7 +661,9 @@ class _$RecipesStateErrorImpl implements RecipesStateError {
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType, errorMessage);
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$RecipesStateErrorImplCopyWith<_$RecipesStateErrorImpl> get copyWith =>
|
||||
@@ -640,7 +679,8 @@ class _$RecipesStateErrorImpl implements RecipesStateError {
|
||||
List<Recipes> recipesList,
|
||||
List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags,
|
||||
int selectedTagsCounter)
|
||||
int selectedTagsCounter,
|
||||
bool isLoading)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) {
|
||||
@@ -653,7 +693,7 @@ class _$RecipesStateErrorImpl implements RecipesStateError {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) {
|
||||
@@ -666,7 +706,7 @@ class _$RecipesStateErrorImpl implements RecipesStateError {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? loading,
|
||||
TResult Function(List<Recipes> recipesList, List<RecipeTag> recipesTags,
|
||||
Set<int> selectedTags, int selectedTagsCounter)?
|
||||
Set<int> selectedTags, int selectedTagsCounter, bool isLoading)?
|
||||
loaded,
|
||||
TResult Function(String errorMessage)? error,
|
||||
required TResult orElse(),
|
||||
@@ -720,7 +760,10 @@ abstract class RecipesStateError implements RecipesState {
|
||||
_$RecipesStateErrorImpl;
|
||||
|
||||
String get errorMessage;
|
||||
@JsonKey(ignore: true)
|
||||
|
||||
/// Create a copy of RecipesState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$RecipesStateErrorImplCopyWith<_$RecipesStateErrorImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user