-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Calendar.kt
161 lines (148 loc) · 5.97 KB
/
Calendar.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.samples.crane.calendar
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.EaseOutQuart
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumedWindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.samples.crane.calendar.model.CalendarState
import androidx.compose.samples.crane.calendar.model.CalendarUiState
import androidx.compose.samples.crane.calendar.model.Month
import androidx.compose.samples.crane.ui.CraneTheme
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.temporal.TemporalAdjusters
import java.time.temporal.WeekFields
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun Calendar(
calendarState: CalendarState,
onDayClicked: (date: LocalDate) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues()
) {
val calendarUiState = calendarState.calendarUiState.value
val numberSelectedDays = calendarUiState.numberSelectedDays.toInt()
val selectedAnimationPercentage = remember(numberSelectedDays) {
Animatable(0f)
}
// Start a Launch Effect when the number of selected days change.
// using .animateTo() we animate the percentage selection from 0f - 1f
LaunchedEffect(numberSelectedDays) {
if (calendarUiState.hasSelectedDates) {
val animationSpec: TweenSpec<Float> = tween(
durationMillis =
(numberSelectedDays.coerceAtLeast(0) * DURATION_MILLIS_PER_DAY)
.coerceAtMost(2000),
easing = EaseOutQuart
)
selectedAnimationPercentage.animateTo(
targetValue = 1f,
animationSpec = animationSpec
)
}
}
LazyColumn(
modifier = modifier.consumedWindowInsets(contentPadding),
contentPadding = contentPadding
) {
calendarState.listMonths.forEach { month ->
itemsCalendarMonth(calendarUiState, onDayClicked, { selectedAnimationPercentage.value }, month)
}
item(key = "bottomSpacer") {
Spacer(
modifier = Modifier.windowInsetsBottomHeight(
WindowInsets.navigationBars
)
)
}
}
}
private fun LazyListScope.itemsCalendarMonth(
calendarUiState: CalendarUiState,
onDayClicked: (LocalDate) -> Unit,
selectedPercentageProvider: () -> Float,
month: Month
) {
item(month.yearMonth.month.name month.yearMonth.year "header") {
MonthHeader(
modifier = Modifier.padding(start = 32.dp, end = 32.dp, top = 32.dp),
month = month.yearMonth.month.name,
year = month.yearMonth.year.toString()
)
}
// Expanding width and centering horizontally
val contentModifier = Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
item(month.yearMonth.month.name month.yearMonth.year "daysOfWeek") {
DaysOfWeek(modifier = contentModifier)
}
// A custom key needs to be given to these items so that they can be found in tests that
// need scrolling. The format of the key is ${year/month/weekNumber}. Thus,
// the key for the fourth week of December 2020 is "2020/12/4"
itemsIndexed(month.weeks, key = { index, _ ->
month.yearMonth.year.toString() "/" month.yearMonth.month.value "/" (index 1).toString()
}) { _, week ->
val beginningWeek = week.yearMonth.atDay(1).plusWeeks(week.number.toLong())
val currentDay = beginningWeek.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
if (calendarUiState.hasSelectedPeriodOverlap(currentDay, currentDay.plusDays(6))) {
WeekSelectionPill(
state = calendarUiState,
currentWeekStart = currentDay,
widthPerDay = CELL_SIZE,
week = week,
selectedPercentageTotalProvider = selectedPercentageProvider
)
}
Week(
calendarUiState = calendarUiState,
modifier = contentModifier,
week = week,
onDayClicked = onDayClicked
)
Spacer(Modifier.height(8.dp))
}
}
internal val CALENDAR_STARTS_ON = WeekFields.ISO
@Preview
@Composable
fun DayPreview() {
CraneTheme {
Calendar(CalendarState(), onDayClicked = { })
}
}