Skip to content

Commit

Permalink
Feat : TheirScreen, ViewModel Flow로만 유지
Browse files Browse the repository at this point in the history
- #53
  • Loading branch information
inwoo13 committed Nov 28, 2024
1 parent b6d5c87 commit 392d41b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 45 deletions.
47 changes: 14 additions & 33 deletions feature/their/src/main/java/com/kolown/their/TheirScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,24 @@ internal fun TheirRoute(
followerId: String,
viewModel: TheirViewModel = hiltViewModel(),
) {
val followerName = viewModel.followerName.collectAsStateWithLifecycle()
val uiState = viewModel.uiState.collectAsStateWithLifecycle()
val state = uiState.value

LaunchedEffect(followerName) {
viewModel.setFollowerName(followerId)
}
LaunchedEffect(followerId) {
viewModel.getFollowerGallery(followerId)
viewModel.setFollowerName(followerId)
}

when (state) {
is UiState.Loading -> {
Log.e("TheirRoute", "Loading: ${state}")
}
is UiState.Success -> {
Log.e("TheirRoute", "Success: ${state.data}")
val pagingItems = state.data.collectAsLazyPagingItems()
val pagerState = rememberLazyStaggeredGridState()

if(isLoggedIn) {
TheirScreen(
popBackStack = popBackStack,
padding = padding,
followerName = followerName.value,
pagingItems = pagingItems,
pagerState = pagerState
)
} else {
RestrictedLoginContent(navigateToLogin)
}

}
val followerName = viewModel.followerName.collectAsStateWithLifecycle()
val pagingItems = viewModel.galleryFlow.collectAsLazyPagingItems()
val pagerState = rememberLazyStaggeredGridState()

is UiState.Failure -> {
Log.e("TheirRoute", "Error: ${state.error}")
}
if(isLoggedIn) {
TheirScreen(
popBackStack = popBackStack,
padding = padding,
followerName = followerName.value,
pagingItems = pagingItems,
pagerState = pagerState
)
} else {
RestrictedLoginContent(navigateToLogin)
}
}

Expand Down
19 changes: 7 additions & 12 deletions feature/their/src/main/java/com/kolown/their/TheirViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.update
import javax.inject.Inject

@HiltViewModel
class TheirViewModel @Inject constructor(
private val postRepository: PostRepository,
private val followRepository: FollowRepository
) : ViewModel() {
private val _uiState =
MutableStateFlow<UiState<Flow<PagingData<PostContentModel>>>>(UiState.Loading)
val uiState = _uiState.asStateFlow()

private val _followerName = MutableStateFlow("Anonymous")
val followerName = _followerName.asStateFlow()

fun getFollowerGallery(followerId: String) {
try {
_uiState.value =
UiState.Success(postRepository.getUserPosts(followerId).cachedIn(viewModelScope))
} catch (e: Exception) {
_uiState.value = UiState.Failure(e)
}
}
private val _userId = MutableStateFlow("")
val galleryFlow = _userId.flatMapLatest { userId ->
postRepository.getUserPosts(userId)
}.cachedIn(viewModelScope)

fun setFollowerName(followerId: String) {
_userId.update { followerId }
followRepository.getFollowerName(followerId)
.onEach { _followerName.value = it }
.catch {
Expand Down

0 comments on commit 392d41b

Please sign in to comment.