damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit da82663634ae269776b0778be399abd29e650c1d
parent aeafdccb028b7a200f23215eb89844f9f209e688
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Thu,  8 Feb 2024 01:25:36 +0000

Add option for custom test host in Damus Purple developer settings

This commit adds a developer setting that allows the use of a custom
host for testing. This was added to allow testing on real devices
without the need for pushing changes into staging.

========
Testing
========

Test 1: Production not affected
-------------------------------

PASS

Device: iPhone 13 Mini
iOS: 17.3
Damus: This version
Steps:
1. Run app on a device logged into a real Damus Purple account
2. Scroll down the home feed. Make sure that other Purple members still show up with a star next to their profile. PASS
3. Go to the Damus Purple screen. Ensure that account info shows up and is correct. PASS
4. Ensure auto-translations appear. PASS

Test 2: Check custom test URL
-----------------------------

PASS

(Continued from test 1)
1. Run local damus-api and damus-website on the same computer
2. Change developer purple env setting to local test
3. Set the host url to the local IP address of the test server
4. Go through LN purchase flow. Ensure it works correctly. PASS

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Models/Purple/DamusPurpleEnvironment.swift | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mdamus/Views/Settings/DeveloperSettingsView.swift | 33+++++++++++++++++++++++++++++++--
2 files changed, 95 insertions(+), 15 deletions(-)

diff --git 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 @@ -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)