Skip to content

Commit

Permalink
Merge pull request #37 from Leanplum/fix-env
Browse files Browse the repository at this point in the history
Fix environment selection
  • Loading branch information
nzagorchev authored Oct 19, 2022
2 parents 7874499 + 525a37e commit 4aa9bf0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
25 changes: 13 additions & 12 deletions Rondo/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class AppContext {

var env: LeanplumEnv? = UserDefaults.standard.env {
didSet {
if env != oldValue {
env?.setNetworkConfig()
}
env?.setNetworkConfig()
}
}

Expand All @@ -51,6 +49,14 @@ class AppContext {
}
}
}

var useApiConfig: Bool = UserDefaults.standard.useApiConfig {
didSet {
if useApiConfig != oldValue {
UserDefaults.standard.useApiConfig = useApiConfig
}
}
}

init() {
// defer to force didSet
Expand Down Expand Up @@ -101,15 +107,8 @@ class AppContext {
]
}

if ApiConfig.shared.apiHostName == "api.leanplum.com",
envs.first?.apiHostName != "api.leanplum.com" {
if env == nil && !useApiConfig {
env = envs.first
} else {
env = LeanplumEnv(
apiHostName: ApiConfig.shared.apiHostName,
ssl: true,
socketHostName: ApiConfig.shared.socketHost,
socketPort: ApiConfig.shared.socketPort)
}
}
}
Expand All @@ -119,7 +118,9 @@ class AppContext {
return
}

self.env = env
if !useApiConfig {
self.env = env
}
self.app = app

switch app.mode {
Expand Down
17 changes: 17 additions & 0 deletions Rondo/Data/UserDefaultExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extension UserDefaults {
case env
case useUNUserNotificationCenterDelegate
case logLevel
case useApiConfig
}

static func observerNotificationNameFor(key: DefaultKey) -> Notification.Name {
Expand Down Expand Up @@ -116,4 +117,20 @@ extension UserDefaults {
}
}
}

var useApiConfig: Bool {
get {
if let config = self[.useApiConfig] as? Bool {
return config
}
return false
}
set {
let oldValue = useApiConfig
self[.useApiConfig] = newValue
if newValue != oldValue {
postObserverNotificationFor(key: .useApiConfig)
}
}
}
}
5 changes: 3 additions & 2 deletions Rondo/UI/Apps/AppsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class AppsViewController: FormViewController {
$0.title = app.name
$0.selectableValue = app
$0.value = app == self.context.app ? app : nil
}.onChange{ (row) in
self.context.app = row.value
}
}
section.onSelectSelectableRow = { [weak self] (cell, cellRow) in
self?.context.app = cellRow.value
}

form +++ section
}
Expand Down
6 changes: 4 additions & 2 deletions Rondo/UI/Environments/EnvironmentsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class EnvironmentsViewController: FormViewController {
$0.title = env.apiHostName
$0.selectableValue = env
$0.value = env == self.context.env ? env : nil
}.onChange{ (row) in
self.context.env = row.value
}
}
section.onSelectSelectableRow = { [weak self] (cell, cellRow) in
self?.context.env = cellRow.value
UserDefaults.standard.env = cellRow.value
}

form +++ section
}
Expand Down
14 changes: 14 additions & 0 deletions Rondo/UI/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HomeViewController: FormViewController {
buildApps()
buildAppInfo()
buildSettingsInfo()
buildUseApiConfig()

if #available(iOS 14, *) {
// Objective-C templates
Expand Down Expand Up @@ -227,4 +228,17 @@ class HomeViewController: FormViewController {
let index = form.firstIndex { $0.tag == "info" } ?? 1
form.insert(section, at: index + 1)
}

func buildUseApiConfig() {
let section = Section("Use ApiConfig")

section <<< SwitchRow {
$0.title = "Use ApiConfig"
$0.value = UserDefaults.standard.useApiConfig
}.onChange({ row in
UserDefaults.standard.useApiConfig = row.value!
})

form +++ section
}
}

0 comments on commit 4aa9bf0

Please sign in to comment.