Skip to content

Commit

Permalink
Merge pull request #57 from hyeeyoung/dev-fix-calendar-bug
Browse files Browse the repository at this point in the history
μΊ˜λ¦°λ”μ—μ„œ 상세뷰 이동 ν›„ λ³΅κ·€ν–ˆμ„ λ•Œ μƒνƒœκ°’μ΄ μ΄ˆκΈ°ν™”λ˜λŠ” 버그 μˆ˜μ •
  • Loading branch information
youngjinc authored Oct 4, 2023
2 parents 6a3ef7d + b50cce5 commit e8ac425
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
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

0 comments on commit e8ac425

Please sign in to comment.