-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Migrate template widget configuration to Compose and Material3 #7288
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
Open
su7ri
wants to merge
16
commits into
home-assistant:main
Choose a base branch
from
su7ri:compose-template-widget-6304
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.
Open
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8ea4572
Migrate template widget configuration to Compose and Material3
su7ri 650e675
Disable the action button while a new template render is in flight
su7ri cd70cee
Merge branch 'main' into compose-template-widget-6304
su7ri b2d06f5
Address review: screenshot test, isBlank, shared parseHtml util, prev…
su7ri 91868ae
Add reference screenshots for TemplateWidgetConfigureScreen
su7ri e440398
Wrap TemplateSection's children in a Column to emit from a single source
claude 0daa207
Fix preview card icon/text colors in dark mode
su7ri 8c80041
Add unit tests for parseHtml
claude e578ca5
Address remaining review feedback on TemplateWidgetConfigureScreen
claude bc72086
Move private constants to the top of their files
claude c0a1e5e
Avoid re-parsing the rendered preview and gate saving on a valid text…
claude d3c5165
Move private ViewModel members to file conventions
claude 71d3de4
Drop unnecessary internal modifier on onTextColorSelected
claude 7b81b8b
Address remaining TimoPtr comments: visibility, tests, screenshots
claude 79dc55d
Move test states to top-level private vals
su7ri b37a35f
Merge branch 'main' into compose-template-widget-6304
su7ri 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 hidden or 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
56 changes: 56 additions & 0 deletions
56
app/src/main/kotlin/io/homeassistant/companion/android/util/compose/HtmlText.kt
This file contains hidden or 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,56 @@ | ||
| package io.homeassistant.companion.android.util.compose | ||
|
|
||
| import android.graphics.Typeface | ||
| import android.text.style.AbsoluteSizeSpan | ||
| import android.text.style.CharacterStyle | ||
| import android.text.style.ForegroundColorSpan | ||
| import android.text.style.RelativeSizeSpan | ||
| import android.text.style.StyleSpan | ||
| import android.text.style.UnderlineSpan | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.text.AnnotatedString | ||
| import androidx.compose.ui.text.SpanStyle | ||
| import androidx.compose.ui.text.buildAnnotatedString | ||
| import androidx.compose.ui.text.font.FontStyle | ||
| import androidx.compose.ui.text.font.FontWeight | ||
| import androidx.compose.ui.text.style.TextDecoration | ||
| import androidx.compose.ui.unit.sp | ||
| import androidx.core.text.HtmlCompat | ||
|
|
||
| private val LINE_BREAK_REGEX = "(\r\n|\r|\n)|(\\\\r\\\\n|\\\\r|\\\\n)".toRegex() | ||
| private const val DEFAULT_RELATIVE_SIZE_SP = 12 | ||
|
|
||
| /** | ||
| * Converts HTML, as returned by a rendered Home Assistant template, into an [AnnotatedString] by | ||
| * translating the [android.text.Spanned] styles produced by [HtmlCompat.fromHtml] into Compose | ||
| * [SpanStyle]s. Shared by every screen that displays a rendered template. | ||
| */ | ||
| fun parseHtml(renderedText: String): AnnotatedString = buildAnnotatedString { | ||
| // Replace both actual and literal (escaped) line break characters with <br> | ||
| val renderedSpanned = HtmlCompat.fromHtml( | ||
| renderedText.replace(LINE_BREAK_REGEX, "<br>"), | ||
| HtmlCompat.FROM_HTML_MODE_LEGACY, | ||
| ) | ||
| append(renderedSpanned.toString()) | ||
| renderedSpanned.getSpans(0, renderedSpanned.length, CharacterStyle::class.java).forEach { span -> | ||
| val start = renderedSpanned.getSpanStart(span) | ||
| val end = renderedSpanned.getSpanEnd(span) | ||
| when (span) { | ||
| is AbsoluteSizeSpan -> addStyle(SpanStyle(fontSize = span.size.sp), start, end) | ||
| is ForegroundColorSpan -> addStyle(SpanStyle(color = Color(span.foregroundColor)), start, end) | ||
| is RelativeSizeSpan -> { | ||
| addStyle(SpanStyle(fontSize = (span.sizeChange * DEFAULT_RELATIVE_SIZE_SP).sp), start, end) | ||
| } | ||
| is StyleSpan -> when (span.style) { | ||
| Typeface.BOLD -> addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end) | ||
| Typeface.ITALIC -> addStyle(SpanStyle(fontStyle = FontStyle.Italic), start, end) | ||
| Typeface.BOLD_ITALIC -> addStyle( | ||
| SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic), | ||
| start, | ||
| end, | ||
| ) | ||
| } | ||
| is UnderlineSpan -> addStyle(SpanStyle(textDecoration = TextDecoration.Underline), start, end) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.