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 93ca5c6 commit 016fc7c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 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,15 +27,47 @@ import com.terning.feature.R
import com.terning.feature.filtering.filteringtwo.component.StatusTwoRadioGroup

@Composable
fun FilteringTwoScreen(
fun FilteringTwoRoute(
grade: Int,
onNextClick: (Int, Int) -> Unit,
navigateUp: () -> Unit,
onButtonClick: (Int) -> Unit = {},
viewModel: FilteringTwoViewModel = hiltViewModel()
) {
val isButtonValid = remember { mutableStateOf(false) }
var workingPeriod 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 FilteringTwoSideEffect.NavigateUp -> navigateUp()
}
}
}

FilteringTwoScreen(
grade = grade,
onNextClick = onNextClick,
navigateUp = viewModel::navigateUp,
onButtonClick = { index ->
viewModel.updateWorkingPeriod(index)
viewModel.updateButtonValidation()
},
buttonState = state.isButtonValid,
workingPeriod = state.workingPeriod
)
}

@Composable
fun FilteringTwoScreen(
grade: Int,
onNextClick: (Int, Int) -> Unit,
navigateUp: () -> Unit,
onButtonClick: (Int) -> Unit,
buttonState: Boolean,
workingPeriod: Int
) {
Column(
modifier = Modifier
) {
Expand Down Expand Up @@ -73,8 +106,6 @@ fun FilteringTwoScreen(
StatusTwoRadioGroup(
onButtonClick = { index ->
onButtonClick(index)
isButtonValid.value = true
workingPeriod = index
}
)
Spacer(modifier = Modifier.weight(1f))
Expand All @@ -84,7 +115,7 @@ fun FilteringTwoScreen(
text = R.string.filtering_button,
onButtonClick = { onNextClick(grade, workingPeriod) },
modifier = Modifier.padding(bottom = 12.dp),
isEnabled = isButtonValid.value
isEnabled = buttonState
)
}
}
Expand All @@ -97,7 +128,10 @@ fun FilteringTwoScreenPreview() {
FilteringTwoScreen(
grade = 1,
onNextClick = { _, _ -> },
navigateUp = { }
navigateUp = { },
onButtonClick = { },
buttonState = true,
workingPeriod = 1
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.terning.feature.filtering.filteringtwo

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

data class FilteringTwoState(
val isButtonValid: Boolean = false,
val workingPeriod: Int = -1
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.terning.feature.filtering.filteringtwo

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.terning.feature.filtering.filteringone.FilteringOneSideEffect
import com.terning.feature.filtering.filteringone.FilteringOneState
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 FilteringTwoViewModel : ViewModel(){

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

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

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

fun updateWorkingPeriod(workingPeriod: Int) {
_state.value = _state.value.copy(workingPeriod = workingPeriod)
}

fun navigateUp() =
viewModelScope.launch { _sideEffects.emit(FilteringTwoSideEffect.NavigateUp) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import com.terning.core.navigation.Route
import com.terning.feature.filtering.filteringtwo.FilteringTwoScreen
import com.terning.feature.filtering.filteringthree.navigation.navigateFilteringThree
import com.terning.feature.filtering.filteringtwo.FilteringTwoRoute
import kotlinx.serialization.Serializable

fun NavController.navigateFilteringTwo(
Expand All @@ -26,15 +26,15 @@ fun NavGraphBuilder.filteringTwoNavGraph(
) {
composable<FilteringTwo> {
val args = it.toRoute<FilteringTwo>()
FilteringTwoScreen(
FilteringTwoRoute(
grade = args.grade,
onNextClick = { _, workingPeriod ->
navHostController.navigateFilteringThree(
grade = args.grade,
workingPeriod = workingPeriod
)
},
navigateUp = { navHostController.navigateUp() }
navigateUp = navHostController::navigateUp
)
}
}
Expand Down

0 comments on commit 016fc7c

Please sign in to comment.