forked from eatvio/eatvio_app
feat(app): Recipe layouts
This commit is contained in:
@@ -16,6 +16,9 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
|
||||
RecipeEditBloc(RecipeEditState initialState) : super(initialState) {
|
||||
on<RecipeEditInitialEvent>(_onInitialize);
|
||||
on<EditRecipe>(_editRecipe);
|
||||
on<AddStep>(_addStep);
|
||||
on<RemoveStep>(_removeStep);
|
||||
on<ReloadScreen>(_reloadScreen);
|
||||
}
|
||||
|
||||
_onInitialize(
|
||||
@@ -44,16 +47,44 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
|
||||
var currentState = state as RecipeEditLoadedState;
|
||||
emit(currentState.copyWith(
|
||||
recipeModelObj: currentState.recipeModelObj,
|
||||
recipe: currentState.recipe,
|
||||
title: currentState.title,
|
||||
token: currentState.token,
|
||||
recipe: currentState.recipe,
|
||||
description: currentState.description,
|
||||
video_url: currentState.video_url));
|
||||
video_url: currentState.video_url,
|
||||
steps: currentState.steps,
|
||||
stepDescriptions: currentState.stepDescriptions,
|
||||
tags: currentState.tags));
|
||||
} else {
|
||||
final Map<String, dynamic> defaultStep = {
|
||||
'description': '',
|
||||
'image': '',
|
||||
};
|
||||
|
||||
List<dynamic> steps = data['steps'];
|
||||
List<TextEditingController> stepDescriptions = [];
|
||||
|
||||
if (data['steps'] == null) {
|
||||
stepDescriptions.add(TextEditingController());
|
||||
data['steps'].add(defaultStep);
|
||||
} else {
|
||||
for (var step in steps) {
|
||||
stepDescriptions
|
||||
.add(TextEditingController(text: step['description']));
|
||||
}
|
||||
}
|
||||
|
||||
emit(RecipeEditLoadedState(
|
||||
recipe: data,
|
||||
title: TextEditingController(text: data['title']),
|
||||
description: TextEditingController(text: data['description']),
|
||||
video_url: TextEditingController(text: data['video_url'])));
|
||||
token: token,
|
||||
recipe: data,
|
||||
title: TextEditingController(text: data['title']),
|
||||
description: TextEditingController(text: data['description']),
|
||||
video_url: TextEditingController(text: data['video_url']),
|
||||
tagsController: TextEditingController(),
|
||||
tags: await _fetchTags(data['id']),
|
||||
stepDescriptions: stepDescriptions,
|
||||
steps: steps,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
|
||||
@@ -92,6 +123,42 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _addStep(AddStep event, Emitter<RecipeEditState> emit) async {
|
||||
if (state is RecipeEditLoadedState) {
|
||||
var currentState = state as RecipeEditLoadedState;
|
||||
|
||||
List<dynamic>? updatedSteps = currentState.steps;
|
||||
List<TextEditingController>? updatedStepDescriptions =
|
||||
currentState.stepDescriptions;
|
||||
|
||||
updatedSteps!.add({
|
||||
'description': '',
|
||||
'image': '',
|
||||
});
|
||||
updatedStepDescriptions!.add(TextEditingController());
|
||||
emit(currentState.copyWith(
|
||||
steps: updatedSteps, stepDescriptions: updatedStepDescriptions));
|
||||
await _reloadScreen(event, emit);
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _removeStep(
|
||||
RemoveStep event, Emitter<RecipeEditState> emit) async {
|
||||
if (state is RecipeEditLoadedState) {
|
||||
var currentState = state as RecipeEditLoadedState;
|
||||
|
||||
List<dynamic>? updatedSteps = currentState.steps;
|
||||
List<TextEditingController>? updatedStepDescriptions =
|
||||
currentState.stepDescriptions;
|
||||
|
||||
updatedSteps!.removeAt(event.id);
|
||||
updatedStepDescriptions!.removeAt(event.id);
|
||||
emit(currentState.copyWith(
|
||||
steps: updatedSteps, stepDescriptions: updatedStepDescriptions));
|
||||
await _reloadScreen(event, emit);
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<http.Response> sendRecipe(
|
||||
Map<String, dynamic>? recipe,
|
||||
String? title,
|
||||
@@ -123,4 +190,49 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
_fetchTags(int id) async {
|
||||
final box = await Hive.openBox('token');
|
||||
final token = box.get('token');
|
||||
|
||||
if (token == null) {
|
||||
NavigatorService.popAndPushNamed(
|
||||
AppRoutes.authScreen,
|
||||
);
|
||||
}
|
||||
|
||||
final response = await http.get(
|
||||
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getTags/$id'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
final data = jsonDecode(response.body);
|
||||
return data;
|
||||
}
|
||||
|
||||
FutureOr<void> _reloadScreen(
|
||||
RecipeEditEvent? event, Emitter<RecipeEditState> emit) async {
|
||||
print('Reloading..');
|
||||
final box = await Hive.openBox('token');
|
||||
final token = box.get('token');
|
||||
if (token == null) {
|
||||
NavigatorService.popAndPushNamed(
|
||||
AppRoutes.authScreen,
|
||||
);
|
||||
}
|
||||
if (state is RecipeEditLoadedState) {
|
||||
var currentState = state as RecipeEditLoadedState;
|
||||
emit(currentState.copyWith(
|
||||
recipeModelObj: currentState.recipeModelObj,
|
||||
recipe: currentState.recipe,
|
||||
title: currentState.title,
|
||||
token: currentState.token,
|
||||
description: currentState.description,
|
||||
video_url: currentState.video_url,
|
||||
steps: currentState.steps,
|
||||
stepDescriptions: currentState.stepDescriptions,
|
||||
tags: currentState.tags));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,3 +19,22 @@ class EditRecipe extends RecipeEditEvent {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AddStep extends RecipeEditEvent {
|
||||
AddStep();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class RemoveStep extends RecipeEditEvent {
|
||||
RemoveStep(this.id);
|
||||
final int id;
|
||||
@override
|
||||
List<Object?> get props => [id];
|
||||
}
|
||||
|
||||
class ReloadScreen extends RecipeEditEvent {
|
||||
ReloadScreen();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
@@ -24,32 +24,52 @@ class RecipeEditState extends Equatable {
|
||||
class RecipeEditLoadedState extends RecipeEditState {
|
||||
RecipeEditLoadedState(
|
||||
{this.recipeModelObj,
|
||||
this.token,
|
||||
this.recipe,
|
||||
this.title,
|
||||
this.description,
|
||||
this.video_url});
|
||||
this.video_url,
|
||||
this.tagsController,
|
||||
this.stepDescriptions,
|
||||
this.tags,
|
||||
this.steps});
|
||||
|
||||
RecipeEditModel? recipeModelObj;
|
||||
Map<String, dynamic>? recipe;
|
||||
String? token;
|
||||
TextEditingController? title;
|
||||
TextEditingController? description;
|
||||
TextEditingController? video_url;
|
||||
TextEditingController? tagsController;
|
||||
List<dynamic>? steps;
|
||||
List<dynamic>? tags;
|
||||
List<TextEditingController>? stepDescriptions;
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[recipeModelObj, recipe, title, description, video_url];
|
||||
RecipeEditLoadedState copyWith({
|
||||
RecipeEditModel? recipeModelObj,
|
||||
Map<String, dynamic>? recipe,
|
||||
String? token,
|
||||
TextEditingController? title,
|
||||
TextEditingController? description,
|
||||
TextEditingController? video_url,
|
||||
TextEditingController? tagsController,
|
||||
List<dynamic>? steps,
|
||||
List<dynamic>? tags,
|
||||
List<TextEditingController>? stepDescriptions,
|
||||
}) {
|
||||
return RecipeEditLoadedState(
|
||||
recipeModelObj: recipeModelObj ?? this.recipeModelObj,
|
||||
token: token ?? this.token,
|
||||
recipe: recipe ?? this.recipe,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
video_url: video_url ?? this.video_url,
|
||||
tagsController: tagsController ?? this.tagsController,
|
||||
steps: steps ?? this.steps,
|
||||
tags: tags ?? this.tags,
|
||||
stepDescriptions: stepDescriptions ?? this.stepDescriptions,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user