Add back local profile for tvOS

This commit is contained in:
世界
2026-01-01 19:47:49 +08:00
parent 9b179fe5cd
commit 55e01a89b5
7 changed files with 200 additions and 184 deletions
@@ -6,11 +6,13 @@
/// Using UIButton via UIViewRepresentable bypasses this issue.
struct TVToolbarButton: UIViewRepresentable {
let title: String
var isEnabled: Bool = true
let action: () -> Void
func makeUIView(context: Context) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.isEnabled = isEnabled
button.setContentHuggingPriority(.required, for: .horizontal)
button.setContentCompressionResistancePriority(.required, for: .horizontal)
button.addTarget(context.coordinator, action: #selector(Coordinator.buttonTapped), for: .primaryActionTriggered)
@@ -19,6 +21,7 @@
func updateUIView(_ uiView: UIButton, context: Context) {
uiView.setTitle(title, for: .normal)
uiView.isEnabled = isEnabled
uiView.invalidateIntrinsicContentSize()
uiView.sizeToFit()
context.coordinator.action = action
@@ -1,4 +1,3 @@
#if os(iOS) || os(macOS)
import Foundation
import Library
import SwiftUI
@@ -76,6 +75,18 @@
}
}
.navigationBarTitleDisplayMode(.inline)
#elseif os(tvOS)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
if !readOnly {
TVToolbarButton(title: String(localized: "Save"), isEnabled: viewModel.isChanged) {
Task {
await viewModel.saveContent()
}
}
}
}
}
#endif
}
@@ -104,6 +115,15 @@
@ViewBuilder
private var defaultEditorView: some View {
#if os(tvOS)
ScrollView {
TextField("", text: readOnly ? .constant(viewModel.profileContent) : $viewModel.profileContent, axis: .vertical)
.lineLimit(1000)
.font(Font.system(.caption2, design: .monospaced))
.autocorrectionDisabled(true)
.disabled(readOnly)
}
#else
Group {
if readOnly {
TextEditor(text: .constant(viewModel.profileContent))
@@ -117,7 +137,6 @@
.textContentType(.init(rawValue: ""))
.padding()
#endif
}
}
#endif
}
}
@@ -1,4 +1,3 @@
#if os(iOS) || os(macOS)
import Foundation
import Library
import SwiftUI
@@ -70,5 +69,3 @@
try profile.write(profileContent)
}
}
#endif
@@ -51,8 +51,8 @@ public struct NewProfileView: View {
.multilineTextAlignment(.trailing)
}
Picker(selection: $viewModel.profileType) {
#if !os(tvOS)
Text("Local").tag(ProfileType.local)
#if !os(tvOS)
Text("iCloud").tag(ProfileType.icloud)
#endif
Text("Remote").tag(ProfileType.remote)
@@ -190,6 +190,11 @@ public struct NewProfileView: View {
.navigationTitle("New Profile")
.disabled(viewModel.isSaving)
.alert($viewModel.alert)
.onChangeCompat(of: viewModel.createSucceeded) { newValue in
if newValue {
dismiss()
}
}
#if os(iOS)
.fileImporter(
isPresented: $viewModel.pickerPresented,
@@ -6,6 +6,7 @@ import SwiftUI
@MainActor
public final class NewProfileViewModel: BaseViewModel {
@Published public var isSaving = false
@Published public var createSucceeded = false
@Published public var profileName = ""
#if !os(tvOS)
@Published public var profileType = ProfileType.local
@@ -81,6 +82,7 @@ public final class NewProfileViewModel: BaseViewModel {
if sendUpdateNotification {
environments.profileUpdate.send()
}
createSucceeded = true
dismiss?()
#if os(macOS)
@@ -18,16 +18,14 @@ public struct ProfileActionToolbar: View {
}
public var body: some View {
#if os(iOS)
#if os(iOS) || os(tvOS)
iosBody
#elseif os(tvOS)
tvOSBody
#elseif os(macOS)
macOSBody
#endif
}
#if os(iOS)
#if os(iOS) || os(tvOS)
private var iosBody: some View {
Section("Action") {
if profile.type != .remote {
@@ -49,12 +47,6 @@ public struct ProfileActionToolbar: View {
}
#endif
#if os(tvOS)
private var tvOSBody: some View {
EmptyView()
}
#endif
#if os(macOS)
private var macOSBody: some View {
VStack(spacing: 0) {
@@ -1,4 +1,3 @@
#if os(iOS) || os(macOS)
import SwiftUI
public struct ProfileEditorKey: EnvironmentKey {
@@ -11,4 +10,3 @@
set { self[ProfileEditorKey.self] = newValue }
}
}
#endif