@@ -36,6 +36,7 @@ import androidx.compose.material.MaterialTheme
3636import androidx.compose.material.Surface
3737import androidx.compose.material.Text
3838import androidx.compose.runtime.Composable
39+ import androidx.compose.samples.crane.R
3940import androidx.compose.samples.crane.calendar.model.CalendarDay
4041import androidx.compose.samples.crane.calendar.model.CalendarMonth
4142import androidx.compose.samples.crane.calendar.model.DayOfWeek
@@ -48,10 +49,14 @@ import androidx.compose.samples.crane.util.SemiRect
4849import androidx.compose.ui.Alignment
4950import androidx.compose.ui.Modifier
5051import androidx.compose.ui.graphics.Color
52+ import androidx.compose.ui.res.stringResource
5153import androidx.compose.ui.semantics.SemanticsPropertyKey
5254import androidx.compose.ui.semantics.SemanticsPropertyReceiver
53- import androidx.compose.ui.semantics.contentDescription
55+ import androidx.compose.ui.semantics.clearAndSetSemantics
5456import androidx.compose.ui.semantics.semantics
57+ import androidx.compose.ui.semantics.stateDescription
58+ import androidx.compose.ui.semantics.text
59+ import androidx.compose.ui.text.AnnotatedString
5560import androidx.compose.ui.tooling.preview.Preview
5661import androidx.compose.ui.unit.dp
5762import com.google.accompanist.insets.navigationBarsHeight
@@ -78,7 +83,7 @@ fun Calendar(
7883
7984@Composable
8085private fun MonthHeader (modifier : Modifier = Modifier , month : String , year : String ) {
81- Row (modifier = modifier) {
86+ Row (modifier = modifier.clearAndSetSemantics { } ) {
8287 Text (
8388 modifier = Modifier .weight(1f ),
8489 text = month,
@@ -112,10 +117,7 @@ private fun Week(
112117 Day (
113118 day,
114119 onDayClicked,
115- Modifier .semantics {
116- contentDescription = " ${month.name} ${day.value} "
117- dayStatusProperty = day.status
118- }
120+ month
119121 )
120122 }
121123 Surface (modifier = spaceModifiers, color = rightFillColor) {
@@ -126,7 +128,7 @@ private fun Week(
126128
127129@Composable
128130private fun DaysOfWeek (modifier : Modifier = Modifier ) {
129- Row (modifier = modifier) {
131+ Row (modifier = modifier.clearAndSetSemantics { } ) {
130132 for (day in DayOfWeek .values()) {
131133 Day (name = day.name.take(1 ))
132134 }
@@ -137,20 +139,28 @@ private fun DaysOfWeek(modifier: Modifier = Modifier) {
137139private fun Day (
138140 day : CalendarDay ,
139141 onDayClicked : (CalendarDay ) -> Unit ,
142+ month : CalendarMonth ,
140143 modifier : Modifier = Modifier
141144) {
142145 val enabled = day.status != DaySelectedStatus .NonClickable
143146 DayContainer (
144- modifier = modifier,
145- onClick = { if (day.status != DaySelectedStatus .NonClickable ) onDayClicked(day) },
147+ modifier = modifier.semantics {
148+ if (enabled) text = AnnotatedString (" ${month.name} ${day.value} ${month.year} " )
149+ dayStatusProperty = day.status
150+ },
151+ selected = day.status != DaySelectedStatus .NoSelected ,
152+ onClick = { onDayClicked(day) },
146153 onClickEnabled = enabled,
147- backgroundColor = day.status.color(MaterialTheme .colors)
154+ backgroundColor = day.status.color(MaterialTheme .colors),
155+ onClickLabel = stringResource(id = R .string.click_label_select)
148156 ) {
149157 DayStatusContainer (status = day.status) {
150158 Text (
151159 modifier = Modifier
152160 .fillMaxSize()
153- .wrapContentSize(Alignment .Center ),
161+ .wrapContentSize(Alignment .Center )
162+ // Parent will handle semantics
163+ .clearAndSetSemantics {},
154164 text = day.value,
155165 style = MaterialTheme .typography.body1.copy(color = Color .White )
156166 )
@@ -173,17 +183,33 @@ private fun Day(name: String) {
173183@Composable
174184private fun DayContainer (
175185 modifier : Modifier = Modifier ,
186+ selected : Boolean = false,
176187 onClick : () -> Unit = { },
177188 onClickEnabled : Boolean = true,
178189 backgroundColor : Color = Color .Transparent ,
190+ onClickLabel : String? = null,
179191 content : @Composable () -> Unit
180192) {
181193 // What if this doesn't fit the screen? - LayoutFlexible(1f) + LayoutAspectRatio(1f)
194+ val stateDescriptionLabel = stringResource(
195+ if (selected) R .string.state_descr_selected else R .string.state_descr_not_selected
196+ )
182197 Surface (
183- modifier = modifier.size(width = CELL_SIZE , height = CELL_SIZE ),
198+ modifier = modifier
199+ .size(width = CELL_SIZE , height = CELL_SIZE )
200+ .then(
201+ if (onClickEnabled) {
202+ modifier.semantics {
203+ stateDescription = stateDescriptionLabel
204+ }
205+ } else {
206+ modifier.clearAndSetSemantics { }
207+ }
208+ ),
184209 onClick = onClick,
185210 enabled = onClickEnabled,
186- color = backgroundColor
211+ color = backgroundColor,
212+ onClickLabel = onClickLabel
187213 ) {
188214 content()
189215 }
0 commit comments