Restore last used server and path on cold launch#5145
Conversation
The SwiftUI lifecycle migration dropped scene state restoration, so the app always reopened on the first server at its default page. Persist the last-active server id and a host-agnostic path in SettingsStore and restore them at launch, rebuilding the path against the currently active base URL.
|
Found 5 unused localization strings in the codebase. Click to see detailsTo remove them, run the |
There was a problem hiding this comment.
Pull request overview
This PR restores “cold launch” behavior for the SwiftUI lifecycle by persisting (1) the last active server identifier and (2) a host-agnostic relative path (path?query#fragment) as the web view navigates, then using those values to select the initial server and optionally restore the last page when “Remember Last Page” is enabled.
Changes:
- Persist last-active server ID and relative URL path in
SettingsStorefromWKWebViewnavigation updates. - Restore initial server selection (always) and initial path (only when enabled) during launch via
OnboardingStateObservable. - Thread the restored
initialPaththrough SwiftUI (ContainerView→HomeAssistantView→FrontendView) intoWebViewController, and add unit tests for the restoration logic.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/App/WebView/WebViewControllerTests.swift | Adds unit tests for rebuilding a stored relative path onto the active base URL (needs a small fix for try/XCTUnwrap). |
| Tests/App/Container/OnboardingStateObservableTests.swift | New tests for preferred initial server selection and initial-path restoration gating. |
| Sources/Shared/Settings/SettingsStore.swift | Adds persisted properties for last-active server identifier and last-active URL path. |
| Sources/App/Frontend/WebView/WebViewController/WebViewController+WebViewSetup.swift | Persists server ID and relative path during main web view URL observation. |
| Sources/App/Frontend/WebView/WebViewController/WebViewController+URLLoading.swift | Restores a saved relative path onto the currently resolved base URL on first load; adds restoredURL(base:relativePath:). |
| Sources/App/Frontend/WebView/WebViewController/WebViewController.swift | Adds initialURLPath to carry the restored relative path into initial loading. |
| Sources/App/Frontend/WebView/HomeAssistantView/HomeAssistantViewModel.swift | Adds initialPath to the view model so SwiftUI can pass launch-only restore intent down to the web view host. |
| Sources/App/Frontend/WebView/HomeAssistantView/HomeAssistantView.swift | Plumbs initialPath into FrontendView. |
| Sources/App/Frontend/WebView/FrontendView.swift | Replaces prior restoration input with initialPath, wiring it into WebViewController.initialURLPath. |
| Sources/App/Container/OnboardingStateObservable.swift | Implements launch server selection from persisted ID and optional initial-path restoration based on settings + server match. |
| Sources/App/Container/ContainerView.swift | Passes the initialPath associated value into HomeAssistantView for launch-only restoration. |
The added initialPath initializer used a default argument, which does not satisfy the protocol's init(server:onWebViewController:) requirement. Provide the exact required initializer delegating to the initialPath variant.
Summary
The SwiftUI lifecycle migration removed UIKit scene state restoration, so a cold launch always reopened on the first server at its default page instead of the last one in use.
This persists the last-active server id and a host-agnostic path (path/query/fragment, never the host) in
SettingsStoreas the web view navigates, and restores them at launch. The server is always restored; the last page is restored only when "Remember Last Page" is enabled. The path is rebuilt against whichever base URL is active at load time, so it survives connectivity/location changes (internal, external, remote UI).Screenshots
Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#
Any other notes