Skip to content

Commit 591f5a6

Browse files
CopilotbgoncalCopilot
authored
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

4 files changed

+110
-264
lines changed

Sources/App/Home/HomeView.swift

Lines changed: 0 additions & 149 deletions
This file was deleted.

Sources/App/Home/HomeViewModel.swift

Lines changed: 0 additions & 93 deletions
This file was deleted.

Sources/App/WebView/ExperimentalSpace/Home/HomeView.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ struct HomeView: View {
8686
}
8787
}
8888

89+
private func switchToServer(_ server: Server) {
90+
guard server.identifier != viewModel.server.identifier else { return }
91+
viewModel.switchToServer(server)
92+
}
93+
8994
// MARK: - Content Views
9095

9196
private var contentView: some View {
@@ -420,6 +425,24 @@ struct HomeView: View {
420425
} label: {
421426
Label(L10n.HomeView.Menu.settings, systemSymbol: .gearshape)
422427
}
428+
429+
if Current.servers.all.count > 1 {
430+
Section(L10n.ServersSelection.title) {
431+
ForEach(Current.servers.all, id: \.identifier) { server in
432+
Button {
433+
switchToServer(server)
434+
} label: {
435+
HStack {
436+
Text(server.info.name)
437+
if viewModel.server.identifier == server.identifier {
438+
Spacer()
439+
Image(systemSymbol: .checkmark)
440+
}
441+
}
442+
}
443+
}
444+
}
445+
}
423446
} label: {
424447
Image(systemSymbol: .ellipsis)
425448
}

0 commit comments

Comments
 (0)