Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

캘린더에서 상세뷰 이동 후 복귀했을 때 상태값이 초기화되는 버그 수정 #57

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hyeeyoung.wishboard.presentation.calendar

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.time.LocalDate

class CalendarViewModel : ViewModel() {
private val _selectedDate = MutableStateFlow(LocalDate.now())
val selectedDate get() = _selectedDate.asStateFlow()

private val latestCalendarPage = MutableStateFlow(INITIAL_PAGE)

fun changeCalendarPage(currentPate: Int) {
val diff = currentPate - latestCalendarPage.value

if (diff < 0) {
_selectedDate.value = _selectedDate.value.minusMonths(1)
} else if (diff > 0) {
_selectedDate.value = _selectedDate.value.plusMonths(1)
}

latestCalendarPage.value = currentPate
}

fun updateSelectedDate(date: LocalDate) {
_selectedDate.value = date
}

companion object {
const val PAGE_COUNT = Int.MAX_VALUE
const val INITIAL_PAGE = PAGE_COUNT / 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun CalendarTable(
}
}

Column() {
Column {
WishBoardDivider()
HorizontalPager(pageCount = pageCount, state = pagerState) {
DateTable(selectedDate, onSelect, notiDateList)
Expand All @@ -67,9 +67,9 @@ fun DateTable(
val cellSize = screenWidth / COL_SIZE
val dateCellModifier = Modifier.size(cellSize)

Column() {
Column {
repeat(rowSize) { r ->
Row() {
Row {
repeat(COL_SIZE) { c ->
if (day > lastDay) return
val dateOrNull =
Expand Down Expand Up @@ -156,7 +156,7 @@ fun CalendarTablePreview() {
onSelect = {},
notiDateList = listOf(LocalDate.of(2023, 7, 3), LocalDate.of(2023, 7, 20)),
pagerState = rememberPagerState(),
pageCount = 1,
pageCount = 24,
onChangePage = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.hyeeyoung.wishboard.config.navigation.screen.MainScreen
import com.hyeeyoung.wishboard.designsystem.style.WishBoardTheme
import com.hyeeyoung.wishboard.designsystem.style.WishboardTheme
import com.hyeeyoung.wishboard.presentation.calendar.CalendarViewModel
import com.hyeeyoung.wishboard.presentation.calendar.CalendarViewModel.Companion.INITIAL_PAGE
import com.hyeeyoung.wishboard.presentation.calendar.CalendarViewModel.Companion.PAGE_COUNT
import com.hyeeyoung.wishboard.presentation.calendar.component.CalendarHeader
import com.hyeeyoung.wishboard.presentation.calendar.component.CalendarSchedule
import com.hyeeyoung.wishboard.presentation.calendar.component.CalendarTable
import com.hyeeyoung.wishboard.presentation.model.NotiItem
import com.hyeeyoung.wishboard.presentation.util.type.NotiType
import java.time.LocalDate
import java.time.LocalDateTime

private const val PAGE_COUNT = Int.MAX_VALUE
private const val INITIAL_PAGE = PAGE_COUNT / 2

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CalendarScreen(navController: NavHostController) {
fun CalendarScreen(navController: NavHostController, viewModel: CalendarViewModel = viewModel()) {
// TODO 서버 연동 후 삭제
val notiList = listOf(
NotiItem(
Expand Down Expand Up @@ -112,37 +112,37 @@ fun CalendarScreen(navController: NavHostController) {
}

WishboardTheme {
var selectedDate by remember { mutableStateOf(LocalDate.now()) }
var prevPage by remember { mutableStateOf(INITIAL_PAGE) }
val selectedDate = viewModel.selectedDate.collectAsState()
val curMonthNoti =
notiList.filter { it.notiDate.year == selectedDate.year && it.notiDate.month == selectedDate.month }
val curDateNoti = curMonthNoti.filter { it.notiDate.dayOfMonth == selectedDate.dayOfMonth }
notiList.filter {
it.notiDate.year == selectedDate.value.year && it.notiDate.month == selectedDate.value.month
}
val curDateNoti = curMonthNoti.filter { it.notiDate.dayOfMonth == selectedDate.value.dayOfMonth }
val pagerState = rememberPagerState(initialPage = INITIAL_PAGE)

Scaffold(
topBar = { CalendarHeader(selectedDate = selectedDate, onClickBack = { navController.popBackStack() }) },
topBar = {
CalendarHeader(
selectedDate = selectedDate.value,
onClickBack = { navController.popBackStack() },
)
},
) { paddingValues ->
Column(
modifier = Modifier
.background(WishBoardTheme.colors.white)
.padding(top = paddingValues.calculateTopPadding()),
) {
CalendarTable(
selectedDate = selectedDate,
onSelect = { selectedDate = it },
selectedDate = selectedDate.value,
onSelect = { date -> viewModel.updateSelectedDate(date) },
notiDateList = curMonthNoti.map { it.notiDate.toLocalDate() },
pagerState = pagerState,
pageCount = PAGE_COUNT,
onChangePage = { page ->
val diff = page - prevPage
if (diff < 0) {
selectedDate = selectedDate.minusMonths(1)
} else if (diff > 0) selectedDate = selectedDate.plusMonths(1)
prevPage = page
},
onChangePage = { page -> viewModel.changeCalendarPage(page) },
)
CalendarSchedule(
selectedDate = selectedDate,
selectedDate = selectedDate.value,
notiItems = curDateNoti,
onClickSchedule = { id ->
navController.navigate("${MainScreen.WishItemDetail.route}/$id")
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ androidx-test-espresso = "3.5.1"
junit = "4.13.2"

compose-compiler = "1.4.4"
compose-bom = "2023.05.01"
compose-bom = "2023.01.00"
material = "1.1.1"
compose-navigation = "2.5.3"
compose-constraintlayout = "1.0.1"
Expand Down