-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add custom accessibility annotations to AnnotatedStringResource #1402
Draft
francoisadam
wants to merge
3
commits into
adevinta:main
Choose a base branch
from
francoisadam:accessibility-extensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
catalog/src/main/kotlin/com/adevinta/spark/catalog/accessibility/AccessibilityDemoScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2024 Adevinta | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.adevinta.spark.catalog.accessibility | ||
|
||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
|
||
@Composable | ||
public fun AccessibilityDemoScreen( | ||
contentPadding: PaddingValues, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val navController = rememberNavController() | ||
|
||
NavHost( | ||
modifier = modifier, | ||
navController = navController, | ||
startDestination = Accessibility, | ||
builder = { | ||
composable(route = Accessibility) { | ||
AccessibilityScreen(contentPadding) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
internal const val Accessibility = "accessibility" |
185 changes: 185 additions & 0 deletions
185
catalog/src/main/kotlin/com/adevinta/spark/catalog/accessibility/AccessibilityScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* Copyright (c) 2024 Adevinta | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.adevinta.spark.catalog.accessibility | ||
|
||
import android.content.Intent | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.consumeWindowInsets | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import com.adevinta.spark.SparkTheme | ||
import com.adevinta.spark.catalog.R | ||
import com.adevinta.spark.components.buttons.ButtonOutlined | ||
import com.adevinta.spark.components.divider.DividerIntent | ||
import com.adevinta.spark.components.divider.HorizontalDivider | ||
import com.adevinta.spark.components.spacer.VerticalSpacer | ||
import com.adevinta.spark.components.text.Text | ||
import com.adevinta.spark.icons.SparkIcons | ||
import com.adevinta.spark.icons.WheelOutline | ||
import com.adevinta.spark.res.annotatedStringResource | ||
import com.adevinta.spark.tokens.highlight | ||
|
||
@Composable | ||
public fun AccessibilityScreen( | ||
contentPadding: PaddingValues, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val context = LocalContext.current | ||
val accessibilityTypes: List<AccessibilityType> by remember { | ||
mutableStateOf(getAllAccessibilityRes()) | ||
} | ||
Column( | ||
modifier = modifier.fillMaxSize(), | ||
) { | ||
ButtonOutlined( | ||
icon = SparkIcons.WheelOutline, | ||
onClick = { | ||
context.startActivity(Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS)) | ||
}, | ||
modifier = Modifier.padding(16.dp), | ||
) { | ||
Text(text = stringResource(id = R.string.accessibility_settings_shortcut)) | ||
} | ||
|
||
LazyColumn( | ||
modifier = modifier | ||
.consumeWindowInsets(contentPadding) | ||
.fillMaxSize() | ||
.padding(horizontal = 16.dp), | ||
contentPadding = contentPadding, | ||
verticalArrangement = Arrangement.spacedBy(8.dp), | ||
) { | ||
items(accessibilityTypes.size) { index -> | ||
val accessibilityType = accessibilityTypes[index] | ||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { | ||
Text(text = stringResource(id = R.string.accessibility_settings_type_label)) | ||
Text( | ||
text = stringResource(id = accessibilityType.type), | ||
style = SparkTheme.typography.body1.highlight, | ||
) | ||
} | ||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { | ||
Text(text = stringResource(id = R.string.accessibility_settings_input_label)) | ||
Text( | ||
text = annotatedStringResource(id = accessibilityType.input), | ||
color = SparkTheme.colors.support, | ||
) | ||
} | ||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { | ||
Text(text = stringResource(id = R.string.accessibility_settings_result_label)) | ||
Text( | ||
text = annotatedStringResource(id = accessibilityType.result), | ||
color = SparkTheme.colors.accent, | ||
) | ||
} | ||
VerticalSpacer(8.dp) | ||
HorizontalDivider(intent = DividerIntent.Outline) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private data class AccessibilityType( | ||
val type: Int, | ||
val input: Int, | ||
val result: Int, | ||
) | ||
|
||
private fun getAllAccessibilityRes() = listOf( | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_cardinal_type, | ||
input = R.string.accessibility_settings_cardinal_input, | ||
result = R.string.accessibility_settings_cardinal_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_date_type, | ||
input = R.string.accessibility_settings_date_input, | ||
result = R.string.accessibility_settings_date_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_decimal_type, | ||
input = R.string.accessibility_settings_decimal_input, | ||
result = R.string.accessibility_settings_decimal_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_digits_type, | ||
input = R.string.accessibility_settings_digits_input, | ||
result = R.string.accessibility_settings_digits_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_electronic_type, | ||
input = R.string.accessibility_settings_electronic_input, | ||
result = R.string.accessibility_settings_electronic_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_fraction_type, | ||
input = R.string.accessibility_settings_fraction_input, | ||
result = R.string.accessibility_settings_fraction_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_measure_type, | ||
input = R.string.accessibility_settings_measure_input, | ||
result = R.string.accessibility_settings_measure_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_money_type, | ||
input = R.string.accessibility_settings_money_input, | ||
result = R.string.accessibility_settings_money_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_ordinal_type, | ||
input = R.string.accessibility_settings_ordinal_input, | ||
result = R.string.accessibility_settings_ordinal_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_telephone_type, | ||
input = R.string.accessibility_settings_telephone_input, | ||
result = R.string.accessibility_settings_telephone_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_text_type, | ||
input = R.string.accessibility_settings_text_input, | ||
result = R.string.accessibility_settings_text_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_time_type, | ||
input = R.string.accessibility_settings_time_input, | ||
result = R.string.accessibility_settings_time_result, | ||
), | ||
AccessibilityType( | ||
type = R.string.accessibility_settings_verbatim_type, | ||
input = R.string.accessibility_settings_verbatim_input, | ||
result = R.string.accessibility_settings_verbatim_result, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to fully understand why we duplicated this part of the code. Can't we merge the tts handling with the already existing
buildWithSpan
which is already parsing the entire string/annotations once?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the frustrating part, TtsSpan in spannable uses metadata to add info for Tts to better read the words but this is not possible yet in compose and AnnotatedStrings (well only Verbatim is possible for now)
So the only way I found was to actually reformat the string itself to make it more readable by Tts.
The first part with
buildWithAccessibilitySpan
updates the strings before usingappend
, and the second part withbuildWithSpanStyle
only adds styling to the annotated string (and so needs to be done after theappend
)But if you know how to extend the TtsAnnotation from Compose, or a better way to do this I'm all ears π