Skip to content

Commit

Permalink
feat: 대화 기록 보러가기 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
aengzu committed Sep 24, 2024
1 parent bb6f82d commit bdfcb2d
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 29 deletions.
13 changes: 13 additions & 0 deletions lib/data/mapper/conversation_mapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:palink_v2/data/models/chat/conversation_response.dart';
import 'package:palink_v2/domain/model/chat/conversation.dart';

extension ConversationMapper on ConversationResponse {
Conversation toDomain() {
return Conversation(
conversationId: conversationId,
day: DateTime.parse(day),
userId: userId,
characterId: characterId,
);
}
}
1 change: 1 addition & 0 deletions lib/data/models/chat/conversation_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ class ConversationResponse {
factory ConversationResponse.fromJson(Map<String, dynamic> json) => _$ConversationResponseFromJson(json);
Map<String, dynamic> toJson() => _$ConversationResponseToJson(this);
}

28 changes: 17 additions & 11 deletions lib/data/repository/chat_repositoryImpl.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import 'package:palink_v2/data/api/chat/chat_api.dart';
import 'package:palink_v2/data/models/chat/ai_response_response.dart';
import 'package:palink_v2/data/models/chat/conversation_request.dart';
import 'package:palink_v2/data/models/chat/conversation_response.dart';
import 'package:palink_v2/data/models/chat/conversations_response.dart';
import 'package:palink_v2/data/models/chat/message_request.dart';
import 'package:palink_v2/data/models/chat/message_response.dart';
import 'package:palink_v2/data/models/chat/messages_response.dart';
Expand All @@ -16,12 +16,14 @@ class ChatRepositoryImpl implements ChatRepository {
ChatRepositoryImpl(this.chatApi);

@override
Future<ConversationResponse> createConversation(ConversationRequest conversationRequest) {
Future<ConversationResponse> createConversation(
ConversationRequest conversationRequest) {
return chatApi.createConversation(conversationRequest);
}

@override
Future<MessageResponse?> saveMessage(int conversationId, MessageRequest messageRequest) {
Future<MessageResponse?> saveMessage(
int conversationId, MessageRequest messageRequest) {
return chatApi.saveMessage(conversationId, messageRequest);
}

Expand All @@ -31,25 +33,29 @@ class ChatRepositoryImpl implements ChatRepository {
}

@override
Future<ConversationResponse> fetchConversationByChatRoomId(int conversationId) {
Future<ConversationResponse> fetchConversationByChatRoomId(
int conversationId) {
return chatApi.getConversationById(conversationId);
}

@override
Future<List<AIResponseResponse>> fetchAIResponseByMessageId(int conversationId, int messageId) {
Future<List<AIResponseResponse>> fetchAIResponseByMessageId(
int conversationId, int messageId) {
return chatApi.getAIResponsesByMessageId(conversationId, messageId);
}

@override
Future<List<AIResponseResponse>> fetchAIResponsesByConversationId(int conversationId) {
Future<List<AIResponseResponse>> fetchAIResponsesByConversationId(
int conversationId) {
return chatApi.getAIResponsesByConversationId(conversationId);
}

@override
Future<List<ConversationResponse>> fetchConversationsByUserId(int userId) {
throw chatApi.getConversationsByUserId(userId);
Future<List<ConversationResponse>> fetchConversationsByUserId(
int userId) async {
ConversationsResponse response =
await chatApi.getConversationsByUserId(userId);
return response
.conversations; // ConversationsResponse에서 conversations 리스트 추출
}


}

4 changes: 4 additions & 0 deletions lib/di/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import 'package:palink_v2/domain/usecase/generate_initial_message_usecase.dart';
import 'package:palink_v2/domain/usecase/generate_response_usecase.dart';
import 'package:palink_v2/domain/usecase/get_ai_message_usecase.dart';
import 'package:palink_v2/domain/usecase/get_ai_messages_usecase.dart';
import 'package:palink_v2/domain/usecase/get_chatroom_by_user.dart';
import 'package:palink_v2/domain/usecase/get_random_mindset_usecase.dart';
import 'package:palink_v2/domain/usecase/get_user_info_usecase.dart';
import 'package:palink_v2/domain/usecase/save_feedback_usecase.dart';
Expand All @@ -49,6 +50,7 @@ import 'package:palink_v2/presentation/screens/auth/controller/login_view_model.
import 'package:palink_v2/presentation/screens/auth/controller/signup_view_model.dart';
import 'package:palink_v2/presentation/screens/character_select/controller/character_select_viewmodel.dart';
import 'package:palink_v2/presentation/screens/chatting/controller/tip_viewmodel.dart';
import 'package:palink_v2/presentation/screens/mypage/controller/myfeedbacks_viewmodel.dart';
import 'package:palink_v2/presentation/screens/mypage/controller/mypage_viewmodel.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../data/repository/mindset_repositoryImpl.dart';
Expand Down Expand Up @@ -137,6 +139,7 @@ void _setupUseCases() {
getIt.registerFactory<GetAIMessagesUsecase>(() => GetAIMessagesUsecase());
getIt.registerFactory<GetAIMessageUsecase>(() => GetAIMessageUsecase());
getIt.registerFactory<SaveFeedbackUseCase>(() => SaveFeedbackUseCase());
getIt.registerFactory<GetChatroomByUser>(() => GetChatroomByUser(getIt<ChatRepository>(), getIt<UserRepository>()));


}
Expand All @@ -147,6 +150,7 @@ void _setupViewModels() {
getIt.registerFactory<MypageViewModel>(() => MypageViewModel(getUserInfoUseCase: getIt<GetUserInfoUseCase>()));
getIt.registerLazySingleton<CharacterSelectViewModel>(() => CharacterSelectViewModel(fetchCharactersUsecase: getIt<FetchCharactersUsecase>()));
getIt.registerFactory<TipViewModel>(() => TipViewModel());
getIt.registerFactory<MyfeedbacksViewmodel>(() => MyfeedbacksViewmodel());
}

Future<AppDatabase> _setupDatabase() async {
Expand Down
4 changes: 2 additions & 2 deletions lib/domain/model/chat/conversation.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:palink_v2/data/models/chat/conversation_response.dart';

class Conversation {
String day;
DateTime day;
int userId;
int characterId;
int conversationId;
Expand All @@ -17,7 +17,7 @@ class Conversation {
factory Conversation.fromResponse(ConversationResponse response) {
return Conversation(
conversationId: response.conversationId,
day: response.day,
day: DateTime.parse(response.day),
userId: response.userId,
characterId: response.characterId,
);
Expand Down
14 changes: 12 additions & 2 deletions lib/domain/usecase/get_chatroom_by_user.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:palink_v2/data/mapper/conversation_mapper.dart';
import 'package:palink_v2/domain/model/chat/conversation.dart';
import 'package:palink_v2/domain/repository/chat_repository.dart';
import 'package:palink_v2/data/models/chat/conversation_response.dart';
import 'package:palink_v2/domain/repository/user_repository.dart';
Expand All @@ -8,8 +10,16 @@ class GetChatroomByUser {

GetChatroomByUser(this.chatRepository, this.userRepository);

Future<List<ConversationResponse>> execute() async {
Future<List<Conversation>> execute() async {
int? userId = userRepository.getUserId();
return await chatRepository.fetchConversationsByUserId(userId!);

// 서버에서 받아온 ConversationResponse 데이터를 처리하기 위해 await 사용
List<ConversationResponse> response =
await chatRepository.fetchConversationsByUserId(userId!);

// 변환 작업 수행: List<ConversationResponse> -> List<Conversation>
List<Conversation> conversations =
response.map((convResp) => convResp.toDomain()).toList();
return conversations;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
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 MyfeedbacksViewmodel extends GetxController {
final GetChatroomByUser getChatroomByUser;
final GetChatroomByUser getChatroomByUser = Get.put(getIt<GetChatroomByUser>());
final CharacterRepository characterRepository = Get.put(getIt<CharacterRepository>());

MyfeedbacksViewmodel(this.getChatroomByUser);
List<Conversation> chatrooms = [];
Map<int, Character> characters = {}; // 캐릭터 정보 저장

MyfeedbacksViewmodel();

@override
void onInit() {
Expand All @@ -14,13 +22,21 @@ class MyfeedbacksViewmodel extends GetxController {

void _loadChatRooms() async {
try {
final chatroom = await getChatroomByUser.execute();
if (chatroom != null) {
} else {
Get.snackbar('Error', 'Failed to load user data');
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', 'An error occurred while loading user data');
// 에러 발생 시 처리
Get.snackbar('Error', 'Failed to load chatrooms');
}
}
}
46 changes: 43 additions & 3 deletions lib/presentation/screens/mypage/view/myfeedbacks_view.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:palink_v2/core/theme/app_fonts.dart';
import '../../common/appbar_perferred_size.dart';
import 'package:palink_v2/domain/model/character/character.dart';
import 'package:palink_v2/presentation/screens/common/appbar_perferred_size.dart';
import 'package:palink_v2/presentation/screens/mypage/controller/myfeedbacks_viewmodel.dart';

class MyfeedbacksView extends StatelessWidget {
final MyfeedbacksViewmodel viewModel = Get.put(MyfeedbacksViewmodel());

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xfff5f5f5),
appBar: AppBar(
backgroundColor: Colors.white,
title: Text('PALINK', style: textTheme().titleLarge),
title: Text('내 피드백 기록', style: textTheme().titleMedium),
centerTitle: false,
bottom: appBarBottomLine(),
),
body: Text('피드백들'),
body: GetBuilder<MyfeedbacksViewmodel>(
builder: (viewModel) {
if (viewModel.chatrooms.isEmpty) {
return const Center(child: Text('피드백이 없습니다.'));
}

return ListView.builder(
itemCount: viewModel.chatrooms.length,
itemBuilder: (context, index) {
var chatroom = viewModel.chatrooms[index];
var character = viewModel.characters[chatroom.characterId];
return Column(
children: [
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 15.0),
tileColor: Colors.white, // 배경을 하얀색으로 설정
leading: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.asset(character!.image)
),
title: Text(character != null ? character.name : '익명', style: textTheme().titleMedium),
subtitle: Text(_formatDate(chatroom.day)),
horizontalTitleGap: 30.0,
),
const Divider(),
],
);
},
);
},
),
);
}


// 날짜 포맷팅 함수
String _formatDate(DateTime date) {
return '${date.year}년 ${date.month}월 ${date.day}일 ${date.hour}시 ${date.minute}분';
}
}
8 changes: 4 additions & 4 deletions lib/presentation/screens/mypage/view/mypage_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class MypageView extends StatelessWidget {
children: [
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
title: const Text('내 친구들 보러가기'),
onTap: () => _showComingSoonDialog(context),
title: const Text('지난 피드백 보러가기'),
onTap: () => Get.to(() => MyfeedbacksView()),
trailing: const Icon(Icons.arrow_forward_ios),
),
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
title: const Text('지난 피드백 보러가기'),
onTap: () => Get.to(() => MyfeedbacksView()),
title: const Text('내 친구들 보러가기'),
onTap: () => _showComingSoonDialog(context),
trailing: const Icon(Icons.arrow_forward_ios),
),
ListTile(
Expand Down

0 comments on commit bdfcb2d

Please sign in to comment.