From 279379d69d3b80ad8fbe5474da9de217655e6fae Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 6 Jun 2023 16:23:36 -0300 Subject: [PATCH] Allow changing endpoint from Swift Demo UI --- .../iosAppSwift.xcodeproj/project.pbxproj | 4 +-- .../iosAppSwift/iosAppSwift/ContentView.swift | 28 +++++++++++++++---- Demos/iosAppSwift/iosAppSwift/Prelude.swift | 6 ---- .../iosAppSwift/iosAppSwiftApp.swift | 7 ++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Demos/iosAppSwift/iosAppSwift.xcodeproj/project.pbxproj b/Demos/iosAppSwift/iosAppSwift.xcodeproj/project.pbxproj index 3293718c..3b9a1925 100644 --- a/Demos/iosAppSwift/iosAppSwift.xcodeproj/project.pbxproj +++ b/Demos/iosAppSwift/iosAppSwift.xcodeproj/project.pbxproj @@ -284,7 +284,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 61; + CURRENT_PROJECT_VERSION = 70; DEVELOPMENT_ASSET_PATHS = "\"iosAppSwift/Preview Content\""; DEVELOPMENT_TEAM = 9P5JVC2F34; ENABLE_PREVIEWS = YES; @@ -317,7 +317,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 61; + CURRENT_PROJECT_VERSION = 70; DEVELOPMENT_ASSET_PATHS = "\"iosAppSwift/Preview Content\""; DEVELOPMENT_TEAM = 9P5JVC2F34; ENABLE_PREVIEWS = YES; diff --git a/Demos/iosAppSwift/iosAppSwift/ContentView.swift b/Demos/iosAppSwift/iosAppSwift/ContentView.swift index 097ab81c..9598c889 100644 --- a/Demos/iosAppSwift/iosAppSwift/ContentView.swift +++ b/Demos/iosAppSwift/iosAppSwift/ContentView.swift @@ -10,6 +10,7 @@ enum ExampleError: Error { } struct ContentView: View { + @AppStorage("rollbar_endpoint") var endpoint = "https://api.rollbar.com/api/1/item/" @AppStorage("rollbar_post_client_item_access_token") var accessToken = "" let example = Example() @@ -20,16 +21,22 @@ struct ContentView: View { .tint(.blue) } - func restartIfValid(_ accessToken: String) { - guard accessToken.isValid else { return } + func accessTokenIsValid(_ accessToken: String) -> Bool { + #"(^[a-f0-9]{32}$)"#.matches(in: accessToken)?.count == 1 + } + + func restartIfValid(_ endpoint: String, _ accessToken: String) { + guard accessTokenIsValid(accessToken) else { return } let config = Rollbar.configuration().mutableCopy() + config.destination.endpoint = endpoint config.destination.accessToken = accessToken Rollbar.update(withConfiguration: config) - Rollbar.infoMessage("Rollbar Apple SDK access token changed.") + Rollbar.infoMessage("Rollbar Apple SDK access token and/or endpoint changed.") - print("Rollbar Apple SDK access token changed to \(accessToken)") + print("[Rollbar Demo] Updated endpoint: \(endpoint)") + print("[Rollbar Demo] Updated access token: \(accessToken)") } var body: some View { @@ -38,13 +45,22 @@ struct ContentView: View { .font(.title) .padding(.bottom) + TextField("endpoint", text: $endpoint) + .foregroundColor(.accentColor) + .multilineTextAlignment(.center) + .textFieldStyle(.roundedBorder) + .textContentType(.URL) + .textCase(.lowercase) + .lineLimit(1) + .onChange(of: endpoint) { endpoint in restartIfValid(endpoint, accessToken) } + TextField("post client item access token", text: $accessToken) - .foregroundColor(accessToken.isValid ? .accentColor : .red) + .foregroundColor(accessTokenIsValid(accessToken) ? .accentColor : .red) .textFieldStyle(.roundedBorder) .multilineTextAlignment(.center) .textCase(.lowercase) .lineLimit(1) - .onChange(of: accessToken, perform: restartIfValid) + .onChange(of: accessToken) { accessToken in restartIfValid(endpoint, accessToken) } .padding(.bottom) ScrollView { diff --git a/Demos/iosAppSwift/iosAppSwift/Prelude.swift b/Demos/iosAppSwift/iosAppSwift/Prelude.swift index 97827926..8a479804 100644 --- a/Demos/iosAppSwift/iosAppSwift/Prelude.swift +++ b/Demos/iosAppSwift/iosAppSwift/Prelude.swift @@ -1,11 +1,5 @@ import Foundation -extension String { - var isValid: Bool { - #"(^[a-f0-9]{32}$)"#.matches(in: self)?.count == 1 - } -} - extension StringProtocol { /// Returns an array of `Substring`s by matching the given `String` using diff --git a/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift b/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift index c198dd9a..aad4f6cd 100644 --- a/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift +++ b/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift @@ -13,6 +13,7 @@ struct iosAppSwiftApp: App { } class AppDelegate: NSObject, UIApplicationDelegate { + @AppStorage("rollbar_endpoint") var endpoint = "https://api.rollbar.com/api/1/item/" @AppStorage("rollbar_post_client_item_access_token") var accessToken = "" func application( @@ -29,11 +30,15 @@ class AppDelegate: NSObject, UIApplicationDelegate { withAccessToken: accessToken, environment: environment) + // The config endpoint already defaults to `api.rollbar.com` so setting it is unnecessary. + // This is an implementation detail of this application and should very rarely be needed. + config.destination.endpoint = endpoint + config.loggingOptions.codeVersion = codeVersion // Optionally defined whether rate limited occurrences should be dropped or // kept in a queue. Defaults to drop. - config.loggingOptions.rateLimitBehavior = .queue + config.loggingOptions.rateLimitBehavior = .drop // or .queue // Optionally anonymize the IP address //config.loggingOptions.captureIp = RollbarCaptureIpType.anonymize