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

[TNT-175] 캘린더 컴포넌트 구현 #57

Merged
merged 5 commits into from
Jan 31, 2025
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
1 change: 1 addition & 0 deletions core/designsystem/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ android {

dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.calendar.compose)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package co.kr.tnt.designsystem.component.calendar

import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import co.kr.tnt.core.designsystem.R
import co.kr.tnt.designsystem.theme.TnTTheme
import java.time.YearMonth
import java.time.format.DateTimeFormatter

@Composable
fun TnTCalendarSelector(
onClickPrevious: () -> Unit,
onClickNext: () -> Unit,
modifier: Modifier = Modifier,
yearMonth: YearMonth = YearMonth.now(),
) {
Comment on lines +19 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주간 캘린더에서도 < , > 버튼 누르면 월 단위로 이동해야하나요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 주 단위로 이동하면서 월이 바뀌면 바뀐 월로 보여지도록 구현할 수 있네요! 👍

val context = LocalContext.current
val dateFormatter = remember {
DateTimeFormatter.ofPattern(context.getString(R.string.year_month_format))
}

Row(
modifier = modifier,
verticalAlignment = Alignment.CenterVertically,
) {
IconButton(onClick = onClickPrevious) {
Icon(
painter = painterResource(id = R.drawable.ic_bold_arrow_left),
contentDescription = "arrow left",
tint = TnTTheme.colors.neutralColors.Neutral300,
)
}
Text(
dateFormatter.format(yearMonth),
style = TnTTheme.typography.h3,
color = TnTTheme.colors.neutralColors.Neutral900,
)
IconButton(onClick = onClickNext) {
Icon(
painter = painterResource(id = R.drawable.ic_bold_arrow_right),
contentDescription = "arrow right",
tint = TnTTheme.colors.neutralColors.Neutral300,
)
}
}
}

@Preview(showBackground = true)
@Composable
private fun TnTCalendarSelectorPreview() {
TnTTheme {
TnTCalendarSelector(
onClickPrevious = { },
onClickNext = { },
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package co.kr.tnt.designsystem.component.calendar

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import co.kr.tnt.designsystem.component.calendar.composable.CalendarCell
import co.kr.tnt.designsystem.component.calendar.composable.CalendarCellWithIndicator
import co.kr.tnt.designsystem.component.calendar.composable.WeekLabels
import co.kr.tnt.designsystem.component.calendar.model.DayIndicatorState
import co.kr.tnt.designsystem.component.calendar.model.DayState
import co.kr.tnt.designsystem.theme.TnTTheme
import com.kizitonwose.calendar.compose.CalendarState
import com.kizitonwose.calendar.compose.HorizontalCalendar
import com.kizitonwose.calendar.compose.rememberCalendarState
import com.kizitonwose.calendar.core.DayPosition
import com.kizitonwose.calendar.core.daysOfWeek
import com.kizitonwose.calendar.core.yearMonth
import java.time.LocalDate

@Composable
fun TnTMonthCalendar(
modifier: Modifier = Modifier,
state: CalendarState = rememberCalendarState(),
dayState: (day: LocalDate) -> DayState = { DayState() },
onClickDay: ((day: LocalDate) -> Unit)? = null,
) {
val daysOfWeek = remember { daysOfWeek(state.firstDayOfWeek) }

HorizontalCalendar(
modifier = modifier,
state = state,
monthHeader = {
WeekLabels(daysOfWeek = daysOfWeek)
},
dayContent = { day ->
if (day.position == DayPosition.MonthDate) {
CalendarCell(
date = day.date,
state = dayState(day.date),
onClick = { onClickDay?.invoke(day.date) },
)
}
},
)
}

@Composable
fun TnTIndicatorMonthCalendar(
modifier: Modifier = Modifier,
state: CalendarState = rememberCalendarState(),
dayState: (day: LocalDate) -> DayState = { DayState() },
indicatorState: (day: LocalDate) -> DayIndicatorState = { DayIndicatorState() },
onClickDay: ((day: LocalDate) -> Unit)? = null,
) {
val daysOfWeek = remember { daysOfWeek(state.firstDayOfWeek) }

HorizontalCalendar(
modifier = modifier,
state = state,
monthHeader = {
WeekLabels(daysOfWeek = daysOfWeek)
},
dayContent = { day ->
if (day.position == DayPosition.MonthDate) {
CalendarCellWithIndicator(
date = day.date,
dayState = dayState(day.date),
indicatorState = indicatorState(day.date),
onClick = { onClickDay?.invoke(day.date) },
)
}
},
)
}

@Preview(showBackground = true)
@Preview(showBackground = true, widthDp = 500)
@Composable
private fun TnTMonthCalendarPreview() {
val now = LocalDate.now()

TnTTheme {
TnTMonthCalendar(
state = rememberCalendarState(
startMonth = now.yearMonth.minusMonths(12),
endMonth = now.yearMonth.plusMonths(12),
firstVisibleMonth = now.yearMonth,
),
dayState = { date ->
DayState(isSelected = date == now)
},
)
}
}

@Preview(showBackground = true)
@Preview(showBackground = true, widthDp = 500)
@Composable
private fun TnTIndicatorMonthCalendarPreview() {
val now = LocalDate.now()

TnTTheme {
TnTIndicatorMonthCalendar(
state = rememberCalendarState(
startMonth = now.yearMonth.minusMonths(12),
endMonth = now.yearMonth.plusMonths(12),
firstVisibleMonth = now.yearMonth,
),
dayState = { date ->
DayState(isSelected = date == now)
},
indicatorState = indicator@{ date ->
if (date.dayOfMonth % 2 == 0) {
return@indicator DayIndicatorState(
count = 10,
showText = true,
)
}

DayIndicatorState(
showIcon = true,
)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package co.kr.tnt.designsystem.component.calendar

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import co.kr.tnt.designsystem.component.calendar.composable.CalendarCell
import co.kr.tnt.designsystem.component.calendar.composable.CalendarCellWithIndicator
import co.kr.tnt.designsystem.component.calendar.composable.WeekLabels
import co.kr.tnt.designsystem.component.calendar.model.DayIndicatorState
import co.kr.tnt.designsystem.component.calendar.model.DayState
import co.kr.tnt.designsystem.theme.TnTTheme
import com.kizitonwose.calendar.compose.WeekCalendar
import com.kizitonwose.calendar.compose.weekcalendar.WeekCalendarState
import com.kizitonwose.calendar.compose.weekcalendar.rememberWeekCalendarState
import com.kizitonwose.calendar.core.daysOfWeek
import java.time.LocalDate

@Composable
fun TnTWeekCalendar(
modifier: Modifier = Modifier,
state: WeekCalendarState = rememberWeekCalendarState(),
dayState: (day: LocalDate) -> DayState = { DayState() },
onClickDay: ((day: LocalDate) -> Unit)? = null,
) {
val daysOfWeek = remember { daysOfWeek(state.firstDayOfWeek) }

WeekCalendar(
modifier = modifier,
state = state,
weekHeader = {
WeekLabels(daysOfWeek = daysOfWeek)
},
dayContent = { day ->
CalendarCell(
date = day.date,
state = dayState(day.date),
onClick = { onClickDay?.invoke(day.date) },
)
},
)
}

@Composable
fun TnTIndicatorWeekCalendar(
modifier: Modifier = Modifier,
state: WeekCalendarState = rememberWeekCalendarState(),
dayState: (day: LocalDate) -> DayState = { DayState() },
indicatorState: (day: LocalDate) -> DayIndicatorState = { DayIndicatorState() },
onClickDay: ((day: LocalDate) -> Unit)? = null,
) {
val daysOfWeek = remember { daysOfWeek(state.firstDayOfWeek) }

WeekCalendar(
modifier = modifier,
state = state,
weekHeader = {
WeekLabels(daysOfWeek = daysOfWeek)
},
dayContent = { day ->
CalendarCellWithIndicator(
date = day.date,
dayState = dayState(day.date),
indicatorState = indicatorState(day.date),
onClick = { onClickDay?.invoke(day.date) },
)
},
)
}

@Preview(showBackground = true)
@Preview(showBackground = true, widthDp = 500)
@Composable
private fun TnTWeekCalendarPreview() {
val now = LocalDate.now()

TnTTheme {
TnTWeekCalendar(
state = rememberWeekCalendarState(
startDate = now.minusMonths(6),
endDate = now.plusMonths(6),
firstVisibleWeekDate = now,
),
dayState = { date ->
DayState(isSelected = date == now)
},
)
}
}

@Preview(showBackground = true)
@Preview(showBackground = true, widthDp = 500)
@Composable
private fun TnTIndicatorWeekCalendarPreview() {
val now = LocalDate.now()

TnTTheme {
TnTIndicatorWeekCalendar(
state = rememberWeekCalendarState(
startDate = now.minusMonths(6),
endDate = now.plusMonths(6),
Comment on lines +100 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MonthCalendar는 12로 써있던데 여기선 6인 이유가 있나용?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주간 캘린더는 월간 캘린더에 비해 보여지는 부분이 적기도 하고, Preview 라서 대충 넣은 것도 있습니다(..)

firstVisibleWeekDate = now,
),
dayState = { date ->
DayState(isSelected = date == now)
},
indicatorState = indicator@{ date ->
if (date.dayOfMonth % 2 == 0) {
return@indicator DayIndicatorState(
count = 10,
showText = true,
)
}

DayIndicatorState(
showIcon = true,
)
},
)
}
}
Loading