feat(diary): Recipe create api route enabled

This commit is contained in:
2024-08-15 16:03:39 +03:00
parent bd9bf4843e
commit aced7e8bcb
15 changed files with 603 additions and 212 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/features/diary/models/product.dart';
import 'package:eatvio_app/features/recipes/models/ingredient.dart';
import 'package:eatvio_app/features/recipes/models/recipe_model.dart';
import 'package:eatvio_app/features/recipes/models/image.dart' as image;
@@ -49,8 +50,8 @@ class RecipeCreateScreen extends StatelessWidget {
);
}
Widget _buildRecipeCreate(
BuildContext context, RecipeCreateStateLoaded state) {
Widget _buildRecipeCreate(BuildContext context, RecipeCreateStateLoaded state) {
List<String> _options = state.autocompleteList.map((Product item) => item.name).toList();
return Scaffold(
appBar: AppBar(
toolbarHeight: 0,
@@ -86,18 +87,16 @@ class RecipeCreateScreen extends StatelessWidget {
Text('Тип блюда'),
SizedBox(height: 8),
CustomDropDown(
value: state.types[0],
value: state.selectedType,
items: state.types,
onChanged: changeType(context),
),
SizedBox(height: 16),
Text('Изображение'),
GestureDetector(
onTap: () async {
final pickedFile = await state.picker
.pickImage(source: ImageSource.gallery);
context
.read<RecipeCreateCubit>()
.uploadRecipeImage(pickedFile);
final pickedFile = await state.picker.pickImage(source: ImageSource.gallery);
context.read<RecipeCreateCubit>().uploadRecipeImage(pickedFile);
},
child: state.recipeImage == null
? Container(
@@ -121,31 +120,71 @@ class RecipeCreateScreen extends StatelessWidget {
],
),
)
: Image.file(File(state.recipeImage!.path),
height: 200, fit: BoxFit.cover),
: Image.file(File(state.recipeImage!.path), height: 200, fit: BoxFit.cover),
),
SizedBox(height: 16),
Text('Время приготовления'),
SizedBox(height: 8),
CustomDropDown(
items: state.cookingTimes,
value: state.cookingTimes[0],
value: state.selectedCookingTime,
onChanged: changeCookingTime(context),
),
SizedBox(height: 16),
Text('Статус'),
SizedBox(height: 8),
CustomDropDown(
items: state.statuses,
value: state.statuses[0],
value: state.selectedStatus,
onChanged: changeStatus(context),
),
SizedBox(height: 24),
if (!state.ingredients.isEmpty)
for (int j = 0; j < state.ingredients.length; j++)
_buildIngredient(state.ingredients[j]),
for (int j = 0; j < state.ingredients.length; j++) _buildIngredient(state.ingredients[j]),
SizedBox(height: 24),
CustomTextFormField(
hintText: 'Введите название продукта',
controller: state.productNameController,
RawAutocomplete<String>(
optionsBuilder: _buildOptions(_options),
fieldViewBuilder: (
BuildContext context,
productEditingController,
FocusNode focusNodeName,
VoidCallback onFieldSubmitted,
) {
return CustomTextFormField(
hintText: 'Введите название продукта',
controller: TextEditingController(text: state.productNameController.text),
focusNode: focusNodeName,
onChanged: getAutocomplete(context, focusNodeName),
);
},
optionsViewBuilder: (
BuildContext context,
AutocompleteOnSelected<String> onSelected,
Iterable<String> options,
) {
return Align(
alignment: Alignment.topLeft,
child: Material(
elevation: 4.0,
child: SizedBox(
height: 200.0,
child: ListView.builder(
padding: const EdgeInsets.all(8.0),
itemCount: options.length,
itemBuilder: (BuildContext context, int index) {
final String option = options.elementAt(index);
return GestureDetector(
onTap: selectProduct(onSelected, option, state),
child: ListTile(
title: Text(option),
),
);
},
),
),
),
);
},
),
SizedBox(height: 24),
CustomTextFormField(
@@ -153,18 +192,14 @@ class RecipeCreateScreen extends StatelessWidget {
fillColor: appTheme.gray50,
controller: state.ingredientController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
),
SizedBox(height: 8),
Row(children: [
Text('гр'),
Switch(
value: state.ingredientAsItems,
onChanged: (bool value) => context
.read<RecipeCreateCubit>()
.switchUnits(value),
onChanged: (bool value) => context.read<RecipeCreateCubit>().switchUnits(value),
activeColor: appTheme.blueA400,
inactiveThumbColor: Colors.grey,
inactiveTrackColor: Colors.grey.shade300,
@@ -187,16 +222,10 @@ class RecipeCreateScreen extends StatelessWidget {
'+',
style: theme.textTheme.titleSmall,
),
onPressed: () => context
.read<RecipeCreateCubit>()
.addIngredient(new Ingredient(
product_name:
state.productNameController.text,
portion: int.parse(
state.ingredientController.text),
unit: state.ingredientAsItems
? 'pcs'
: 'gr'))),
onPressed: () => context.read<RecipeCreateCubit>().addIngredient(new Ingredient(
product_name: state.productNameController.text,
portion: int.parse(state.ingredientController.text),
unit: state.ingredientAsItems ? 'pcs' : 'gr'))),
),
SizedBox(height: 24),
Text('Описание рецепта'),
@@ -207,8 +236,7 @@ class RecipeCreateScreen extends StatelessWidget {
controller: state.stepDescriptionController,
),
SizedBox(height: 24),
for (int i = 0; i < state.numberOfSteps; i++)
_buildStep(context, i, state),
for (int i = 0; i < state.numberOfSteps; i++) _buildStep(context, i, state),
SizedBox(height: 24),
Container(
height: 48,
@@ -221,8 +249,7 @@ class RecipeCreateScreen extends StatelessWidget {
borderRadius: BorderRadius.circular(8),
),
backgroundColor: appTheme.blueA400,
onPressed: () =>
context.read<RecipeCreateCubit>().addStep(),
onPressed: () => context.read<RecipeCreateCubit>().addStep(),
label: Text(
'+',
style: theme.textTheme.titleSmall,
@@ -241,8 +268,7 @@ class RecipeCreateScreen extends StatelessWidget {
borderRadius: BorderRadius.circular(8),
),
backgroundColor: Colors.grey,
onPressed: () =>
context.read<RecipeCreateCubit>().removeStep(),
onPressed: () => context.read<RecipeCreateCubit>().removeStep(),
label: Text(
'-',
style: theme.textTheme.titleSmall,
@@ -269,7 +295,7 @@ class RecipeCreateScreen extends StatelessWidget {
borderRadius: BorderRadius.circular(8),
),
backgroundColor: appTheme.blueA400,
onPressed: () {},
onPressed: () => context.read<RecipeCreateCubit>().publishRecipe(createRecipeFromState(state)),
label: Text(
'Опубликовать',
style: theme.textTheme.titleSmall,
@@ -285,23 +311,15 @@ class RecipeCreateScreen extends StatelessWidget {
);
}
_buildStep(
BuildContext context, int stepNumber, RecipeCreateStateLoaded state) {
_buildStep(BuildContext context, int stepNumber, RecipeCreateStateLoaded state) {
return Column(
children: [
Row(children: [
Text('Шаг ${stepNumber + 1}'),
Spacer(),
Text('Всего шагов: ${state.numberOfSteps}')
]),
Row(children: [Text('Шаг ${stepNumber + 1}'), Spacer(), Text('Всего шагов: ${state.numberOfSteps}')]),
SizedBox(height: 8),
GestureDetector(
onTap: () async {
final pickedFile =
await state.picker.pickImage(source: ImageSource.gallery);
context
.read<RecipeCreateCubit>()
.uploadStepImage(pickedFile, stepNumber);
final pickedFile = await state.picker.pickImage(source: ImageSource.gallery);
context.read<RecipeCreateCubit>().uploadStepImage(pickedFile, stepNumber);
},
child: state.stepImages[stepNumber] == null
? Container(
@@ -326,8 +344,7 @@ class RecipeCreateScreen extends StatelessWidget {
],
),
)
: Image.file(File(state.stepImages[stepNumber]!.path),
height: 150, fit: BoxFit.cover)),
: Image.file(File(state.stepImages[stepNumber]!.path), height: 150, fit: BoxFit.cover)),
SizedBox(height: 16),
CustomTextFormField(
hintText: 'Введите описание шага',
@@ -361,9 +378,7 @@ class RecipeCreateScreen extends StatelessWidget {
top: 5.v,
),
child: Text(
ingredient.product_name.length > 8
? ingredient.product_name.substring(0, 8)
: ingredient.product_name,
ingredient.product_name.length > 8 ? ingredient.product_name.substring(0, 8) : ingredient.product_name,
style: CustomTextStyles.titleMedium16,
),
),
@@ -374,9 +389,7 @@ class RecipeCreateScreen extends StatelessWidget {
bottom: 3.v,
),
child: Text(
ingredient.portion.toString() +
' ' +
ingredient.unit.toString().tr,
ingredient.portion.toString() + ' ' + ingredient.unit.toString().tr,
),
),
Spacer(),
@@ -395,35 +408,75 @@ class RecipeCreateScreen extends StatelessWidget {
);
}
_buildOptions(List<String> _options) {
return (TextEditingValue textEditingValue) {
return _options.where((String option) {
return true;
});
};
}
selectProduct(AutocompleteOnSelected<String> onSelected, String option, RecipeCreateStateLoaded state) {
state.productNameController.text = option;
return () {
onSelected(option);
};
}
getAutocomplete(BuildContext context, FocusNode focusNode) {
return (value) {
if (value.length > 2) {
context.read<RecipeCreateCubit>().inputProduct(value);
}
};
}
changeType(BuildContext context) {
return (value) {
context.read<RecipeCreateCubit>().changeType(value);
};
}
changeStatus(BuildContext context) {
return (value) {
context.read<RecipeCreateCubit>().changeStatus(value);
};
}
changeCookingTime(BuildContext context) {
return (value) {
context.read<RecipeCreateCubit>().changeCookingTime(value);
};
}
RecipeModel createRecipeFromState(RecipeCreateStateLoaded state) {
// Подготовка ингредиентов
List<Ingredient> ingredients = state.ingredients;
// Подготовка шагов (например, можно создать шаги из stepControllers)
List<step.Step> steps = [];
for (int i = 0; i < state.stepControllers.length; i++) {
image.Image picStep = image.Image(path: state.stepImages[i]?.path ?? "");
if (state.stepControllers[i].text.isNotEmpty) {
steps.add(step.Step(description: state.stepControllers[i].text));
steps.add(step.Step(
description: state.stepControllers[i].text,
image: picStep,
));
}
}
// Подготовка изображения
image.Image pic = image.Image(path: state.recipeImage?.path ?? '');
// Создание экземпляра RecipeModel
return RecipeModel(
final RecipeModel recipe = RecipeModel(
id: null,
ingredients: ingredients,
steps: steps,
state: 'draft',
description: state.titleController.text,
type: state.types.first.value,
state: state.selectedStatus.value,
type: state.selectedType.value,
cooking_time: state.selectedCookingTime.value,
description: state.stepDescriptionController.text,
title: state.titleController.text,
image: pic,
video_url: state.youtubeLinkController.text.isNotEmpty
? state.youtubeLinkController.text
: null,
cooking_time:
state.cookingTimes.isNotEmpty ? state.cookingTimes.first.value : null,
video_url: state.youtubeLinkController.text.isNotEmpty ? state.youtubeLinkController.text : null,
);
print(recipe);
return recipe;
}
}