DefaultsObserver.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // SwiftyUserDefaults
  3. //
  4. // Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. import Foundation
  25. public protocol DefaultsDisposable {
  26. func dispose()
  27. }
  28. #if !os(Linux)
  29. public final class DefaultsObserver<T: DefaultsSerializable>: NSObject, DefaultsDisposable {
  30. public struct Update {
  31. public let kind: NSKeyValueChange
  32. public let indexes: IndexSet?
  33. public let isPrior: Bool
  34. public let newValue: T.T?
  35. public let oldValue: T.T?
  36. init(dict: [NSKeyValueChangeKey: Any], key: DefaultsKey<T>) {
  37. // swiftlint:disable:next force_cast
  38. kind = NSKeyValueChange(rawValue: dict[.kindKey] as! UInt)!
  39. indexes = dict[.indexesKey] as? IndexSet
  40. isPrior = dict[.notificationIsPriorKey] as? Bool ?? false
  41. oldValue = Update.deserialize(dict[.oldKey], for: key) ?? key.defaultValue
  42. newValue = Update.deserialize(dict[.newKey], for: key) ?? key.defaultValue
  43. }
  44. private static func deserialize(_ value: Any?, for key: DefaultsKey<T>) -> T.T? {
  45. guard let value = value else { return nil }
  46. let bridge = T._defaults
  47. if bridge.isSerialized() {
  48. return bridge.deserialize(value)
  49. } else {
  50. return value as? T.T
  51. }
  52. }
  53. }
  54. private let key: DefaultsKey<T>
  55. private let userDefaults: UserDefaults
  56. private let handler: ((Update) -> Void)
  57. private var didRemoveObserver = false
  58. init(key: DefaultsKey<T>, userDefaults: UserDefaults, options: NSKeyValueObservingOptions, handler: @escaping ((Update) -> Void)) {
  59. self.key = key
  60. self.userDefaults = userDefaults
  61. self.handler = handler
  62. super.init()
  63. userDefaults.addObserver(self, forKeyPath: key._key, options: options, context: nil)
  64. }
  65. deinit {
  66. dispose()
  67. }
  68. // swiftlint:disable:next block_based_kvo
  69. public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
  70. guard let change = change, object != nil, keyPath == key._key else {
  71. return
  72. }
  73. let update = Update(dict: change, key: key)
  74. handler(update)
  75. }
  76. public func dispose() {
  77. // We use this local property because when you use `removeObserver` when you are
  78. // not actually observing anymore, you'll receive a runtime error.
  79. if didRemoveObserver { return }
  80. didRemoveObserver = true
  81. userDefaults.removeObserver(self, forKeyPath: key._key, context: nil)
  82. }
  83. }
  84. #endif