Skip to content

Commit

Permalink
[fix] #192 마이피드 조회하고 나서 success 상태가 계속 관찰되는 문제 해결
Browse files Browse the repository at this point in the history
- UiState.Empty로 초기화
- 실패 상태는 flow catch 확장함수 이용
  • Loading branch information
leeeha committed Sep 13, 2023
1 parent 3258f22 commit 4a4f7de
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.recyclerview.widget.SimpleItemAnimator
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -50,7 +49,7 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_
initAdapter()
initBackButtonClickListener()

collectMyFeedPagingData()
initGetMyFeedListStateObserver()
initGetDetailFeedStateObserver()
initPostLikeStateObserver()
initDeleteFeedStateObserver()
Expand Down Expand Up @@ -195,13 +194,24 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_
}
}

private fun collectMyFeedPagingData() {
viewLifeCycleScope.launch {
viewModel.myFeedPagingData.collectLatest { pagingData ->
Timber.e("PAGING DATA COLLECT in Fragment")
myFeedAdapter.submitData(pagingData)
}
}
private fun initGetMyFeedListStateObserver() {
viewModel.getMyFeedListState.flowWithLifecycle(viewLifeCycle)
.onEach { state ->
when (state) {
is UiState.Success -> {
Timber.e("PAGING DATA SUBMIT in Fragment")
val pagingData = state.data
myFeedAdapter.submitData(pagingData)
viewModel.initGetMyFeedListState()
}

is UiState.Failure -> {
snackBar(binding.root) { state.msg }
}

else -> {}
}
}.launchIn(viewLifeCycleScope)
}

private fun initPagingLoadStateListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.go.sopt.winey.data.model.remote.request.RequestPostLikeDto
import org.go.sopt.winey.domain.entity.DetailFeed
Expand All @@ -24,8 +25,10 @@ import javax.inject.Inject
class MyFeedViewModel @Inject constructor(
private val feedRepository: FeedRepository
) : ViewModel() {
private val _myFeedPagingData: Flow<PagingData<WineyFeed>> by lazy { getMyFeedList() }
val myFeedPagingData get() = _myFeedPagingData
private val _getMyFeedListState =
MutableStateFlow<UiState<PagingData<WineyFeed>>>(UiState.Empty)
val getMyFeedListState: StateFlow<UiState<PagingData<WineyFeed>>> =
_getMyFeedListState.asStateFlow()

private val _getDetailFeedState =
MutableStateFlow<UiState<DetailFeed?>>(UiState.Loading)
Expand All @@ -38,9 +41,27 @@ class MyFeedViewModel @Inject constructor(
private val _deleteMyFeedState = MutableStateFlow<UiState<Unit>>(UiState.Empty)
val deleteMyFeedState: StateFlow<UiState<Unit>> = _deleteMyFeedState.asStateFlow()

private fun getMyFeedList(): Flow<PagingData<WineyFeed>> {
Timber.e("PAGING DATA LOAD in ViewModel")
return feedRepository.getMyFeedList().cachedIn(viewModelScope)
init {
getMyFeedList()
}

private fun getMyFeedList() {
viewModelScope.launch {
_getMyFeedListState.emit(UiState.Loading)

feedRepository.getMyFeedList().cachedIn(viewModelScope)
.catch { t ->
handleFailureState(_getMyFeedListState, t)
}
.collectLatest { pagingData ->
Timber.e("PAGING DATA COLLECT in ViewModel")
_getMyFeedListState.emit(UiState.Success(pagingData))
}
}
}

fun initGetMyFeedListState() {
_getMyFeedListState.value = UiState.Empty
}

fun getDetailFeed(feedId: Int) {
Expand Down

0 comments on commit 4a4f7de

Please sign in to comment.