From 490ada02d6ca91dda502ad233ddfe38de0ea55a5 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 4 Oct 2023 09:24:36 +0800 Subject: [PATCH] Add the self hosting options to the Macos app --- .../App/Views/SelfHostSettingsView.swift | 197 +++++++++++------- .../Sources/App/Views/WelcomeView.swift | 10 +- 2 files changed, 120 insertions(+), 87 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/SelfHostSettingsView.swift b/apple/OmnivoreKit/Sources/App/Views/SelfHostSettingsView.swift index d9a0c0ba7..837650c10 100644 --- a/apple/OmnivoreKit/Sources/App/Views/SelfHostSettingsView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/SelfHostSettingsView.swift @@ -1,96 +1,133 @@ -#if os(iOS) - import Models - import Services - import SwiftUI - import Utils - import Views - class SelfHostSettingsViewModel: ObservableObject { - @State var showCreateError = false +import Models +import Services +import SwiftUI +import Utils +import Views + +class SelfHostSettingsViewModel: ObservableObject { + @State var showCreateError = false +} + +struct SelfHostSettingsView: View { + @State var apiServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.serverBaseURL.rawValue) ?? "" + @State var webServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.webAppBaseURL.rawValue) ?? "" + @State var ttsServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.ttsBaseURL.rawValue) ?? "" + + @State var showConfirmAlert = false + + @Environment(\.dismiss) private var dismiss + + @EnvironmentObject var dataService: DataService + @StateObject var viewModel = SelfHostSettingsViewModel() + + var allFieldsSet: Bool { + apiServerAddress.count > 0 && webServerAddress.count > 0 && ttsServerAddress.count > 0 } - struct SelfHostSettingsView: View { - @State var apiServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.serverBaseURL.rawValue) ?? "" - @State var webServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.webAppBaseURL.rawValue) ?? "" - @State var ttsServerAddress = UserDefaults.standard.string(forKey: AppEnvironmentUserDefaultKey.ttsBaseURL.rawValue) ?? "" + var saveButton: some View { + Button(action: { + showConfirmAlert = true + }, label: { + Text(LocalText.genericSave) + }) + .disabled(!allFieldsSet) + } - @State var showConfirmAlert = false + var cancelButton: some View { + Button(action: { + dismiss() + }, label: { + Text(LocalText.cancelGeneric) + }) + } - @Environment(\.dismiss) private var dismiss - - @EnvironmentObject var dataService: DataService - @StateObject var viewModel = SelfHostSettingsViewModel() - - var allFieldsSet: Bool { - apiServerAddress.count > 0 && webServerAddress.count > 0 && ttsServerAddress.count > 0 - } - - var saveButton: some View { - Button(action: { - showConfirmAlert = true - }, label: { - Text(LocalText.genericSave) - }) - .disabled(!allFieldsSet) - } - - var body: some View { - Form { - Section("API Server Base URL") { - TextField("URL", text: $apiServerAddress, prompt: Text("https://api-prod.omnivore.app")) - .keyboardType(.URL) - .autocorrectionDisabled(true) - .textInputAutocapitalization(.never) - } - - Section("Web Server URL") { - TextField("URL", text: $webServerAddress, prompt: Text("https://omnivore.app")) - .keyboardType(.URL) - .autocorrectionDisabled(true) - .textInputAutocapitalization(.never) - } - - Section("Text-to-speech Server URL") { - TextField("URL", text: $ttsServerAddress, prompt: Text("https://tts.omnivore.app")) - .keyboardType(.URL) - .autocorrectionDisabled(true) - .textInputAutocapitalization(.never) - } - - Section { - Section { - Text(""" - Omnivore is a free and open-source project and allows self-hosting. - - If you have chosen to deploy your own server instance, fill in the \ - above fields to connect to your private self-hosted instance. - - [Learn more about self-hosting Omnivore](https://docs.omnivore.app/self-hosting/self-hosting.html) - """) - .accentColor(.blue) - } - } + var body: some View { + #if os(iOS) + NavigationView { + innerBody } - .accentColor(.appGrayText) - .alert(isPresented: $showConfirmAlert) { - Alert( - title: Text("Changing your environment settings will close the app."), - dismissButton: .cancel(Text(LocalText.genericOk)) { - AppEnvironment.setCustom(serverBaseURL: apiServerAddress, - webAppBaseURL: webServerAddress, - ttsBaseURL: ttsServerAddress) - dataService.switchAppEnvironment(appEnvironment: AppEnvironment.custom) + #elseif os(macOS) + innerBody + #endif + } + + var innerBody: some View { + ScrollView { + VStack { + Text("API Server Base URL").frame(maxWidth: .infinity, alignment: .leading) + TextField("URL", text: $apiServerAddress, prompt: Text("https://api-prod.omnivore.app")) + .autocorrectionDisabled(true) + #if os(iOS) + .keyboardType(.URL) + .textInputAutocapitalization(.never) + #endif + + Text("Web Server URL").frame(maxWidth: .infinity, alignment: .leading) + TextField("URL", text: $webServerAddress, prompt: Text("https://omnivore.app")) + .autocorrectionDisabled(true) + #if os(iOS) + .keyboardType(.URL) + .textInputAutocapitalization(.never) + #endif + + Text("Text-to-speech Server URL").frame(maxWidth: .infinity, alignment: .leading) + TextField("URL", text: $ttsServerAddress, prompt: Text("https://tts.omnivore.app")) + .autocorrectionDisabled(true) + #if os(iOS) + .keyboardType(.URL) + .textInputAutocapitalization(.never) + #endif + + Text(""" + Omnivore is a free and open-source project and allows self-hosting. + + If you have chosen to deploy your own server instance, fill in the \ + above fields to connect to your private self-hosted instance. + + [Learn more about self-hosting Omnivore](https://docs.omnivore.app/self-hosting/self-hosting.html) + """) + .accentColor(.blue) + .frame(maxWidth: .infinity, alignment: .leading) + + #if os(macOS) + Spacer() + HStack { + cancelButton + Spacer() + saveButton } - ) + .frame(maxWidth: .infinity, alignment: .center) + .padding() + #endif } + } + .padding() + .frame(maxWidth: .infinity, maxHeight: .infinity) + + .accentColor(.appGrayText) + .alert(isPresented: $showConfirmAlert) { + Alert( + title: Text("Changing your environment settings will close the app."), + dismissButton: .cancel(Text(LocalText.genericOk)) { + AppEnvironment.setCustom(serverBaseURL: apiServerAddress, + webAppBaseURL: webServerAddress, + ttsBaseURL: ttsServerAddress) + dataService.switchAppEnvironment(appEnvironment: AppEnvironment.custom) + } + ) + } + .navigationTitle("Self-hosting Options") + #if os(iOS) .navigationViewStyle(.stack) - .navigationTitle("Self-hosting Options") .navigationBarTitleDisplayMode(.inline) .navigationBarItems(leading: Button(action: { dismiss() }, label: { Text(LocalText.cancelGeneric) }), trailing: saveButton) - } + #else + .frame(minWidth: 400, minHeight: 600) + #endif } -#endif +} diff --git a/apple/OmnivoreKit/Sources/App/Views/WelcomeView.swift b/apple/OmnivoreKit/Sources/App/Views/WelcomeView.swift index 06c56f3d5..bba4802ba 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WelcomeView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WelcomeView.swift @@ -248,13 +248,9 @@ struct WelcomeView: View { .sheet(isPresented: $showDebugModal) { DebugMenuView(selectedEnvironment: $selectedEnvironment) } - #if os(iOS) - .sheet(isPresented: $showAdvancedLogin) { - NavigationView { - SelfHostSettingsView() - } - } - #endif + .sheet(isPresented: $showAdvancedLogin) { + SelfHostSettingsView() + } .alert(deletedAccountConfirmationMessage, isPresented: $authenticator.showAppleRevokeTokenAlert) { Button("View Details") { openURL(URL(string: "https://support.apple.com/en-us/HT210426")!)