Skip to content

Commit da82663

Browse files
danieldaquinojb55
authored andcommitted
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 <[email protected]> Reviewed-by: William Casarin <[email protected]> Signed-off-by: William Casarin <[email protected]>
1 parent aeafdcc commit da82663

File tree

2 files changed

+95
-15
lines changed

2 files changed

+95
-15
lines changed

damus/Models/Purple/DamusPurpleEnvironment.swift

+64-13
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
import Foundation
99

10-
enum DamusPurpleEnvironment: String, CaseIterable, Codable, Identifiable, StringCodable, Equatable {
11-
case local_test
10+
enum DamusPurpleEnvironment: CaseIterable, Codable, Identifiable, StringCodable, Equatable, Hashable {
11+
static var allCases: [DamusPurpleEnvironment] = [.local_test(host: nil), .staging, .production]
12+
13+
case local_test(host: String?)
1214
case staging
1315
case production
1416

1517
func text_description() -> String {
1618
switch self {
1719
case .local_test:
18-
return NSLocalizedString("Test (localhost)", comment: "Label indicating a localhost test environment for Damus Purple functionality (Developer feature)")
20+
return NSLocalizedString("Test (local)", comment: "Label indicating a local test environment for Damus Purple functionality (Developer feature)")
1921
case .staging:
2022
return NSLocalizedString("Staging", comment: "Label indicating a staging test environment for Damus Purple functionality (Developer feature)")
2123
case .production:
@@ -25,45 +27,94 @@ enum DamusPurpleEnvironment: String, CaseIterable, Codable, Identifiable, String
2527

2628
func api_base_url() -> URL {
2729
switch self {
28-
case .local_test:
29-
Constants.PURPLE_API_LOCAL_TEST_BASE_URL
30+
case .local_test(let host):
31+
URL(string: "http://\(host ?? "localhost"):8989") ?? Constants.PURPLE_API_LOCAL_TEST_BASE_URL
3032
case .staging:
3133
Constants.PURPLE_API_STAGING_BASE_URL
3234
case .production:
3335
Constants.PURPLE_API_PRODUCTION_BASE_URL
36+
3437
}
3538
}
3639

3740
func purple_landing_page_url() -> URL {
3841
switch self {
39-
case .local_test:
40-
Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL
42+
case .local_test(let host):
43+
URL(string: "http://\(host ?? "localhost"):3000/purple") ?? Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL
4144
case .staging:
4245
Constants.PURPLE_LANDING_PAGE_STAGING_URL
4346
case .production:
4447
Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL
48+
4549
}
4650
}
4751

4852
func damus_website_url() -> URL {
4953
switch self {
50-
case .local_test:
51-
Constants.DAMUS_WEBSITE_LOCAL_TEST_URL
54+
case .local_test(let host):
55+
URL(string: "http://\(host ?? "localhost"):3000") ?? Constants.DAMUS_WEBSITE_LOCAL_TEST_URL
5256
case .staging:
5357
Constants.DAMUS_WEBSITE_STAGING_URL
5458
case .production:
5559
Constants.DAMUS_WEBSITE_PRODUCTION_URL
60+
61+
}
62+
}
63+
64+
func custom_host() -> String? {
65+
switch self {
66+
case .local_test(let host):
67+
return host
68+
default:
69+
return nil
5670
}
5771
}
5872

5973
init?(from string: String) {
60-
guard let initialized = Self.init(rawValue: string) else { return nil }
61-
self = initialized
74+
switch string {
75+
case "local_test":
76+
self = .local_test(host: nil)
77+
case "staging":
78+
self = .staging
79+
case "production":
80+
self = .production
81+
default:
82+
let components = string.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false)
83+
if components.count == 2 && components[0] == "local_test" {
84+
self = .local_test(host: String(components[1]))
85+
} else {
86+
return nil
87+
}
88+
}
6289
}
6390

6491
func to_string() -> String {
65-
return self.rawValue
92+
switch self {
93+
case .local_test(let host):
94+
if let host {
95+
return "local_test:\(host)"
96+
}
97+
return "local_test"
98+
case .staging:
99+
return "staging"
100+
case .production:
101+
return "production"
102+
}
66103
}
67104

68-
var id: String { self.rawValue }
105+
var id: String {
106+
switch self {
107+
case .local_test(let host):
108+
if let host {
109+
return "local_test:\(host)"
110+
}
111+
else {
112+
return "local_test"
113+
}
114+
case .staging:
115+
return "staging"
116+
case .production:
117+
return "production"
118+
}
119+
}
69120
}

damus/Views/Settings/DeveloperSettingsView.swift

+31-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,41 @@ struct DeveloperSettingsView: View {
2828
Toggle(NSLocalizedString("Enable experimental Purple API support", comment: "Developer mode setting to enable experimental Purple API support."), isOn: $settings.enable_experimental_purple_api)
2929
.toggleStyle(.switch)
3030

31-
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) {
31+
Picker(NSLocalizedString("Damus Purple environment", comment: "Prompt selection of the Damus purple environment (Developer feature to switch between real/production mode to test modes)."),
32+
selection: Binding(
33+
get: { () -> DamusPurpleEnvironment in
34+
switch settings.purple_enviroment {
35+
case .local_test(_):
36+
return .local_test(host: nil) // Avoid errors related to a value which is not a valid picker option
37+
default:
38+
return settings.purple_enviroment
39+
}
40+
},
41+
set: { new_value in
42+
settings.purple_enviroment = new_value
43+
}
44+
)
45+
) {
3246
ForEach(DamusPurpleEnvironment.allCases, id: \.self) { purple_environment in
3347
Text(purple_environment.text_description())
34-
.tag(purple_environment.rawValue)
48+
.tag(purple_environment.to_string())
3549
}
3650
}
51+
52+
if case .local_test(_) = settings.purple_enviroment {
53+
TextField(
54+
NSLocalizedString("URL", comment: "Custom URL host for Damus Purple testing"),
55+
text: Binding.init(
56+
get: {
57+
return settings.purple_enviroment.custom_host() ?? ""
58+
}, set: { new_host_value in
59+
settings.purple_enviroment = .local_test(host: new_host_value)
60+
}
61+
)
62+
)
63+
.disableAutocorrection(true)
64+
.autocapitalization(UITextAutocapitalizationType.none)
65+
}
3766

3867
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)
3968
.toggleStyle(.switch)

0 commit comments

Comments
 (0)