-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AN] feat: 채팅 폴링 구현 #764
[AN] feat: 채팅 폴링 구현 #764
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package com.happy.friendogly.data.model | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class ChatRoomDto( | ||
val chatRoomId: Long, | ||
val clubName: String, | ||
val memberCount: Int, | ||
val clubImageUrl: String? = null, | ||
val recentMessage: String?, | ||
val recentMessageCreatedAt: LocalDateTime?, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package com.happy.friendogly.domain.model | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class ChatRoom( | ||
val chatRoomId: Long, | ||
val clubName: String, | ||
val memberCount: Int, | ||
val clubImageUrl: String? = null, | ||
val recentMessage: String?, | ||
val recentMessageCreatedAt: LocalDateTime?, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,15 +26,19 @@ class ChatListFragment : | |
binding.vm = viewModel | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
viewModel.getChats() | ||
override fun onHiddenChanged(hidden: Boolean) { | ||
super.onHiddenChanged(hidden) | ||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 메서드는 어떤 역할을 하고, 로직도 설명해주면 좋을 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 불필요한 업데이트를 방지하고자, MainActivity에서 show를 할 때만 폴링을 진행하는 로직입니다! |
||
if (hidden) { | ||
viewModel.cancelPolling() | ||
} else { | ||
viewModel.getPollingChats() | ||
} | ||
} | ||
|
||
private fun initAdapter() { | ||
adapter = ChatListAdapter(this) | ||
binding.rcvChatList.adapter = adapter | ||
viewModel.getChats() | ||
viewModel.getPollingChats() | ||
viewModel.chats.observe(viewLifecycleOwner) { chats -> | ||
adapter.submitList(chats) | ||
} | ||
|
@@ -43,7 +47,7 @@ class ChatListFragment : | |
|
||
private fun swipeEvent() { | ||
binding.swipelayoutChatListRefresh.setOnRefreshListener { | ||
viewModel.getChats() | ||
viewModel.getPollingChats() | ||
binding.swipelayoutChatListRefresh.isRefreshing = false | ||
} | ||
} | ||
|
@@ -59,7 +63,7 @@ class ChatListFragment : | |
) { result -> | ||
|
||
if (result.resultCode == LEAVE_CHAT_CODE) { | ||
viewModel.getChats() | ||
viewModel.getPollingChats() | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ import com.happy.friendogly.domain.usecase.GetChatListUseCase | |
import com.happy.friendogly.presentation.base.BaseViewModel | ||
import com.happy.friendogly.presentation.ui.chatlist.uimodel.ChatListUiModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
|
@@ -23,19 +26,30 @@ class ChatListViewModel | |
var memberId: Long = 0L | ||
private set | ||
|
||
private var pollJob: Job? = null | ||
|
||
val isChatEmpty = | ||
MediatorLiveData<Boolean>().apply { | ||
addSource(_chats) { | ||
value = it.isEmpty() | ||
} | ||
} | ||
|
||
fun getChats() { | ||
viewModelScope.launch { | ||
getChatListUseCase.invoke().onSuccess { room -> | ||
_chats.value = room.chatRooms.map { it.toUiModel() } | ||
memberId = room.myMemberId | ||
fun getPollingChats() { | ||
pollJob?.cancel() | ||
pollJob = | ||
viewModelScope.launch { | ||
while (this.isActive) { | ||
getChatListUseCase.invoke().onSuccess { room -> | ||
_chats.value = room.chatRooms.map { it.toUiModel() } | ||
memberId = room.myMemberId | ||
} | ||
delay(1000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delay를 작성한 이유가 무엇일까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 폴링 주기를 생각했을 때, 1초가 너무 느리지 않은 적당한 업데이트 주기라고 판단했습니다. |
||
} | ||
} | ||
} | ||
} | ||
|
||
fun cancelPolling() { | ||
pollJob?.cancel() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package com.happy.friendogly.remote.model.response | ||
|
||
import com.happy.friendogly.remote.util.JavaLocalDateTimeSerializer | ||
import kotlinx.serialization.Serializable | ||
import java.time.LocalDateTime | ||
|
||
@Serializable | ||
data class ChatRoomResponse( | ||
val chatRoomId: Long, | ||
val clubName: String, | ||
val memberCount: Int, | ||
val clubImageUrl: String? = null, | ||
val title: String, | ||
val imageUrl: String? = null, | ||
val recentMessage: String?, | ||
@Serializable(with = JavaLocalDateTimeSerializer::class) | ||
val recentMessageCreatedAt: LocalDateTime?, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
채팅방 입실/퇴실일 경우 null이 들어옵니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네