Files
VirtualBuddy/VirtualUI/Source/Settings/PreferencesView.swift
2024-03-22 11:19:11 -03:00

197 lines
5.7 KiB
Swift

//
// PreferencesView.swift
// VirtualBuddy
//
// Created by Guilherme Rambo on 05/06/22.
//
import SwiftUI
import VirtualCore
import DeepLinkSecurity
/// This is a shell that handles showing the correct version depending on the OS and setting the library path,
/// which is the only preference in common between legacy OS (Monterey) and modern OSes (Ventura and later).
public struct PreferencesView: View {
var deepLinkSentinel: () -> DeepLinkSentinel
public init(deepLinkSentinel: @escaping @autoclosure () -> DeepLinkSentinel) {
self.deepLinkSentinel = deepLinkSentinel
}
@EnvironmentObject var container: VBSettingsContainer
private var settings: VBSettings {
get { container.settings }
nonmutating set { container.settings = newValue }
}
@State private var libraryPathText = ""
public var body: some View {
Group {
ModernSettingsView(libraryPathText: $libraryPathText, setLibraryPath: setLibraryPath, showOpenPanel: showOpenPanel)
.environmentObject(deepLinkSentinel())
}
.alert("Error", isPresented: $isShowingErrorAlert, actions: {
Button("OK") { isShowingErrorAlert = false }
}, message: {
Text(errorMessage ?? "")
})
.onChange(of: settings.libraryURL) { newValue in
libraryPathText = newValue.path
}
.onAppearOnce {
libraryPathText = settings.libraryURL.path
}
}
@State private var isShowingErrorAlert = false
@State private var errorMessage: String?
private func setLibraryPath(to newValue: String) {
let url = URL(fileURLWithPath: newValue)
if let errorMessage = url.performWriteTest() {
libraryPathText = settings.libraryURL.path
isShowingErrorAlert = true
self.errorMessage = errorMessage
} else {
settings.libraryURL = url
libraryPathText = url.path
self.errorMessage = nil
}
}
private func showOpenPanel() {
guard let newURL = NSOpenPanel.run(accepting: [.folder], directoryURL: settings.libraryURL, defaultDirectoryKey: "library") else {
return
}
guard newURL != settings.libraryURL else { return }
setLibraryPath(to: newURL.path)
}
}
/// Settings view for modern OSes (Ventura and later).
@available(macOS 13.0, *)
private struct ModernSettingsView: View {
@Binding var libraryPathText: String
var setLibraryPath: (String) -> Void
var showOpenPanel: () -> Void
@EnvironmentObject var container: VBSettingsContainer
@EnvironmentObject var sentinel: DeepLinkSentinel
private var settings: VBSettings {
get { container.settings }
nonmutating set { container.settings = newValue }
}
@State private var showingAutomationSecuritySheet = false
var body: some View {
Form {
Section {
LabeledContent("Location") {
HStack {
TextField("", text: $libraryPathText)
.onSubmit {
setLibraryPath(libraryPathText)
}
Button {
showOpenPanel()
} label: {
Image(systemName: "ellipsis.circle")
}
.buttonStyle(.borderless)
.help("Choose")
}
.labelsHidden()
}
} header: {
Text("Library Storage")
} footer: {
Text("This is where VirtualBuddy will store your virtual machines and installer images downloaded within the app.")
.foregroundStyle(.secondary)
}
Section {
LabeledContent("Control which apps can automate VirtualBuddy") {
Button {
showingAutomationSecuritySheet = true
} label: {
Image(systemName: "ellipsis.circle")
}
.buttonStyle(.borderless)
}
} header: {
Text("Automation")
}
}
.formStyle(.grouped)
.sheet(isPresented: $showingAutomationSecuritySheet) {
automationSecuritySheet
}
}
@ViewBuilder
private var automationSecuritySheet: some View {
DeepLinkAuthManagementUI(sentinel: sentinel)
}
}
extension URL {
func performWriteTest() -> String? {
if !FileManager.default.fileExists(atPath: path) {
return "The directory \(lastPathComponent) doesn't exist."
}
do {
let testFileURL = appendingPathComponent(".vbtest-\(UUID().uuidString)")
guard FileManager.default.createFile(atPath: testFileURL.path, contents: nil) else {
throw CocoaError(.fileWriteNoPermission)
}
try FileManager.default.removeItem(at: testFileURL)
return nil
} catch {
return "VirtualBuddy is unable to write files to the directory \(lastPathComponent). Please check the permissions for that directory or choose a different one."
}
}
}
struct LibraryPathError: LocalizedError {
var errorDescription: String?
init(_ msg: String) { self.errorDescription = msg }
}
// MARK: - Previews
#if DEBUG
private extension VBSettingsContainer {
static let preview: VBSettingsContainer = {
VBSettingsContainer(with: UserDefaults())
}()
}
@available(macOS 14.0, *)
#Preview("Settings") {
PreferencesView(deepLinkSentinel: .preview)
.environmentObject(VBSettingsContainer.preview)
}
#endif