feat: implement recipe drafting functionality and refactor MyRecipesTab to be stateless

This commit is contained in:
mso7nueh
2026-02-16 12:46:55 +03:00
parent 516dfa4158
commit d4f41fe6cf
13 changed files with 388 additions and 153 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:eatvio_app/core/image_compressing/image_compress_utility.dart';
@@ -23,55 +24,59 @@ class RecipeRepository {
}
}
Future<void> publishRecipe(RecipeModel recipe, [bool update = false, String? slug]) async {
try {
if (recipe.image == null || (recipe.image?.isEmpty ?? true)) {
return;
}
final image = await ImageCompressUtility.compressFile(File(recipe.image ?? ''));
final multiPartFile = await MultipartFile.fromFile(image.path);
Future<FormData> _buildFormData(RecipeModel recipe, {String state = 'active'}) async {
if (recipe.image == null || (recipe.image?.isEmpty ?? true)) {
throw Exception('Recipe image is required');
}
final image = await ImageCompressUtility.compressFile(File(recipe.image ?? ''));
final multiPartFile = await MultipartFile.fromFile(image.path);
final ingredientsMap = recipe.ingredients?.map((ingredient) {
if (ingredient != null) {
return ingredient.toJson();
}
}).toList() ??
[];
final stepsMap = [];
for (final Step? step in recipe.steps ?? []) {
if (step != null) {
if ((step.image == null || (step.image?.isEmpty ?? true)) && step.description != null) {
stepsMap.add({
'description_md': step.description ?? "",
});
} else if (step.image != null && (step.image?.isNotEmpty ?? false)) {
final image = await ImageCompressUtility.compressFile(File(step.image ?? ''));
final multipartFile = await MultipartFile.fromFile(image.path);
stepsMap.add({
'description_md': step.description ?? "",
'image': multipartFile,
});
final ingredientsMap = recipe.ingredients?.map((ingredient) {
if (ingredient != null) {
return ingredient.toJson();
}
}).toList() ??
[];
final stepsMap = [];
for (final Step? step in recipe.steps ?? []) {
if (step != null) {
if ((step.image == null || (step.image?.isEmpty ?? true)) && step.description != null) {
stepsMap.add({
'description_md': step.description ?? "",
});
} else if (step.image != null && (step.image?.isNotEmpty ?? false)) {
final image = await ImageCompressUtility.compressFile(File(step.image ?? ''));
final multipartFile = await MultipartFile.fromFile(image.path);
stepsMap.add({
'description_md': step.description ?? "",
'image': multipartFile,
});
}
}
final formMap = {
'type': recipe.type,
'state': 'active',
'title': recipe.title,
'tags[]': recipe.tags ?? [],
'description_md': recipe.description,
'video_url': recipe.videoUrl,
'ingredients': ingredientsMap,
'steps': stepsMap,
'cooking_time': recipe.totalCookingTime,
'active_time': recipe.activeCookTime,
'image': multiPartFile,
'servings': recipe.servings,
'energy_per_serving': recipe.energyPerServing,
};
formMap.removeWhere((k, v) => v == null || (v is String && v.isEmpty));
final formData = FormData.fromMap(formMap);
}
final formMap = {
'type': recipe.type,
'state': state,
'title': recipe.title,
'tags[]': recipe.tags ?? [],
'description_md': recipe.description,
'video_url': recipe.videoUrl,
'ingredients': ingredientsMap,
'steps': stepsMap,
'cooking_time': recipe.totalCookingTime,
'active_time': recipe.activeCookTime,
'image': multiPartFile,
'servings': recipe.servings,
'energy_per_serving': recipe.energyPerServing,
};
formMap.removeWhere((k, v) => v == null || (v is String && v.isEmpty));
return FormData.fromMap(formMap);
}
Future<void> publishRecipe(RecipeModel recipe, [bool update = false, String? slug]) async {
try {
final formData = await _buildFormData(recipe, state: 'active');
if (update) {
await recipeService.updateRecipe(slug: slug!, formData: formData);
} else {
@@ -83,6 +88,38 @@ class RecipeRepository {
}
}
Future<void> saveAsDraft(RecipeModel recipe, [bool update = false, String? slug]) async {
try {
final formData = await _buildFormData(recipe, state: 'archived');
log(formData.fields.toString());
if (update) {
final response = await recipeService.updateRecipe(slug: slug!, formData: formData);
final recipeId = response.data['data']?['id'] ?? response.data['id'];
if (recipeId != null) {
await recipeService.draftRecipe(id: recipeId as int);
}
} else {
final response = await recipeService.publishRecipe(formData: formData);
final recipeId = response.data['data']?['id'] ?? response.data['id'];
if (recipeId != null) {
await recipeService.draftRecipe(id: recipeId as int);
}
}
} catch (e, s) {
await Sentry.captureException(e, stackTrace: s);
rethrow;
}
}
Future<void> draftExistingRecipe(int id) async {
try {
await recipeService.draftRecipe(id: id);
} catch (e, s) {
await Sentry.captureException(e, stackTrace: s);
rethrow;
}
}
Future<RecipeModel> getRecipe(String recipeID) async {
try {
return await recipeService.getRecipe(recipeID);