Skip to content

Commit

Permalink
feat: DB로부터 채팅 내역 반영 및 TextField 터치 시 스크롤 맨아래로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyun-kim committed Dec 15, 2024
1 parent 5aa8abf commit d1480b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
32 changes: 24 additions & 8 deletions Feature-DM/Sources/Chatting/DirectMessageChattingReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by 김정윤 on 12/10/24.
//

import Foundation
import Utils
import NetworkKit
import ComposableArchitecture
Expand All @@ -19,28 +20,35 @@ public struct DirectMessageChattingReducer {
var chatRoomInfo: DirectMessage? = nil
var chatList: [DmChatting] = []
var text: String = ""
var isFocused = false
}

public enum Action: BindableAction {
case binding(BindingAction<State>)
case connectSocket // 소켓 연결
case setFocus(Bool)
case connectSocket // 소켓 연결
case disconnectSocket // 소켓 끊기
case saveChatRoom
case saveChatRoom // 채팅방 저장
case appendChat(DmChatting) // 채팅 보내고 데이터 추가
case sendMyChat // 채팅 보내기
case sendedChat // 채팅 보낸 후
case getAllChats // 서버/DB로부터 채팅 받아오기
case getAllChatsFromDB // DB로부터 채팅 내역 가져오기
case getAllChatsFromServer(after: String) // 서버로부터 채팅 내역 가져오기
case setAllChats([DmChatting]) // 서버 내 채팅 반영
}

public var body: some Reducer<State, Action> {
BindingReducer()
Reduce { state, action in
switch action {
case .setFocus(let focus):
state.isFocused = focus
return .none

case .connectSocket:
guard let info = state.chatRoomInfo else { return .none }
SocketService.shared.establishConnection(router: .dm(id: info.roomId))
return .send(.getAllChats)
return .none

case .appendChat(let chat):
state.chatList.append(chat)
Expand All @@ -55,10 +63,18 @@ public struct DirectMessageChattingReducer {
ChatRepository.shared.saveDmChatRoom(roomId: info.roomId, list: state.chatList)
return .none

case .getAllChats:
case .getAllChatsFromDB:
guard let info = state.chatRoomInfo else { return .none }
guard let dmChatRoom = ChatRepository.shared.readDmChatRoom(roomId: info.roomId) else {
return .send(.getAllChatsFromServer(after: info.createdAt))
}
let chatList = Array(dmChatRoom.chats).compactMap { $0.toDmChatting() }
state.chatList = chatList
return .send(.getAllChatsFromServer(after: dmChatRoom.lastReadDate))

case .getAllChatsFromServer(let after):
return .run { [id = UserDefaultsManager.shared.recentGroupId, info = state.chatRoomInfo] send in
guard let info else { return }
let after = ChatRepository.shared.getDmLastReadDate(roomId: info.roomId, createdAt: info.createdAt)
do {
let result = try await NetworkService.shared.getDmChattings(workspaceId: id, roomId: info.roomId, after: after)
await send(.setAllChats(result.map { $0.toDmChatting() }))
Expand All @@ -68,8 +84,8 @@ public struct DirectMessageChattingReducer {
}

case .setAllChats(let chats):
state.chatList = chats
return .none
state.chatList.append(contentsOf: chats)
return .send(.connectSocket)

case .sendMyChat:
return .run { [info = state.chatRoomInfo, id = UserDefaultsManager.shared.recentGroupId, message = state.text] send in
Expand Down
12 changes: 9 additions & 3 deletions Feature-DM/Sources/Chatting/DirectMessageChattingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ struct DirectMessageChattingView: View {
}
.onChange(of: store.chatRoomInfo) { oldValue, newValue in
guard let newValue else { return }
store.send(.connectSocket)
store.send(.getAllChatsFromDB)
}
.onChange(of: isFocused) { oldValue, newValue in
store.send(.setFocus(newValue))
}
.onChange(of: store.isFocused) { oldValue, newValue in
guard newValue else { return }
guard let last = store.chatList.last else { return }
print("⭐️", last)
proxy.scrollTo(last.dmId)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.22) {
withAnimation {
proxy.scrollTo(last.dmId, anchor: .top)
}
}
}
}
.navigationTitle(store.chatRoomInfo?.user.nickname ?? "")
Expand Down

0 comments on commit d1480b4

Please sign in to comment.