Skip to content
Snippets Groups Projects

Add user defaults flag for reporting enabled and reporting optional

Merged Bruno Muniz requested to merge feature/reporting-switcher into development
All threads resolved!

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Bruno Muniz requested review from @darrarski.xx

    requested review from @darrarski.xx

  • Bruno Muniz added 1 commit

    added 1 commit

    • 9aed43d1 - Add pre-actions for run and archive on release scheme

    Compare with previous version

    • 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)
            }
          )
        }
      }
  • Bruno Muniz added 1 commit

    added 1 commit

    Compare with previous version

  • Bruno Muniz resolved all threads

    resolved all threads

  • Dariusz Rybicki
  • Bruno Muniz added 1 commit

    added 1 commit

    Compare with previous version

  • Bruno Muniz resolved all threads

    resolved all threads

  • Dariusz Rybicki approved this merge request

    approved this merge request

  • Bruno Muniz mentioned in commit 43cefcf6

    mentioned in commit 43cefcf6

  • merged

  • Please register or sign in to reply
    Loading