diff --git a/lib/presentation/chat/chat_select_screen/bloc/chat_select_bloc.dart b/lib/presentation/chat/chat_select_screen/bloc/chat_select_bloc.dart index 9f4208dd..a4fa971a 100644 --- a/lib/presentation/chat/chat_select_screen/bloc/chat_select_bloc.dart +++ b/lib/presentation/chat/chat_select_screen/bloc/chat_select_bloc.dart @@ -1,7 +1,9 @@ import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:hive/hive.dart'; import 'package:http/http.dart' as http; +import 'package:intl/intl.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'dart:convert'; import '/core/app_export.dart'; @@ -17,29 +19,29 @@ class ChatSelectBloc extends Bloc { ChatSelectBloc(ChatSelectState initialState) : super(initialState) { on(_onInitialize); } - List fillChatEntryList() { - return [ - ChatItemModel( - userImage: ImageConstant.imgLogo, - username: "Ксения Обрамова", - dateText: "9/29", - greetingText: "Привет, хорошая статья"), - ChatItemModel( - userImage: ImageConstant.imgLogo, - username: "Ксения Обрамова", - dateText: "9/29", - greetingText: "Привет, хорошая статья"), - ChatItemModel( - userImage: ImageConstant.imgLogo, - username: "Данил Иванов", - dateText: "9/29", - greetingText: "Посоветуй пожалуйста рецепт..."), - ChatItemModel( - userImage: ImageConstant.imgLogo, - username: "Данил Иванов", - dateText: "9/29", - greetingText: "Посоветуй пожалуйста рецепт...") - ]; + + List fillChatEntryList(List chatRooms) { + List chats = []; + for (int i = 0; i < chatRooms.length; i++) { + String userName; + String userAvatarPath; + if (chatRooms[i]['last_message']['is_author_from_user']) { + userName = chatRooms[i]['users']['from_chat_user']['name']; + userAvatarPath = + chatRooms[i]['users']['from_chat_user']['profile_photo_path'] ?? ''; + } else { + userName = chatRooms[i]['users']['to_chat_user']['name']; + userAvatarPath = + chatRooms[i]['users']['to_chat_user']['profile_photo_path'] ?? ''; + } + chats.add(ChatItemModel( + userImage: userAvatarPath, + username: userName, + dateText: DateFormat('dd.MM').format( + DateTime.parse(chatRooms[i]['last_message']['updated_at'])), + greetingText: chatRooms[i]['last_message']['data'] ?? '')); + } + return chats; } Future _onInitialize( @@ -62,6 +64,7 @@ class ChatSelectBloc extends Bloc { ); if (response.statusCode == 200) { final data = jsonDecode(response.body); + print(data['chatRooms']); if (data is Map && data.containsKey('chatRooms')) { if (state is ChatSelectLoadedState) { var currentState = state as ChatSelectLoadedState; @@ -71,7 +74,8 @@ class ChatSelectBloc extends Bloc { )); } else { emit(ChatSelectLoadedState( - chatRooms: data['chatRooms'], + chatRooms: fillChatEntryList(data['chatRooms']), + //chatRooms: [], )); } } else { diff --git a/lib/presentation/chat/chat_select_screen/chat_select_screen.dart b/lib/presentation/chat/chat_select_screen/chat_select_screen.dart index 29a2a4cd..3717019a 100644 --- a/lib/presentation/chat/chat_select_screen/chat_select_screen.dart +++ b/lib/presentation/chat/chat_select_screen/chat_select_screen.dart @@ -76,7 +76,7 @@ class ChatSelectScreen extends StatelessWidget { hintText: "Поиск", ), SizedBox(height: 20.v), - _buildChatList(context), + _buildChatList(context, state), ElevatedButton( onPressed: (() => print(state.chatRooms)), child: Text("asdasd")) @@ -90,25 +90,19 @@ class ChatSelectScreen extends StatelessWidget { } } -_buildChatList(BuildContext context) { - return BlocSelector( - selector: (state) => state.chatSelectModelObj, - builder: (context, chatSelectModelObj) { - return ListView.separated( - shrinkWrap: true, - separatorBuilder: (context, index) { - return SizedBox( - height: 10.v, - ); - }, - itemCount: chatSelectModelObj?.userprofileItemList.length ?? 0, - itemBuilder: (context, index) { - ChatItemModel model = - chatSelectModelObj?.userprofileItemList[index] ?? ChatItemModel(); - return UserprofileItemWidget( - model, - ); - }, +_buildChatList(BuildContext context, ChatSelectLoadedState state) { + return ListView.separated( + shrinkWrap: true, + separatorBuilder: (context, index) { + return SizedBox( + height: 10.v, + ); + }, + itemCount: state.chatRooms?.length ?? 0, + itemBuilder: (context, index) { + ChatItemModel model = state.chatRooms?[index] ?? ChatItemModel(); + return UserprofileItemWidget( + model, ); }, ); diff --git a/lib/presentation/chat/chat_select_screen/models/chat_select_model.dart b/lib/presentation/chat/chat_select_screen/models/chat_select_model.dart index 4a3b9669..cc47e820 100644 --- a/lib/presentation/chat/chat_select_screen/models/chat_select_model.dart +++ b/lib/presentation/chat/chat_select_screen/models/chat_select_model.dart @@ -6,15 +6,15 @@ import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_entr /// This class defines the variables used in the [chat_select_screen], /// and is typically used to hold data that is passed between different parts of the application. class ChatSelectModel extends Equatable { - ChatSelectModel({this.userprofileItemList = const []}); + ChatSelectModel({this.chatList = const []}); - List userprofileItemList; - ChatSelectModel copyWith({List? userprofileItemList}) { + List chatList; + ChatSelectModel copyWith({List? chatList}) { return ChatSelectModel( - userprofileItemList: userprofileItemList ?? this.userprofileItemList, + chatList: chatList ?? this.chatList, ); } @override - List get props => [userprofileItemList]; + List get props => [chatList]; } diff --git a/lib/presentation/chat/chat_select_screen/widgets/chat_entry_widget.dart b/lib/presentation/chat/chat_select_screen/widgets/chat_entry_widget.dart index 0df63990..ffad0614 100644 --- a/lib/presentation/chat/chat_select_screen/widgets/chat_entry_widget.dart +++ b/lib/presentation/chat/chat_select_screen/widgets/chat_entry_widget.dart @@ -1,6 +1,7 @@ // ignore_for_file: must_be_immutable import 'package:flutter/material.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:eatvio_app/core/app_export.dart'; import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_entry.dart'; @@ -24,7 +25,8 @@ class UserprofileItemWidget extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ CustomImageView( - imagePath: userprofileItemModelObj.userImage!, + imagePath: + dotenv.env['STORAGE_URL']! + userprofileItemModelObj.userImage!, height: 60.adaptSize, width: 60.adaptSize, radius: BorderRadius.circular(