Skip to content

Commit

Permalink
Feat: 투데이 UiState 로딩 화면 구현 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaemin-Yoo committed May 10, 2023
1 parent 9369d92 commit 0fbee36
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.infinity.omos.R
import com.infinity.omos.ui.main.today.UiState
import com.infinity.omos.ui.view.OmosFieldView
import com.infinity.omos.ui.onboarding.error.ErrorField
import de.hdodenhof.circleimageview.CircleImageView
import timber.log.Timber

@BindingAdapter("isGone")
fun bindGone(view: View, isGone: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TodayFragment : Fragment() {
savedInstanceState: Bundle?
): View {
binding = FragmentTodayBinding.inflate(inflater, container, false).apply {
vm = viewModel
lifecycleOwner = viewLifecycleOwner
}
return binding.root
Expand Down
42 changes: 28 additions & 14 deletions app/src/main/java/com/infinity/omos/ui/main/today/TodayViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import com.infinity.omos.data.user.toPresentation
import com.infinity.omos.repository.today.TodayRepository
import com.infinity.omos.utils.DataStoreManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
Expand All @@ -38,12 +38,15 @@ class TodayViewModel @Inject constructor(
private val _lovedMusicUiState = MutableStateFlow<LovedMusicUiState>(LovedMusicUiState.Loading)
val lovedMusicUiState = _lovedMusicUiState.asStateFlow()

private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
val uiState = _uiState.asStateFlow()

init {
refresh()
}

private fun fetchTodayMusic() {
viewModelScope.launch {
private fun fetchTodayMusic(): Job {
return viewModelScope.launch {
todayRepository.getTodayMusic()
.mapCatching { it.toPresentation() }
.onSuccess { music ->
Expand All @@ -55,8 +58,8 @@ class TodayViewModel @Inject constructor(
}
}

private fun fetchFamousRecords() {
viewModelScope.launch {
private fun fetchFamousRecords(): Job {
return viewModelScope.launch {
todayRepository.getFamousRecords()
.mapCatching { records -> records.map { it.toPresentation() } }
.onSuccess { records ->
Expand All @@ -68,8 +71,8 @@ class TodayViewModel @Inject constructor(
}
}

private fun fetchRecommendedDjs() {
viewModelScope.launch {
private fun fetchRecommendedDjs(): Job {
return viewModelScope.launch {
todayRepository.getRecommendedDjs()
.mapCatching { djs -> djs.map { it.toPresentation() } }
.onSuccess { djs ->
Expand All @@ -81,8 +84,8 @@ class TodayViewModel @Inject constructor(
}
}

private fun fetchLovedMusic() {
viewModelScope.launch {
private fun fetchLovedMusic(): Job {
return viewModelScope.launch {
todayRepository.getLovedMusic(userId)
.mapCatching { it.toPresentation() }
.onSuccess { music ->
Expand All @@ -95,10 +98,16 @@ class TodayViewModel @Inject constructor(
}

fun refresh() {
fetchTodayMusic()
fetchFamousRecords()
fetchRecommendedDjs()
fetchLovedMusic()
_uiState.value = UiState.Loading
viewModelScope.launch {
val job1 = fetchTodayMusic()
val job2 = fetchFamousRecords()
val job3 = fetchRecommendedDjs()
val job4 = fetchLovedMusic()

joinAll(job1, job2, job3, job4)
_uiState.value = UiState.Complete
}
}
}

Expand All @@ -124,4 +133,9 @@ sealed interface LovedMusicUiState {
data class Success(val lovedMusic: LovedMusicModel) : LovedMusicUiState
object Error : LovedMusicUiState
object Loading : LovedMusicUiState
}

sealed interface UiState {
object Complete : UiState
object Loading : UiState
}
Loading

0 comments on commit 0fbee36

Please sign in to comment.