From 05f7daf15955626cb29cf8254ccf89d04ed057cf Mon Sep 17 00:00:00 2001 From: boiledegg Date: Wed, 10 Jul 2024 05:57:26 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR/#20]=20SelectedDateState=20=EA=B8=B0?= =?UTF-8?q?=EB=B0=98=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../terning/feature/calendar/CalendarMonth.kt | 7 +++-- .../feature/calendar/CalendarMonths.kt | 3 +- .../terning/feature/calendar/CalendarRoute.kt | 13 +++------ .../feature/calendar/CalendarViewModel.kt | 29 +++++++++++++++---- .../terning/feature/calendar/CalendarWeek.kt | 10 ++++--- .../feature/calendar/CalendarWeekWithScrap.kt | 5 ++-- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarMonth.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarMonth.kt index 48cab63ce..fc9636da3 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarMonth.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarMonth.kt @@ -18,6 +18,7 @@ import com.terning.core.designsystem.theme.TerningPointTheme import com.terning.core.extension.isToday import com.terning.feature.calendar.models.MonthData import com.terning.feature.calendar.models.Scrap +import com.terning.feature.calendar.models.SelectedDateState import java.time.LocalDate import java.time.YearMonth @@ -26,7 +27,7 @@ fun CalendarMonth( modifier: Modifier = Modifier, monthData: MonthData, onDateSelected: (LocalDate) -> Unit, - selectedDate: LocalDate?, + selectedDate: SelectedDateState, scrapLists: List> = listOf() ) { Column( @@ -48,7 +49,7 @@ fun CalendarMonth( ) { CalendarDay( dayData = day, - isSelected = selectedDate == day.date, + isSelected = selectedDate.selectedDate == day.date && selectedDate.isEnabled, isToday = day.date.isToday(), onDateSelected = onDateSelected ) @@ -79,7 +80,7 @@ fun CalendarMonthPreview() { TerningPointTheme { CalendarMonth( monthData = MonthData(YearMonth.now()), - selectedDate = LocalDate.now(), + selectedDate = SelectedDateState(LocalDate.now(), true), onDateSelected = {}, scrapLists = listOf() ) diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarMonths.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarMonths.kt index 25eb94ca0..5d4ecc101 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarMonths.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarMonths.kt @@ -10,6 +10,7 @@ import com.terning.feature.calendar.models.CalendarDefaults.flingBehavior import com.terning.feature.calendar.models.CalendarState.Companion.getDateByPage import com.terning.feature.calendar.models.MonthData import com.terning.feature.calendar.models.Scrap +import com.terning.feature.calendar.models.SelectedDateState import java.time.LocalDate import java.time.YearMonth @@ -18,7 +19,7 @@ fun CalendarMonths( listState: LazyListState, onDateSelected: (LocalDate) -> Unit, pages: Int, - selectedDate: LocalDate?, + selectedDate: SelectedDateState, scrapLists: List>, modifier: Modifier = Modifier, ) { diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarRoute.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarRoute.kt index 3acf797fe..03afb65e3 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarRoute.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarRoute.kt @@ -59,7 +59,6 @@ fun CalendarScreen( var currentPage by remember { mutableIntStateOf(listState.firstVisibleItemIndex) } var isListExpanded by remember { mutableStateOf(false) } - var isWeekEnabled by remember { mutableStateOf(false) } LaunchedEffect(key1 = listState) { snapshotFlow { listState.firstVisibleItemIndex } @@ -72,13 +71,9 @@ fun CalendarScreen( } } - LaunchedEffect(key1 = selectedDate) { - isWeekEnabled = selectedDate != null - } - BackHandler { - if (isWeekEnabled) { - viewModel.updateSelectedDate(selectedDate?: LocalDate.now()) + if (selectedDate.isEnabled) { + viewModel.updateSelectedDate(selectedDate.selectedDate) } } @@ -114,7 +109,7 @@ fun CalendarScreen( ) AnimatedContent( - targetState = isWeekEnabled, + targetState = selectedDate.isEnabled, transitionSpec = { if (!targetState) { slideInVertically { fullHeight -> -fullHeight } togetherWith @@ -143,7 +138,7 @@ fun CalendarScreen( CalendarWeekWithScrap( modifier = Modifier .fillMaxSize(), - selectedDate = selectedDate ?: LocalDate.now(), + selectedDate = selectedDate, scrapLists = viewModel.mockScrapList, onDateSelected = { viewModel.updateSelectedDate(it) diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarViewModel.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarViewModel.kt index 9eda16612..6a286807b 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarViewModel.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarViewModel.kt @@ -10,9 +10,11 @@ import com.terning.core.designsystem.theme.CalPurple import com.terning.core.designsystem.theme.CalRed import com.terning.core.designsystem.theme.CalYellow import com.terning.feature.calendar.models.Scrap +import com.terning.feature.calendar.models.SelectedDateState 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 @@ -20,15 +22,29 @@ import javax.inject.Inject @HiltViewModel class CalendarViewModel @Inject constructor( ) : ViewModel() { - private val _selectedDate = MutableStateFlow(null) + private val _selectedDate = MutableStateFlow( + SelectedDateState( + selectedDate = LocalDate.now(), + isEnabled = false + ) + ) val selectedDate get() = _selectedDate.asStateFlow() fun updateSelectedDate(date: LocalDate) = viewModelScope.launch { - if (_selectedDate.value != date) { - _selectedDate.value = date + if (_selectedDate.value.selectedDate != date) { + _selectedDate.update { currentState -> + currentState.copy( + selectedDate = date, + isEnabled = true + ) + } } else { - _selectedDate.value = null - + _selectedDate.update { currentState -> + currentState.copy( + selectedDate = date, + isEnabled = !_selectedDate.value.isEnabled + ) + } } } @@ -63,18 +79,21 @@ class CalendarViewModel @Inject constructor( ) ) } + 3 -> { list.add( i, listOf() ) } + 4 -> { list.add( i, listOf() ) } + 5 -> { list.add( i, diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarWeek.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarWeek.kt index 20c4d993d..7fb0e5d43 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarWeek.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarWeek.kt @@ -13,17 +13,19 @@ import androidx.compose.ui.unit.dp import com.terning.core.extension.getWeekIndexContainingSelectedDate import com.terning.core.extension.isToday import com.terning.feature.calendar.models.MonthData +import com.terning.feature.calendar.models.SelectedDateState import java.time.LocalDate import java.time.YearMonth @Composable fun CalendarWeek( modifier: Modifier = Modifier, - selectedDate: LocalDate, + selectedDate: SelectedDateState, onDateSelected: (LocalDate) -> Unit = {} ) { - val monthData = MonthData(YearMonth.of(selectedDate.year, selectedDate.monthValue)) - val currentWeek = selectedDate.getWeekIndexContainingSelectedDate() + val date = selectedDate.selectedDate + val monthData = MonthData(YearMonth.of(date.year, date.monthValue)) + val currentWeek = date.getWeekIndexContainingSelectedDate() val pagerState = rememberPagerState ( initialPage = currentWeek, @@ -42,7 +44,7 @@ fun CalendarWeek( items(items = monthData.calendarMonth.weekDays[page]) { day -> CalendarDay( dayData = day, - isSelected = selectedDate == day.date, + isSelected = selectedDate.selectedDate == day.date && selectedDate.isEnabled, isToday = day.date.isToday(), onDateSelected = onDateSelected ) diff --git a/feature/src/main/java/com/terning/feature/calendar/CalendarWeekWithScrap.kt b/feature/src/main/java/com/terning/feature/calendar/CalendarWeekWithScrap.kt index e8c28d5f2..00a1adde4 100644 --- a/feature/src/main/java/com/terning/feature/calendar/CalendarWeekWithScrap.kt +++ b/feature/src/main/java/com/terning/feature/calendar/CalendarWeekWithScrap.kt @@ -14,12 +14,13 @@ import androidx.compose.ui.unit.dp import com.terning.core.designsystem.theme.Grey200 import com.terning.core.designsystem.theme.White import com.terning.feature.calendar.models.Scrap +import com.terning.feature.calendar.models.SelectedDateState import java.time.LocalDate @Composable fun CalendarWeekWithScrap( modifier: Modifier = Modifier, - selectedDate: LocalDate?, + selectedDate: SelectedDateState, scrapLists: List> = listOf(), onDateSelected: (LocalDate) -> Unit ) { @@ -40,7 +41,7 @@ fun CalendarWeekWithScrap( modifier = Modifier .fillMaxWidth() .background(White), - selectedDate = selectedDate?: LocalDate.now(), + selectedDate = selectedDate, onDateSelected = onDateSelected ) }