feat(diary): Photo added to state manager

This commit is contained in:
2024-03-29 15:22:32 +03:00
parent a712471d0c
commit 04dba8d486
3 changed files with 82 additions and 25 deletions

View File

@@ -79,6 +79,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
moderationExpanded: currentState.moderationExpanded,
token: currentState.token,
meals: currentState.meals,
portions: currentState.portions,
photos: currentState.photos,
wakeupController: currentState.wakeupController,
retireController: currentState.retireController,
commentController: currentState.commentController,
@@ -90,7 +92,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
carbsController: currentState.carbsController,
selectedSatiety: currentState.selectedSatiety,
selectedMood: currentState.selectedMood,
portions: currentState.portions,
diaryDate: currentState.diaryDate,
diaryId: currentState.diaryId,
productQuantityController:
@@ -108,6 +109,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
token: token,
meals: diary['meals'],
portions: diary['meals'][0]['portions'],
photos: diary['meals'][0]['photos'],
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
retireController: toTime(diary['retire'] ?? "23:00"),
mealTimeController:
@@ -183,7 +185,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
final diary = jsonDecode(diaryResponse.body)['diary'];
if (diary is Map<String, dynamic>) {
print(diary);
final String diaryDate = diary['date'] ?? DateTime.now().toString();
// Продолжайте обработку данных дневника
// Если текущее состояние - начальное, создаем новое загруженное состояние
@@ -197,6 +198,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
token: token,
meals: diary['meals'],
portions: diary['meals'][0]['portions'],
photos: diary['meals'][0]['photos'],
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
retireController: toTime(diary['retire'] ?? "23:00"),
mealTimeController:
@@ -216,6 +218,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
autocompleteList: [],
diaryEditModelObj: state.diaryEditModelObj
?.copyWith(mealList: fillMealList()))));
print("Updated");
} catch (e) {
print("Ошибка при создании нового состояния $e");
@@ -333,9 +336,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
currentState.copyWith(wakeupController: event.selectedTime);
print("Wakeup time changed to ${event.selectedTime}");
emit(updatedState); // Передаем обновленное состояние в emit
} else {
} else
print("Wakeup time change error");
}
}
}
@@ -365,17 +367,17 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
final wakeupString =
formatter.format(DateTime(2022, 1, 1, wakeup.hour, wakeup.minute));
final Map<String, dynamic> credentials = {
'wakeup': wakeupString,
'wakeUp': wakeupString,
};
var response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,
);
var response = await http.post(Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8);
return response;
}
@@ -446,6 +448,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
DiaryEditLoadedState updatedState = currentState.copyWith(
currentMeal: event.value,
portions: meal['portions'],
photos: meal['photos'],
mealTimeController: toTime(meal['time'] ?? "1$id:00"),
selectedMood: meal['mood'],
selectedSatiety: meal['satiety']);
@@ -655,13 +658,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
if (state is DiaryEditLoadedState) {
var currentState = state as DiaryEditLoadedState;
String imageUrl = await uploadImageToServer(
event.image.path,
currentState.diaryId,
currentState.currentMeal!.id!,
currentState.token);
print(imageUrl);
await uploadImageToServer(event.image.path, currentState.diaryId,
currentState.currentMeal!.id!, currentState.token);
}
}
@@ -673,7 +671,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
var request = http.MultipartRequest('POST', Uri.parse(url));
request.headers['Authorization'] = 'Bearer $token';
request.files.add(await http.MultipartFile.fromPath('image', imagePath));
request.files.add(await http.MultipartFile.fromPath('photo', imagePath));
var client = http.Client();
var streamedResponse = await client.send(request);
@@ -681,6 +679,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
if (streamedResponse.statusCode == 200) {
var response = await http.Response.fromStream(streamedResponse);
var imageUrl = response.body;
print('Image successfully uploaded');
return imageUrl;
} else {
throw Exception(

View File

@@ -24,6 +24,7 @@ class DiaryEditLoadedState extends DiaryEditState {
this.token,
this.meals,
this.portions,
this.photos,
this.autocompleteList,
this.wakeupController,
this.retireController,
@@ -49,6 +50,7 @@ class DiaryEditLoadedState extends DiaryEditState {
String? token;
List<dynamic>? meals;
List<dynamic>? portions;
List<dynamic>? photos;
List<dynamic>? autocompleteList;
SelectionPopupModel? currentMeal;
TimeOfDay? wakeupController;
@@ -73,6 +75,8 @@ class DiaryEditLoadedState extends DiaryEditState {
diaryDate,
token,
meals,
portions,
photos,
autocompleteList,
wakeupController,
retireController,
@@ -99,6 +103,7 @@ class DiaryEditLoadedState extends DiaryEditState {
String? token,
List<dynamic>? meals,
List<dynamic>? portions,
List<dynamic>? photos,
List<dynamic>? autocompleteList,
TimeOfDay? wakeupController,
TimeOfDay? retireController,

View File

@@ -394,6 +394,9 @@ class DiaryEditScreen extends StatelessWidget {
SizedBox(height: 30.v),
_buildAddPhoto(context),
SizedBox(height: 30.v),
...state.photos!
.map((photo) => _buildPhoto(context, photo))
.toList(),
Text(
"Ваша сытость после приёма пищи",
style: CustomTextStyles.bodyMediumBlack900,
@@ -628,6 +631,58 @@ class DiaryEditScreen extends StatelessWidget {
);
}
_buildP(BuildContext context, Map<String, dynamic> portion) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 10.h),
padding: EdgeInsets.symmetric(
horizontal: 8.h,
vertical: 6.v,
),
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder5,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.only(
left: 7.h,
top: 5.v,
),
child: Text(
portion['product_name'].length > 8
? portion['product_name'].substring(0, 8)
: portion['product_name'],
style: CustomTextStyles.titleMedium16,
)),
Padding(
padding: EdgeInsets.only(
left: 20.h,
top: 3.v,
bottom: 3.v,
),
child: Text(
portion['quantity'].toString() +
' ' +
portion['portion'].toString(),
),
),
Spacer(),
GestureDetector(
onTap: removePortion(context, portion),
child: Padding(
padding: const EdgeInsets.only(right: 10),
child: Icon(
Icons.close,
color: appTheme.blueGray300,
),
)),
],
),
);
}
removePortion(BuildContext context, Map<String, dynamic> portion) {
return () {
context.read<DiaryEditBloc>().add(RemovePortionEvent(portion['id']));
@@ -1114,9 +1169,7 @@ class DiaryEditScreen extends StatelessWidget {
);
if (pickedTime != null && pickedTime != state.wakeupController) {
state.wakeupController = pickedTime;
context.read<DiaryEditBloc>().add(WakeupEvent(
pickedTime,
));
context.read<DiaryEditBloc>().add(WakeupEvent(pickedTime));
}
}