forked from eatvio/eatvio_app
feat(diary): Photo added to state manager
This commit is contained in:
@@ -79,6 +79,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
moderationExpanded: currentState.moderationExpanded,
|
moderationExpanded: currentState.moderationExpanded,
|
||||||
token: currentState.token,
|
token: currentState.token,
|
||||||
meals: currentState.meals,
|
meals: currentState.meals,
|
||||||
|
portions: currentState.portions,
|
||||||
|
photos: currentState.photos,
|
||||||
wakeupController: currentState.wakeupController,
|
wakeupController: currentState.wakeupController,
|
||||||
retireController: currentState.retireController,
|
retireController: currentState.retireController,
|
||||||
commentController: currentState.commentController,
|
commentController: currentState.commentController,
|
||||||
@@ -90,7 +92,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
carbsController: currentState.carbsController,
|
carbsController: currentState.carbsController,
|
||||||
selectedSatiety: currentState.selectedSatiety,
|
selectedSatiety: currentState.selectedSatiety,
|
||||||
selectedMood: currentState.selectedMood,
|
selectedMood: currentState.selectedMood,
|
||||||
portions: currentState.portions,
|
|
||||||
diaryDate: currentState.diaryDate,
|
diaryDate: currentState.diaryDate,
|
||||||
diaryId: currentState.diaryId,
|
diaryId: currentState.diaryId,
|
||||||
productQuantityController:
|
productQuantityController:
|
||||||
@@ -108,6 +109,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
token: token,
|
token: token,
|
||||||
meals: diary['meals'],
|
meals: diary['meals'],
|
||||||
portions: diary['meals'][0]['portions'],
|
portions: diary['meals'][0]['portions'],
|
||||||
|
photos: diary['meals'][0]['photos'],
|
||||||
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
|
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
|
||||||
retireController: toTime(diary['retire'] ?? "23:00"),
|
retireController: toTime(diary['retire'] ?? "23:00"),
|
||||||
mealTimeController:
|
mealTimeController:
|
||||||
@@ -183,7 +185,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
final diary = jsonDecode(diaryResponse.body)['diary'];
|
final diary = jsonDecode(diaryResponse.body)['diary'];
|
||||||
|
|
||||||
if (diary is Map<String, dynamic>) {
|
if (diary is Map<String, dynamic>) {
|
||||||
print(diary);
|
|
||||||
final String diaryDate = diary['date'] ?? DateTime.now().toString();
|
final String diaryDate = diary['date'] ?? DateTime.now().toString();
|
||||||
// Продолжайте обработку данных дневника
|
// Продолжайте обработку данных дневника
|
||||||
// Если текущее состояние - начальное, создаем новое загруженное состояние
|
// Если текущее состояние - начальное, создаем новое загруженное состояние
|
||||||
@@ -197,6 +198,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
token: token,
|
token: token,
|
||||||
meals: diary['meals'],
|
meals: diary['meals'],
|
||||||
portions: diary['meals'][0]['portions'],
|
portions: diary['meals'][0]['portions'],
|
||||||
|
photos: diary['meals'][0]['photos'],
|
||||||
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
|
wakeupController: toTime(diary['wakeup'] ?? "09:00"),
|
||||||
retireController: toTime(diary['retire'] ?? "23:00"),
|
retireController: toTime(diary['retire'] ?? "23:00"),
|
||||||
mealTimeController:
|
mealTimeController:
|
||||||
@@ -216,6 +218,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
autocompleteList: [],
|
autocompleteList: [],
|
||||||
diaryEditModelObj: state.diaryEditModelObj
|
diaryEditModelObj: state.diaryEditModelObj
|
||||||
?.copyWith(mealList: fillMealList()))));
|
?.copyWith(mealList: fillMealList()))));
|
||||||
|
|
||||||
print("Updated");
|
print("Updated");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Ошибка при создании нового состояния $e");
|
print("Ошибка при создании нового состояния $e");
|
||||||
@@ -333,9 +336,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
currentState.copyWith(wakeupController: event.selectedTime);
|
currentState.copyWith(wakeupController: event.selectedTime);
|
||||||
print("Wakeup time changed to ${event.selectedTime}");
|
print("Wakeup time changed to ${event.selectedTime}");
|
||||||
emit(updatedState); // Передаем обновленное состояние в emit
|
emit(updatedState); // Передаем обновленное состояние в emit
|
||||||
} else {
|
} else
|
||||||
print("Wakeup time change error");
|
print("Wakeup time change error");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,17 +367,17 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
final wakeupString =
|
final wakeupString =
|
||||||
formatter.format(DateTime(2022, 1, 1, wakeup.hour, wakeup.minute));
|
formatter.format(DateTime(2022, 1, 1, wakeup.hour, wakeup.minute));
|
||||||
final Map<String, dynamic> credentials = {
|
final Map<String, dynamic> credentials = {
|
||||||
'wakeup': wakeupString,
|
'wakeUp': wakeupString,
|
||||||
};
|
};
|
||||||
var response = await http.post(
|
|
||||||
Uri.parse(url),
|
var response = await http.post(Uri.parse(url),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer $token',
|
'Authorization': 'Bearer $token',
|
||||||
},
|
},
|
||||||
body: json.encode(credentials),
|
body: json.encode(credentials),
|
||||||
encoding: utf8,
|
encoding: utf8);
|
||||||
);
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,6 +448,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
DiaryEditLoadedState updatedState = currentState.copyWith(
|
DiaryEditLoadedState updatedState = currentState.copyWith(
|
||||||
currentMeal: event.value,
|
currentMeal: event.value,
|
||||||
portions: meal['portions'],
|
portions: meal['portions'],
|
||||||
|
photos: meal['photos'],
|
||||||
mealTimeController: toTime(meal['time'] ?? "1$id:00"),
|
mealTimeController: toTime(meal['time'] ?? "1$id:00"),
|
||||||
selectedMood: meal['mood'],
|
selectedMood: meal['mood'],
|
||||||
selectedSatiety: meal['satiety']);
|
selectedSatiety: meal['satiety']);
|
||||||
@@ -655,13 +658,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
if (state is DiaryEditLoadedState) {
|
if (state is DiaryEditLoadedState) {
|
||||||
var currentState = state as DiaryEditLoadedState;
|
var currentState = state as DiaryEditLoadedState;
|
||||||
|
|
||||||
String imageUrl = await uploadImageToServer(
|
await uploadImageToServer(event.image.path, currentState.diaryId,
|
||||||
event.image.path,
|
currentState.currentMeal!.id!, currentState.token);
|
||||||
currentState.diaryId,
|
|
||||||
currentState.currentMeal!.id!,
|
|
||||||
currentState.token);
|
|
||||||
|
|
||||||
print(imageUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -673,7 +671,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
var request = http.MultipartRequest('POST', Uri.parse(url));
|
var request = http.MultipartRequest('POST', Uri.parse(url));
|
||||||
request.headers['Authorization'] = 'Bearer $token';
|
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 client = http.Client();
|
||||||
var streamedResponse = await client.send(request);
|
var streamedResponse = await client.send(request);
|
||||||
@@ -681,6 +679,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
|||||||
if (streamedResponse.statusCode == 200) {
|
if (streamedResponse.statusCode == 200) {
|
||||||
var response = await http.Response.fromStream(streamedResponse);
|
var response = await http.Response.fromStream(streamedResponse);
|
||||||
var imageUrl = response.body;
|
var imageUrl = response.body;
|
||||||
|
print('Image successfully uploaded');
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
} else {
|
} else {
|
||||||
throw Exception(
|
throw Exception(
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ class DiaryEditLoadedState extends DiaryEditState {
|
|||||||
this.token,
|
this.token,
|
||||||
this.meals,
|
this.meals,
|
||||||
this.portions,
|
this.portions,
|
||||||
|
this.photos,
|
||||||
this.autocompleteList,
|
this.autocompleteList,
|
||||||
this.wakeupController,
|
this.wakeupController,
|
||||||
this.retireController,
|
this.retireController,
|
||||||
@@ -49,6 +50,7 @@ class DiaryEditLoadedState extends DiaryEditState {
|
|||||||
String? token;
|
String? token;
|
||||||
List<dynamic>? meals;
|
List<dynamic>? meals;
|
||||||
List<dynamic>? portions;
|
List<dynamic>? portions;
|
||||||
|
List<dynamic>? photos;
|
||||||
List<dynamic>? autocompleteList;
|
List<dynamic>? autocompleteList;
|
||||||
SelectionPopupModel? currentMeal;
|
SelectionPopupModel? currentMeal;
|
||||||
TimeOfDay? wakeupController;
|
TimeOfDay? wakeupController;
|
||||||
@@ -73,6 +75,8 @@ class DiaryEditLoadedState extends DiaryEditState {
|
|||||||
diaryDate,
|
diaryDate,
|
||||||
token,
|
token,
|
||||||
meals,
|
meals,
|
||||||
|
portions,
|
||||||
|
photos,
|
||||||
autocompleteList,
|
autocompleteList,
|
||||||
wakeupController,
|
wakeupController,
|
||||||
retireController,
|
retireController,
|
||||||
@@ -99,6 +103,7 @@ class DiaryEditLoadedState extends DiaryEditState {
|
|||||||
String? token,
|
String? token,
|
||||||
List<dynamic>? meals,
|
List<dynamic>? meals,
|
||||||
List<dynamic>? portions,
|
List<dynamic>? portions,
|
||||||
|
List<dynamic>? photos,
|
||||||
List<dynamic>? autocompleteList,
|
List<dynamic>? autocompleteList,
|
||||||
TimeOfDay? wakeupController,
|
TimeOfDay? wakeupController,
|
||||||
TimeOfDay? retireController,
|
TimeOfDay? retireController,
|
||||||
|
|||||||
@@ -394,6 +394,9 @@ class DiaryEditScreen extends StatelessWidget {
|
|||||||
SizedBox(height: 30.v),
|
SizedBox(height: 30.v),
|
||||||
_buildAddPhoto(context),
|
_buildAddPhoto(context),
|
||||||
SizedBox(height: 30.v),
|
SizedBox(height: 30.v),
|
||||||
|
...state.photos!
|
||||||
|
.map((photo) => _buildPhoto(context, photo))
|
||||||
|
.toList(),
|
||||||
Text(
|
Text(
|
||||||
"Ваша сытость после приёма пищи",
|
"Ваша сытость после приёма пищи",
|
||||||
style: CustomTextStyles.bodyMediumBlack900,
|
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) {
|
removePortion(BuildContext context, Map<String, dynamic> portion) {
|
||||||
return () {
|
return () {
|
||||||
context.read<DiaryEditBloc>().add(RemovePortionEvent(portion['id']));
|
context.read<DiaryEditBloc>().add(RemovePortionEvent(portion['id']));
|
||||||
@@ -1114,9 +1169,7 @@ class DiaryEditScreen extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
if (pickedTime != null && pickedTime != state.wakeupController) {
|
if (pickedTime != null && pickedTime != state.wakeupController) {
|
||||||
state.wakeupController = pickedTime;
|
state.wakeupController = pickedTime;
|
||||||
context.read<DiaryEditBloc>().add(WakeupEvent(
|
context.read<DiaryEditBloc>().add(WakeupEvent(pickedTime));
|
||||||
pickedTime,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user