Skip to content

Commit

Permalink
[REFACTOR/#305] DayModel 마이그레이션 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
boiledEgg-s committed Dec 11, 2024
1 parent 3f8d781 commit 15744dd
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 357 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.terning.feature.calendar.calendar

import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
Expand All @@ -25,17 +26,20 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.terning.core.analytics.EventType
import com.terning.core.analytics.LocalTracker
import com.terning.core.designsystem.component.topappbar.CalendarTopAppBar
import com.terning.core.designsystem.extension.getWeekIndexContainingSelectedDate
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.state.CalendarUiState
import com.terning.feature.calendar.calendar.model.DayModel
import com.terning.feature.calendar.calendar.state.LocalPagerState
import com.terning.feature.calendar.calendar.model.TerningCalendarModel.Companion.LocalCalendarModel
import com.terning.feature.calendar.calendar.model.CalendarUiState
import com.terning.feature.calendar.calendar.model.LocalPagerState
import com.terning.feature.calendar.list.CalendarListRoute
import com.terning.feature.calendar.month.CalendarMonthRoute
import com.terning.feature.calendar.week.CalendarWeekRoute
import kotlinx.coroutines.launch
import timber.log.Timber
import java.time.LocalDate

@Composable
Expand Down Expand Up @@ -72,8 +76,8 @@ fun CalendarRoute(
private fun CalendarScreen(
uiState: CalendarUiState,
navigateToAnnouncement: (Long) -> Unit,
onClickNewDate: (LocalDate) -> Unit,
updateSelectedDate: (LocalDate) -> Unit,
onClickNewDate: (DayModel) -> Unit,
updateSelectedDate: (DayModel) -> Unit,
disableListVisibility: () -> Unit,
disableWeekVisibility: () -> Unit,
onClickListButton: () -> Unit,
Expand All @@ -86,19 +90,26 @@ private fun CalendarScreen(
pageCount = { uiState.calendarModel.pageCount }
)

BackHandler(enabled = uiState.isWeekEnabled) {
disableWeekVisibility()
}

LaunchedEffect(key1 = pagerState, key2 = uiState.selectedDate) {
snapshotFlow { pagerState.currentPage }
.collect { current ->
//val date = getLocalDateByPage(current)
val date = uiState.calendarModel.getLocalDateByPage(current)
val month = uiState.calendarModel.getMonthModelByPage(current)


val newDate = LocalDate.of(
date.year,
date.month,
uiState.selectedDate.dayOfMonth.coerceAtMost(date.month.minLength())
uiState.selectedDate.date.dayOfMonth.coerceAtMost(date.month.minLength())
)
updateSelectedDate(newDate)

val currentWeek = newDate.getWeekIndexContainingSelectedDate(month.inDays)
Timber.tag("WeekIndex").d("In CalendarRoute: ${currentWeek.toString()}")
updateSelectedDate(DayModel(newDate, currentWeek))
}
}

Expand Down Expand Up @@ -165,7 +176,9 @@ private fun CalendarScreen(
.fillMaxSize(),
navigateUp = disableWeekVisibility,
navigateToAnnouncement = navigateToAnnouncement,
updateSelectedDate = onClickNewDate
updateSelectedDate = {
onClickNewDate(it)
}
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package com.terning.feature.calendar.calendar

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.terning.feature.calendar.calendar.model.CalendarUiState
import com.terning.feature.calendar.calendar.model.DayModel
import com.terning.feature.calendar.calendar.state.CalendarUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.time.LocalDate
import javax.inject.Inject

@HiltViewModel
class CalendarViewModel @Inject constructor() : ViewModel() {
private var _uiState: MutableStateFlow<CalendarUiState> = MutableStateFlow(CalendarUiState())
val uiState get() = _uiState.asStateFlow()

fun onSelectNewDate(selectedDate: LocalDate) = viewModelScope.launch {
if (_uiState.value.selectedDate == selectedDate) {
_uiState.update { currentState ->
currentState.copy(
isWeekEnabled = !_uiState.value.isWeekEnabled
)
}
fun onSelectNewDate(selectedDate: DayModel) {
if (_uiState.value.selectedDate.date == selectedDate.date) {
updateWeekVisibility(!_uiState.value.isWeekEnabled)
} else {
_uiState.update { currentState ->
currentState.copy(
Expand All @@ -33,27 +27,23 @@ class CalendarViewModel @Inject constructor() : ViewModel() {
}
}

fun updateSelectedDate(date: LocalDate) = viewModelScope.launch {
_uiState.update { currentState ->
fun updateSelectedDate(value: DayModel) = _uiState.update { currentState ->
currentState.copy(
selectedDate = date
selectedDate = value
)
}
}

fun updateListVisibility(visibility: Boolean) = viewModelScope.launch {
_uiState.update { currentState ->

fun updateListVisibility(value: Boolean) = _uiState.update { currentState ->
currentState.copy(
isListEnabled = visibility
isListEnabled = value
)
}
}

fun updateWeekVisibility(visibility: Boolean) = viewModelScope.launch {
_uiState.update { currentState ->

fun updateWeekVisibility(value: Boolean) = _uiState.update { currentState ->
currentState.copy(
isWeekEnabled = visibility
isWeekEnabled = value
)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.terning.core.designsystem.theme.TerningTheme
import com.terning.feature.calendar.R

@Composable
fun CalendarWeekDaysHeader(
fun WeekDaysHeader(
modifier: Modifier = Modifier,
) {
Row(
Expand Down Expand Up @@ -56,6 +56,6 @@ fun CalendarWeekDaysHeader(
@Composable
fun WeekDaysHeaderPreview() {
TerningPointTheme {
CalendarWeekDaysHeader()
WeekDaysHeader()
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.terning.feature.calendar.calendar.model
package com.terning.feature.calendar.calendar.state

import androidx.compose.foundation.pager.PagerState
import androidx.compose.runtime.compositionLocalOf
import java.time.LocalDate
import com.terning.feature.calendar.calendar.model.DayModel
import com.terning.feature.calendar.calendar.model.TerningCalendarModel

data class CalendarUiState(
val selectedDate: LocalDate = LocalDate.now(),
val selectedDate: DayModel = DayModel(),
val calendarModel: TerningCalendarModel = TerningCalendarModel(),
val isListEnabled: Boolean = false,
val isWeekEnabled: Boolean = false
val isWeekEnabled: Boolean = false,
)

val LocalPagerState = compositionLocalOf<PagerState> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import com.terning.core.designsystem.theme.TerningTheme
import com.terning.domain.calendar.entity.CalendarScrapDetail
import com.terning.feature.calendar.R
import com.terning.feature.calendar.calendar.model.TerningCalendarModel.Companion.LocalCalendarModel
import com.terning.feature.calendar.calendar.model.LocalPagerState
import com.terning.feature.calendar.calendar.state.LocalPagerState
import com.terning.feature.calendar.list.component.CalendarScrapList
import com.terning.feature.calendar.list.model.CalendarListUiState
import com.terning.feature.dialog.cancel.ScrapCancelDialog
Expand Down
Loading

0 comments on commit 15744dd

Please sign in to comment.