Add user defaults flag for reporting enabled and reporting optional
All threads resolved!
All threads resolved!
Merge request reports
Activity
requested review from @darrarski.xx
assigned to @brunomunizaf
added 1 commit
- 9aed43d1 - Add pre-actions for run and archive on release scheme
- Resolved by Bruno Muniz
It's ok, but we can improve it. What about creating a
ReportingStatus
service, that will be the single source of truth for the feature status? It can look like this:First, the
ReportingStatusIsOptional
functor that reads value from info plist:public struct ReportingStatusIsOptional { public var get: () -> Bool } extension ReportingStatusIsOptional { public static func live( plist url: URL = Bundle.main.url(forResource: "Info", withExtension: "plist")! ) -> ReportingStatusIsOptional { ReportingStatusIsOptional { struct Plist: Decodable { let isReportingOptional : Bool } guard let data = try? Data(contentsOf: url), let infoPlist = try? PropertyListDecoder().decode(Plist.self, from: data) else { return true } return infoPlist.isReportingOptional } } }
Second, the
ReportingStatusIsEnabled
functor that allows to get, set, and observe value from user defaults:public struct ReportingStatusIsEnabled { public var get: () -> Bool public var set: (Bool) -> Void public var publisher: () -> AnyPublisher<Bool, Never> } extension ReportingStatusIsEnabled { public static func live(userDefaults: UserDefaults = .standard) -> ReportingStatusIsEnabled { ReportingStatusIsEnabled( get: { userDefaults.isReportingEnabled }, set: { enabled in userDefaults.isReportingEnabled = enabled }, publisher: { userDefaults.publisher(for: \.isReportingEnabled).eraseToAnyPublisher() } ) } } private extension UserDefaults { static let isReportingEnabledKey = "isReportingEnabled" @objc var isReportingEnabled: Bool { get { bool(forKey: Self.isReportingEnabledKey) } set { set(newValue, forKey: Self.isReportingEnabledKey) } } }
Finally, the
ReportingStatus
functor which uses two of the above, and provides a single source of truth if the feature should be enabled or not (isEnabled
&isEnabledPublisher
), as well as if we need to present the switcher (isOptional
), and we can use it to enable the feature too (enable
).public struct ReportingStatus { public var isOptional: () -> Bool public var isEnabled: () -> Bool public var isEnabledPublisher: () -> AnyPublisher<Bool, Never> public var enable: (Bool) -> Void } extension ReportingStatus { public static func live( isOptional: ReportingStatusIsOptional = .live(), isEnabled: ReportingStatusIsEnabled = .live() ) -> ReportingStatus { ReportingStatus( isOptional: { isOptional.get() }, isEnabled: { if isOptional.get() == false { return true } return isEnabled.get() }, isEnabledPublisher: { if isOptional.get() == false { return Just(true).eraseToAnyPublisher() } return isEnabled.publisher() }, enable: { enabled in isEnabled.set(enabled) } ) } }
- Resolved by Bruno Muniz
- Resolved by Bruno Muniz
mentioned in commit 43cefcf6
Please register or sign in to reply