Skip to content

Commit

Permalink
[FEAT/#183] 상태 클래스 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
boiledEgg-s committed Aug 25, 2024
1 parent b629157 commit c3db39d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
Expand All @@ -28,23 +27,20 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.terning.core.designsystem.component.topappbar.CalendarTopAppBar
import com.terning.core.designsystem.theme.Grey200
import com.terning.core.designsystem.theme.White
import com.terning.feature.calendar.calendar.component.ScreenTransition
import com.terning.feature.calendar.calendar.component.WeekDaysHeader
import com.terning.feature.calendar.calendar.model.CalendarModel
import com.terning.feature.calendar.calendar.model.CalendarModel.Companion.getLocalDateByPage
import com.terning.feature.calendar.calendar.model.CalendarModel.Companion.getYearMonthByPage
import com.terning.feature.calendar.calendar.model.CalendarUiState
import com.terning.feature.calendar.list.CalendarListRoute
import com.terning.feature.calendar.month.CalendarMonthRoute
import com.terning.feature.calendar.week.CalendarWeekRoute
import com.terning.feature.intern.navigation.navigateIntern
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import java.time.YearMonth
import java.time.LocalDate

@Composable
fun CalendarRoute(
Expand All @@ -53,61 +49,61 @@ fun CalendarRoute(
viewModel: CalendarViewModel = hiltViewModel()
) {
val lifecycleOwner = LocalLifecycleOwner.current
val calendarUiState by viewModel.uiState.collectAsStateWithLifecycle(lifecycleOwner = lifecycleOwner)
val uiState by viewModel.uiState.collectAsStateWithLifecycle(lifecycleOwner = lifecycleOwner)

BackHandler {
if (calendarUiState.isWeekEnabled) {
viewModel.updateSelectedDate(calendarUiState.selectedDate)
} else if (calendarUiState.isListEnabled) {
if (uiState.isWeekEnabled) {
viewModel.updateSelectedDate(uiState.selectedDate)
} else if (uiState.isListEnabled) {
viewModel.updateListVisibility(false)
} else {
navigateUp()
}
}

CalendarScreen(
calendarUiState = calendarUiState,
uiState = uiState,
navigateToAnnouncement = navigateToAnnouncement,
viewModel = viewModel
updateSelectedDate = viewModel::updateSelectedDate,
updatePage = viewModel::updatePage,
onClickListButton = {
viewModel.updateListVisibility(!uiState.isListEnabled)
if (uiState.isWeekEnabled) { viewModel.updateWeekVisibility(false) }
}
)
}

@Composable
private fun CalendarScreen(
modifier: Modifier = Modifier,
calendarUiState: CalendarUiState,
uiState: CalendarUiState,
navigateToAnnouncement: (Long) -> Unit,
viewModel: CalendarViewModel = hiltViewModel()
updateSelectedDate: (LocalDate) -> Unit,
updatePage: (Int) -> Unit,
onClickListButton: () -> Unit,
modifier: Modifier = Modifier,
) {
val calendarModel = remember { CalendarModel() }
val coroutineScope = rememberCoroutineScope()

val listState = rememberLazyListState(
initialFirstVisibleItemIndex = calendarModel.initialPage
initialFirstVisibleItemIndex = uiState.calendarModel.initialPage
)

var currentPage by rememberSaveable { mutableIntStateOf(listState.firstVisibleItemIndex) }
LaunchedEffect(key1 = listState) {
snapshotFlow { listState.firstVisibleItemIndex }
.distinctUntilChanged()
.collect {
currentPage = listState.firstVisibleItemIndex
updatePage(listState.firstVisibleItemIndex)
}
}

Scaffold(
modifier = modifier,
topBar = {
val coroutineScope = rememberCoroutineScope()
CalendarTopAppBar(
date = getYearMonthByPage(currentPage),
isListExpanded = calendarUiState.isListEnabled,
isWeekExpanded = calendarUiState.isWeekEnabled,
onListButtonClicked = {
viewModel.updateListVisibility(!calendarUiState.isListEnabled)
if (calendarUiState.isWeekEnabled) {
viewModel.updateWeekVisibility(false)
}
},
date = getYearMonthByPage(uiState.currentPage),
isListExpanded = uiState.isListEnabled,
isWeekExpanded = uiState.isWeekEnabled,
onListButtonClicked = onClickListButton,
onMonthNavigationButtonClicked = { direction ->
coroutineScope.launch {
listState.animateScrollToItem(
Expand All @@ -119,7 +115,7 @@ private fun CalendarScreen(
}
) { paddingValues ->
ScreenTransition(
targetState = !calendarUiState.isListEnabled,
targetState = !uiState.isListEnabled,
transitionOne = slideInHorizontally { fullWidth -> -fullWidth } togetherWith
slideOutHorizontally { fullWidth -> fullWidth },
transitionTwo = slideInHorizontally { fullWidth -> fullWidth } togetherWith
Expand All @@ -138,31 +134,29 @@ private fun CalendarScreen(
)

ScreenTransition(
targetState = !calendarUiState.isWeekEnabled,
targetState = !uiState.isWeekEnabled,
transitionOne = slideInVertically { fullHeight -> -fullHeight } togetherWith
slideOutVertically { fullHeight -> fullHeight },
transitionTwo = slideInVertically { fullHeight -> fullHeight } togetherWith
slideOutVertically { fullHeight -> -fullHeight },
contentOne = {
CalendarMonthRoute(
listState = listState,
pages = calendarModel.pageCount,
selectedDate = calendarUiState.selectedDate,
updateSelectedDate = { newDate -> viewModel.updateSelectedDate(newDate) },
pages = uiState.calendarModel.pageCount,
selectedDate = uiState.selectedDate,
updateSelectedDate = updateSelectedDate,
modifier = Modifier
.fillMaxSize()
.background(White),
)
},
contentTwo = {
CalendarWeekRoute(
calendarUiState = calendarUiState,
calendarUiState = uiState,
modifier = Modifier
.fillMaxSize(),
navigateToAnnouncement = navigateToAnnouncement,
updateSelectedDate = { newDate ->
viewModel.updateSelectedDate(newDate)
}
updateSelectedDate = updateSelectedDate
)
}
)
Expand All @@ -171,7 +165,7 @@ private fun CalendarScreen(
contentTwo = {
CalendarListRoute(
listState = listState,
pages = calendarModel.pageCount,
pages = uiState.calendarModel.pageCount,
navigateToAnnouncement = navigateToAnnouncement,
modifier = Modifier
.fillMaxSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class CalendarViewModel @Inject constructor() : ViewModel() {
}
}

fun updatePage(page: Int) = viewModelScope.launch {
_uiState.update { currentState ->
currentState.copy(
currentPage = page
)
}
}

fun updateListVisibility(
visibility: Boolean
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.time.Year
import java.time.YearMonth

@Immutable
internal class CalendarModel internal constructor(
class CalendarModel internal constructor(
startYear: Int = START_YEAR,
endYear: Int = END_YEAR
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import java.time.LocalDate

data class CalendarUiState(
val selectedDate: LocalDate = LocalDate.now(),
val calendarModel: CalendarModel = CalendarModel(),
val currentPage: Int = 0,
val isListEnabled: Boolean = false,
val isWeekEnabled: Boolean = false
)

0 comments on commit c3db39d

Please sign in to comment.