iOS/Sources/App/Servers/ServerSelectView.swift
Copilot b8898cc6fd
Migrate SettingsViewController to SwiftUI with NavigationSplitView on macOS (#3968)
## Summary

Migrates SettingsViewController from UIKit/Eureka to SwiftUI. macOS uses
NavigationSplitView (sidebar + detail), iOS uses NavigationStack with
list navigation.

**Changes:**
- **SettingsView.swift** (new): SwiftUI implementation with
platform-specific navigation
- macOS: Sidebar with setting categories, NavigationStack in detail pane
  - iOS: List-based navigation with NavigationStack
- Platform filtering: macOS hides gestures, watch, CarPlay, NFC, help,
whatsNew
- Supports `contentSections` parameter for filtering displayed sections
(defaults to `.all`)
- ServersObserver for real-time server updates via ServerObserver
protocol
- Wrapper views embed UIKit controllers not yet migrated (location,
notifications, NFC, complications, actions)

- **SettingsItem.swift**: Enhanced with `visibleCases(for:)` method to
filter items based on contentSections parameter

- **SettingsSceneDelegate.swift**: Simplified from 189→36 lines
  - Returns SwiftUI SettingsView for all platforms
  - Removed UINavigationController management, NSToolbar delegate

- **WebViewController.swift**, **WebViewExternalMessageHandler.swift**,
**ConnectionSecurityLevelBlockView.swift**: Updated to present SwiftUI
SettingsView

- **SettingsViewController.swift**: Deleted (253 lines removed) - all
functionality migrated to SwiftUI

Example usage with content filtering:
```swift
// Show all sections (default)
SettingsView()

// Show only servers section
SettingsView(contentSections: .servers)
```

Example navigation on macOS:
```swift
NavigationSplitView {
    List(selection: $selectedItem) {
        ForEach(SettingsItem.visibleCases(for: contentSections)) { item in
            NavigationLink(value: item) {
                Label(item.title) { item.icon }
            }
        }
    }
} detail: {
    NavigationStack {
        if let selectedItem {
            selectedItem.destinationView
        }
    }
}
```

## Screenshots


## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#

## Any other notes
Complete migration from UIKit/Eureka to SwiftUI with full feature parity
including contentSections support. UIKit controllers
(SettingsDetailViewController, NotificationSettingsViewController, etc.)
remain embedded via `embed()` function until individually migrated.

<!-- START COPILOT CODING AGENT SUFFIX -->



<details>

<summary>Original prompt</summary>

> Migrate SettingsViewController to SwiftUI, on macOS it should be a
split view (sidebar + content). If needed to embed UIKit controllers in
the middle use "embed(<UIViewController>)"


</details>



<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
2025-11-13 00:40:05 +00:00

164 lines
5.0 KiB
Swift

import Shared
import SwiftUI
struct ServerSelectView: View {
@Environment(\.dismiss) private var dismiss
@State private var showSheet = true
let prompt: String?
let includeSettings: Bool
let selectAction: (Server) -> Void
var body: some View {
VStack {}
.background(Color.clear)
.sheet(isPresented: $showSheet) {
if #available(iOS 16.0, *) {
content
.presentationDetents([.medium, .large])
.presentationDragIndicator(.hidden)
} else {
content
}
}
.onChange(of: showSheet) { newValue in
if !newValue {
dismiss()
}
}
}
private var content: some View {
NavigationView {
List {
if let prompt {
Section {
Text(prompt)
.font(.footnote)
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
.padding(.horizontal)
}
}
Section {
ForEach(Current.servers.all, id: \.identifier) { server in
ServerSelectViewRow(server: server) {
selectAction(server)
dismiss()
}
}
}
}
.navigationViewStyle(.stack)
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(L10n.ServersSelection.title)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
CloseButton {
showSheet = false
}
}
ToolbarItem(placement: .topBarLeading) {
if includeSettings {
SettingsButton(tint: Color.haPrimary) {
dismiss()
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise)
.done { controller in
controller.showSettingsViewController()
}
}
}
}
}
}
}
}
struct ServerSelectViewRow: View {
@State private var userName: String = ""
@State private var profilePictureURL: URL?
@State private var selected = false
let server: Server
let action: () -> Void
var body: some View {
Button(action: {
action()
}, label: {
HStack(spacing: DesignSystem.Spaces.two) {
profilePicture
VStack {
Group {
Text(server.info.name)
.font(.headline)
.foregroundStyle(Color(uiColor: .label))
Text(userName)
.font(.caption)
.foregroundStyle(Color(uiColor: .secondaryLabel))
}
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
}
Image(systemSymbol: selected ? .checkmarkCircleFill : .circle)
}
})
.tint(Color.haPrimary)
.onAppear {
updateSelectionIndicator()
loadUserNameAndProfilePicture()
}
}
private var profilePicture: some View {
AsyncImage(url: profilePictureURL) { image in
image
.resizable()
} placeholder: {
ZStack {
Image(systemSymbol: .circleFill)
.resizable()
Text(String(userName.first ?? Character(" ")))
.foregroundStyle(.white)
.font(.body.bold())
}
}
.frame(width: 40, height: 40)
.clipShape(Circle())
.overlay(
Circle()
.stroke(Color.haPrimary, lineWidth: 2)
)
}
private func updateSelectionIndicator() {
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise).done { controller in
selected = controller.server == server
}
}
private func loadUserNameAndProfilePicture() {
Current.api(for: server)?.connection.caches.user.once { user in
userName = user.name.orEmpty
}
Current.api(for: server)?.profilePictureURL { url in
profilePictureURL = url
}
}
}
#Preview("Servers") {
ServerSelectView(prompt: nil, includeSettings: true) { _ in }
}
#Preview("Servers with prompt") {
ServerSelectView(prompt: "Are you sure?", includeSettings: false) { _ in }
}
#Preview("Rows") {
List {
ServerSelectViewRow(server: ServerFixture.standard) {}
}
}