Skip to content

Commit

Permalink
Update README.MD
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechOsak committed Mar 27, 2024
1 parent ac6f67f commit 466c722
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 112 deletions.
79 changes: 76 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,86 @@ Features:
| Web(Wasm) ||
| Desktop ||

# Usage
# Setup

[![Maven Central](https://img.shields.io/badge/dynamic/xml.svg?label=Maven%20Central&color=blue&url=https://repo1.maven.org/maven2/io/github/wojciechosak/calendar/maven-metadata.xml&query=(//metadata/versioning/versions/version)[not(contains(text(),%27-%27))][last()])](https://repo1.maven.org/maven2/io/github/wojciechosak/calendar/)

In Android project:

```groovy
dependencies {
implementation 'io.github.wojciechosak:calendar:<latest-version>'
}
```

In Kotlin Multiplatform project:

# Documentation:
```groovy
commonMain.dependencies {
implementation 'io.github.wojciechosak:calendar:<latest-version>'
}
```

```kotlin
CalendarView(
config = rememberCalendarState(
startDate = LocalDate.today(),
monthOffset = 0
),
day = { dayState ->
// define your day composable here!
}
)
```

Documentation can be found here:
# Composables

Simply use in Compose any view you want:

| View type | Preview |
|:--------------:|:--------------------------------------------------:|
| CalendarView | <img src="readme/calendar-view.png" height="250"/> |
| HorizontalView | <img src="readme/horizontal.gif" height="250"/> |
| VerticalView | <img src="readme/vertical.gif" height="250"/> |
| WeekView | <img src="readme/weekview.gif" height="250"/> |
| MonthPicker | <img src="readme/monthpicker.png" height="250"/> |
| YearPicker | <img src="readme/yearpicker.gif" height="250"/> |

Each view get as parameter day cell composable. Thanks to that your calendar can look whatever you like:
<img src="readme/sample1.png" height="250"/>
<img src="readme/sample2.png" height="250"/>

## Range selection:

```kotlin
CalendarView(
config = rememberCalendarState(
startDate = startDate,
monthOffset = monthOffset,
),
selectionMode = SelectionMode.Range,
rangeConfig = RangeConfig(rangeIllustrator = RoundedRangeIllustrator(Pallete.LightGreen)),
)
```

<img src="readme/range.png" height="250"/>

You can draw yourself implementations of RangeIllustrator, or use predefined `RoundedRangeIllustrator`
and `UnderlineIllustrator`.

## Selection callbacks:

```kotlin
CalendarView(
config = ...,
onDateSelected = { date ->
// date: List<LocalDate>
},
selectionMode = SelectionMode.Multiply(3) // SelectionMode.Single or SelectionMode.Range
)
```

---

# Sample project:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.wojciechosak.calendar.view

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Text
Expand Down Expand Up @@ -73,37 +74,38 @@ fun CalendarView(
},
)
}
Column {
if (config.value.showHeader) {
header(yearMonth.month, yearMonth.year)
}

if (config.value.showHeader) {
header(yearMonth.month, yearMonth.year)
}

LazyVerticalGrid(
columns = GridCells.Fixed(7),
horizontalArrangement = horizontalArrangement,
verticalArrangement = verticalArrangement,
userScrollEnabled = false,
modifier = modifier,
) {
val state = config.value
val weekDaysCount = if (state.showWeekdays) 7 else 0
LazyVerticalGrid(
columns = GridCells.Fixed(7),
horizontalArrangement = horizontalArrangement,
verticalArrangement = verticalArrangement,
userScrollEnabled = false,
modifier = modifier,
) {
val state = config.value
val weekDaysCount = if (state.showWeekdays) 7 else 0

items(previousMonthDays + daysInCurrentMonth + nextMonthDays + weekDaysCount) { iteration ->
Item(
iteration = iteration,
config = config,
weekDaysCount = weekDaysCount,
previousMonthDays = previousMonthDays,
daysInCurrentMonth = daysInCurrentMonth,
dayOfWeekLabel = dayOfWeekLabel,
yearMonth = yearMonth,
state = state,
selectionMode = selectionMode,
onDateSelected = onDateSelected,
isActiveDay = isActiveDay,
rangeConfig = rangeConfig,
day = day,
)
items(previousMonthDays + daysInCurrentMonth + nextMonthDays + weekDaysCount) { iteration ->
Item(
iteration = iteration,
config = config,
weekDaysCount = weekDaysCount,
previousMonthDays = previousMonthDays,
daysInCurrentMonth = daysInCurrentMonth,
dayOfWeekLabel = dayOfWeekLabel,
yearMonth = yearMonth,
state = state,
selectionMode = selectionMode,
onDateSelected = onDateSelected,
isActiveDay = isActiveDay,
rangeConfig = rangeConfig,
day = day,
)
}
}
}
}
Expand Down Expand Up @@ -170,7 +172,7 @@ private fun Item(
selectDate(
date = newDate,
mode = selectionMode,
list = selectedDates,
list = config.value.selectedDates,
)
config.value = config.value.copy(selectedDates = selectionList)
onDateSelected(selectionList)
Expand Down Expand Up @@ -202,7 +204,6 @@ private fun selectDate(
if (list.firstOrNull() == date) return list
val result = mutableListOf<LocalDate>()
result.addAll(list)

when (mode) {
is SelectionMode.Multiply -> {
result.add(0, date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun HorizontalCalendarView(
),
modifier: Modifier = Modifier,
pageSize: PageSize = PageSize.Fill,
beyondBoundsPageCount: Int = 0,
beyondBoundsPageCount: Int = 3,
contentPadding: PaddingValues = PaddingValues(0.dp),
calendarAnimator: CalendarAnimator = CalendarAnimator(startDate),
calendarView: @Composable (monthOffset: Int) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@ package io.wojciechosak.calendar.view

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import io.wojciechosak.calendar.modifiers.passTouchGesture
import kotlinx.datetime.Month

@Composable
fun MonthPicker(
columns: Int = 4,
horizontalArrangement: Arrangement.Horizontal = Arrangement.Center,
horizontalArrangement: Alignment.Horizontal = Alignment.CenterHorizontally,
verticalArrangement: Arrangement.Vertical = Arrangement.Center,
modifier: Modifier = Modifier,
userScrollEnabled: Boolean = true,
monthCount: Int = 12,
onMonthSelected: (Month) -> Unit = {},
monthView: @Composable (month: Month) -> Unit = { month ->
Text(month.name)
Column(
verticalArrangement = verticalArrangement,
horizontalAlignment = horizontalArrangement,
modifier = Modifier.aspectRatio(1f),
) {
Text(
month.name,
textAlign = TextAlign.Center,
)
}
},
) {
require(monthCount in 0..12) {
throw IllegalArgumentException("Month count should be greater than 0 and <= 12!")
}
LazyVerticalGrid(
columns = GridCells.Fixed(columns),
horizontalArrangement = horizontalArrangement,
verticalArrangement = verticalArrangement,
userScrollEnabled = userScrollEnabled,
modifier = modifier,
) {
Expand All @@ -39,11 +49,7 @@ fun MonthPicker(
Box(
modifier =
Modifier.passTouchGesture {
selectedMonth?.let { month ->
onMonthSelected(
month,
)
}
selectedMonth?.let { month -> onMonthSelected(month) }
},
contentAlignment = Alignment.Center,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fun VerticalCalendarView(
modifier: Modifier = Modifier,
pageSize: PageSize = PageSize.Fill,
contentPadding: PaddingValues = PaddingValues(0.dp),
beyondBoundsPageCount: Int = 3,
calendarView: @Composable (monthOffset: Int) -> Unit,
) {
val pagerState =
Expand All @@ -38,7 +39,7 @@ fun VerticalCalendarView(
state = pagerState,
modifier = modifier.fillMaxWidth(),
pageSize = pageSize,
beyondBoundsPageCount = 0,
beyondBoundsPageCount = beyondBoundsPageCount,
contentPadding = contentPadding,
) {
val index = it - INITIAL_PAGE_INDEX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ fun YearPicker(
pageSize: PageSize = PageSize.Fill,
onYearSelected: (Int) -> Unit = {},
yearView: @Composable (year: Int) -> Unit = { year ->
Text(year.toString(), modifier = Modifier.padding(32.dp))
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
Text(year.toString(), modifier = Modifier.padding(35.dp))
}
},
) {
val pagerState =
Expand Down
Binary file added readme/calendar-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/horizontal.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/monthpicker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/range.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/sample1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/sample2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/vertical.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/weekview.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/yearpicker.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeGesturesPadding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.OutlinedButton
Expand All @@ -32,26 +31,24 @@ class CalendarViewScreen : NamedScreen {

@Composable
override fun Content() {
Box(Modifier.safeGesturesPadding()) {
CalendarView(
day = { state ->
DayView(
date = state.date,
isDotVisible = state.isActiveDay || Random.nextBoolean(),
onClick = { },
)
},
config =
rememberCalendarState(
startDate = MonthYear(year = 1994, month = Month.APRIL).toLocalDate(),
monthOffset = 0,
showNextMonthDays = false,
showPreviousMonthDays = false,
showHeader = false,
showWeekdays = false,
),
)
}
CalendarView(
day = { state ->
DayView(
date = state.date,
isDotVisible = state.isActiveDay || Random.nextBoolean(),
onClick = { },
)
},
config =
rememberCalendarState(
startDate = MonthYear(year = 1994, month = Month.APRIL).toLocalDate(),
monthOffset = 0,
showNextMonthDays = false,
showPreviousMonthDays = false,
showHeader = false,
showWeekdays = false,
),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package io.wojciechosak.calendar.calendar.screens

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
Expand Down Expand Up @@ -49,12 +52,17 @@ class FullDateScreen : NamedScreen {
date = date.copy(month = it)
mode = DatePickerMode.YEAR
}) {
Text(
text = it.name.substring(IntRange(0, 2)).capitalize(),
color = Color.Black,
textAlign = TextAlign.Center,
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.aspectRatio(1f),
)
) {
Text(
text = it.name.substring(IntRange(0, 2)).capitalize(),
color = Color.Black,
textAlign = TextAlign.Center,
)
}
}
}

Expand Down
Loading

0 comments on commit 466c722

Please sign in to comment.