Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸš€ 3단계 - GitHub(UI μƒνƒœ) #33

Open
wants to merge 11 commits into
base: simuelunbo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class GithubRepoResponse(
@SerialName("id")
val id: Long,
@SerialName("full_name")
val fullName: String?,
val fullName: String,
@SerialName("description")
val description: String?,
)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/nextstep/github/model/GithubRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package nextstep.github.model

data class GithubRepo(
val id: Long,
val fullName: String?,
val fullName: String,
val description: String?,
)
116 changes: 86 additions & 30 deletions app/src/main/java/nextstep/github/ui/github/GithubRepoListScreen.kt
Original file line number Diff line number Diff line change
@@ -1,60 +1,80 @@
package nextstep.github.ui.github

import android.util.Log
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.launch
import nextstep.github.R
import nextstep.github.model.GithubRepo
import nextstep.github.ui.github.component.GithubRepoItem
import nextstep.github.ui.github.component.GithubRepoLoading
import nextstep.github.ui.github.component.GithubRepoSuccess
import nextstep.github.ui.theme.GithubTheme

@Composable
fun GithubRepositoryListScreen(
modifier: Modifier = Modifier,
viewModel: GithubRepoViewModel = viewModel(factory = GithubRepoViewModel.Factory),
) {
val repositoryList by viewModel.githubRepositories.collectAsStateWithLifecycle()
GithubRepositoryListScreen(repositories = repositoryList)
val uiState by viewModel.githubUiState.collectAsStateWithLifecycle()
val context = LocalContext.current
val snackBarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()

LaunchedEffect(true) {
viewModel.errorFlow.collect {
Comment on lines +39 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ°•μ˜ μ‹œκ°„μ—μ„œλ„ 닀룬 λ‚΄μš©μ΄κΈ΄ν•œλ°,
https://medium.com/androiddevelopers/viewmodel-one-off-event-antipatterns-16a1da869b95
μ€€λ³΄λ‹˜μ˜ 의견이 κΆκΈˆν•©λ‹ˆλ‹€! (의견만 주셔도 쒋을거 κ°™μ•„μš”!)

coroutineScope.launch {
val snackBarResult = snackBarHostState.showSnackbar(
message = context.getString(R.string.error_message),
actionLabel = context.getString(R.string.error_retry)
)
if (snackBarResult == SnackbarResult.ActionPerformed) run { viewModel::retryGithubRepo }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보톡 λ²”μœ„ μ§€μ •ν•¨μˆ˜λŠ” μ–Έμ œμ“°μ‹œλ‚˜μš”?
λ²”μœ„λ₯Ό μ§€μ •ν•˜κ±°λ‚˜, 가독성을 μœ„ν•΄ μ‚¬μš©ν•˜κ³€ν•˜μ§€λ§Œ
ν˜„μž¬λŠ” 무의미 해보이고, 였히렀 μ‹€μˆ˜λ₯Ό μœ λ°œν•˜λŠ” μœ„ν—˜ν•œ μ½”λ“œλŠ” μ•„λ‹κΉŒμš” :)

run { viewModel::retryGithubRepo } 의 결과값은 λ¬΄μ—‡μΌκΉŒμš”? :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν•΄λ‹Ή μ½”λ“œκ°€ λ™μž‘ν•˜λŠ”μ§€ ν…ŒμŠ€νŠΈν• μˆ˜μžˆλŠ” μ½”λ“œλ₯Ό μ§œλ΄λ„ 쒋을거 κ°™μ•„μš” :)

}
}
}
GithubRepositoryListScreen(
uiState = uiState,
snackBarHostState = snackBarHostState,
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun GithubRepositoryListScreen(
repositories: List<GithubRepo>,
uiState: GithubRepoUiState,
snackBarHostState: SnackbarHostState,
modifier: Modifier = Modifier
) {
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = { Text("NEXTSTEP Repositories") }
title = { Text(stringResource(R.string.top_bar_title)) }
)
}
},
snackbarHost = { SnackbarHost(hostState = snackBarHostState)}
) {
LazyColumn(
modifier = modifier
.fillMaxSize()
.padding(it),
) {
items(repositories) {repo ->
GithubRepoItem(githubRepo = repo)
HorizontalDivider()
}
if (uiState.isLoading) {
GithubRepoLoading(modifier = Modifier.padding(it))
}
GithubRepoSuccess(
modifier = Modifier.padding(it),
repositories = uiState.repositories
)
}
}

Expand All @@ -63,14 +83,50 @@ private fun GithubRepositoryListScreen(
private fun GithubRepositoryListScreenPreview() {
GithubTheme {
GithubRepositoryListScreen(
repositories = listOf(
GithubRepo(1, "NextStep/Test", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ"),
GithubRepo(2, "NextStep/Test2", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ2"),
GithubRepo(3, "NextStep/Test3", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ3"),
GithubRepo(4, "NextStep/Test4", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ4"),
GithubRepo(5, "NextStep/Test5", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ5"),
GithubRepo(6, "NextStep/Test6", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ6"),
uiState = GithubRepoUiState(
isLoading = false,
repositories = listOf(
GithubRepo(1, "NextStep/Test", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ"),
GithubRepo(2, "NextStep/Test2", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ2"),
GithubRepo(3, "NextStep/Test3", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ3"),
GithubRepo(4, "NextStep/Test4", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ4"),
GithubRepo(5, "NextStep/Test5", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ5"),
GithubRepo(6, "NextStep/Test6", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ6"),
),
),
snackBarHostState = SnackbarHostState()
)
}
}

@Preview(showBackground = true)
@Composable
private fun GithubRepositoryListScreenErrorPreview() {
val coroutineScope = rememberCoroutineScope()
val snackBarHostState = remember { SnackbarHostState() }
LaunchedEffect(Unit) {
coroutineScope.launch {
snackBarHostState.showSnackbar(
message = "μ—λŸ¬ 메세지",
actionLabel = "μž¬μ‹œλ„"
)
}
}
Comment on lines +105 to +114

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ€€λ³΄λ‹˜μ΄ μƒκ°ν•˜μ‹œλŠ” Preview의 기쀀은 λ¬΄μ—‡μΈκ°€μš” :)
μœ„μ˜ λ‘œμ§μ€ Preview둜 보긴 νž˜λ“€μ§€ μ•Šμ„κΉŒμš”?

μ΄λ²€νŠΈλ“€μ€ ui ν…ŒμŠ€νŠΈλ‘œ μž‘μ„±ν•΄λ³΄λ©΄ μ–΄λ–¨κΉŒμš”? :)

GithubTheme {
GithubRepositoryListScreen(
uiState = GithubRepoUiState(isLoading = true),
snackBarHostState = snackBarHostState
)
}
}

@Preview(showBackground = true)
@Composable
private fun GithubRepositoryListScreenLoadingPreview() {
GithubTheme {
GithubRepositoryListScreen(
uiState = GithubRepoUiState(isLoading = true),
snackBarHostState = SnackbarHostState()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nextstep.github.ui.github

import nextstep.github.model.GithubRepo

data class GithubRepoUiState (
val isLoading: Boolean = false,
val repositories: List<GithubRepo> = emptyList(),
)

43 changes: 32 additions & 11 deletions app/src/main/java/nextstep/github/ui/github/GithubRepoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,46 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import nextstep.github.GithubApplication
import nextstep.github.data.repository.GithubRepository
import nextstep.github.model.GithubRepo

class GithubRepoViewModel(
private val githubRepoRepository: GithubRepository,
) : ViewModel() {

val githubRepositories: StateFlow<List<GithubRepo>> = flow<List<GithubRepo>> {
emit(githubRepoRepository.getRepositories("next-step"))
}.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(),
emptyList()
)
private val _githubUiState: MutableStateFlow<GithubRepoUiState> = MutableStateFlow(GithubRepoUiState())
val githubUiState: StateFlow<GithubRepoUiState> = _githubUiState.asStateFlow()

private val _errorFlow = MutableSharedFlow<Throwable>()
val errorFlow = _errorFlow.asSharedFlow()

init {
fetchRepositories()
}

private fun fetchRepositories() {
viewModelScope.launch(CoroutineExceptionHandler { _, throwable ->
_errorFlow.tryEmit(throwable)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ—λŸ¬κ°€ λ°œμƒν•œλ‹€λ©΄, tryEmit이 잘 μž‘λ™ν• κΉŒμš”? :)

}) {
_githubUiState.update { it.copy(isLoading = true) }
val repositories = githubRepoRepository.getRepositories("next-step")
_githubUiState.update { it.copy(repositories = repositories, isLoading = false) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ€€λ³΄λ‹˜μ΄ μƒκ°ν•˜λŠ” UiStateλ₯Ό μ„€κ³„ν•˜λŠ” 기쀀이 λ¬΄μ—‡μΈκ°€μš”?
μˆœμˆ˜ν•˜κ²Œ κΆκΈˆν•©λ‹ˆλ‹€ :)

}
}

fun retryGithubRepo() {
fetchRepositories()
}


companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fun GithubRepoItem(
.padding(16.dp),
) {
Text(
text = githubRepo.fullName ?: "Unknown",
text = githubRepo.fullName,
style = MaterialTheme.typography.titleLarge,
)
Text(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nextstep.github.ui.github.component

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun GithubRepoLoading(
modifier: Modifier = Modifier,
) {
Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}

@Preview(showBackground = true)
@Composable
private fun GithubRepoLoadingPreview() {
GithubRepoLoading()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextstep.github.ui.github.component

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import nextstep.github.model.GithubRepo
import nextstep.github.ui.theme.GithubTheme

@Composable
fun GithubRepoSuccess(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GithubRepoSuccessλΌλŠ” 이름은 데이터 κ΄€μ μ˜ λ„€μ΄λ°μ•„λ‹κΉŒμš”?
GithubRepoList 같은 직관적인 넀이밍이 쒀더 κ°€λ…μ„±μžˆμ§€ μ•Šμ„κΉŒμš” :)

repositories: List<GithubRepo>,
modifier: Modifier = Modifier,
) {
LazyColumn(modifier = modifier.fillMaxSize()) {
items(repositories) { repo ->
GithubRepoItem(githubRepo = repo)
HorizontalDivider()
}
}
}

@Preview(showBackground = true)
@Composable
private fun GithubRepoSuccessPreview() {
GithubTheme {
GithubRepoSuccess(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ¦¬μŠ€νŠΈκ°€ λΉ„μ–΄μžˆμ„λ•ŒλŠ” μ–΄λ–€ 화면이 λ‚˜μ˜€λ‚˜μš”?
μš”κ΅¬μ‚¬ν•­ λͺ©λ‘μ΄ 빈 κ²½μš°μ—λŠ” 빈 ν™”λ©΄ UIλ₯Ό λ…ΈμΆœν•œλ‹€.(ν”Όκ·Έλ§ˆ μ°Έμ‘°)을 μΆ”κ°€ν•΄μ£Όμ„Έμš” :)

repositories = listOf(
GithubRepo(1, "NextStep/Test", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ"),
GithubRepo(2, "NextStep/Test2", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ2"),
GithubRepo(3, "NextStep/Test3", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ3"),
GithubRepo(4, "NextStep/Test4", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ4"),
GithubRepo(5, "NextStep/Test5", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ5"),
GithubRepo(6, "NextStep/Test6", "ν…ŒμŠ€νŠΈ μ €μž₯μ†Œ6"),
)
)
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">GitHub</string>
<string name="top_bar_title">"NEXTSTEP Repositories"</string>
<string name="error_message">μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.</string>
<string name="error_retry">μž¬μ‹œλ„</string>
</resources>