generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
47 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
app/src/main/java/org/sopt/and/presentation/home/HomeEffect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.sopt.and.presentation.home | ||
|
||
sealed interface HomeEffect { | ||
data class ShowError(val message: String) : HomeEffect | ||
data object NavigateToDetail : HomeEffect | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/org/sopt/and/presentation/home/HomeIntent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.sopt.and.presentation.home | ||
|
||
sealed interface HomeIntent { | ||
data object LoadInitialData : HomeIntent | ||
data object RefreshContent : HomeIntent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/org/sopt/and/presentation/home/HomeState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.sopt.and.presentation.home | ||
|
||
data class HomeState( | ||
val bannerImages: List<Int> = emptyList(), | ||
val editorPicks: List<String> = emptyList(), | ||
val top20Items: List<String> = emptyList(), | ||
val isLoading: Boolean = false | ||
) |
68 changes: 42 additions & 26 deletions
68
app/src/main/java/org/sopt/and/presentation/home/HomeViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,60 @@ | ||
package org.sopt.and.presentation.home | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.R | ||
import javax.inject.Inject | ||
|
||
data class HomeUiState( | ||
val bannerImages: List<Int> = listOf(), | ||
val editorPicks: List<String> = listOf(), | ||
val top20Items: List<String> = listOf() | ||
) | ||
@HiltViewModel | ||
class HomeViewModel @Inject constructor() : ViewModel() { | ||
private val _state = MutableStateFlow(HomeState()) | ||
val state = _state.asStateFlow() | ||
|
||
class HomeViewModel : ViewModel() { | ||
private val _uiState = MutableStateFlow(HomeUiState()) | ||
val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow() | ||
private val _effect = Channel<HomeEffect>() | ||
val effect = _effect.receiveAsFlow() | ||
|
||
init { | ||
loadBannerImages() | ||
loadEditorPicks() | ||
loadTop20Items() | ||
processIntent(HomeIntent.LoadInitialData) | ||
} | ||
|
||
private fun loadBannerImages() { | ||
val images = listOf( | ||
R.drawable.banner_image, | ||
R.drawable.banner_image, | ||
R.drawable.banner_image, | ||
R.drawable.banner_image | ||
) | ||
_uiState.value = _uiState.value.copy(bannerImages = images) | ||
fun processIntent(intent: HomeIntent) { | ||
when (intent) { | ||
HomeIntent.LoadInitialData -> loadInitialData() | ||
HomeIntent.RefreshContent -> refreshContent() | ||
} | ||
} | ||
|
||
private fun loadEditorPicks() { | ||
val picks = List(5) { "추천작 $it" } | ||
_uiState.value = _uiState.value.copy(editorPicks = picks) | ||
private fun loadInitialData() { | ||
viewModelScope.launch { | ||
_state.update { it.copy(isLoading = true) } | ||
|
||
val images = listOf( | ||
R.drawable.banner_image, | ||
R.drawable.banner_image, | ||
R.drawable.banner_image, | ||
R.drawable.banner_image | ||
) | ||
val picks = List(5) { "추천작 $it" } | ||
val items = List(20) { "Top $it" } | ||
|
||
_state.update { it.copy( | ||
bannerImages = images, | ||
editorPicks = picks, | ||
top20Items = items, | ||
isLoading = false | ||
) } | ||
} | ||
} | ||
|
||
private fun loadTop20Items() { | ||
val items = List(20) { "Top $it" } | ||
_uiState.value = _uiState.value.copy(top20Items = items) | ||
private fun refreshContent() { | ||
loadInitialData() | ||
} | ||
} | ||
} |