Commit 591f5a6
Add server switcher to experimental HomeView toolbar menu (#4227)
## Summary
Adds a submenu to the experimental HomeView toolbar that displays
available servers below the Settings option. When multiple servers are
configured, users can switch between them directly from the toolbar
menu, with the currently selected server indicated by a checkmark.
Server switching logic is implemented using the ViewModel pattern for
proper separation of concerns and state management.
## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#
## Any other notes
### Implementation Details
**HomeView Changes**
(`Sources/App/WebView/ExperimentalSpace/Home/HomeView.swift`):
- Modified the "..." toolbar menu to include a "Servers" section below
Settings
- Server list only appears when multiple servers are configured
- Checkmark indicates the currently active server
- `switchToServer` method delegates to ViewModel for server switching
```swift
if Current.servers.all.count > 1 {
Section("Servers") {
ForEach(Current.servers.all, id: \.identifier) { server in
Button { switchToServer(server) } label: {
HStack {
Text(server.info.name)
if viewModel.server.identifier == server.identifier {
Spacer()
Image(systemSymbol: .checkmark)
}
}
}
}
}
}
private func switchToServer(_ server: Server) {
guard server.identifier != viewModel.server.identifier else { return }
viewModel.switchToServer(server)
}
```
**HomeViewModel Changes**
(`Sources/App/WebView/ExperimentalSpace/Home/HomeViewModel.swift`):
- Server switching logic moved to ViewModel for better architecture
- `server` property with `didSet` observer triggers
`handleServerChange()` when server changes
- `switchToServer(_ newServer: Server)` public method for initiating
server switches
- `handleServerChange()` private method that:
- Stops all current subscriptions
- Resets all state (entities, configuration, etc.)
- Loads configuration for the new server
- Reloads entities automatically
**Architecture Improvements**:
- **Separation of Concerns**: Server switching logic resides in
ViewModel instead of View
- **State Management**: Proper state reset and cleanup when switching
servers
- **Data Reloading**: Automatic entity and configuration reload for new
server
- **Subscription Management**: Stops old server subscriptions before
switching
- **Code Organization**: Improved ViewModel structure with clear section
markers
Single server configurations see no change in behavior - the menu
continues to show existing options without the server list.
<!-- START COPILOT CODING AGENT SUFFIX -->
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
> In HomeView toolbar, blow settings, add a submenu to switch between
Current.servers.all
</details>
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/home-assistant/iOS/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent b621410 commit 591f5a6
File tree
4 files changed
+110
-264
lines changed- Sources/App
- Home
- WebView/ExperimentalSpace/Home
4 files changed
+110
-264
lines changedThis file was deleted.
This file was deleted.
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
| |||
420 | 425 | | |
421 | 426 | | |
422 | 427 | | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
423 | 446 | | |
424 | 447 | | |
425 | 448 | | |
| |||
0 commit comments