Files
eatvio_app/lib/features/complaining/presentation/cubit/complaining_cubit.dart

83 lines
3.1 KiB
Dart

import 'dart:developer';
import 'package:eatvio_app/core/enums/complained_content.dart';
import 'package:eatvio_app/features/complaining/presentation/complaining_bottom_sheet.dart';
import 'package:eatvio_app/features/complaining/presentation/cubit/complaining_state.dart';
import 'package:eatvio_app/features/complaining/repositories/complaining_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ComplainingCubit extends Cubit<ComplainingState> {
final ComplainingRepository repository;
ComplainingCubit({required this.repository}) : super(ComplainingState());
void setContentTypeAndIdentifier({required ComplainedContent contentType, int? id, String? slug}) {
emit(state.copyWith(contentType: contentType, contentId: id, contentSlug: slug));
}
void setCategory(ComplainType category) {
emit(state.copyWith(category: category));
}
void setTags(List<String> tags) {
emit(state.copyWith.complainInfo(tags: tags));
}
void toggleDescription() {
if (state.complainInfo.description != null) {
emit(state.copyWith.complainInfo(description: null));
} else {
emit(state.copyWith.complainInfo(description: ''));
}
}
void setDescription(String description) {
emit(state.copyWith.complainInfo(description: description));
}
Future<void> complain() async {
try {
final category = state.category;
final id = state.contentId;
final slug = state.contentSlug;
final contentType = state.contentType;
if (category != null && contentType != null) {
switch (contentType) {
case ComplainedContent.diet:
if (id != null) {
await repository.sendDietComplain(id: id, category: category.getValue(), body: state.complainInfo);
emit(state.copyWith(wasSentSuccessfully: true));
return;
}
case ComplainedContent.plan:
if (slug != null) {
await repository.sendPlanComplain(slug: slug, category: category.getValue(), body: state.complainInfo);
emit(state.copyWith(wasSentSuccessfully: true));
return;
}
case ComplainedContent.recipe:
if (slug != null) {
await repository.sendRecipeComplain(slug: slug, category: category.getValue(), body: state.complainInfo);
emit(state.copyWith(wasSentSuccessfully: true));
return;
}
case ComplainedContent.profile:
if (slug != null) {
await repository.sendProfileComplain(slug: slug, category: category.getValue(), body: state.complainInfo);
emit(state.copyWith(wasSentSuccessfully: true));
return;
}
case ComplainedContent.posts:
if (id != null) {
await repository.sendBlogPostsComplain(id: id, category: category.getValue(), body: state.complainInfo);
emit(state.copyWith(wasSentSuccessfully: true));
return;
}
}
}
} catch (e, st) {
log(e.toString(), stackTrace: st);
}
emit(state.copyWith(wasSentSuccessfully: false));
}
}