Files
iOS/Sources/App/Settings/Components/YamlPreviewSection.swift
Bruno Pantaleão Gonçalves 7cbdcb762f Remove legacy actions (#4584)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## 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
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-06-04 15:20:24 +02:00

119 lines
3.7 KiB
Swift

import Shared
import SwiftUI
import UIKit
/// A reusable SwiftUI section that renders a YAML preview with a share button.
///
/// Replaces the Eureka `YamlSection` used across settings forms. The YAML value
/// is passed in via `yaml`; callers are responsible for rebuilding the string
/// in response to form changes (typically via a derived computed property).
struct YamlPreviewSection: View {
let header: String
let shareTitle: String
let yaml: String
@State private var showShareSheet = false
@State private var showYamlSheet = false
init(
header: String,
shareTitle: String = L10n.YamlPreview.share,
yaml: String
) {
self.header = header
self.shareTitle = shareTitle
self.yaml = yaml
}
var body: some View {
Section(header: Text(header)) {
Button {
showYamlSheet = true
} label: {
HStack(alignment: .top) {
Text(yaml)
.font(.system(.caption, design: .monospaced))
.foregroundColor(.secondary)
.lineLimit(6)
.frame(maxWidth: .infinity, alignment: .leading)
Image(systemSymbol: .chevronRight)
.font(.caption)
.foregroundColor(.secondary)
}
}
Button {
showShareSheet = true
} label: {
Label {
Text(shareTitle)
.foregroundColor(.primary)
} icon: {
Image(systemSymbol: .squareAndArrowUp)
}
}
}
.sheet(isPresented: $showShareSheet) {
YamlShareSheet(activityItems: [yaml])
}
.sheet(isPresented: $showYamlSheet) {
YamlCodePreviewView(yaml: yaml)
}
}
}
/// A SwiftUI wrapper around `UIActivityViewController` used to share YAML or
/// other plain text content from settings screens.
struct YamlShareSheet: UIViewControllerRepresentable {
let activityItems: [Any]
var applicationActivities: [UIActivity]?
func makeUIViewController(context: Context) -> UIActivityViewController {
UIActivityViewController(
activityItems: activityItems,
applicationActivities: applicationActivities
)
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
}
/// Full-screen readable YAML preview, presented as a sheet from
/// `YamlPreviewSection`. Provides a copy-to-pasteboard button and close action.
struct YamlCodePreviewView: View {
let yaml: String
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationView {
ScrollView {
Text(yaml)
.font(.system(.body, design: .monospaced))
.textSelection(.enabled)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
.background(Color(UIColor.secondarySystemBackground))
.navigationTitle("YAML")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.closeLabel) {
dismiss()
}
}
ToolbarItem(placement: .primaryAction) {
Button {
UIPasteboard.general.string = yaml
} label: {
Label(L10n.Nfc.Detail.copy, systemSymbol: .docOnDoc)
}
}
}
}
.navigationViewStyle(.stack)
}
}