PropertyWrappers.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #if swift(>=5.1)
  25. public struct SwiftyUserDefaultOptions: OptionSet {
  26. public static let cached = SwiftyUserDefaultOptions(rawValue: 1 << 0)
  27. public static let observed = SwiftyUserDefaultOptions(rawValue: 1 << 2)
  28. public let rawValue: Int
  29. public init(rawValue: Int) {
  30. self.rawValue = rawValue
  31. }
  32. }
  33. @propertyWrapper
  34. public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T {
  35. public let key: DefaultsKey<T>
  36. public let options: SwiftyUserDefaultOptions
  37. public var wrappedValue: T {
  38. get {
  39. if options.contains(.cached) {
  40. return _value ?? Defaults[key: key]
  41. } else {
  42. return Defaults[key: key]
  43. }
  44. }
  45. set {
  46. _value = newValue
  47. Defaults[key: key] = newValue
  48. }
  49. }
  50. private var _value: T.T?
  51. private var observation: DefaultsDisposable?
  52. public init<KeyStore>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>, adapter: DefaultsAdapter<KeyStore>, options: SwiftyUserDefaultOptions = []) {
  53. self.key = adapter.keyStore[keyPath: keyPath]
  54. self.options = options
  55. if options.contains(.observed) {
  56. observation = adapter.observe(key) { [weak self] update in
  57. self?._value = update.newValue
  58. }
  59. }
  60. }
  61. public init(keyPath: KeyPath<DefaultsKeys, DefaultsKey<T>>, options: SwiftyUserDefaultOptions = []) {
  62. self.key = Defaults.keyStore[keyPath: keyPath]
  63. self.options = options
  64. if options.contains(.observed) {
  65. observation = Defaults.observe(key) { [weak self] update in
  66. self?._value = update.newValue
  67. }
  68. }
  69. }
  70. deinit {
  71. observation?.dispose()
  72. }
  73. }
  74. #endif