Source.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Source.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/17.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// Represents an image source setting for Kingfisher methods.
  28. ///
  29. /// A ``Source`` value indicates the way in which the target image can be retrieved and cached.
  30. ///
  31. /// - `network`: The target image should be retrieved from the network remotely. The associated ``Resource``
  32. /// value defines detailed information like image URL and cache key.
  33. /// - `provider`: The target image should be provided in a data format. Normally, it can be an image
  34. /// from local storage or in any other encoding format (like Base64).
  35. ///
  36. public enum Source {
  37. /// Represents the source task identifier when setting an image to a view with extension methods.
  38. public enum Identifier {
  39. /// The underlying value type of source identifier.
  40. public typealias Value = UInt
  41. static private(set) var current: Value = 0
  42. // Not thread safe. Expected to be always called on the main thread.
  43. static func next() -> Value {
  44. assert(Thread.isMainThread, "The identifier `next()` should only be called on main thread.")
  45. current += 1
  46. return current
  47. }
  48. }
  49. // MARK: Member Cases
  50. /// The target image should be fetched from the network remotely. The associated `Resource`
  51. /// value defines detailed information such as the image URL and cache key.
  52. case network(Resource)
  53. /// The target image should be provided in a data format, typically as an image
  54. /// from local storage or in any other encoding format, such as Base64.
  55. case provider(ImageDataProvider)
  56. // MARK: Getting Properties
  57. /// The cache key defined for this source value.
  58. public var cacheKey: String {
  59. switch self {
  60. case .network(let resource): return resource.cacheKey
  61. case .provider(let provider): return provider.cacheKey
  62. }
  63. }
  64. /// The URL defined for this source value.
  65. ///
  66. /// For a ``Source/network(_:)`` source, it is the ``Resource/downloadURL`` of associated ``Resource`` instance.
  67. /// For a ``Source/provider(_:)`` value, it is always `nil`.
  68. public var url: URL? {
  69. switch self {
  70. case .network(let resource): return resource.downloadURL
  71. case .provider(let provider): return provider.contentURL
  72. }
  73. }
  74. }
  75. extension Source: Hashable {
  76. public static func == (lhs: Source, rhs: Source) -> Bool {
  77. switch (lhs, rhs) {
  78. case (.network(let r1), .network(let r2)):
  79. return r1.cacheKey == r2.cacheKey && r1.downloadURL == r2.downloadURL
  80. case (.provider(let p1), .provider(let p2)):
  81. return p1.cacheKey == p2.cacheKey && p1.contentURL == p2.contentURL
  82. case (.provider(_), .network(_)):
  83. return false
  84. case (.network(_), .provider(_)):
  85. return false
  86. }
  87. }
  88. public func hash(into hasher: inout Hasher) {
  89. switch self {
  90. case .network(let r):
  91. hasher.combine(r.cacheKey)
  92. hasher.combine(r.downloadURL)
  93. case .provider(let p):
  94. hasher.combine(p.cacheKey)
  95. hasher.combine(p.contentURL)
  96. }
  97. }
  98. }
  99. extension Source {
  100. var asResource: Resource? {
  101. guard case .network(let resource) = self else {
  102. return nil
  103. }
  104. return resource
  105. }
  106. }