Skip to content

Migrate template widget configuration to Compose and Material3 - #7288

Open
su7ri wants to merge 6 commits into
home-assistant:mainfrom
su7ri:compose-template-widget-6304
Open

Migrate template widget configuration to Compose and Material3#7288
su7ri wants to merge 6 commits into
home-assistant:mainfrom
su7ri:compose-template-widget-6304

Conversation

@su7ri

@su7ri su7ri commented Aug 1, 2026

Copy link
Copy Markdown

Summary

Migrates TemplateWidgetConfigureActivity from a legacy XML/View Binding
layout to Jetpack Compose with Material 3, following the same pattern
established for the entity widget in #7007 (referenced from #6307 as the
example to follow for this series of widget migrations).

TemplateWidgetConfigureActivity.kt is now a thin wrapper (~60 lines,
down from ~250) around:

  • TemplateWidgetConfigureState: immutable UI state, including a
    TemplatePreview sealed type (Empty/Rendered/Error) replacing the old
    single TextView that mixed rendered output, error messages, and the
    "blank" placeholder.
  • TemplateWidgetConfigureViewModel: the restore/save/render logic,
    moved out of the Activity into a Hilt ViewModel with StateFlow.
  • TemplateWidgetConfigureScreen: the Compose UI, reusing the shared
    WidgetBackgroundTypeDropdown / WidgetTextColorDropdown composables.

The live template preview still renders basic HTML from the server
response; the HTML→AnnotatedString conversion follows the same approach
already used on the Wear OS template tile screen
(SettingsWearTemplateTile.kt), duplicated here rather than extracted
to keep this PR focused on the widget migration. Happy to extract it to
a shared util in a follow-up if maintainers prefer.

Closes #6304

Checklist

  • New or updated tests have been added to cover the changes following the testing guidelines.
  • The code follows the project's code style and best_practices.
  • The changes have been thoroughly tested, and edge cases have been considered.
  • Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.
  • I have read the Open Home Foundation AI Policy.

Select exactly one option that describes AI usage in this contribution:

  • I have not used AI for this contribution.
  • AI assistance was used for this contribution.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

Screenshots

Screenshot 2026-08-01 at 10 28 48

Any other notes

Part of the ongoing effort to migrate widget configuration screens to
Compose/Material 3 (see #6303, #6305, #6306, #6308, and others).

Copilot AI review requested due to automatic review settings August 1, 2026 09:30

@home-assistant home-assistant Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @su7ri

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant

home-assistant Bot commented Aug 1, 2026

Copy link
Copy Markdown

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
home-assistant Bot marked this pull request as draft August 1, 2026 09:30
@su7ri
su7ri marked this pull request as ready for review August 1, 2026 09:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Not ready to approve

The ViewModel can leave a stale “Rendered” preview while a new render is in-flight, allowing users to save an unvalidated template/server selection before rendering completes.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

Migrates the Template widget configuration flow from a legacy XML/ViewBinding Activity to a Compose + Material3 screen driven by an assisted Hilt ViewModel, aligning the widget configuration architecture with the recent entity widget migration pattern.

Changes:

  • Replaced the legacy TemplateWidgetConfigureActivity UI with a Compose screen (TemplateWidgetConfigureScreen) under HATheme.
  • Introduced TemplateWidgetConfigureViewModel + immutable TemplateWidgetConfigureState (including a TemplatePreview sealed type) to own restore/render/save logic via StateFlow.
  • Added focused unit tests for restore/render/save behavior; removed the obsolete XML layout.
File summaries
File Description
app/src/main/kotlin/io/homeassistant/companion/android/widgets/template/TemplateWidgetConfigureActivity.kt Converts the Activity into a thin Compose wrapper that delegates logic to the ViewModel.
app/src/main/kotlin/io/homeassistant/companion/android/widgets/template/TemplateWidgetConfigureScreen.kt New Compose UI for configuring the widget, including HTML preview rendering.
app/src/main/kotlin/io/homeassistant/companion/android/widgets/template/TemplateWidgetConfigureState.kt Adds immutable UI state and a sealed preview model for rendered/empty/error states.
app/src/main/kotlin/io/homeassistant/companion/android/widgets/template/TemplateWidgetConfigureViewModel.kt New assisted Hilt ViewModel handling restore/render/save and widget pin/update flows.
app/src/test/kotlin/io/homeassistant/companion/android/widgets/template/TemplateWidgetConfigureViewModelTest.kt Adds unit tests for state restoration, rendering, and persistence behavior.
app/src/main/res/layout/widget_template_configure.xml Removes the legacy XML configuration layout.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

@jpelgrom jpelgrom left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up!

Could you add a screenshot test as well? You already have a Compose preview so it shouldn't be too difficult :)

The live template preview still renders basic HTML from the server response; the HTML→AnnotatedString conversion follows the same approach already used on the Wear OS template tile screen (SettingsWearTemplateTile.kt), duplicated here rather than extracted to keep this PR focused on the widget migration. Happy to extract it to a shared util in a follow-up if maintainers prefer.

As it is only ~30 lines you can extract it into a util file in this PR instead of duplicating code.

Fixes an Android Lint (Slack compose-lints) finding: composables should
only emit content from one source at their top level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rework UI of TemplateWidgetConfigureActivity to compose and Material3

5 participants