Skip to content

Commit

Permalink
refactor: 홈 화면 event SharedFlow 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Oct 4, 2023
1 parent 2493969 commit 8c25373
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.festago.festago.presentation.ui.home.festivallist.FestivalListFragmen
import com.festago.festago.presentation.ui.home.mypage.MyPageFragment
import com.festago.festago.presentation.ui.home.ticketlist.TicketListFragment
import com.festago.festago.presentation.ui.signin.SignInActivity
import com.festago.festago.presentation.util.repeatOnStarted
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down Expand Up @@ -48,12 +49,14 @@ class HomeActivity : AppCompatActivity() {
}

private fun initObserve() {
vm.event.observe(this) { event ->
when (event) {
is HomeEvent.ShowFestivalList -> showFestivalList()
is HomeEvent.ShowTicketList -> showTicketList()
is HomeEvent.ShowMyPage -> showMyPage()
is HomeEvent.ShowSignIn -> showSignIn()
repeatOnStarted(this) {
vm.event.collect { event ->
when (event) {
is HomeEvent.ShowFestivalList -> showFestivalList()
is HomeEvent.ShowTicketList -> showTicketList()
is HomeEvent.ShowMyPage -> showMyPage()
is HomeEvent.ShowSignIn -> showSignIn()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package com.festago.festago.presentation.ui.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.festago.festago.presentation.ui.home.HomeItemType.FESTIVAL_LIST
import com.festago.festago.presentation.ui.home.HomeItemType.MY_PAGE
import com.festago.festago.presentation.ui.home.HomeItemType.TICKET_LIST
import com.festago.festago.presentation.util.MutableSingleLiveData
import com.festago.festago.presentation.util.SingleLiveData
import com.festago.festago.repository.AuthRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val authRepository: AuthRepository) : ViewModel() {

private val _event = MutableSingleLiveData<HomeEvent>()
val event: SingleLiveData<HomeEvent> = _event
private val _event = MutableSharedFlow<HomeEvent>()
val event: SharedFlow<HomeEvent> = _event.asSharedFlow()

fun loadHomeItem(homeItemType: HomeItemType) {
when {
homeItemType == FESTIVAL_LIST -> _event.setValue(HomeEvent.ShowFestivalList)
!authRepository.isSigned -> _event.setValue(HomeEvent.ShowSignIn)
homeItemType == TICKET_LIST -> _event.setValue(HomeEvent.ShowTicketList)
homeItemType == MY_PAGE -> _event.setValue(HomeEvent.ShowMyPage)
viewModelScope.launch {
when {
homeItemType == FESTIVAL_LIST -> _event.emit(HomeEvent.ShowFestivalList)
!authRepository.isSigned -> _event.emit(HomeEvent.ShowSignIn)
homeItemType == TICKET_LIST -> _event.emit(HomeEvent.ShowTicketList)
homeItemType == MY_PAGE -> _event.emit(HomeEvent.ShowMyPage)
}
}
}
}

0 comments on commit 8c25373

Please sign in to comment.