forked from eatvio/eatvio_app
fixed layout of diets feed and added text styles
This commit is contained in:
@@ -1,15 +1,44 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/data/diet/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_state.dart';
|
||||
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class DietsCubit extends Cubit<DietsState> {
|
||||
final FeedRepository feedRepository;
|
||||
final DietRepository dietsRepository;
|
||||
int currentPage = 1;
|
||||
late int lastPage;
|
||||
bool isLoading = false;
|
||||
String? _query;
|
||||
late final StreamSubscription likesStateSubscription;
|
||||
late final StreamSubscription favoriteStateSubscription;
|
||||
late final StreamSubscription commentsStateSubscription;
|
||||
DietsCubit({
|
||||
required this.feedRepository,
|
||||
required this.dietsRepository,
|
||||
}) : super(const DietsState.loading()) {
|
||||
likesStateSubscription = dietsRepository.dietLikeState.listen((likesState) {
|
||||
_changeDietLikeState(likesState);
|
||||
});
|
||||
favoriteStateSubscription = dietsRepository.dietFavoriteState.listen((favoriteState) {
|
||||
_changeDietFavoriteState(favoriteState);
|
||||
});
|
||||
commentsStateSubscription = dietsRepository.dietCommentsState.listen((commentsState) {
|
||||
_changeDietCommentsState(commentsState);
|
||||
});
|
||||
}
|
||||
|
||||
DietsCubit({required this.feedRepository}) : super(const DietsState.loading());
|
||||
@override
|
||||
Future<void> close() async {
|
||||
likesStateSubscription.cancel();
|
||||
favoriteStateSubscription.cancel();
|
||||
commentsStateSubscription.cancel();
|
||||
super.close();
|
||||
}
|
||||
|
||||
loadDiets({
|
||||
String? query,
|
||||
@@ -20,14 +49,18 @@ class DietsCubit extends Cubit<DietsState> {
|
||||
query: query,
|
||||
);
|
||||
lastPage = dietsPagination.meta.lastPage;
|
||||
emit(DietsState.loaded(
|
||||
dietsList: dietsPagination.data,
|
||||
isLoading: isLoading,
|
||||
),);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: dietsPagination.data,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(DietsState.error(
|
||||
errorMessage: e.toString(),
|
||||
),);
|
||||
emit(
|
||||
DietsState.error(
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,4 +93,195 @@ class DietsCubit extends Cubit<DietsState> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> likeDiet(int id, int index) async {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[index];
|
||||
if (currentDiet.wasLikedByMe) {
|
||||
dietsForModification[index] = currentDiet.copyWith(
|
||||
totalLikes: currentDiet.totalLikes - 1,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
dietsForModification[index] = currentDiet.copyWith(
|
||||
totalLikes: currentDiet.totalLikes + 1,
|
||||
wasLikedByMe: true,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
EasyDebounce.debounce(
|
||||
'like_diet',
|
||||
Duration(milliseconds: 200),
|
||||
() async {
|
||||
try {
|
||||
await dietsRepository.likeDiet(id);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...currentDiets,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _changeDietLikeState(DietLikesState likesState) {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[likesState.index];
|
||||
|
||||
dietsForModification[likesState.index] = currentDiet.copyWith(
|
||||
totalLikes: likesState.totalLikes,
|
||||
wasLikedByMe: likesState.wasLikedByMe,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _changeDietFavoriteState(DietFavoriteState favoriteState) {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[favoriteState.index];
|
||||
|
||||
dietsForModification[favoriteState.index] = currentDiet.copyWith(
|
||||
isFavorite: favoriteState.isFavorite,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _changeDietCommentsState(DietCommentsState commentsState) {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[commentsState.index];
|
||||
|
||||
dietsForModification[commentsState.index] = currentDiet.copyWith(
|
||||
totalComments: commentsState.totalComments,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> addDietToFavorite(int id, int index) async {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[index];
|
||||
|
||||
dietsForModification[index] = currentDiet.copyWith(
|
||||
isFavorite: true,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_diet_from_favorite',
|
||||
Duration(milliseconds: 200),
|
||||
() async {
|
||||
try {
|
||||
await dietsRepository.addToFavorite(id);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...currentDiets,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> removeDietFromFavorite(int id, int index) async {
|
||||
if (state is! DietsStateLoaded) return;
|
||||
final currentDiets = (state as DietsStateLoaded).dietsList;
|
||||
final dietsForModification = [...currentDiets];
|
||||
final currentDiet = currentDiets[index];
|
||||
|
||||
dietsForModification[index] = currentDiet.copyWith(
|
||||
isFavorite: false,
|
||||
);
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...dietsForModification,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_diet_from_favorite',
|
||||
Duration(milliseconds: 200),
|
||||
() async {
|
||||
try {
|
||||
await dietsRepository.removeFromFavorite(id);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
emit(
|
||||
DietsState.loaded(
|
||||
dietsList: [
|
||||
...currentDiets,
|
||||
],
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user