Skip to content

Commit

Permalink
Merge pull request #4 from Roel4990/Feature/#2
Browse files Browse the repository at this point in the history
[Feat/#2] Migrate from java.time to kotlinx.datetime for broader compatibility
  • Loading branch information
DongChyeon authored Dec 29, 2024
2 parents 800c8bc + 5f14bf4 commit b91e53d
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 55 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ android {
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand All @@ -52,7 +51,6 @@ android {

dependencies {
implementation(project(":calendar"))
coreLibraryDesugaring(libs.desugar.jdk.libs)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand All @@ -62,6 +60,7 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.kotlinx.datetime)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/dongchyeon/calendar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.dongchyeon.calendar.ui.Calendar
import com.dongchyeon.calendar.ui.theme.DongChyeonCalendarTheme
import java.time.LocalDate
import com.dongchyeon.calendar.util.now
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate
import kotlinx.datetime.plus

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -34,12 +37,12 @@ class MainActivity : ComponentActivity() {

val events = listOf(
CalendarEvent(
date = LocalDate.now().plusDays(1),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = CircleShape
),
CalendarEvent(
date = LocalDate.now().plusDays(2),
date = LocalDate.now().plus(DatePeriod(days = 2)),
imgUrl = "https://picsum.photos/200/300",
imgShape = RoundedCornerShape(8.dp)
),
Expand Down
3 changes: 1 addition & 2 deletions calendar/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ android {
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand All @@ -43,7 +42,6 @@ android {
}

dependencies {
coreLibraryDesugaring(libs.desugar.jdk.libs)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand All @@ -56,6 +54,7 @@ dependencies {
implementation(libs.material)
implementation(libs.coil.compose)
implementation(libs.coil.network)
implementation(libs.kotlinx.datetime)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.dongchyeon.calendar

import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.Shape
import java.time.LocalDate
import kotlinx.datetime.LocalDate

@Immutable
data class CalendarEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dongchyeon.calendar.model

import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate
import kotlinx.datetime.minus
import kotlinx.datetime.plus

data class DateYearMonth(
val year: Int,
val month: Int
) {
override fun toString(): String = "$year-${month.toString().padStart(2, '0')}"

internal fun atDay(day: Int): LocalDate {
return LocalDate(year, month, day)
}

internal fun atEndOfMonth(): LocalDate {
val firstDay = LocalDate(year, month, 1)
val firstDayNextMonth = firstDay.plus(DatePeriod(months = 1))
return firstDayNextMonth.minus(DatePeriod(days = 1))
}

internal fun lengthOfMonth(): Int {
val firstDayThisMonth = LocalDate(year, month, 1)
val firstDayNextMonth = firstDayThisMonth.plus(DatePeriod(months = 1))
val lastDayThisMonth = firstDayNextMonth.minus(DatePeriod(days = 1))
return lastDayThisMonth.dayOfMonth
}

companion object {
internal fun from(localDate: LocalDate): DateYearMonth {
return DateYearMonth(
year = localDate.year,
month = localDate.monthNumber
)
}
}
}
63 changes: 32 additions & 31 deletions calendar/src/main/java/com/dongchyeon/calendar/ui/Calendar.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dongchyeon.calendar.ui

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -35,21 +38,27 @@ import com.dongchyeon.calendar.CalendarIndicatorConfig
import com.dongchyeon.calendar.CalendarLanguage
import com.dongchyeon.calendar.CalendarWeekHeaderConfig
import com.dongchyeon.calendar.R
import com.dongchyeon.calendar.model.DateYearMonth
import com.dongchyeon.calendar.theme.CalendarTheme
import com.dongchyeon.calendar.ui.component.DayBackgroundImage
import com.dongchyeon.calendar.util.formatYearMonthInEnglish
import com.dongchyeon.calendar.util.formatYearMonthInKorean
import com.dongchyeon.calendar.util.getNumberWeeks
import com.dongchyeon.calendar.util.minusMonths
import com.dongchyeon.calendar.util.noRippleClickable
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.YearMonth
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
import java.time.temporal.WeekFields
import com.dongchyeon.calendar.util.now
import com.dongchyeon.calendar.util.plusMonths
import com.dongchyeon.calendar.util.previousOrSame
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.LocalDate
import kotlinx.datetime.plus

@Composable
fun Calendar(
modifier: Modifier = Modifier,
events: List<CalendarEvent> = emptyList(),
currentYearMonth: YearMonth = YearMonth.from(LocalDate.now()),
currentYearMonth: DateYearMonth = DateYearMonth.from(LocalDate.now()),
selectedDate: LocalDate,
headerConfig: CalendarHeaderConfig = CalendarHeaderConfig.default(),
weekHeaderConfig: CalendarWeekHeaderConfig = CalendarWeekHeaderConfig.default(),
Expand Down Expand Up @@ -109,19 +118,17 @@ fun MonthSelector(
modifier: Modifier = Modifier,
config: CalendarHeaderConfig,
calendarLanguage: CalendarLanguage,
yearMonth: YearMonth,
yearMonth: DateYearMonth,
isTablet: Boolean,
onPrevClick: () -> Unit,
onNextClick: () -> Unit
) {
val formattedMonth = when (calendarLanguage) {
CalendarLanguage.EN -> {
val locale = java.util.Locale.ENGLISH
val formatter = DateTimeFormatter.ofPattern("MMMM yyyy", locale)
yearMonth.atDay(1).format(formatter) // e.g., "February 2024"
formatYearMonthInEnglish(yearMonth)
}
CalendarLanguage.KO -> {
"${yearMonth.year}${yearMonth.monthValue}" // e.g., "2024년 12월"
formatYearMonthInKorean(yearMonth)
}
}

Expand Down Expand Up @@ -203,29 +210,30 @@ fun WeekHeader(
}
}

@SuppressLint("NewApi")
@Composable
fun Week(
modifier: Modifier = Modifier,
itemWidth: Dp,
events: List<CalendarEvent>,
weekNumber: Long,
currentMonth: YearMonth,
currentMonth: DateYearMonth,
selectedDate: LocalDate,
dayConfig: CalendarDayConfig,
indicatorConfig: CalendarIndicatorConfig,
isTablet: Boolean,
onDayClicked: (LocalDate) -> Unit
) {
val beginningWeek = currentMonth.atDay(1).plusWeeks(weekNumber)
var currentDay = beginningWeek.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY))
val beginningWeek = currentMonth.atDay(1).plus(DatePeriod(days = (weekNumber * 7).toInt()))
var currentDay = beginningWeek.previousOrSame(DayOfWeek.SUNDAY)

Row(
modifier = modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
for (i in 0..6) {
if (currentDay.month == currentMonth.month) {
if (currentDay.monthNumber == currentMonth.month) {
val matchedEvent = events.find {
it.date.year == currentDay.year &&
it.date.month == currentDay.month &&
Expand All @@ -245,7 +253,7 @@ fun Week(
} else {
Box(modifier = Modifier.size(itemWidth))
}
currentDay = currentDay.plusDays(1)
currentDay = currentDay.plus(DatePeriod(days = 1))
}
}
}
Expand Down Expand Up @@ -311,13 +319,6 @@ private fun Day(
}
}

fun YearMonth.getNumberWeeks(weekFields: WeekFields = WeekFields.SUNDAY_START): Int {
val firstWeekNumber = this.atDay(1)[weekFields.weekOfMonth()]
val lastWeekNumber = this.atEndOfMonth()[weekFields.weekOfMonth()]

return lastWeekNumber - firstWeekNumber + 1
}

@Preview
@Composable
fun PreviewCalendar() {
Expand All @@ -326,37 +327,37 @@ fun PreviewCalendar() {

val events = listOf(
CalendarEvent(
date = LocalDate.now().plusDays(1),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = CircleShape
),
CalendarEvent(
date = LocalDate.now().plusDays(2),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = RoundedCornerShape(8.dp)
),
CalendarEvent(
date = LocalDate.now().plusDays(3),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = CircleShape
),
CalendarEvent(
date = LocalDate.now().plusDays(4),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = RoundedCornerShape(8.dp)
),
CalendarEvent(
date = LocalDate.now().plusDays(5),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = CircleShape
),
CalendarEvent(
date = LocalDate.now().plusDays(6),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = RoundedCornerShape(8.dp)
),
CalendarEvent(
date = LocalDate.now().plusDays(7),
date = LocalDate.now().plus(DatePeriod(days = 1)),
imgUrl = "https://picsum.photos/200/300",
imgShape = CircleShape
),
Expand Down
Loading

0 comments on commit b91e53d

Please sign in to comment.