Skip to content

Commit

Permalink
Allow changing endpoint from Swift Demo UI
Browse files Browse the repository at this point in the history
  • Loading branch information
matux committed Jun 6, 2023
1 parent b971aa1 commit 279379d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Demos/iosAppSwift/iosAppSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 22 additions & 6 deletions Demos/iosAppSwift/iosAppSwift/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions Demos/iosAppSwift/iosAppSwift/Prelude.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 279379d

Please sign in to comment.