From 6869287c85bd7c4e3faedcd78d8cb920702e3e2d Mon Sep 17 00:00:00 2001 From: maxxkim Date: Fri, 3 May 2024 13:20:52 +0300 Subject: [PATCH] feat(chat): Cheatroom layouts and API data load --- .../chat/chat_screen/bloc/chat_bloc.dart | 42 +++++++- .../chat/chat_screen/bloc/chat_state.dart | 34 +++++- .../my_profile_screen/my_profile_screen.dart | 102 +++++++++--------- 3 files changed, 123 insertions(+), 55 deletions(-) diff --git a/lib/presentation/chat/chat_screen/bloc/chat_bloc.dart b/lib/presentation/chat/chat_screen/bloc/chat_bloc.dart index 47ed9dec..0f4a2add 100644 --- a/lib/presentation/chat/chat_screen/bloc/chat_bloc.dart +++ b/lib/presentation/chat/chat_screen/bloc/chat_bloc.dart @@ -1,5 +1,9 @@ import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:hive/hive.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; import '/core/app_export.dart'; import 'package:eatvio_app/presentation/chat/chat_screen/models/chat_model.dart'; part 'chat_event.dart'; @@ -15,6 +19,42 @@ class ChatBloc extends Bloc { ChatInitialEvent event, Emitter emit, ) async { - emit(state.copyWith(chatModelObj: state.chatModelObj?.copyWith())); + try { + final box = await Hive.openBox('token'); + final token = box.get('token'); + if (token == null) { + NavigatorService.popAndPushNamed( + AppRoutes.authScreen, + ); + } + final int id = 1; + final response = await http.get( + Uri.parse(dotenv.env['SERVER_ROOT']! + '/chat/$id/show'), + headers: { + 'Authorization': 'Bearer $token', + }, + ); + if (response.statusCode == 200) { + final data = jsonDecode(response.body); + print(data['chatRooms']); + if (data is Map && data.containsKey('chatRooms')) { + if (state is ChatLoadedState) { + var currentState = state as ChatLoadedState; + emit(currentState.copyWith()); + } else { + emit(ChatLoadedState()); + } + } else { + // Один или несколько ключевых полей отсутствуют, обрабатываем ошибку + emit(ChatErrorState('Некоторые необходимые данные отсутствуют.')); + } + } else { + // Статус ответа не равен 200, обрабатываем ошибку + emit(ChatErrorState('Ошибка сервера: ${response.statusCode}')); + } + } catch (e) { + // Ошибка при декодировании JSON или другая ошибка выполнения + emit(ChatErrorState('Ошибка при получении данных о чатах: $e')); + } } } diff --git a/lib/presentation/chat/chat_screen/bloc/chat_state.dart b/lib/presentation/chat/chat_screen/bloc/chat_state.dart index 20be159f..aae133dc 100644 --- a/lib/presentation/chat/chat_screen/bloc/chat_state.dart +++ b/lib/presentation/chat/chat_screen/bloc/chat_state.dart @@ -3,8 +3,26 @@ part of 'chat_bloc.dart'; /// Represents the state of Chat in the application. +/// + class ChatState extends Equatable { ChatState({ + this.chatModelObj, + }); + + ChatModel? chatModelObj; + + @override + List get props => [chatModelObj]; + ChatState copyWith({ChatModel? chatModelObj}) { + return ChatState( + chatModelObj: chatModelObj ?? this.chatModelObj, + ); + } +} + +class ChatLoadedState extends ChatState { + ChatLoadedState({ this.selectedDropDownValue, this.chatModelObj, }); @@ -23,9 +41,21 @@ class ChatState extends Equatable { ChatModel? chatModelObj, }) { return ChatState( - selectedDropDownValue: - selectedDropDownValue ?? this.selectedDropDownValue, chatModelObj: chatModelObj ?? this.chatModelObj, ); } } + +class ChatErrorState extends ChatState { + final String errorMessage; + + ChatErrorState(this.errorMessage); + + @override + List get props => [errorMessage]; +} + +class ChatInitialState extends ChatState { + ChatInitialState({required ChatModel chatModelObj}) + : super(chatModelObj: chatModelObj); +} diff --git a/lib/presentation/profile/my_profile_screen/my_profile_screen.dart b/lib/presentation/profile/my_profile_screen/my_profile_screen.dart index d4b699a7..ad417337 100644 --- a/lib/presentation/profile/my_profile_screen/my_profile_screen.dart +++ b/lib/presentation/profile/my_profile_screen/my_profile_screen.dart @@ -207,7 +207,7 @@ class MyProfileScreen extends StatelessWidget { context, state.chartPoints?.values .map((e) => e['weight']) - .toList() + .toList() .map((i) => i.toDouble()) .toList() ?? [0.0, 0.0, 0.0, 0.0], @@ -800,56 +800,56 @@ class MyProfileScreen extends StatelessWidget { //final digitsOnly = FilteringTextInputFormatter.digitsOnly; return AlertDialog( title: Center(child: Text("Добавить замер")), - + content: SizedBox( - width: 200, // Ширина диалогового окна - height: 300, // Высота диалогового окна - child: ListView( - children: [ - Padding( - padding: EdgeInsets.symmetric(vertical: 5.v), - child: Text("chest_circumference".tr, - style: theme.textTheme.bodyLarge), - ), - SizedBox(height: 5.v), - CustomTextFormField( - controller: state.chestCircumferenceController, - keyboardType: TextInputType.number, - inputFormatters: [ - FilteringTextInputFormatter - .digitsOnly, // Ограничение ввода только цифрами + width: 200, // Ширина диалогового окна + height: 300, // Высота диалогового окна + child: ListView( + children: [ + Padding( + padding: EdgeInsets.symmetric(vertical: 5.v), + child: Text("chest_circumference".tr, + style: theme.textTheme.bodyLarge), + ), + SizedBox(height: 5.v), + CustomTextFormField( + controller: state.chestCircumferenceController, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter + .digitsOnly, // Ограничение ввода только цифрами + ], + ), + Padding( + padding: EdgeInsets.symmetric(vertical: 15.v), + child: Text("waist_circumference".tr, + style: theme.textTheme.bodyLarge), + ), + SizedBox(height: 5.v), + CustomTextFormField( + controller: state.waistCircumferenceController, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter + .digitsOnly, // Ограничение ввода только цифрами + ], + ), + Padding( + padding: EdgeInsets.symmetric(vertical: 15.v), + child: Text("hip_circumference".tr, + style: theme.textTheme.bodyLarge), + ), + SizedBox(height: 5.v), + CustomTextFormField( + controller: state.hipCircumferenceController, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter + .digitsOnly, // Ограничение ввода только цифрами + ], + ), ], - ), - Padding( - padding: EdgeInsets.symmetric(vertical: 15.v), - child: Text("waist_circumference".tr, - style: theme.textTheme.bodyLarge), - ), - SizedBox(height: 5.v), - CustomTextFormField( - controller: state.waistCircumferenceController, - keyboardType: TextInputType.number, - inputFormatters: [ - FilteringTextInputFormatter - .digitsOnly, // Ограничение ввода только цифрами - ], - ), - Padding( - padding: EdgeInsets.symmetric(vertical: 15.v), - child: Text("hip_circumference".tr, - style: theme.textTheme.bodyLarge), - ), - SizedBox(height: 5.v), - CustomTextFormField( - controller: state.hipCircumferenceController, - keyboardType: TextInputType.number, - inputFormatters: [ - FilteringTextInputFormatter - .digitsOnly, // Ограничение ввода только цифрами - ], - ), - ], - )), + )), actions: [ TextButton( onPressed: onTapOk(context), @@ -866,7 +866,7 @@ class MyProfileScreen extends StatelessWidget { backgroundColor: Colors.white, titlePadding: EdgeInsets.all( 20.0), // Можете настроить отступы по вашему желанию - ); + ); }, ); } @@ -957,10 +957,8 @@ class MyProfileScreen extends StatelessWidget { } void saveWeight(MyProfileLoadedState state, BuildContext context) { - addWeight(state, context); Navigator.of(context).pop(); // Закрыть диалоговое окно после изменения веса - } onTapOk(BuildContext context) {