Defaults+Subscripts.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 DefaultsProviding {
  26. associatedtype KeyStore: DefaultsKeyStore
  27. subscript<T: DefaultsSerializable>(key key: DefaultsKey<T>) -> T.T where T: OptionalType, T.T == T { get nonmutating set }
  28. subscript<T: DefaultsSerializable>(key key: DefaultsKey<T>) -> T.T where T.T == T { get nonmutating set }
  29. subscript<T: DefaultsSerializable>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T: OptionalType, T.T == T { get nonmutating set }
  30. subscript<T: DefaultsSerializable>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T.T == T { get nonmutating set }
  31. subscript<T: DefaultsSerializable>(dynamicMember keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T: OptionalType, T.T == T { get nonmutating set }
  32. subscript<T: DefaultsSerializable>(dynamicMember keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T.T == T { get nonmutating set }
  33. }
  34. extension DefaultsAdapter: DefaultsProviding {
  35. public subscript<T: DefaultsSerializable>(key key: DefaultsKey<T>) -> T.T where T: OptionalType, T.T == T {
  36. get {
  37. return defaults[key]
  38. }
  39. nonmutating set {
  40. defaults[key] = newValue
  41. }
  42. }
  43. public subscript<T: DefaultsSerializable>(key key: DefaultsKey<T>) -> T.T where T.T == T {
  44. get {
  45. return defaults[key]
  46. }
  47. nonmutating set {
  48. defaults[key] = newValue
  49. }
  50. }
  51. public subscript<T: DefaultsSerializable>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T: OptionalType, T.T == T {
  52. get {
  53. return defaults[keyStore[keyPath: keyPath]]
  54. }
  55. nonmutating set {
  56. defaults[keyStore[keyPath: keyPath]] = newValue
  57. }
  58. }
  59. public subscript<T: DefaultsSerializable>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T.T == T {
  60. get {
  61. return defaults[keyStore[keyPath: keyPath]]
  62. }
  63. nonmutating set {
  64. defaults[keyStore[keyPath: keyPath]] = newValue
  65. }
  66. }
  67. // Weird flex, but needed these two for the dynamicMemberLookup :shrug:
  68. public subscript<T: DefaultsSerializable>(dynamicMember keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T: OptionalType, T.T == T {
  69. get {
  70. return self[keyPath]
  71. }
  72. nonmutating set {
  73. self[keyPath] = newValue
  74. }
  75. }
  76. public subscript<T: DefaultsSerializable>(dynamicMember keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> T.T where T.T == T {
  77. get {
  78. return self[keyPath]
  79. }
  80. nonmutating set {
  81. self[keyPath] = newValue
  82. }
  83. }
  84. }
  85. public extension UserDefaults {
  86. subscript<T: DefaultsSerializable>(key: DefaultsKey<T>) -> T.T where T: OptionalType, T.T == T {
  87. get {
  88. if let value = T._defaults.get(key: key._key, userDefaults: self), let _value = value as? T.T.Wrapped {
  89. // swiftlint:disable:next force_cast
  90. return _value as! T
  91. } else if let defaultValue = key.defaultValue {
  92. return defaultValue
  93. } else {
  94. return T.T.__swifty_empty
  95. }
  96. }
  97. set {
  98. T._defaults.save(key: key._key, value: newValue, userDefaults: self)
  99. }
  100. }
  101. subscript<T: DefaultsSerializable>(key: DefaultsKey<T>) -> T.T where T.T == T {
  102. get {
  103. if let value = T._defaults.get(key: key._key, userDefaults: self) {
  104. return value
  105. } else if let defaultValue = key.defaultValue {
  106. return defaultValue
  107. } else {
  108. fatalError("Shouldn't happen, please report!")
  109. }
  110. }
  111. set {
  112. T._defaults.save(key: key._key, value: newValue, userDefaults: self)
  113. }
  114. }
  115. }