DamusPurpleEnvironment.swift (3861B)
1 // 2 // DamusPurpleEnvironment.swift 3 // damus 4 // 5 // Created by Daniel D’Aquino on 2024-01-29. 6 // 7 8 import Foundation 9 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?) 14 case staging 15 case production 16 17 func text_description() -> String { 18 switch self { 19 case .local_test: 20 return NSLocalizedString("Test (local)", comment: "Label indicating a local test environment for Damus Purple functionality (Developer feature)") 21 case .staging: 22 return NSLocalizedString("Staging", comment: "Label indicating a staging test environment for Damus Purple functionality (Developer feature)") 23 case .production: 24 return NSLocalizedString("Production", comment: "Label indicating the production environment for Damus Purple") 25 } 26 } 27 28 func api_base_url() -> URL { 29 switch self { 30 case .local_test(let host): 31 URL(string: "http://\(host ?? "localhost"):8989") ?? Constants.PURPLE_API_LOCAL_TEST_BASE_URL 32 case .staging: 33 Constants.PURPLE_API_STAGING_BASE_URL 34 case .production: 35 Constants.PURPLE_API_PRODUCTION_BASE_URL 36 37 } 38 } 39 40 func purple_landing_page_url() -> URL { 41 switch self { 42 case .local_test(let host): 43 URL(string: "http://\(host ?? "localhost"):3000/purple") ?? Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL 44 case .staging: 45 Constants.PURPLE_LANDING_PAGE_STAGING_URL 46 case .production: 47 Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL 48 49 } 50 } 51 52 func damus_website_url() -> URL { 53 switch self { 54 case .local_test(let host): 55 URL(string: "http://\(host ?? "localhost"):3000") ?? Constants.DAMUS_WEBSITE_LOCAL_TEST_URL 56 case .staging: 57 Constants.DAMUS_WEBSITE_STAGING_URL 58 case .production: 59 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 70 } 71 } 72 73 init?(from string: String) { 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 } 89 } 90 91 func to_string() -> String { 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 } 103 } 104 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 } 120 }