DefaultsObserver.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 where T == T.T {
  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<T: DefaultsSerializable>(_ value: Any?, for key: DefaultsKey<T>) -> T.T? where T.T == T {
  45. guard let value = value else { return nil }
  46. let deserialized = T._defaults.deserialize(value)
  47. let ret: T.T?
  48. if key.isOptional, let _deserialized = deserialized, let __deserialized = _deserialized as? OptionalTypeCheck, !__deserialized.isNil {
  49. ret = __deserialized as? T.T
  50. } else if !key.isOptional {
  51. ret = deserialized ?? value as? T.T
  52. } else {
  53. ret = value as? T.T
  54. }
  55. return ret
  56. }
  57. }
  58. private let key: DefaultsKey<T>
  59. private let userDefaults: UserDefaults
  60. private let handler: ((Update) -> Void)
  61. private var didRemoveObserver = false
  62. init(key: DefaultsKey<T>, userDefaults: UserDefaults, options: NSKeyValueObservingOptions, handler: @escaping ((Update) -> Void)) {
  63. self.key = key
  64. self.userDefaults = userDefaults
  65. self.handler = handler
  66. super.init()
  67. userDefaults.addObserver(self, forKeyPath: key._key, options: options, context: nil)
  68. }
  69. deinit {
  70. dispose()
  71. }
  72. // swiftlint:disable:next block_based_kvo
  73. public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
  74. guard let change = change, object != nil, keyPath == key._key else {
  75. return
  76. }
  77. let update = Update(dict: change, key: key)
  78. handler(update)
  79. }
  80. public func dispose() {
  81. // We use this local property because when you use `removeObserver` when you are
  82. // not actually observing anymore, you'll receive a runtime error.
  83. if didRemoveObserver { return }
  84. didRemoveObserver = true
  85. userDefaults.removeObserver(self, forKeyPath: key._key, context: nil)
  86. }
  87. }
  88. #endif