Skip to content

Commit

Permalink
[REFACTOR/#20] SelectedDateState 기반 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
boiledEgg-s committed Jul 9, 2024
1 parent 6e89f2a commit 05f7daf
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,7 +27,7 @@ fun CalendarMonth(
modifier: Modifier = Modifier,
monthData: MonthData,
onDateSelected: (LocalDate) -> Unit,
selectedDate: LocalDate?,
selectedDate: SelectedDateState,
scrapLists: List<List<Scrap>> = listOf()
) {
Column(
Expand All @@ -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
)
Expand Down Expand Up @@ -79,7 +80,7 @@ fun CalendarMonthPreview() {
TerningPointTheme {
CalendarMonth(
monthData = MonthData(YearMonth.now()),
selectedDate = LocalDate.now(),
selectedDate = SelectedDateState(LocalDate.now(), true),
onDateSelected = {},
scrapLists = listOf()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,7 +19,7 @@ fun CalendarMonths(
listState: LazyListState,
onDateSelected: (LocalDate) -> Unit,
pages: Int,
selectedDate: LocalDate?,
selectedDate: SelectedDateState,
scrapLists: List<List<Scrap>>,
modifier: Modifier = Modifier,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -114,7 +109,7 @@ fun CalendarScreen(
)

AnimatedContent(
targetState = isWeekEnabled,
targetState = selectedDate.isEnabled,
transitionSpec = {
if (!targetState) {
slideInVertically { fullHeight -> -fullHeight } togetherWith
Expand Down Expand Up @@ -143,7 +138,7 @@ fun CalendarScreen(
CalendarWeekWithScrap(
modifier = Modifier
.fillMaxSize(),
selectedDate = selectedDate ?: LocalDate.now(),
selectedDate = selectedDate,
scrapLists = viewModel.mockScrapList,
onDateSelected = {
viewModel.updateSelectedDate(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ 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

@HiltViewModel
class CalendarViewModel @Inject constructor(
) : ViewModel() {
private val _selectedDate = MutableStateFlow<LocalDate?>(null)
private val _selectedDate = MutableStateFlow<SelectedDateState>(
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
)
}
}
}

Expand Down Expand Up @@ -63,18 +79,21 @@ class CalendarViewModel @Inject constructor(
)
)
}

3 -> {
list.add(
i,
listOf()
)
}

4 -> {
list.add(
i,
listOf()
)
}

5 -> {
list.add(
i,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Scrap>> = listOf(),
onDateSelected: (LocalDate) -> Unit
) {
Expand All @@ -40,7 +41,7 @@ fun CalendarWeekWithScrap(
modifier = Modifier
.fillMaxWidth()
.background(White),
selectedDate = selectedDate?: LocalDate.now(),
selectedDate = selectedDate,
onDateSelected = onDateSelected
)
}
Expand Down

0 comments on commit 05f7daf

Please sign in to comment.