forked from eatvio/eatvio_app
feat(app): Recipe layouts
This commit is contained in:
@@ -16,6 +16,9 @@ class RecipeCreateBloc extends Bloc<RecipeCreateEvent, RecipeCreateState> {
|
||||
RecipeCreateBloc(RecipeCreateState initialState) : super(initialState) {
|
||||
on<RecipeCreateInitialEvent>(_onInitialize);
|
||||
on<CreateRecipe>(_createRecipe);
|
||||
on<AddStep>(_addStep);
|
||||
on<RemoveStep>(_removeStep);
|
||||
on<ReloadScreen>(_reloadScreen);
|
||||
}
|
||||
|
||||
_onInitialize(
|
||||
@@ -34,22 +37,65 @@ class RecipeCreateBloc extends Bloc<RecipeCreateEvent, RecipeCreateState> {
|
||||
emit(currentState.copyWith(
|
||||
recipeModelObj: currentState.recipeModelObj,
|
||||
title: currentState.title,
|
||||
token: currentState.token,
|
||||
description: currentState.description,
|
||||
video_url: currentState.video_url,
|
||||
steps: currentState.steps));
|
||||
steps: currentState.steps,
|
||||
stepDescriptions: currentState.stepDescriptions,
|
||||
tags: currentState.tags));
|
||||
} else {
|
||||
final Map<String, dynamic> step = {
|
||||
'description': '',
|
||||
'image': '',
|
||||
};
|
||||
emit(RecipeCreateLoadedState(
|
||||
token: token,
|
||||
title: TextEditingController(),
|
||||
description: TextEditingController(),
|
||||
video_url: TextEditingController(),
|
||||
tags: TextEditingController(),
|
||||
stepDescriptions: [TextEditingController()],
|
||||
steps: [step]));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _addStep(
|
||||
AddStep event, Emitter<RecipeCreateState> emit) async {
|
||||
if (state is RecipeCreateLoadedState) {
|
||||
var currentState = state as RecipeCreateLoadedState;
|
||||
|
||||
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<RecipeCreateState> emit) async {
|
||||
if (state is RecipeCreateLoadedState) {
|
||||
var currentState = state as RecipeCreateLoadedState;
|
||||
|
||||
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<void> _createRecipe(
|
||||
CreateRecipe event, Emitter<RecipeCreateState> emit) async {
|
||||
if (state is RecipeCreateLoadedState) {
|
||||
@@ -59,6 +105,7 @@ class RecipeCreateBloc extends Bloc<RecipeCreateEvent, RecipeCreateState> {
|
||||
currentState.title?.text,
|
||||
currentState.video_url?.text,
|
||||
currentState.description?.text,
|
||||
currentState.steps,
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
@@ -70,17 +117,14 @@ class RecipeCreateBloc extends Bloc<RecipeCreateEvent, RecipeCreateState> {
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<http.Response> sendRecipe(
|
||||
String? title,
|
||||
String? video_url,
|
||||
String? description,
|
||||
) async {
|
||||
FutureOr<http.Response> sendRecipe(String? title, String? video_url,
|
||||
String? description, List<dynamic>? steps) async {
|
||||
String url = '${dotenv.env['SERVER_ROOT']!}/recipe/create';
|
||||
|
||||
final Map<String, dynamic> credentials = {
|
||||
'title': title,
|
||||
'description': description,
|
||||
'video_url': video_url,
|
||||
'steps': steps,
|
||||
'status': 'active',
|
||||
};
|
||||
|
||||
@@ -95,4 +139,28 @@ class RecipeCreateBloc extends Bloc<RecipeCreateEvent, RecipeCreateState> {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
FutureOr<void> _reloadScreen(
|
||||
RecipeCreateEvent? event, Emitter<RecipeCreateState> 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 RecipeCreateLoadedState) {
|
||||
var currentState = state as RecipeCreateLoadedState;
|
||||
emit(currentState.copyWith(
|
||||
recipeModelObj: currentState.recipeModelObj,
|
||||
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 CreateRecipe extends RecipeCreateEvent {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AddStep extends RecipeCreateEvent {
|
||||
AddStep();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class RemoveStep extends RecipeCreateEvent {
|
||||
RemoveStep(this.id);
|
||||
final int id;
|
||||
@override
|
||||
List<Object?> get props => [id];
|
||||
}
|
||||
|
||||
class ReloadScreen extends RecipeCreateEvent {
|
||||
ReloadScreen();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
@@ -24,33 +24,52 @@ class RecipeCreateState extends Equatable {
|
||||
class RecipeCreateLoadedState extends RecipeCreateState {
|
||||
RecipeCreateLoadedState(
|
||||
{this.recipeModelObj,
|
||||
this.token,
|
||||
this.title,
|
||||
this.description,
|
||||
this.video_url,
|
||||
this.tags,
|
||||
this.stepDescriptions,
|
||||
this.steps});
|
||||
|
||||
RecipeCreateModel? recipeModelObj;
|
||||
String? token;
|
||||
TextEditingController? title;
|
||||
TextEditingController? description;
|
||||
TextEditingController? video_url;
|
||||
TextEditingController? tags;
|
||||
List<dynamic>? steps;
|
||||
List<TextEditingController>? stepDescriptions;
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[recipeModelObj, title, description, video_url, steps];
|
||||
List<Object?> get props => [
|
||||
recipeModelObj,
|
||||
title,
|
||||
token,
|
||||
description,
|
||||
video_url,
|
||||
steps,
|
||||
tags,
|
||||
stepDescriptions
|
||||
];
|
||||
RecipeCreateLoadedState copyWith({
|
||||
RecipeCreateModel? recipeModelObj,
|
||||
String? token,
|
||||
TextEditingController? title,
|
||||
TextEditingController? description,
|
||||
TextEditingController? video_url,
|
||||
TextEditingController? tags,
|
||||
List<dynamic>? steps,
|
||||
List<TextEditingController>? stepDescriptions,
|
||||
}) {
|
||||
return RecipeCreateLoadedState(
|
||||
recipeModelObj: recipeModelObj ?? this.recipeModelObj,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
video_url: video_url ?? this.video_url,
|
||||
steps: steps ?? this.steps,
|
||||
);
|
||||
recipeModelObj: recipeModelObj ?? this.recipeModelObj,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
video_url: video_url ?? this.video_url,
|
||||
token: token ?? this.token,
|
||||
steps: steps ?? this.steps,
|
||||
tags: tags ?? this.tags,
|
||||
stepDescriptions: stepDescriptions ?? this.stepDescriptions);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user