Skip to content

Commit 5a46a78

Browse files
Add min/max week (#116)
* Add min/max week * Update detekt * Add documentation * Update github actions
1 parent 1cdea2b commit 5a46a78

File tree

18 files changed

+311
-97
lines changed

18 files changed

+311
-97
lines changed

.github/workflows/check.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
name: Check
22
on:
33
pull_request:
4+
45
jobs:
56
detekt:
67
name: Detekt Check
78
runs-on: ubuntu-latest
89
steps:
910
- name: Checkout Repository
10-
uses: actions/checkout@v2
11+
uses: actions/checkout@v4
1112

1213
- name: Cache Gradle
13-
uses: actions/cache@v2
14+
uses: actions/cache@v4
1415
with:
1516
path: |
1617
~/.gradle/caches/
@@ -29,15 +30,16 @@ jobs:
2930

3031
- name: Stop Gradle
3132
run: ./gradlew --stop
33+
3234
unit-tests:
3335
name: Unit Tests
3436
runs-on: ubuntu-latest
3537
steps:
3638
- name: Checkout Repository
37-
uses: actions/checkout@v2
39+
uses: actions/checkout@v4
3840

3941
- name: Cache Gradle
40-
uses: actions/cache@v2
42+
uses: actions/cache@v4
4143
with:
4244
path: |
4345
~/.gradle/caches/
@@ -56,16 +58,17 @@ jobs:
5658

5759
- name: Stop Gradle
5860
run: ./gradlew --stop
61+
5962
ui-tests:
6063
name: FTL UI Tests
6164
needs: [unit-tests, detekt]
6265
runs-on: ubuntu-latest
6366
steps:
6467
- name: Checkout Repository
65-
uses: actions/checkout@v2
68+
uses: actions/checkout@v4
6669

6770
- name: Cache Gradle
68-
uses: actions/cache@v2
71+
uses: actions/cache@v4
6972
with:
7073
path: |
7174
~/.gradle/caches/
@@ -88,7 +91,7 @@ jobs:
8891
credentials_json: ${{ secrets.GCLOUD_AUTH_KEY }}
8992

9093
- name: Set up Cloud SDK
91-
uses: google-github-actions/setup-gcloud@v0
94+
uses: google-github-actions/setup-gcloud@v2
9295

9396
- name: Use gcloud CLI
9497
run: gcloud info
@@ -101,15 +104,16 @@ jobs:
101104

102105
- name: Stop Gradle
103106
run: ./gradlew --stop
107+
104108
compatibility-check:
105109
name: Compatibility Check
106110
runs-on: ubuntu-latest
107111
steps:
108112
- name: Checkout Repository
109-
uses: actions/checkout@v2
113+
uses: actions/checkout@v4
110114

111115
- name: Cache Gradle
112-
uses: actions/cache@v2
116+
uses: actions/cache@v4
113117
with:
114118
path: |
115119
~/.gradle/caches/

.github/workflows/publish-snapshot.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Publish Snaphot
1+
name: Publish Snapshot
22
on:
33
workflow_dispatch:
44
push:
@@ -7,16 +7,16 @@ on:
77

88
jobs:
99
deploy-maven-snapshot:
10-
name: Deploy To Maven Snaphot
10+
name: Deploy To Maven Snapshot
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout Repository
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515
with:
16-
fetch-depth: '0'
16+
fetch-tags: true
1717

1818
- name: Cache Gradle
19-
uses: actions/cache@v2
19+
uses: actions/cache@v4
2020
with:
2121
path: |
2222
~/.gradle/caches/

.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout Repository
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515
with:
16-
fetch-depth: '0'
16+
fetch-tags: true
1717

1818
- name: Cache Gradle
19-
uses: actions/cache@v2
19+
uses: actions/cache@v4
2020
with:
2121
path: |
2222
~/.gradle/caches/

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,16 @@ Library is available on Maven Central repository.
2020
```
2121
Snapshots are available on [Sonatype’s snapshots repository](https://s01.oss.sonatype.org/content/repositories/snapshots/io/github/boguszpawlowski/composecalendar/).
2222

23-
## Compose versions
24-
| Compose Version | Compose Calendar Version |
25-
|-------------------|----------------------------|
26-
| 1.0.0 | 0.3.0 |
27-
| 1.1.0 | 0.5.1 |
28-
| 1.2.0 | 0.6.0 |
29-
| 1.2.1 | 1.0.2 |
30-
| 1.3.1 | 1.1.0 |
31-
3223
## Supported features
3324
- Selection (single, multiple or a range of days)
34-
- Every day as first day of week
25+
- Chose day as first day of week
3526
- Showing/hiding adjacent months
3627
- Month and week headers
3728
- Customizable month container
3829
- Fully customizable day content
3930
- Horizontal swipe for changing a current month
4031
- Month / Week mode
32+
- Min / Max Month (or Week)
4133

4234
## Basic Usage
4335

@@ -144,6 +136,10 @@ Apart from rendering your own components inside the calendar, you can modify it
144136
- `showAdjacentMonths` - whenever to render days from adjacent months. Defaults to `true`.
145137
- `firstDayOfWeek` - you can pass the `DayOfWeek` which you want you week to start with. It defaults to the first day of week of the `Locale.default()`.
146138
- `horizontalScrollEnabled` - a Boolean flag which enables month to be changed by a horizontal swipe. Defaults to `true`.
139+
- `minMonth` - a `YearMonth` object representing the minimum month that can be shown in the calendar. By default there is no minimum month.
140+
- `maxMonth` - a `YearMonth` object representing the maximum month that can be shown in the calendar. By default there is no maximum month.
141+
142+
> :exclamation: You cannot set `minMonth` to be lower than `maxMonth` and vice versa. If you do so, the calendar state won't change.
147143
148144
Apart from this, `Calendar` you can pass a `Modifier` object like in any other composable.
149145

@@ -213,6 +209,9 @@ Selection modes are represented by `SelectionMode` enum, with following values:
213209
- `Period` - selectable period - implemented by `start` and `end` dates. - selection will contain all dates between start and the end date.
214210
This implementation of SelectionState also allows for handling side-effects and vetoing the state change via `confirmSelectionChange` callback.
215211

212+
## Week Calendar
213+
Apart from the default calendar, there is also a week calendar, which shows a single week at a time. It can be used in the same way as the default calendar, and has the same customization options.
214+
216215
## KotlinX DateTime
217216
As the core of the library is built on `java.time` library, on Android it requires to use [core libary desugaring](https://developer.android.com/studio/write/java8-support) to be able to access it's API.
218217
As a result it's features may be unavailable to some project built around different date-time libraries (e.g. kotlinx-datetime). Although the project wont be migrating from `java.time`, as it's the best suited for it, there is a separate `kotlinx-datetime` artifact for those who need to use the library from a codebase based on it. It doesn't consist of a separate version of `ComposeCalendar` features, but offers a small bunch of utilities, that will enable you to create your own wrapper, as briefly presented in `KotlinDateTimeSample`. If the provided functionality, doesn't match your use-case, please submit an issue.

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object Material {
8484
}
8585

8686
object DetektLib {
87-
const val Version = "1.21.0"
87+
const val Version = "1.23.4"
8888

8989
const val PluginId = "io.gitlab.arturbosch.detekt"
9090
const val Plugin = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$Version"

library/api/library.api

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public final class io/github/boguszpawlowski/composecalendar/WeekCalendarKt {
7676
public static final fun SelectableWeekCalendar (Landroidx/compose/ui/Modifier;Ljava/time/DayOfWeek;Ljava/time/LocalDate;ZZLio/github/boguszpawlowski/composecalendar/WeekCalendarState;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
7777
public static final fun StaticWeekCalendar (Landroidx/compose/ui/Modifier;Ljava/time/DayOfWeek;Ljava/time/LocalDate;ZLio/github/boguszpawlowski/composecalendar/WeekCalendarState;ZLkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
7878
public static final fun WeekCalendar (Lio/github/boguszpawlowski/composecalendar/WeekCalendarState;Landroidx/compose/ui/Modifier;Ljava/time/LocalDate;Ljava/time/DayOfWeek;ZZLkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
79-
public static final fun rememberSelectableWeekCalendarState (Ljava/time/DayOfWeek;Lio/github/boguszpawlowski/composecalendar/week/Week;Ljava/util/List;Lio/github/boguszpawlowski/composecalendar/selection/SelectionMode;Lkotlin/jvm/functions/Function1;Lio/github/boguszpawlowski/composecalendar/header/WeekState;Lio/github/boguszpawlowski/composecalendar/selection/DynamicSelectionState;Landroidx/compose/runtime/Composer;II)Lio/github/boguszpawlowski/composecalendar/WeekCalendarState;
80-
public static final fun rememberWeekCalendarState (Ljava/time/DayOfWeek;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/header/WeekState;Landroidx/compose/runtime/Composer;II)Lio/github/boguszpawlowski/composecalendar/WeekCalendarState;
79+
public static final fun rememberSelectableWeekCalendarState (Ljava/time/DayOfWeek;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;Ljava/util/List;Lio/github/boguszpawlowski/composecalendar/selection/SelectionMode;Lkotlin/jvm/functions/Function1;Lio/github/boguszpawlowski/composecalendar/header/WeekState;Lio/github/boguszpawlowski/composecalendar/selection/DynamicSelectionState;Landroidx/compose/runtime/Composer;II)Lio/github/boguszpawlowski/composecalendar/WeekCalendarState;
80+
public static final fun rememberWeekCalendarState (Ljava/time/DayOfWeek;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/header/WeekState;Landroidx/compose/runtime/Composer;II)Lio/github/boguszpawlowski/composecalendar/WeekCalendarState;
8181
}
8282

8383
public final class io/github/boguszpawlowski/composecalendar/WeekCalendarState {
@@ -159,15 +159,19 @@ public final class io/github/boguszpawlowski/composecalendar/header/MonthStateKt
159159
public abstract interface class io/github/boguszpawlowski/composecalendar/header/WeekState {
160160
public static final field Companion Lio/github/boguszpawlowski/composecalendar/header/WeekState$Companion;
161161
public abstract fun getCurrentWeek ()Lio/github/boguszpawlowski/composecalendar/week/Week;
162+
public abstract fun getMaxWeek ()Lio/github/boguszpawlowski/composecalendar/week/Week;
163+
public abstract fun getMinWeek ()Lio/github/boguszpawlowski/composecalendar/week/Week;
162164
public abstract fun setCurrentWeek (Lio/github/boguszpawlowski/composecalendar/week/Week;)V
165+
public abstract fun setMaxWeek (Lio/github/boguszpawlowski/composecalendar/week/Week;)V
166+
public abstract fun setMinWeek (Lio/github/boguszpawlowski/composecalendar/week/Week;)V
163167
}
164168

165169
public final class io/github/boguszpawlowski/composecalendar/header/WeekState$Companion {
166170
public final fun Saver ()Landroidx/compose/runtime/saveable/Saver;
167171
}
168172

169173
public final class io/github/boguszpawlowski/composecalendar/header/WeekStateKt {
170-
public static final fun WeekState (Lio/github/boguszpawlowski/composecalendar/week/Week;)Lio/github/boguszpawlowski/composecalendar/header/WeekState;
174+
public static final fun WeekState (Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;)Lio/github/boguszpawlowski/composecalendar/header/WeekState;
171175
}
172176

173177
public final class io/github/boguszpawlowski/composecalendar/selection/DynamicSelectionHandler {
@@ -223,6 +227,7 @@ public final class io/github/boguszpawlowski/composecalendar/week/Week {
223227
public static final field $stable I
224228
public static final field Companion Lio/github/boguszpawlowski/composecalendar/week/Week$Companion;
225229
public fun <init> (Ljava/util/List;)V
230+
public final fun compareTo (Lio/github/boguszpawlowski/composecalendar/week/Week;)I
226231
public final fun component1 ()Ljava/util/List;
227232
public final fun copy (Ljava/util/List;)Lio/github/boguszpawlowski/composecalendar/week/Week;
228233
public static synthetic fun copy$default (Lio/github/boguszpawlowski/composecalendar/week/Week;Ljava/util/List;ILjava/lang/Object;)Lio/github/boguszpawlowski/composecalendar/week/Week;
@@ -234,6 +239,7 @@ public final class io/github/boguszpawlowski/composecalendar/week/Week {
234239
public final fun getYearMonth ()Ljava/time/YearMonth;
235240
public fun hashCode ()I
236241
public final fun inc ()Lio/github/boguszpawlowski/composecalendar/week/Week;
242+
public final fun minusWeeks (J)Lio/github/boguszpawlowski/composecalendar/week/Week;
237243
public final fun plusWeeks (J)Lio/github/boguszpawlowski/composecalendar/week/Week;
238244
public fun toString ()Ljava/lang/String;
239245
}
@@ -243,3 +249,7 @@ public final class io/github/boguszpawlowski/composecalendar/week/Week$Companion
243249
public static synthetic fun now$default (Lio/github/boguszpawlowski/composecalendar/week/Week$Companion;Ljava/time/DayOfWeek;ILjava/lang/Object;)Lio/github/boguszpawlowski/composecalendar/week/Week;
244250
}
245251

252+
public final class io/github/boguszpawlowski/composecalendar/week/WeekKt {
253+
public static final fun between (Ljava/time/temporal/ChronoUnit;Lio/github/boguszpawlowski/composecalendar/week/Week;Lio/github/boguszpawlowski/composecalendar/week/Week;)I
254+
}
255+

library/src/main/java/io/github/boguszpawlowski/composecalendar/Calendar.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,14 @@ public fun <T : SelectionState> Calendar(
231231
* @param initialSelection initial selection of the composable
232232
* @param initialSelectionMode initial mode of the selection
233233
* @param confirmSelectionChange callback for optional side-effects handling and vetoing the state change
234+
* @param minMonth first month that can be shown
235+
* @param maxMonth last month that can be shown
234236
*/
235237
@Composable
236238
public fun rememberSelectableCalendarState(
237239
initialMonth: YearMonth = YearMonth.now(),
238-
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarMonthRange),
239-
maxMonth: YearMonth = initialMonth.plusMonths(DefaultCalendarMonthRange),
240+
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarPagerRange),
241+
maxMonth: YearMonth = initialMonth.plusMonths(DefaultCalendarPagerRange),
240242
initialSelection: List<LocalDate> = emptyList(),
241243
initialSelectionMode: SelectionMode = SelectionMode.Single,
242244
confirmSelectionChange: (newValue: List<LocalDate>) -> Boolean = { true },
@@ -258,12 +260,14 @@ public fun rememberSelectableCalendarState(
258260
* Helper function for providing a [CalendarState] implementation without a selection mechanism.
259261
*
260262
* @param initialMonth initially rendered month
263+
* @param minMonth first month that can be shown
264+
* @param maxMonth last month that can be shown
261265
*/
262266
@Composable
263267
public fun rememberCalendarState(
264268
initialMonth: YearMonth = YearMonth.now(),
265-
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarMonthRange),
266-
maxMonth: YearMonth = initialMonth.plusMonths(DefaultCalendarMonthRange),
269+
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarPagerRange),
270+
maxMonth: YearMonth = initialMonth.plusMonths(DefaultCalendarPagerRange),
267271
monthState: MonthState = rememberSaveable(saver = MonthState.Saver()) {
268272
MonthState(
269273
initialMonth = initialMonth,
@@ -273,4 +277,4 @@ public fun rememberCalendarState(
273277
},
274278
): CalendarState<EmptySelectionState> = remember { CalendarState(monthState, EmptySelectionState) }
275279

276-
internal const val DefaultCalendarMonthRange = 10000L
280+
internal const val DefaultCalendarPagerRange = 10_000L

library/src/main/java/io/github/boguszpawlowski/composecalendar/WeekCalendar.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,24 @@ public fun <T : SelectionState> WeekCalendar(
207207
* @param initialSelection initial selection of the composable
208208
* @param initialSelectionMode initial mode of the selection
209209
* @param confirmSelectionChange callback for optional side-effects handling and vetoing the state change
210+
* @param minWeek first week that can be shown
211+
* @param maxWeek last week that can be shown
210212
*/
211213
@Composable
212214
public fun rememberSelectableWeekCalendarState(
213215
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
214216
initialWeek: Week = Week.now(firstDayOfWeek),
217+
minWeek: Week = initialWeek.minusWeeks(DefaultCalendarPagerRange),
218+
maxWeek: Week = initialWeek.plusWeeks(DefaultCalendarPagerRange),
215219
initialSelection: List<LocalDate> = emptyList(),
216220
initialSelectionMode: SelectionMode = SelectionMode.Single,
217221
confirmSelectionChange: (newValue: List<LocalDate>) -> Boolean = { true },
218222
weekState: WeekState = rememberSaveable(saver = WeekState.Saver()) {
219-
WeekState(initialWeek = initialWeek)
223+
WeekState(
224+
initialWeek = initialWeek,
225+
minWeek = minWeek,
226+
maxWeek = maxWeek,
227+
)
220228
},
221229
selectionState: DynamicSelectionState = rememberSaveable(
222230
saver = DynamicSelectionState.Saver(confirmSelectionChange),
@@ -231,13 +239,21 @@ public fun rememberSelectableWeekCalendarState(
231239
*
232240
* @param firstDayOfWeek first day of a week, defaults to current locale's
233241
* @param initialWeek initially rendered week
242+
* @param minWeek first week that can be shown
243+
* @param maxWeek last week that can be shown
234244
*/
235245
@Composable
236246
public fun rememberWeekCalendarState(
237247
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
238248
initialWeek: Week = Week.now(firstDayOfWeek),
249+
minWeek: Week = initialWeek.minusWeeks(DefaultCalendarPagerRange),
250+
maxWeek: Week = initialWeek.plusWeeks(DefaultCalendarPagerRange),
239251
weekState: WeekState = rememberSaveable(saver = WeekState.Saver()) {
240-
WeekState(initialWeek = initialWeek)
252+
WeekState(
253+
initialWeek = initialWeek,
254+
minWeek = minWeek,
255+
maxWeek = maxWeek,
256+
)
241257
},
242258
): WeekCalendarState<EmptySelectionState> =
243259
remember { WeekCalendarState(weekState, EmptySelectionState) }

0 commit comments

Comments
 (0)