forked from eatvio/eatvio_app
feat(diary): Modal window for finishDay, event and bloc method forFinishDay. addPortion and removePortion
This commit is contained in:
@@ -32,6 +32,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
on<AddComment>(_addComment); //пользователь нажимает "Добавить комментарий"
|
on<AddComment>(_addComment); //пользователь нажимает "Добавить комментарий"
|
||||||
on<ProductInput>(
|
on<ProductInput>(
|
||||||
_inputProduct); //пользователь нажимает "Добавить комментарий"
|
_inputProduct); //пользователь нажимает "Добавить комментарий"
|
||||||
|
on<FinishDay>(_finishDay); //пользователь нажимает "Добавить комментарий"
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onInitialize(
|
Future<void> _onInitialize(
|
||||||
@@ -394,19 +395,12 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
currentState.currentMeal!.id,
|
currentState.currentMeal!.id,
|
||||||
currentState.diaryId);
|
currentState.diaryId);
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
dynamic responseData = jsonDecode(response.body);
|
dynamic newPortion = jsonDecode(response.body)['portion'];
|
||||||
var newPortion = responseData['portion']
|
print(newPortion);
|
||||||
as int; // Assuming the portion quantity is an integer
|
|
||||||
|
|
||||||
// Ensure meals and id are correctly accessed and handle null values
|
|
||||||
var currentPortions =
|
|
||||||
currentState.meals?[id ?? 0]['portions'] as int ?? 0;
|
|
||||||
|
|
||||||
var updatedState = currentState.copyWith(
|
var updatedState = currentState.copyWith(
|
||||||
portions: currentPortions + newPortion,
|
portions: currentState.meals?[id ?? 0]['portions'], // + newPortion,
|
||||||
);
|
);
|
||||||
|
emit(updatedState); // Передаем обновленное состояние в emit
|
||||||
emit(updatedState);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -472,7 +466,83 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _removePortion(
|
FutureOr<void> _removePortion(
|
||||||
RemovePortionEvent event, Emitter<DiaryEditState> emit) {}
|
RemovePortionEvent event, Emitter<DiaryEditState> emit) async {
|
||||||
|
if (state is DiaryEditLoadedState) {
|
||||||
|
var currentState = state as DiaryEditLoadedState;
|
||||||
|
|
||||||
|
int? id = currentState.currentMeal?.id;
|
||||||
|
|
||||||
|
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||||
|
var response = await sendRemovePortion(
|
||||||
|
currentState.token, event.portionId, id, currentState.diaryId);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
print("removed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<http.Response> sendRemovePortion(
|
||||||
|
String? token,
|
||||||
|
int? portionID,
|
||||||
|
int? mealID,
|
||||||
|
int diaryID,
|
||||||
|
) async {
|
||||||
|
String url = dotenv.env['SERVER_ROOT']! +
|
||||||
|
'/diary/$diaryID/meal/$mealID/portions/$portionID/delete';
|
||||||
|
|
||||||
|
var response = await http.delete(Uri.parse(url),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer $token',
|
||||||
|
},
|
||||||
|
encoding: utf8);
|
||||||
|
print(response.body);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<FutureOr<void>> _finishDay(
|
||||||
|
FinishDay event, Emitter<DiaryEditState> emit) async {
|
||||||
|
print("imageUrl");
|
||||||
|
if (state is DiaryEditLoadedState) {
|
||||||
|
var currentState = state as DiaryEditLoadedState;
|
||||||
|
|
||||||
|
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||||
|
var response = await sendFinishDay(
|
||||||
|
currentState.token,
|
||||||
|
currentState.diaryId,
|
||||||
|
currentState.wakeupController,
|
||||||
|
currentState.retireController,
|
||||||
|
currentState.commentController?.text,
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
print("finished");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<http.Response> sendFinishDay(
|
||||||
|
String? token,
|
||||||
|
int diaryID,
|
||||||
|
TimeOfDay? wakeup,
|
||||||
|
TimeOfDay? retire,
|
||||||
|
String? comment,
|
||||||
|
) async {
|
||||||
|
final Map<String, dynamic> credentials = {
|
||||||
|
'wakeup': wakeup,
|
||||||
|
'retire': retire,
|
||||||
|
'comment': comment,
|
||||||
|
};
|
||||||
|
String url = dotenv.env['SERVER_ROOT']! + '/diary/$diaryID/finishDay';
|
||||||
|
var response = await http.post(Uri.parse(url),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer $token',
|
||||||
|
},
|
||||||
|
body: json.encode(credentials),
|
||||||
|
encoding: utf8);
|
||||||
|
print(response.body);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
Future<FutureOr<void>> _uploadImages(
|
Future<FutureOr<void>> _uploadImages(
|
||||||
UploadImages event, Emitter<DiaryEditState> emit) async {
|
UploadImages event, Emitter<DiaryEditState> emit) async {
|
||||||
|
|||||||
@@ -113,3 +113,10 @@ class ChangeMeal extends DiaryEditEvent {
|
|||||||
@override
|
@override
|
||||||
List<Object?> get props => [value, meals];
|
List<Object?> get props => [value, meals];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FinishDay extends DiaryEditEvent {
|
||||||
|
FinishDay();
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|||||||
@@ -900,10 +900,49 @@ class DiaryEditScreen extends StatelessWidget {
|
|||||||
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: Colors.transparent,
|
color: Colors.transparent,
|
||||||
), // Установите прозрачный фон
|
),
|
||||||
|
onPressed: () => onFinishDayPressed(context),
|
||||||
|
// Установите прозрачный фон
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function onFinishDayPressed(BuildContext context) {
|
||||||
|
return () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext dialogContext) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text("Подтвердите действие"),
|
||||||
|
content: Text("Вы уверены, что хотите завершить день?"),
|
||||||
|
actions: <Widget>[
|
||||||
|
TextButton(
|
||||||
|
child: Text("Отмена"),
|
||||||
|
onPressed: () => closeDialog(dialogContext),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
child: Text("Подтвердить"),
|
||||||
|
onPressed: () => sendFinishDayData(dialogContext, context),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Function sendFinishDayData(BuildContext dialogContext, BuildContext context) {
|
||||||
|
return () {
|
||||||
|
closeDialog(dialogContext);
|
||||||
|
context.read<DiaryEditBloc>().add(FinishDay());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Function closeDialog(BuildContext dialogContext) {
|
||||||
|
return () {
|
||||||
|
Navigator.of(dialogContext).pop();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
_buildMood(BuildContext context, DiaryEditLoadedState state) {
|
_buildMood(BuildContext context, DiaryEditLoadedState state) {
|
||||||
return Container(
|
return Container(
|
||||||
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
||||||
|
|||||||
Reference in New Issue
Block a user