Skip to content

Commit eafc096

Browse files
committed
Add week container parameter to compose and compose-multiplatform
1 parent 1494a7a commit eafc096

File tree

6 files changed

+120
-44
lines changed

6 files changed

+120
-44
lines changed

compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/compose/Calendar.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ private fun Calendar(
242242
* placed above each week on the calendar.
243243
* @param weekFooter a composable block which describes the week footer content. The footer is
244244
* placed below each week on the calendar.
245+
* @param weekContainer a composable block which describes the entire week content. This is the
246+
* container where all the week contents are placed (header => days => footer). This is useful if
247+
* you want to customize the week container, for example, with a background color or other effects.
248+
* The actual container content is provided in the block and must be called after your desired
249+
* customisations are rendered.
245250
*/
246251
@Composable
247252
public fun WeekCalendar(
@@ -254,6 +259,7 @@ public fun WeekCalendar(
254259
dayContent: @Composable BoxScope.(WeekDay) -> Unit,
255260
weekHeader: (@Composable ColumnScope.(Week) -> Unit)? = null,
256261
weekFooter: (@Composable ColumnScope.(Week) -> Unit)? = null,
262+
weekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit)? = null,
257263
): Unit = WeekCalendarImpl(
258264
modifier = modifier,
259265
state = state,
@@ -263,6 +269,7 @@ public fun WeekCalendar(
263269
dayContent = dayContent,
264270
weekHeader = weekHeader,
265271
weekFooter = weekFooter,
272+
weekContainer = weekContainer,
266273
contentPadding = contentPadding,
267274
onItemPlaced = state.placementInfo::onItemPlaced,
268275
)

compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/compose/weekcalendar/WeekCalendar.kt

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import androidx.compose.foundation.layout.ColumnScope
77
import androidx.compose.foundation.layout.IntrinsicSize
88
import androidx.compose.foundation.layout.PaddingValues
99
import androidx.compose.foundation.layout.Row
10+
import androidx.compose.foundation.layout.fillMaxWidth
1011
import androidx.compose.foundation.layout.width
12+
import androidx.compose.foundation.lazy.LazyItemScope
1113
import androidx.compose.foundation.lazy.LazyRow
1214
import androidx.compose.runtime.Composable
1315
import androidx.compose.runtime.getValue
@@ -20,6 +22,7 @@ import androidx.compose.ui.layout.onPlaced
2022
import com.kizitonwose.calendar.compose.CalendarDefaults.flingBehavior
2123
import com.kizitonwose.calendar.compose.ItemCoordinates
2224
import com.kizitonwose.calendar.compose.ItemCoordinatesStore
25+
import com.kizitonwose.calendar.compose.or
2326
import com.kizitonwose.calendar.core.Week
2427
import com.kizitonwose.calendar.core.WeekDay
2528
import com.kizitonwose.calendar.core.format.toIso8601String
@@ -35,6 +38,7 @@ internal fun WeekCalendarImpl(
3538
dayContent: @Composable BoxScope.(WeekDay) -> Unit,
3639
weekHeader: (@Composable ColumnScope.(Week) -> Unit)? = null,
3740
weekFooter: (@Composable ColumnScope.(Week) -> Unit)? = null,
41+
weekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit)? = null,
3842
onItemPlaced: (itemCoordinates: ItemCoordinates) -> Unit,
3943
) {
4044
LazyRow(
@@ -54,31 +58,39 @@ internal fun WeekCalendarImpl(
5458
val itemCoordinatesStore = remember(week.days.first().date) {
5559
ItemCoordinatesStore(currentOnItemPlaced)
5660
}
57-
Column(
58-
modifier = Modifier
59-
.then(
60-
if (calendarScrollPaged) {
61-
Modifier.fillParentMaxWidth()
62-
} else {
63-
Modifier.width(IntrinsicSize.Max)
64-
},
65-
)
66-
.onPlaced(itemCoordinatesStore::onItemRootPlaced),
67-
) {
68-
weekHeader?.invoke(this, week)
69-
Row {
70-
for ((column, day) in week.days.withIndex()) {
71-
Box(
72-
modifier = Modifier
73-
.then(if (calendarScrollPaged) Modifier.weight(1f) else Modifier)
74-
.clipToBounds()
75-
.onFirstDayPlaced(column, itemCoordinatesStore::onFirstDayPlaced),
76-
) {
77-
dayContent(day)
61+
val hasWeekContainer = weekContainer != null
62+
Box(Modifier.onPlaced(itemCoordinatesStore::onItemRootPlaced)) {
63+
weekContainer.or(defaultWeekContainer)(week) {
64+
Column(
65+
modifier = Modifier
66+
.then(
67+
if (calendarScrollPaged) {
68+
if (hasWeekContainer) {
69+
Modifier.fillMaxWidth()
70+
} else {
71+
Modifier.fillParentMaxWidth()
72+
}
73+
} else {
74+
Modifier.width(IntrinsicSize.Max)
75+
},
76+
),
77+
) {
78+
weekHeader?.invoke(this, week)
79+
Row {
80+
for ((column, day) in week.days.withIndex()) {
81+
Box(
82+
modifier = Modifier
83+
.then(if (calendarScrollPaged) Modifier.weight(1f) else Modifier)
84+
.clipToBounds()
85+
.onFirstDayPlaced(column, itemCoordinatesStore::onFirstDayPlaced),
86+
) {
87+
dayContent(day)
88+
}
89+
}
7890
}
91+
weekFooter?.invoke(this, week)
7992
}
8093
}
81-
weekFooter?.invoke(this, week)
8294
}
8395
}
8496
}
@@ -92,3 +104,6 @@ private inline fun Modifier.onFirstDayPlaced(
92104
} else {
93105
this
94106
}
107+
108+
private val defaultWeekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit) =
109+
{ _, container -> container() }

compose-multiplatform/sample/src/commonMain/kotlin/Example7Page.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ fun Example7Page() {
6969
selection = it
7070
}
7171
},
72+
// Draw a thin border around each week.
73+
weekContainer = { week, container ->
74+
Box(
75+
modifier = Modifier
76+
.padding(start = 3.dp, top = 3.dp, bottom = 3.dp)
77+
.border(
78+
color = Colors.primary,
79+
width = 2.dp,
80+
shape = RoundedCornerShape(8.dp),
81+
)
82+
.padding(3.dp)
83+
.clip(shape = RoundedCornerShape(8.dp)),
84+
) {
85+
container()
86+
}
87+
},
7288
)
7389
}
7490
}

compose/src/main/java/com/kizitonwose/calendar/compose/Calendar.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ private fun Calendar(
242242
* placed above each week on the calendar.
243243
* @param weekFooter a composable block which describes the week footer content. The footer is
244244
* placed below each week on the calendar.
245+
* @param weekContainer a composable block which describes the entire week content. This is the
246+
* container where all the week contents are placed (header => days => footer). This is useful if
247+
* you want to customize the week container, for example, with a background color or other effects.
248+
* The actual container content is provided in the block and must be called after your desired
249+
* customisations are rendered.
245250
*/
246251
@Composable
247252
public fun WeekCalendar(
@@ -254,6 +259,7 @@ public fun WeekCalendar(
254259
dayContent: @Composable BoxScope.(WeekDay) -> Unit,
255260
weekHeader: (@Composable ColumnScope.(Week) -> Unit)? = null,
256261
weekFooter: (@Composable ColumnScope.(Week) -> Unit)? = null,
262+
weekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit)? = null,
257263
): Unit = WeekCalendarImpl(
258264
modifier = modifier,
259265
state = state,
@@ -263,6 +269,7 @@ public fun WeekCalendar(
263269
dayContent = dayContent,
264270
weekHeader = weekHeader,
265271
weekFooter = weekFooter,
272+
weekContainer = weekContainer,
266273
contentPadding = contentPadding,
267274
onItemPlaced = state.placementInfo::onItemPlaced,
268275
)

compose/src/main/java/com/kizitonwose/calendar/compose/weekcalendar/WeekCalendar.kt

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import androidx.compose.foundation.layout.ColumnScope
77
import androidx.compose.foundation.layout.IntrinsicSize
88
import androidx.compose.foundation.layout.PaddingValues
99
import androidx.compose.foundation.layout.Row
10+
import androidx.compose.foundation.layout.fillMaxWidth
1011
import androidx.compose.foundation.layout.width
12+
import androidx.compose.foundation.lazy.LazyItemScope
1113
import androidx.compose.foundation.lazy.LazyRow
1214
import androidx.compose.runtime.Composable
1315
import androidx.compose.runtime.getValue
@@ -20,6 +22,7 @@ import androidx.compose.ui.layout.onPlaced
2022
import com.kizitonwose.calendar.compose.CalendarDefaults.flingBehavior
2123
import com.kizitonwose.calendar.compose.ItemCoordinates
2224
import com.kizitonwose.calendar.compose.ItemCoordinatesStore
25+
import com.kizitonwose.calendar.compose.or
2326
import com.kizitonwose.calendar.core.Week
2427
import com.kizitonwose.calendar.core.WeekDay
2528

@@ -34,6 +37,7 @@ internal fun WeekCalendarImpl(
3437
dayContent: @Composable BoxScope.(WeekDay) -> Unit,
3538
weekHeader: (@Composable ColumnScope.(Week) -> Unit)? = null,
3639
weekFooter: (@Composable ColumnScope.(Week) -> Unit)? = null,
40+
weekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit)? = null,
3741
onItemPlaced: (itemCoordinates: ItemCoordinates) -> Unit,
3842
) {
3943
LazyRow(
@@ -53,31 +57,39 @@ internal fun WeekCalendarImpl(
5357
val itemCoordinatesStore = remember(week.days.first().date) {
5458
ItemCoordinatesStore(currentOnItemPlaced)
5559
}
56-
Column(
57-
modifier = Modifier
58-
.then(
59-
if (calendarScrollPaged) {
60-
Modifier.fillParentMaxWidth()
61-
} else {
62-
Modifier.width(IntrinsicSize.Max)
63-
},
64-
)
65-
.onPlaced(itemCoordinatesStore::onItemRootPlaced),
66-
) {
67-
weekHeader?.invoke(this, week)
68-
Row {
69-
for ((column, day) in week.days.withIndex()) {
70-
Box(
71-
modifier = Modifier
72-
.then(if (calendarScrollPaged) Modifier.weight(1f) else Modifier)
73-
.clipToBounds()
74-
.onFirstDayPlaced(column, itemCoordinatesStore::onFirstDayPlaced),
75-
) {
76-
dayContent(day)
60+
val hasWeekContainer = weekContainer != null
61+
Box(Modifier.onPlaced(itemCoordinatesStore::onItemRootPlaced)) {
62+
weekContainer.or(defaultWeekContainer)(week) {
63+
Column(
64+
modifier = Modifier
65+
.then(
66+
if (calendarScrollPaged) {
67+
if (hasWeekContainer) {
68+
Modifier.fillMaxWidth()
69+
} else {
70+
Modifier.fillParentMaxWidth()
71+
}
72+
} else {
73+
Modifier.width(IntrinsicSize.Max)
74+
},
75+
),
76+
) {
77+
weekHeader?.invoke(this, week)
78+
Row {
79+
for ((column, day) in week.days.withIndex()) {
80+
Box(
81+
modifier = Modifier
82+
.then(if (calendarScrollPaged) Modifier.weight(1f) else Modifier)
83+
.clipToBounds()
84+
.onFirstDayPlaced(column, itemCoordinatesStore::onFirstDayPlaced),
85+
) {
86+
dayContent(day)
87+
}
88+
}
7789
}
90+
weekFooter?.invoke(this, week)
7891
}
7992
}
80-
weekFooter?.invoke(this, week)
8193
}
8294
}
8395
}
@@ -91,3 +103,6 @@ private inline fun Modifier.onFirstDayPlaced(
91103
} else {
92104
this
93105
}
106+
107+
private val defaultWeekContainer: (@Composable LazyItemScope.(Week, container: @Composable () -> Unit) -> Unit) =
108+
{ _, container -> container() }

sample/src/main/java/com/kizitonwose/calendar/sample/compose/Example7Page.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ fun Example7Page() {
6464
selection = it
6565
}
6666
},
67+
// Draw a thin border around each week.
68+
weekContainer = { week, container ->
69+
Box(
70+
modifier = Modifier
71+
.padding(start = 3.dp, top = 3.dp, bottom = 3.dp)
72+
.border(
73+
color = colorResource(R.color.colorPrimary),
74+
width = 2.dp,
75+
shape = RoundedCornerShape(8.dp),
76+
)
77+
.padding(3.dp)
78+
.clip(shape = RoundedCornerShape(8.dp)),
79+
) {
80+
container()
81+
}
82+
},
6783
)
6884
}
6985
}

0 commit comments

Comments
 (0)