-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from aengzu/feature/history
feat: 대화 히스토리 추가
- Loading branch information
Showing
6 changed files
with
329 additions
and
55 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
lib/presentation/screens/mypage/controller/chat_history_viewmodel.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:get/get.dart'; | ||
import 'package:palink_v2/di/locator.dart'; | ||
import 'package:palink_v2/domain/model/character/character.dart'; | ||
import 'package:palink_v2/domain/model/chat/message.dart'; | ||
import 'package:palink_v2/domain/usecase/fetch_chat_history_usecase.dart'; | ||
|
||
class ChatHistoryViewmodel extends GetxController { | ||
final FetchChatHistoryUsecase getChatHistoryUsecase = Get.put(getIt<FetchChatHistoryUsecase>()); | ||
|
||
List<Message>? messages; // 단일 피드백 정보 저장 | ||
Character? character; // 캐릭터 정보 저장 | ||
int chatroomId; // 채팅방 ID | ||
RxBool conversationNotFound = true.obs; // 404 처리 플래그 | ||
|
||
|
||
ChatHistoryViewmodel({ | ||
required this.chatroomId, | ||
}); | ||
|
||
@override | ||
void onInit() { | ||
super.onInit(); | ||
conversationNotFound.value = true; | ||
loadMessages(); | ||
} | ||
|
||
// 메시지 로드 | ||
void loadMessages() async { | ||
try { | ||
// 메시지 가져오기 | ||
messages = await getChatHistoryUsecase.execute(chatroomId); | ||
messages = messages!.reversed.toList(); | ||
conversationNotFound.value = false; // 404 에러가 발생하지 않은 경우 플래그 설정; | ||
update(); | ||
} catch (e) { | ||
// 404 에러 발생 시 처리 | ||
if (e.toString().contains('404')) { | ||
conversationNotFound.value = true; // 404 에러가 발생한 경우 플래그 설정 | ||
} else { | ||
Get.snackbar('Error', 'Failed to load feedback'); | ||
} | ||
update(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
lib/presentation/screens/mypage/controller/myconversations_viewmodel.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:get/get.dart'; | ||
import 'package:palink_v2/di/locator.dart'; | ||
import 'package:palink_v2/domain/model/chat/conversation.dart'; | ||
import 'package:palink_v2/domain/usecase/get_chatroom_by_user.dart'; | ||
import 'package:palink_v2/domain/model/character/character.dart'; | ||
import 'package:palink_v2/domain/repository/character_repository.dart'; | ||
|
||
class MyconversationsViewmodel extends GetxController { | ||
final GetChatroomByUser getChatroomByUser = Get.put(getIt<GetChatroomByUser>()); | ||
final CharacterRepository characterRepository = Get.put(getIt<CharacterRepository>()); | ||
|
||
List<Conversation> chatrooms = []; | ||
Map<int, Character> characters = {}; // 캐릭터 정보 저장 | ||
|
||
MyconversationsViewmodel(); | ||
|
||
@override | ||
void onInit() { | ||
super.onInit(); | ||
_loadChatRooms(); | ||
} | ||
|
||
void _loadChatRooms() async { | ||
try { | ||
var fetchedData = await getChatroomByUser.execute(); | ||
chatrooms = fetchedData; | ||
|
||
// 캐릭터 ID에 해당하는 캐릭터 데이터 불러오기 | ||
for (var conversation in chatrooms) { | ||
var characterId = conversation.characterId; | ||
|
||
var character = await characterRepository.getCharacterById(characterId); | ||
characters[characterId] = character; // 캐릭터 정보 저장 | ||
} | ||
|
||
update(); // UI 업데이트 | ||
} catch (e) { | ||
// 에러 발생 시 처리 | ||
Get.snackbar('Error', 'Failed to load chatrooms'); | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
lib/presentation/screens/mypage/view/chat_history_view.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:palink_v2/domain/model/character/character.dart'; | ||
import 'package:palink_v2/presentation/screens/chatting/view/components/messages.dart'; | ||
import 'package:palink_v2/presentation/screens/common/appbar_perferred_size.dart'; | ||
import 'package:palink_v2/presentation/screens/main_screens.dart'; | ||
import 'package:palink_v2/presentation/screens/mypage/controller/chat_history_viewmodel.dart'; | ||
import 'package:sizing/sizing.dart'; | ||
|
||
import 'feedback_history_view.dart'; | ||
|
||
class ChatHistoryView extends StatelessWidget { | ||
final int chatroomId; | ||
final ChatHistoryViewmodel viewModel; | ||
final Character character; | ||
|
||
ChatHistoryView({required this.chatroomId, required this.character}) | ||
: viewModel = Get.put(ChatHistoryViewmodel(chatroomId: chatroomId)); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
backgroundColor: Colors.white, | ||
appBar: AppBar( | ||
backgroundColor: Colors.white, | ||
title: const Text('대화 기록'), | ||
bottom: appBarBottomLine(), | ||
), | ||
body: Stack( | ||
children: [ | ||
// 메시지 섹션이 스크롤 가능하도록 설정 | ||
Positioned.fill( | ||
child: Obx(() { | ||
// 대화 데이터가 로드되지 않은 경우 | ||
if (viewModel.conversationNotFound.value) { | ||
return const Center( | ||
child: Text( | ||
'대화가 저장되지 않았습니다.', | ||
style: TextStyle(fontSize: 18, color: Colors.grey), | ||
), | ||
); | ||
} | ||
// 대화가 있는 경우 메시지 리스트를 표시 | ||
return SingleChildScrollView( | ||
child: SizedBox( | ||
height: 0.8.sh, | ||
child: Messages( | ||
messages: viewModel.messages ?? [], | ||
userId: chatroomId, | ||
characterImg: character.image ?? '', | ||
onReactionAdded: (message, reaction) {}, | ||
), | ||
), | ||
); | ||
}), | ||
), | ||
// 버튼을 하단에 고정 | ||
Align( | ||
alignment: Alignment.bottomCenter, | ||
child: SafeArea( | ||
child: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
// 피드백 보기 버튼 추가 | ||
ElevatedButton( | ||
style: ElevatedButton.styleFrom( | ||
foregroundColor: Colors.white, | ||
backgroundColor: Colors.grey, | ||
padding: const EdgeInsets.symmetric(vertical: 20.0, horizontal: 30.0), // 버튼 크기 조절 | ||
shape: RoundedRectangleBorder( | ||
borderRadius: BorderRadius.circular(8.0), // 네모난 모서리 | ||
), | ||
), | ||
onPressed: () { | ||
Get.off(() => const MainScreens()); | ||
}, | ||
child: const Text('홈 화면으로'), | ||
), | ||
const SizedBox(width: 20), // 버튼 사이의 간격 추가 | ||
// 홈 화면으로 버튼 | ||
ElevatedButton( | ||
style: ElevatedButton.styleFrom( | ||
foregroundColor: Colors.white, | ||
backgroundColor: Colors.blueAccent, | ||
padding: const EdgeInsets.symmetric(vertical: 20.0, horizontal: 30.0), // 버튼 크기 조절 | ||
shape: RoundedRectangleBorder( | ||
borderRadius: BorderRadius.circular(8.0), // 네모난 모서리 | ||
), | ||
), | ||
onPressed: () { | ||
Get.to(() => FeedbackHistoryView( | ||
chatroomId: chatroomId, | ||
character: character, | ||
)); | ||
}, | ||
child: const Text('피드백 보기'), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.