From a2c2f410071c3b4dcb6660f9dafdfe0db88d79a3 Mon Sep 17 00:00:00 2001 From: gaeun5744 Date: Mon, 16 Dec 2024 23:24:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20polling=EC=9D=84=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=97=86=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/chatlist/ChatListFragment.kt | 16 +++++++----- .../ui/chatlist/ChatListViewModel.kt | 26 ++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListFragment.kt b/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListFragment.kt index d7e649d80..58cbe3dc1 100644 --- a/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListFragment.kt +++ b/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListFragment.kt @@ -26,15 +26,19 @@ class ChatListFragment : binding.vm = viewModel } - override fun onStart() { - super.onStart() - viewModel.getChats() + override fun onHiddenChanged(hidden: Boolean) { + super.onHiddenChanged(hidden) + 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() } } } diff --git a/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListViewModel.kt b/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListViewModel.kt index 14fbd61a1..78d59590c 100644 --- a/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListViewModel.kt +++ b/android/app/src/main/java/com/happy/friendogly/presentation/ui/chatlist/ChatListViewModel.kt @@ -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,6 +26,8 @@ class ChatListViewModel var memberId: Long = 0L private set + private var pollJob: Job? = null + val isChatEmpty = MediatorLiveData().apply { addSource(_chats) { @@ -30,12 +35,21 @@ class ChatListViewModel } } - 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) + } } - } + } + + fun cancelPolling() { + pollJob?.cancel() } }