Skip to content

Commit

Permalink
Kalendar configuration (#22)
Browse files Browse the repository at this point in the history
* [Github-Actions] Run Ktlint on PR

* [Kalendar-Configuration] Added Text Changes for weekdays

* [Kalendar-Configuration] Added Text Changes for weekdays

* [Kalendar-Configuration] Added Locale Support with default Locale.ENGLISH

* [Kalendar-Configuration] Formatted Code!

* [Kalendar-Configuration] Fixed Ktlint check
  • Loading branch information
hi-manshu committed Jan 30, 2022
1 parent 93fd8ac commit f69298d
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 37 deletions.
6 changes: 2 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ android {
}

dependencies {
// implementation(project(":kalendar
implementation(project(":kalendar"))
// implementation("com.himanshoe:kalendar:endlos:1.0.0-alpha1")
implementation("com.himanshoe:kalendar-endlos:1.0.0-alpha1")
// implementation("com.himanshoe:kalendar:kalendar-endlos:1.0.0-alpha1")
// implementation("com.himanshoe.kalendar:kalendar-endlos:1.0.0-alpha1")
// implementation("com.himanshoe:kalendar-endlos:1.0.0-alpha1")
// implementation(project(mapOf("path" to ":kalendar-endlos")))
// jetpack compose
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") // <- this dependency is required
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/himanshoe/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package com.himanshoe.sample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.himanshoe.kalendar.endlos.ui.Kalendar
import com.himanshoe.kalendar.common.KalendarKonfig
import com.himanshoe.kalendar.ui.Kalendar
import com.himanshoe.kalendar.ui.KalendarType

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Kalendar(
kalendarType = KalendarType.Oceanic(),
kalendarKonfig = KalendarKonfig(weekCharacters = 2),
onCurrentDayClick = { date, event ->
},
errorMessage = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.himanshoe.kalendar.endlos.common

import androidx.annotation.IntRange
import java.util.Locale

/*
* MIT License
*
Expand All @@ -25,7 +29,13 @@ package com.himanshoe.kalendar.endlos.common

/**
* [KalendarKonfig] represents the config needed for rendering calendar
* @param[yearRange] gives the min/max year range
* @param[weekCharacters] helps you set the number of character in Week name, default is 3
* @param[locale] helps you set the locale where default is [Locale.ENGLISH]
*/
data class KalendarKonfig(
val maxYear: Int = 0,
@IntRange(from = 1, to = 4)
val weekCharacters: Int = 3,
val locale: Locale = Locale.ENGLISH
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.dp
import com.himanshoe.kalendar.common.KalendarSelector
import com.himanshoe.kalendar.endlos.common.KalendarKonfig
import com.himanshoe.kalendar.endlos.common.data.KalendarEvent
import com.himanshoe.kalendar.endlos.util.getMonthNameFormatter
import java.time.LocalDate
Expand All @@ -32,6 +33,7 @@ internal fun KalendarMonth(
onCurrentDayClick: (LocalDate, KalendarEvent?) -> Unit,
kalendarSelector: KalendarSelector,
kalendarEvents: List<KalendarEvent>,
kalendarKonfig: KalendarKonfig,
) {
Column(
modifier = Modifier
Expand All @@ -50,9 +52,9 @@ internal fun KalendarMonth(

KalendarHeader(
kalendarSelector = kalendarSelector,
text = monthState.value.format(getMonthNameFormatter()),
text = monthState.value.format(getMonthNameFormatter(kalendarKonfig.locale)),
)
KalendarWeekDayNames()
KalendarWeekDayNames(kalendarKonfig = kalendarKonfig)

val days: List<LocalDate> = getDays(monthState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import com.himanshoe.kalendar.endlos.common.KalendarKonfig
import java.text.DateFormatSymbols

private val weekdays = listOf("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
private const val DAYS_IN_WEEK = 7
private const val ZERO = 0

@Composable
internal fun KalendarWeekDayNames() {
internal fun KalendarWeekDayNames(kalendarKonfig: KalendarKonfig) {
val weekdays = DateFormatSymbols(kalendarKonfig.locale).weekdays.takeLast(DAYS_IN_WEEK)

BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
val width = (maxWidth / 7)
val width = (maxWidth / DAYS_IN_WEEK)
Row(modifier = Modifier.fillMaxWidth()) {
weekdays.forEach { weekDay: String ->
KalendarWeekDay(
modifier = Modifier
.requiredWidth(width)
.wrapContentHeight(),
text = weekDay
text = weekDay.subSequence(ZERO, kalendarKonfig.weekCharacters).toString()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ internal fun KalendarView(
yearMonth.plusMonths(month.toLong()),
onCurrentDayClick,
kalendarSelector,
kalendarEvents
kalendarEvents,
kalendarKonfig
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.himanshoe.kalendar.endlos.util
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import java.time.format.DateTimeFormatter
import java.util.Locale

/*
* MIT License
Expand All @@ -29,10 +30,11 @@ import java.time.format.DateTimeFormatter
*/

@Composable
fun getMonthNameFormatter(): DateTimeFormatter {
fun getMonthNameFormatter(locale: Locale): DateTimeFormatter {
return remember {
DateTimeFormatter.ofPattern(
"MMMM y"
"MMMM y",
locale
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.himanshoe.kalendar.common

import androidx.annotation.IntRange
import java.util.Locale

/*
* MIT License
*
Expand Down Expand Up @@ -26,9 +30,14 @@ package com.himanshoe.kalendar.common
/**
* [KalendarKonfig] represents the config needed for rendering calendar
* @param[yearRange] gives the min/max year range
* @param[weekCharacters] helps you set the number of character in Week name, default is 3
* @param[locale] helps you set the locale where default is [Locale.ENGLISH]
*/
data class KalendarKonfig(
val yearRange: YearRange = YearRange(),
@IntRange(from = 1, to = 4)
val weekCharacters: Int = 3,
val locale: Locale = Locale.ENGLISH,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import com.himanshoe.kalendar.common.KalendarKonfig
import java.text.DateFormatSymbols

private val weekdays = listOf("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
private const val DAYS_IN_WEEK = 7
private const val ZERO = 0

@Composable
internal fun KalendarWeekDayNames() {
internal fun KalendarWeekDayNames(kalendarKonfig: KalendarKonfig) {
val weekdays = DateFormatSymbols(kalendarKonfig.locale).weekdays.takeLast(DAYS_IN_WEEK)

BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
val width = (maxWidth / 7)
val width = (maxWidth / DAYS_IN_WEEK)
Row(modifier = Modifier.fillMaxWidth()) {
weekdays.forEach { weekDay: String ->
KalendarWeekDay(
modifier = Modifier
.requiredWidth(width)
.wrapContentHeight(),
text = weekDay
text = weekDay.subSequence(ZERO, kalendarKonfig.weekCharacters).toString()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal fun KalendarFirey(
) {
KalendarView(
kalendarSelector = kalendarStyle.kalendarSelector,
yearRange = kalendarKonfig.yearRange,
kalendarKonfig = kalendarKonfig,
errorMessageLogged = errorMessageLogged,
selectedDay = selectedDay,
kalendarEvents = kalendarEvents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.dp
import com.himanshoe.kalendar.common.KalendarKonfig
import com.himanshoe.kalendar.common.KalendarSelector
import com.himanshoe.kalendar.common.YearRange
import com.himanshoe.kalendar.common.data.KalendarEvent
import com.himanshoe.kalendar.ui.common.KalendarWeekDayNames
import com.himanshoe.kalendar.util.getMonthNameFormatter
Expand All @@ -31,7 +31,7 @@ private const val DAYS_IN_WEEK = 7
internal fun KalendarMonth(
selectedDay: LocalDate,
yearMonth: YearMonth = YearMonth.now(),
yearRange: YearRange,
kalendarKonfig: KalendarKonfig,
onCurrentDayClick: (LocalDate, KalendarEvent?) -> Unit,
errorMessageLogged: (String) -> Unit,
kalendarSelector: KalendarSelector,
Expand All @@ -54,11 +54,11 @@ internal fun KalendarMonth(

KalendarHeader(
kalendarSelector = kalendarSelector,
text = monthState.value.format(getMonthNameFormatter()),
text = monthState.value.format(getMonthNameFormatter(kalendarKonfig.locale)),
onPreviousMonthClick = {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
val year = monthState.value.year
val isLimitAttached = year.validateMinDate(yearRange.min)
val isLimitAttached = year.validateMinDate(kalendarKonfig.yearRange.min)
if (isLimitAttached) {
monthState.value = monthState.value.minusMonths(1)
} else {
Expand All @@ -68,15 +68,15 @@ internal fun KalendarMonth(
onNextMonthClick = {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
val year = monthState.value.year
val isLimitAttached = year.validateMaxDate(yearRange.max)
val isLimitAttached = year.validateMaxDate(kalendarKonfig.yearRange.max)
if (isLimitAttached) {
monthState.value = monthState.value.plusMonths(1)
} else {
errorMessageLogged("Minimum year limit reached")
}
},
)
KalendarWeekDayNames()
KalendarWeekDayNames(kalendarKonfig = kalendarKonfig)

val days: List<LocalDate> = getDays(monthState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.himanshoe.kalendar.common.KalendarKonfig
import com.himanshoe.kalendar.common.KalendarSelector
import com.himanshoe.kalendar.common.YearRange
import com.himanshoe.kalendar.common.data.KalendarEvent
import com.himanshoe.kalendar.common.theme.Grid
import java.time.LocalDate
Expand All @@ -39,12 +39,11 @@ import java.time.YearMonth
internal fun KalendarView(
yearMonth: YearMonth = YearMonth.now(),
selectedDay: LocalDate,
yearRange: YearRange,
kalendarKonfig: KalendarKonfig,
kalendarSelector: KalendarSelector,
kalendarEvents: List<KalendarEvent>,
onCurrentDayClick: (LocalDate, KalendarEvent?) -> Unit,
errorMessageLogged: (String) -> Unit,

) {
Column(
modifier = Modifier
Expand All @@ -54,7 +53,7 @@ internal fun KalendarView(
KalendarMonth(
selectedDay,
yearMonth,
yearRange,
kalendarKonfig,
onCurrentDayClick,
errorMessageLogged,
kalendarSelector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal fun KalendarOceanic(
) {
KalendarOceanWeek(
kalendarSelector = kalendarStyle.kalendarSelector,
yearRange = kalendarKonfig.yearRange,
kalendarKonfig = kalendarKonfig,
startDate = startDate,
selectedDay = selectedDay,
kalendarEvents = kalendarEvents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import com.himanshoe.kalendar.common.KalendarKonfig
import com.himanshoe.kalendar.common.KalendarSelector
import com.himanshoe.kalendar.common.YearRange
import com.himanshoe.kalendar.common.data.KalendarEvent
import com.himanshoe.kalendar.common.theme.KalendarShape
import com.himanshoe.kalendar.common.ui.KalendarDot
import java.time.LocalDate
import java.time.format.TextStyle

@Composable
internal fun KalendarOceanWeek(
startDate: LocalDate = LocalDate.now(),
selectedDay: LocalDate = startDate,
yearRange: YearRange,
kalendarKonfig: KalendarKonfig,
onCurrentDayClick: (LocalDate, KalendarEvent?) -> Unit,
errorMessageLogged: (String) -> Unit,
kalendarSelector: KalendarSelector,
Expand All @@ -77,9 +78,20 @@ internal fun KalendarOceanWeek(
.fillMaxWidth()
) {
val size = (maxWidth / 7)
val monthName = "${displayWeek.value.last().month.name} ${displayWeek.value.last().year}"
val monthName = "${
displayWeek.value.last().month.getDisplayName(
TextStyle.FULL,
kalendarKonfig.locale
)
} ${displayWeek.value.last().year}"
Column(Modifier.fillMaxWidth()) {
KalendarOceanHeader(monthName, displayWeek, haptic, yearRange, errorMessageLogged)
KalendarOceanHeader(
monthName,
displayWeek,
haptic,
kalendarKonfig.yearRange,
errorMessageLogged
)

Row(
modifier = Modifier
Expand All @@ -95,7 +107,9 @@ internal fun KalendarOceanWeek(
) {
val event: KalendarEvent? = kalendarEvents.find { it.date == date }
Text(
text = date.dayOfWeek.toString().subSequence(0, 2).toString(),
text = date.dayOfWeek
.getDisplayName(TextStyle.FULL, kalendarKonfig.locale).toString()
.subSequence(0, kalendarKonfig.weekCharacters).toString(),
style = MaterialTheme.typography.body1,
modifier = Modifier
.width(size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.himanshoe.kalendar.util
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import java.time.format.DateTimeFormatter
import java.util.Locale

/*
* MIT License
Expand All @@ -29,10 +30,11 @@ import java.time.format.DateTimeFormatter
*/

@Composable
fun getMonthNameFormatter(): DateTimeFormatter {
fun getMonthNameFormatter(locale: Locale): DateTimeFormatter {
return remember {
DateTimeFormatter.ofPattern(
"MMMM y"
"MMMM y",
locale
)
}
}

0 comments on commit f69298d

Please sign in to comment.