From a25bc413ebf1bcc97fc164325fc0c1294c0e86f9 Mon Sep 17 00:00:00 2001 From: Isabel Barrera Date: Fri, 3 Jan 2020 12:21:07 -0500 Subject: [PATCH] Configure OptimizelyLogLevel based on bundle type (#1007) * Setting OptimizelyLogLevel based on bundle type * Formatting * Adding OptimizelyLogLevelType as a file --- Kickstarter-iOS/AppDelegate.swift | 8 +- .../Optimizely+OptimizelyClientType.swift | 11 +++ .../ViewModels/AppDelegateViewModel.swift | 23 ++++-- .../AppDelegateViewModelTests.swift | 74 +++++++++++++++++-- Kickstarter.xcodeproj/project.pbxproj | 4 + Library/OptimizelyLogLevelType.swift | 6 ++ 6 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 Library/OptimizelyLogLevelType.swift diff --git a/Kickstarter-iOS/AppDelegate.swift b/Kickstarter-iOS/AppDelegate.swift index 36d7cafe96..5311eb2260 100644 --- a/Kickstarter-iOS/AppDelegate.swift +++ b/Kickstarter-iOS/AppDelegate.swift @@ -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 @@ -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) diff --git a/Kickstarter-iOS/Library/Optimizely+OptimizelyClientType.swift b/Kickstarter-iOS/Library/Optimizely+OptimizelyClientType.swift index 643303a89b..0ea49a7678 100644 --- a/Kickstarter-iOS/Library/Optimizely+OptimizelyClientType.swift +++ b/Kickstarter-iOS/Library/Optimizely+OptimizelyClientType.swift @@ -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 + } + } +} diff --git a/Kickstarter-iOS/ViewModels/AppDelegateViewModel.swift b/Kickstarter-iOS/ViewModels/AppDelegateViewModel.swift index e11f4b8314..b27e660309 100644 --- a/Kickstarter-iOS/ViewModels/AppDelegateViewModel.swift +++ b/Kickstarter-iOS/ViewModels/AppDelegateViewModel.swift @@ -95,7 +95,7 @@ public protocol AppDelegateViewModelOutputs { var configureFabric: Signal<(), Never> { get } /// Emits when the application should configure Optimizely - var configureOptimizely: Signal { get } + var configureOptimizely: Signal<(String, OptimizelyLogLevelType), Never> { get } /// Return this value in the delegate method. var continueUserActivityReturnValue: MutableProperty { get } @@ -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(), @@ -740,7 +740,7 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi public let applicationIconBadgeNumber: Signal public let configureAppCenterWithData: Signal public let configureFabric: Signal<(), Never> - public let configureOptimizely: Signal + public let configureOptimizely: Signal<(String, OptimizelyLogLevelType), Never> public let continueUserActivityReturnValue = MutableProperty(false) public let findRedirectUrl: Signal public let forceLogout: Signal<(), Never> @@ -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] { diff --git a/Kickstarter-iOS/ViewModels/AppDelegateViewModelTests.swift b/Kickstarter-iOS/ViewModels/AppDelegateViewModelTests.swift index 9cff55c200..3c4f323330 100644 --- a/Kickstarter-iOS/ViewModels/AppDelegateViewModelTests.swift +++ b/Kickstarter-iOS/ViewModels/AppDelegateViewModelTests.swift @@ -16,7 +16,8 @@ final class AppDelegateViewModelTests: TestCase { fileprivate let applicationIconBadgeNumber = TestObserver() fileprivate let configureAppCenterWithData = TestObserver() fileprivate let configureFabric = TestObserver<(), Never>() - fileprivate let configureOptimizely = TestObserver() + fileprivate let configureOptimizelySDKKey = TestObserver() + fileprivate let configureOptimizelyLogLevel = TestObserver() fileprivate let didAcceptReceivingRemoteNotifications = TestObserver<(), Never>() private let findRedirectUrl = TestObserver() fileprivate let forceLogout = TestObserver<(), Never>() @@ -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) @@ -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]) } } @@ -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]) } } @@ -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()) @@ -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) diff --git a/Kickstarter.xcodeproj/project.pbxproj b/Kickstarter.xcodeproj/project.pbxproj index c8dbf63399..62b1d2a36c 100644 --- a/Kickstarter.xcodeproj/project.pbxproj +++ b/Kickstarter.xcodeproj/project.pbxproj @@ -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 */; }; @@ -1638,6 +1639,7 @@ 770E441221137E7400396D46 /* SettingsNotificationPickerCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsNotificationPickerCell.swift; sourceTree = ""; }; 770E441421137F3700396D46 /* SettingsNotificationPickerCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SettingsNotificationPickerCell.xib; sourceTree = ""; }; 770E45ED2114E15D00396D46 /* Crashlytics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Crashlytics.framework; path = Frameworks/Fabric/Crashlytics.framework; sourceTree = ""; }; + 771123FA23BF9E7E00BA5702 /* OptimizelyLogLevelType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptimizelyLogLevelType.swift; sourceTree = ""; }; 771669CF210B93BE007A64A4 /* SettingsNotificationsDataSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsNotificationsDataSourceTests.swift; sourceTree = ""; }; 7717805922F9ED130064A32F /* RewardsCollectionViewFooter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardsCollectionViewFooter.swift; sourceTree = ""; }; 771E3C622289DBA8003E7CF1 /* SheetOverlayViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SheetOverlayViewController.swift; sourceTree = ""; }; @@ -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 */, @@ -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 */, diff --git a/Library/OptimizelyLogLevelType.swift b/Library/OptimizelyLogLevelType.swift new file mode 100644 index 0000000000..9cd64eefe7 --- /dev/null +++ b/Library/OptimizelyLogLevelType.swift @@ -0,0 +1,6 @@ +import Foundation + +public enum OptimizelyLogLevelType { + case error + case debug +}