Skip to content

Commit

Permalink
[FEAT/#218] 필터링 원 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeyubin committed Sep 3, 2024
1 parent 990a776 commit 93ca5c6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.flowWithLifecycle
import com.terning.core.designsystem.component.button.RectangleButton
import com.terning.core.designsystem.component.topappbar.BackButtonTopAppBar
import com.terning.core.designsystem.theme.Grey300
Expand All @@ -26,20 +27,52 @@ import com.terning.feature.R
import com.terning.feature.filtering.filteringone.component.StatusOneRadioGroup

@Composable
fun FilteringOneScreen(
fun FilteringOneRoute(
name: String,
onNextClick: (Int) -> Unit,
navigateUp: () -> Unit,
onButtonClick: (Int) -> Unit = {},
viewModel: FilteringOneViewModel = hiltViewModel()
) {
val isButtonValid = remember { mutableStateOf(false) }
var grade by remember { mutableIntStateOf(-1) }
val state by viewModel.state.collectAsStateWithLifecycle()

val lifecycleOwner = LocalLifecycleOwner.current

LaunchedEffect(viewModel.sideEffects, lifecycleOwner) {
viewModel.sideEffects.flowWithLifecycle(lifecycle = lifecycleOwner.lifecycle)
.collect { sideEffect ->
when (sideEffect) {
is FilteringOneSideEffect.NavigateUp -> navigateUp()
}
}
}

FilteringOneScreen(
name = name,
onButtonClick = { index ->
viewModel.updateGrade(index)
viewModel.updateButtonValidation()
},
onNextClick = onNextClick,
navigateUp = viewModel::navigateUp,
buttonState = state.isButtonValid,
gradeState = state.grade
)
}

@Composable
fun FilteringOneScreen(
name: String,
onNextClick: (Int) -> Unit,
navigateUp: () -> Unit,
onButtonClick: (Int) -> Unit,
buttonState: Boolean,
gradeState: Int
) {
Column(
modifier = Modifier
) {
BackButtonTopAppBar(
onBackButtonClick = { navigateUp() }
onBackButtonClick = navigateUp
)
Column(
modifier = Modifier.fillMaxSize()
Expand Down Expand Up @@ -79,8 +112,6 @@ fun FilteringOneScreen(
StatusOneRadioGroup(
onButtonClick = { index ->
onButtonClick(index)
isButtonValid.value = true
grade = index
}
)
Text(
Expand All @@ -96,9 +127,9 @@ fun FilteringOneScreen(
style = TerningTheme.typography.button0,
paddingVertical = 20.dp,
text = R.string.filtering_button,
onButtonClick = { onNextClick(grade) },
onButtonClick = { onNextClick(gradeState) },
modifier = Modifier.padding(bottom = 12.dp),
isEnabled = isButtonValid.value
isEnabled = buttonState
)
}
}
Expand All @@ -112,7 +143,9 @@ fun FilteringOneScreenPreview() {
name = "터닝이",
onButtonClick = {},
onNextClick = {},
navigateUp = {}
navigateUp = {},
buttonState = true,
gradeState = 1
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.terning.feature.filtering.filteringone

sealed class FilteringOneSideEffect {
data object NavigateUp : FilteringOneSideEffect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.terning.feature.filtering.filteringone

data class FilteringOneState(
val isButtonValid: Boolean = false,
val grade: Int = -1
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.terning.feature.filtering.filteringone

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class FilteringOneViewModel : ViewModel() {

private val _state: MutableStateFlow<FilteringOneState> = MutableStateFlow(FilteringOneState())
val state: StateFlow<FilteringOneState> get() = _state.asStateFlow()

private val _sideEffects = MutableSharedFlow<FilteringOneSideEffect>()
val sideEffects: SharedFlow<FilteringOneSideEffect> get() = _sideEffects.asSharedFlow()

fun updateButtonValidation() {
_state.value = _state.value.copy(isButtonValid = true)
}

fun updateGrade(grade: Int) {
_state.value = _state.value.copy(grade = grade)
}

fun navigateUp() =
viewModelScope.launch { _sideEffects.emit(FilteringOneSideEffect.NavigateUp) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import com.terning.core.navigation.Route
import com.terning.feature.filtering.filteringone.FilteringOneScreen
import com.terning.feature.filtering.filteringone.FilteringOneRoute
import com.terning.feature.filtering.filteringtwo.navigation.navigateFilteringTwo
import kotlinx.serialization.Serializable

Expand All @@ -26,7 +26,7 @@ fun NavGraphBuilder.filteringOneNavGraph(
) {
composable<FilteringOne> {
val args = it.toRoute<FilteringOne>()
FilteringOneScreen(
FilteringOneRoute(
name = args.name,
onNextClick = { grade -> navHostController.navigateFilteringTwo(grade) },
navigateUp = { navHostController.navigateUp() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun FilteringThreeRoute(
workingPeriod: Int,
navigateUp: () -> Unit,
navigateToStartHome: () -> Unit,
viewModel: FilteringViewModel = hiltViewModel(),
viewModel: FilteringThreeViewModel = hiltViewModel(),
) {
val state by viewModel.state.collectAsStateWithLifecycle()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.terning.feature.filtering.filteringthree

data class FilteringState(
data class FilteringThreeState(
val grade: Int = -1,
val workingPeriod: Int = -1,
val startYear: Int = -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class FilteringViewModel @Inject constructor(
class FilteringThreeViewModel @Inject constructor(
private val filteringRepository: FilteringRepository,
private val tokenRepository: TokenRepository
) : ViewModel() {

private val _state = MutableStateFlow(FilteringState())
val state: StateFlow<FilteringState> get() = _state.asStateFlow()
private val _state = MutableStateFlow(FilteringThreeState())
val state: StateFlow<FilteringThreeState> get() = _state.asStateFlow()

private val _sideEffects = MutableSharedFlow<FilteringThreeSideEffect>()
val sideEffects: SharedFlow<FilteringThreeSideEffect> get() = _sideEffects.asSharedFlow()
Expand Down

0 comments on commit 93ca5c6

Please sign in to comment.