forked from eatvio/eatvio_app
feat(recipe): Models, service and repository for eecpe create. Recipe create now uses cubit
This commit is contained in:
170
lib/features/recipes/presentation/bloc/recipe_create_cubit.dart
Normal file
170
lib/features/recipes/presentation/bloc/recipe_create_cubit.dart
Normal file
@@ -0,0 +1,170 @@
|
||||
import 'package:eatvio_app/core/app_export.dart';
|
||||
import 'package:eatvio_app/features/recipes/models/ingredient.dart';
|
||||
import 'package:eatvio_app/features/recipes/presentation/bloc/recipe_create_state.dart';
|
||||
import 'package:eatvio_app/features/recipes/reporitories/recipe_repository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
class RecipeCreateCubit extends Cubit<RecipeCreateState> {
|
||||
final RecipeRepository recipeRepository;
|
||||
|
||||
RecipeCreateCubit({required this.recipeRepository})
|
||||
: super(const RecipeCreateState.loading());
|
||||
|
||||
loadRecipeCreate() {
|
||||
emit(const RecipeCreateStateLoading());
|
||||
try {
|
||||
emit(RecipeCreateState.loaded(
|
||||
picker: ImagePicker(),
|
||||
titleController: TextEditingController(),
|
||||
productNameController: TextEditingController(),
|
||||
ingredientController: TextEditingController(),
|
||||
stepDescriptionController: TextEditingController(),
|
||||
youtubeLinkController: TextEditingController(),
|
||||
stepControllers: [TextEditingController()],
|
||||
stepImages: [null],
|
||||
ingredients: [],
|
||||
numberOfSteps: 1,
|
||||
ingredientAsItems: false,
|
||||
types: [
|
||||
SelectionPopupModel(title: 'Первые блюда'),
|
||||
SelectionPopupModel(title: 'Вторые блюда'),
|
||||
SelectionPopupModel(title: 'Cалаты'),
|
||||
SelectionPopupModel(title: 'Перекусы/закуски'),
|
||||
SelectionPopupModel(title: 'Сладости'),
|
||||
SelectionPopupModel(title: 'Напитки'),
|
||||
SelectionPopupModel(title: 'Прочее'),
|
||||
],
|
||||
cookingTimes: [
|
||||
SelectionPopupModel(title: '10 минут'),
|
||||
SelectionPopupModel(title: '20 минут'),
|
||||
SelectionPopupModel(title: '30 минут'),
|
||||
SelectionPopupModel(title: '1 час'),
|
||||
SelectionPopupModel(title: '2 часа'),
|
||||
SelectionPopupModel(title: '10 дней'),
|
||||
],
|
||||
statuses: [
|
||||
SelectionPopupModel(title: 'Активна'),
|
||||
SelectionPopupModel(title: 'Неактивна'),
|
||||
],
|
||||
));
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
uploadRecipeImage(XFile? pickedFile) {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
var updatedState = currentState.copyWith(
|
||||
recipeImage: pickedFile,
|
||||
);
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
uploadStepImage(XFile? pickedFile, int stepNumber) {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
|
||||
var updatedStepImages = List<XFile?>.from(currentState.stepImages);
|
||||
|
||||
if (stepNumber >= 0 && stepNumber < updatedStepImages.length) {
|
||||
updatedStepImages[stepNumber] = pickedFile;
|
||||
}
|
||||
|
||||
var updatedState = currentState.copyWith(
|
||||
stepImages: updatedStepImages,
|
||||
);
|
||||
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
switchUnits(bool value) {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
var updatedState = currentState.copyWith(ingredientAsItems: value);
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
addIngredient(Ingredient ingredient) {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
|
||||
bool ingredientExists = currentState.ingredients.any(
|
||||
(existingIngredient) =>
|
||||
existingIngredient.product_name == ingredient.product_name);
|
||||
|
||||
if (ingredientExists) {
|
||||
emit(RecipeCreateState.error(
|
||||
errorMessage: 'Ингредиент уже существует.'));
|
||||
return;
|
||||
}
|
||||
|
||||
List<Ingredient> newIngredients =
|
||||
List<Ingredient>.from(currentState.ingredients);
|
||||
newIngredients.add(ingredient);
|
||||
|
||||
var updatedState = currentState.copyWith(ingredients: newIngredients);
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
addStep() {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
List<XFile?> newStepImages = List<XFile?>.from(currentState.stepImages);
|
||||
newStepImages.add(null);
|
||||
List<TextEditingController> newStepControllers =
|
||||
List<TextEditingController>.from(currentState.stepControllers);
|
||||
newStepControllers.add(TextEditingController());
|
||||
var updatedState = currentState.copyWith(
|
||||
stepImages: newStepImages,
|
||||
stepControllers: newStepControllers,
|
||||
numberOfSteps: currentState.numberOfSteps + 1);
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
removeStep() {
|
||||
try {
|
||||
if (state is RecipeCreateStateLoaded) {
|
||||
var currentState = state as RecipeCreateStateLoaded;
|
||||
List<XFile?> newStepImages = currentState.stepImages;
|
||||
newStepImages.removeLast();
|
||||
List<TextEditingController> newStepControllers =
|
||||
List<TextEditingController>.from(currentState.stepControllers);
|
||||
newStepControllers.removeLast();
|
||||
var updatedState = currentState.copyWith(
|
||||
stepImages: newStepImages,
|
||||
stepControllers: newStepControllers,
|
||||
numberOfSteps: currentState.numberOfSteps - 1);
|
||||
emit(updatedState);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RecipeCreateState.error(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:eatvio_app/features/recipes/models/recipe_model.dart';
|
||||
import 'package:eatvio_app/features/recipes/models/ingredient.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
import '../../../../data/models/selectionPopupModel/selection_popup_model.dart';
|
||||
|
||||
part 'recipe_create_state.freezed.dart';
|
||||
|
||||
@freezed
|
||||
abstract class RecipeCreateState with _$RecipeCreateState {
|
||||
const factory RecipeCreateState() = _RecipeCreateState;
|
||||
|
||||
const factory RecipeCreateState.loading() = RecipeCreateStateLoading;
|
||||
|
||||
const factory RecipeCreateState.loaded({
|
||||
RecipeModel? recipe,
|
||||
required ImagePicker picker,
|
||||
required TextEditingController titleController,
|
||||
required TextEditingController productNameController,
|
||||
required TextEditingController ingredientController,
|
||||
required TextEditingController stepDescriptionController,
|
||||
required TextEditingController youtubeLinkController,
|
||||
required List<TextEditingController> stepControllers,
|
||||
XFile? recipeImage,
|
||||
required List<XFile?> stepImages,
|
||||
required List<Ingredient> ingredients,
|
||||
required int numberOfSteps,
|
||||
required bool ingredientAsItems,
|
||||
required List<SelectionPopupModel> types,
|
||||
required List<SelectionPopupModel> cookingTimes,
|
||||
required List<SelectionPopupModel> statuses,
|
||||
}) = RecipeCreateStateLoaded;
|
||||
|
||||
const factory RecipeCreateState.error({
|
||||
required String errorMessage,
|
||||
}) = RecipeCreateStateError;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user