diff --git a/damus/Models/Purple/DamusPurpleEnvironment.swift b/damus/Models/Purple/DamusPurpleEnvironment.swift index 688d0065..6fdc8c08 100644 --- a/damus/Models/Purple/DamusPurpleEnvironment.swift +++ b/damus/Models/Purple/DamusPurpleEnvironment.swift @@ -7,15 +7,17 @@ import Foundation -enum DamusPurpleEnvironment: String, CaseIterable, Codable, Identifiable, StringCodable, Equatable { - case local_test +enum DamusPurpleEnvironment: CaseIterable, Codable, Identifiable, StringCodable, Equatable, Hashable { + static var allCases: [DamusPurpleEnvironment] = [.local_test(host: nil), .staging, .production] + + case local_test(host: String?) case staging case production func text_description() -> String { switch self { case .local_test: - return NSLocalizedString("Test (localhost)", comment: "Label indicating a localhost test environment for Damus Purple functionality (Developer feature)") + return NSLocalizedString("Test (local)", comment: "Label indicating a local test environment for Damus Purple functionality (Developer feature)") case .staging: return NSLocalizedString("Staging", comment: "Label indicating a staging test environment for Damus Purple functionality (Developer feature)") case .production: @@ -25,45 +27,94 @@ enum DamusPurpleEnvironment: String, CaseIterable, Codable, Identifiable, String func api_base_url() -> URL { switch self { - case .local_test: - Constants.PURPLE_API_LOCAL_TEST_BASE_URL + case .local_test(let host): + URL(string: "http://\(host ?? "localhost"):8989") ?? Constants.PURPLE_API_LOCAL_TEST_BASE_URL case .staging: Constants.PURPLE_API_STAGING_BASE_URL case .production: Constants.PURPLE_API_PRODUCTION_BASE_URL + } } func purple_landing_page_url() -> URL { switch self { - case .local_test: - Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL + case .local_test(let host): + URL(string: "http://\(host ?? "localhost"):3000/purple") ?? Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL case .staging: Constants.PURPLE_LANDING_PAGE_STAGING_URL case .production: Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL + } } func damus_website_url() -> URL { switch self { - case .local_test: - Constants.DAMUS_WEBSITE_LOCAL_TEST_URL + case .local_test(let host): + URL(string: "http://\(host ?? "localhost"):3000") ?? Constants.DAMUS_WEBSITE_LOCAL_TEST_URL case .staging: Constants.DAMUS_WEBSITE_STAGING_URL case .production: Constants.DAMUS_WEBSITE_PRODUCTION_URL + + } + } + + func custom_host() -> String? { + switch self { + case .local_test(let host): + return host + default: + return nil } } init?(from string: String) { - guard let initialized = Self.init(rawValue: string) else { return nil } - self = initialized + switch string { + case "local_test": + self = .local_test(host: nil) + case "staging": + self = .staging + case "production": + self = .production + default: + let components = string.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false) + if components.count == 2 && components[0] == "local_test" { + self = .local_test(host: String(components[1])) + } else { + return nil + } + } } func to_string() -> String { - return self.rawValue + switch self { + case .local_test(let host): + if let host { + return "local_test:\(host)" + } + return "local_test" + case .staging: + return "staging" + case .production: + return "production" + } } - var id: String { self.rawValue } + var id: String { + switch self { + case .local_test(let host): + if let host { + return "local_test:\(host)" + } + else { + return "local_test" + } + case .staging: + return "staging" + case .production: + return "production" + } + } } diff --git a/damus/Views/Settings/DeveloperSettingsView.swift b/damus/Views/Settings/DeveloperSettingsView.swift index 674090e0..9f0373c7 100644 --- a/damus/Views/Settings/DeveloperSettingsView.swift +++ b/damus/Views/Settings/DeveloperSettingsView.swift @@ -28,12 +28,41 @@ struct DeveloperSettingsView: View { Toggle(NSLocalizedString("Enable experimental Purple API support", comment: "Developer mode setting to enable experimental Purple API support."), isOn: $settings.enable_experimental_purple_api) .toggleStyle(.switch) - Picker(NSLocalizedString("Damus Purple environment", comment: "Prompt selection of the Damus purple environment (Developer feature to switch between real/production mode to test modes)."), selection: $settings.purple_enviroment) { + Picker(NSLocalizedString("Damus Purple environment", comment: "Prompt selection of the Damus purple environment (Developer feature to switch between real/production mode to test modes)."), + selection: Binding( + get: { () -> DamusPurpleEnvironment in + switch settings.purple_enviroment { + case .local_test(_): + return .local_test(host: nil) // Avoid errors related to a value which is not a valid picker option + default: + return settings.purple_enviroment + } + }, + set: { new_value in + settings.purple_enviroment = new_value + } + ) + ) { ForEach(DamusPurpleEnvironment.allCases, id: \.self) { purple_environment in Text(purple_environment.text_description()) - .tag(purple_environment.rawValue) + .tag(purple_environment.to_string()) } } + + if case .local_test(_) = settings.purple_enviroment { + TextField( + NSLocalizedString("URL", comment: "Custom URL host for Damus Purple testing"), + text: Binding.init( + get: { + return settings.purple_enviroment.custom_host() ?? "" + }, set: { new_host_value in + settings.purple_enviroment = .local_test(host: new_host_value) + } + ) + ) + .disableAutocorrection(true) + .autocapitalization(UITextAutocapitalizationType.none) + } Toggle(NSLocalizedString("Enable experimental Purple In-app purchase support", comment: "Developer mode setting to enable experimental Purple In-app purchase support."), isOn: $settings.enable_experimental_purple_iap_support) .toggleStyle(.switch)