Skip to content

Commit

Permalink
Configure OptimizelyLogLevel based on bundle type (#1007)
Browse files Browse the repository at this point in the history
* Setting OptimizelyLogLevel based on bundle type

* Formatting

* Adding OptimizelyLogLevelType as a file
  • Loading branch information
Isabel Barrera authored Jan 3, 2020
1 parent 4d9ac3e commit a25bc41
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Kickstarter-iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {

self.viewModel.outputs.configureOptimizely
.observeForUI()
.observeValues { [weak self] key in
self?.configureOptimizely(with: key)
.observeValues { [weak self] key, logLevel in
self?.configureOptimizely(with: key, logLevel: logLevel)
}

self.viewModel.outputs.configureAppCenterWithData
Expand Down Expand Up @@ -301,8 +301,8 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {

// MARK: - Functions

private func configureOptimizely(with key: String) {
let optimizelyClient = OptimizelyClient(sdkKey: key)
private func configureOptimizely(with key: String, logLevel: OptimizelyLogLevelType) {
let optimizelyClient = OptimizelyClient(sdkKey: key, defaultLogLevel: logLevel.logLevel)

optimizelyClient.start { [weak self] result in
let shouldUpdateClient = self?.viewModel.inputs.optimizelyConfigured(with: result)
Expand Down
11 changes: 11 additions & 0 deletions Kickstarter-iOS/Library/Optimizely+OptimizelyClientType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ extension OptimizelyResult: OptimizelyResultType {
}
}
}

extension OptimizelyLogLevelType {
public var logLevel: OptimizelyLogLevel {
switch self {
case .error:
return OptimizelyLogLevel.error
case .debug:
return OptimizelyLogLevel.debug
}
}
}
23 changes: 15 additions & 8 deletions Kickstarter-iOS/ViewModels/AppDelegateViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public protocol AppDelegateViewModelOutputs {
var configureFabric: Signal<(), Never> { get }

/// Emits when the application should configure Optimizely
var configureOptimizely: Signal<String, Never> { get }
var configureOptimizely: Signal<(String, OptimizelyLogLevelType), Never> { get }

/// Return this value in the delegate method.
var continueUserActivityReturnValue: MutableProperty<Bool> { get }
Expand Down Expand Up @@ -516,8 +516,8 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
self.configureFabric = self.applicationLaunchOptionsProperty.signal.ignoreValues()

self.configureOptimizely = self.applicationLaunchOptionsProperty.signal
.map { _ in AppEnvironment.current.environmentType }
.map(optimizelySDKKey(for:))
.map { _ in AppEnvironment.current }
.map(optimizelyData(for:))

self.configureAppCenterWithData = Signal.merge(
self.applicationLaunchOptionsProperty.signal.ignoreValues(),
Expand Down Expand Up @@ -740,7 +740,7 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
public let applicationIconBadgeNumber: Signal<Int, Never>
public let configureAppCenterWithData: Signal<AppCenterConfigData, Never>
public let configureFabric: Signal<(), Never>
public let configureOptimizely: Signal<String, Never>
public let configureOptimizely: Signal<(String, OptimizelyLogLevelType), Never>
public let continueUserActivityReturnValue = MutableProperty(false)
public let findRedirectUrl: Signal<URL, Never>
public let forceLogout: Signal<(), Never>
Expand Down Expand Up @@ -954,15 +954,22 @@ extension ShortcutItem {
}
}

private func optimizelySDKKey(for environmentType: EnvironmentType) -> String {
private func optimizelyData(for environment: Environment) -> (String, OptimizelyLogLevelType) {
let environmentType = environment.environmentType
let logLevel = environment.mainBundle.isDebug ? OptimizelyLogLevelType.debug : OptimizelyLogLevelType.error

var sdkKey: String

switch environmentType {
case .production:
return Secrets.OptimizelySDKKey.production
sdkKey = Secrets.OptimizelySDKKey.production
case .staging:
return Secrets.OptimizelySDKKey.staging
sdkKey = Secrets.OptimizelySDKKey.staging
case .development, .local:
return Secrets.OptimizelySDKKey.development
sdkKey = Secrets.OptimizelySDKKey.development
}

return (sdkKey, logLevel)
}

private func visitorCookies() -> [HTTPCookie] {
Expand Down
74 changes: 68 additions & 6 deletions Kickstarter-iOS/ViewModels/AppDelegateViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ final class AppDelegateViewModelTests: TestCase {
fileprivate let applicationIconBadgeNumber = TestObserver<Int, Never>()
fileprivate let configureAppCenterWithData = TestObserver<AppCenterConfigData, Never>()
fileprivate let configureFabric = TestObserver<(), Never>()
fileprivate let configureOptimizely = TestObserver<String, Never>()
fileprivate let configureOptimizelySDKKey = TestObserver<String, Never>()
fileprivate let configureOptimizelyLogLevel = TestObserver<OptimizelyLogLevelType, Never>()
fileprivate let didAcceptReceivingRemoteNotifications = TestObserver<(), Never>()
private let findRedirectUrl = TestObserver<URL, Never>()
fileprivate let forceLogout = TestObserver<(), Never>()
Expand Down Expand Up @@ -44,7 +45,8 @@ final class AppDelegateViewModelTests: TestCase {
self.vm.outputs.applicationIconBadgeNumber.observe(self.applicationIconBadgeNumber.observer)
self.vm.outputs.configureAppCenterWithData.observe(self.configureAppCenterWithData.observer)
self.vm.outputs.configureFabric.observe(self.configureFabric.observer)
self.vm.outputs.configureOptimizely.observe(self.configureOptimizely.observer)
self.vm.outputs.configureOptimizely.map(first).observe(self.configureOptimizelySDKKey.observer)
self.vm.outputs.configureOptimizely.map(second).observe(self.configureOptimizelyLogLevel.observer)
self.vm.outputs.findRedirectUrl.observe(self.findRedirectUrl.observer)
self.vm.outputs.forceLogout.observe(self.forceLogout.observer)
self.vm.outputs.goToActivity.observe(self.goToActivity.observer)
Expand Down Expand Up @@ -127,7 +129,8 @@ final class AppDelegateViewModelTests: TestCase {
withEnvironment(apiService: mockService) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizely.assertValues([Secrets.OptimizelySDKKey.production])
self.configureOptimizelySDKKey
.assertValues([Secrets.OptimizelySDKKey.production])
}
}

Expand All @@ -137,7 +140,64 @@ final class AppDelegateViewModelTests: TestCase {
withEnvironment(apiService: mockService) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizely.assertValues([Secrets.OptimizelySDKKey.staging])
self.configureOptimizelySDKKey
.assertValues([Secrets.OptimizelySDKKey.staging])
}
}

func testConfigureOptimizely_Release() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.release.rawValue,
lang: Language.en.rawValue
)

withEnvironment(mainBundle: mockBundle) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizelyLogLevel
.assertValues([OptimizelyLogLevelType.error])
}
}

func testConfigureOptimizely_Alpha() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.alpha.rawValue,
lang: Language.en.rawValue
)

withEnvironment(mainBundle: mockBundle) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizelyLogLevel
.assertValues([OptimizelyLogLevelType.error])
}
}

func testConfigureOptimizely_Beta() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.beta.rawValue,
lang: Language.en.rawValue
)

withEnvironment(mainBundle: mockBundle) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizelyLogLevel
.assertValues([OptimizelyLogLevelType.error])
}
}

func testConfigureOptimizely_Debug() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.debug.rawValue,
lang: Language.en.rawValue
)

withEnvironment(mainBundle: mockBundle) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizelyLogLevel
.assertValues([OptimizelyLogLevelType.debug])
}
}

Expand All @@ -147,7 +207,8 @@ final class AppDelegateViewModelTests: TestCase {
withEnvironment(apiService: mockService) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizely.assertValues([Secrets.OptimizelySDKKey.staging])
self.configureOptimizelySDKKey
.assertValues([Secrets.OptimizelySDKKey.staging])

let shouldUpdateClient = self.vm.inputs.optimizelyConfigured(with: MockOptimizelyResult())

Expand All @@ -162,7 +223,8 @@ final class AppDelegateViewModelTests: TestCase {
withEnvironment(apiService: mockService) {
self.vm.inputs.applicationDidFinishLaunching(application: UIApplication.shared, launchOptions: nil)

self.configureOptimizely.assertValues([Secrets.OptimizelySDKKey.staging])
self.configureOptimizelySDKKey
.assertValues([Secrets.OptimizelySDKKey.staging])

let shouldUpdateClient = self.vm.inputs.optimizelyConfigured(with: mockResult)

Expand Down
4 changes: 4 additions & 0 deletions Kickstarter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
770E441321137E7400396D46 /* SettingsNotificationPickerCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770E441221137E7400396D46 /* SettingsNotificationPickerCell.swift */; };
770E441521137F3700396D46 /* SettingsNotificationPickerCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 770E441421137F3700396D46 /* SettingsNotificationPickerCell.xib */; };
770E46252114E15D00396D46 /* Crashlytics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 770E45ED2114E15D00396D46 /* Crashlytics.framework */; };
771123FB23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771123FA23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift */; };
771669D0210B93BE007A64A4 /* SettingsNotificationsDataSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771669CF210B93BE007A64A4 /* SettingsNotificationsDataSourceTests.swift */; };
7717805A22F9ED130064A32F /* RewardsCollectionViewFooter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7717805922F9ED130064A32F /* RewardsCollectionViewFooter.swift */; };
771E3C632289DBA8003E7CF1 /* SheetOverlayViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771E3C622289DBA8003E7CF1 /* SheetOverlayViewController.swift */; };
Expand Down Expand Up @@ -1638,6 +1639,7 @@
770E441221137E7400396D46 /* SettingsNotificationPickerCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsNotificationPickerCell.swift; sourceTree = "<group>"; };
770E441421137F3700396D46 /* SettingsNotificationPickerCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SettingsNotificationPickerCell.xib; sourceTree = "<group>"; };
770E45ED2114E15D00396D46 /* Crashlytics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Crashlytics.framework; path = Frameworks/Fabric/Crashlytics.framework; sourceTree = "<group>"; };
771123FA23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptimizelyLogLevelType.swift; sourceTree = "<group>"; };
771669CF210B93BE007A64A4 /* SettingsNotificationsDataSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsNotificationsDataSourceTests.swift; sourceTree = "<group>"; };
7717805922F9ED130064A32F /* RewardsCollectionViewFooter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardsCollectionViewFooter.swift; sourceTree = "<group>"; };
771E3C622289DBA8003E7CF1 /* SheetOverlayViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SheetOverlayViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3319,6 +3321,7 @@
37EB3E4D228CF4FB00076E4C /* NumberFormatterTests.swift */,
D706478F23A2BF4B00B33C20 /* OptimizelyClientType.swift */,
774D98D823A96E7500FC81C2 /* OptimizelyClientTypeTests.swift */,
771123FA23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift */,
1611EF5D23ABD1550051CDCC /* OptimizelyResultType.swift */,
A77D7B061CBAAF5D0077586B /* Paginate.swift */,
A7ED1F1C1E830FDC00BFFA01 /* PaginateTests.swift */,
Expand Down Expand Up @@ -4878,6 +4881,7 @@
9D9F581B1D1324E200CE81DE /* DashboardViewModel.swift in Sources */,
D796867C20FE655300E54C61 /* SettingsFollowCellViewModel.swift in Sources */,
D79A01A42242E8CD004BC6A8 /* AppEnvironmentType.swift in Sources */,
771123FB23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift in Sources */,
A7FC8C061C8F1DEA00C3B49B /* CircleAvatarImageView.swift in Sources */,
771E630C23426B27005967E8 /* CancelPledgeViewModel.swift in Sources */,
A7CA8C0E1D8F241A0086A3E9 /* ProjectNavBarViewModel.swift in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions Library/OptimizelyLogLevelType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

public enum OptimizelyLogLevelType {
case error
case debug
}

0 comments on commit a25bc41

Please sign in to comment.