Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class SettingsBundleHelper: NSObject {
  2. ...
  3.  
  4. fileprivate struct Keys {
  5. ...
  6. static let ToggleAnalytics = "TOGGLE_ANALYTICS"
  7. ...
  8. }
  9.  
  10. weak var analyticsDelegate: AnalyticToggleDelegate?
  11.  
  12. public init(enableSwitching: Bool) {
  13. self.enableSwitching = enableSwitching
  14.  
  15. super.init()
  16. setupKVO()
  17. }
  18.  
  19. fileprivate func setupKVO() {
  20. UserDefaults.standard.addObserver(
  21. self,
  22. forKeyPath: Keys.ToggleAnalytics,
  23. options: NSKeyValueObservingOptions.new,
  24. context: nil
  25. )
  26. }
  27.  
  28. public var analyticsEnabled: Bool {
  29. return UserDefaults.standard.bool(forKey: Keys.ToggleAnalytics)
  30. }
  31.  
  32. override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
  33. if analyticsEnabled {
  34. analyticsDelegate?.addAnalyticProviders()
  35. } else {
  36. analyticsDelegate?.removeAnalyticProviders()
  37. }
  38. }
  39.  
  40. deinit {
  41. UserDefaults.standard.removeObserver(self, forKeyPath: Keys.ToggleAnalytics)
  42. }
  43. ...
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement