Source.swift 4.4 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: Sendable {
  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. @MainActor static private(set) var current: Value = 0
  42. // Not thread safe. Expected to be always called on the main thread.
  43. @MainActor static func next() -> Value {
  44. current += 1
  45. return current
  46. }
  47. }
  48. // MARK: Member Cases
  49. /// The target image should be fetched from the network remotely. The associated `Resource`
  50. /// value defines detailed information such as the image URL and cache key.
  51. case network(any Resource)
  52. /// The target image should be provided in a data format, typically as an image
  53. /// from local storage or in any other encoding format, such as Base64.
  54. case provider(any ImageDataProvider)
  55. // MARK: Getting Properties
  56. /// The cache key defined for this source value.
  57. public var cacheKey: String {
  58. switch self {
  59. case .network(let resource): return resource.cacheKey
  60. case .provider(let provider): return provider.cacheKey
  61. }
  62. }
  63. /// The URL defined for this source value.
  64. ///
  65. /// For a ``Source/network(_:)`` source, it is the ``Resource/downloadURL`` of associated ``Resource`` instance.
  66. /// For a ``Source/provider(_:)`` value, it is always `nil`.
  67. public var url: URL? {
  68. switch self {
  69. case .network(let resource): return resource.downloadURL
  70. case .provider(let provider): return provider.contentURL
  71. }
  72. }
  73. }
  74. extension Source: Hashable {
  75. public static func == (lhs: Source, rhs: Source) -> Bool {
  76. switch (lhs, rhs) {
  77. case (.network(let r1), .network(let r2)):
  78. return r1.cacheKey == r2.cacheKey && r1.downloadURL == r2.downloadURL
  79. case (.provider(let p1), .provider(let p2)):
  80. return p1.cacheKey == p2.cacheKey && p1.contentURL == p2.contentURL
  81. case (.provider(_), .network(_)):
  82. return false
  83. case (.network(_), .provider(_)):
  84. return false
  85. }
  86. }
  87. public func hash(into hasher: inout Hasher) {
  88. switch self {
  89. case .network(let r):
  90. hasher.combine(r.cacheKey)
  91. hasher.combine(r.downloadURL)
  92. case .provider(let p):
  93. hasher.combine(p.cacheKey)
  94. hasher.combine(p.contentURL)
  95. }
  96. }
  97. }
  98. extension Source {
  99. var asResource: (any Resource)? {
  100. guard case .network(let resource) = self else {
  101. return nil
  102. }
  103. return resource
  104. }
  105. }