Source.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 setting source for Kingfisher methods.
  28. ///
  29. /// A `Source` value indicates the way how the target image can be retrieved and cached.
  30. ///
  31. /// - network: The target image should be got from network remotely. The associated `Resource`
  32. /// value defines detail 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. public enum Source {
  36. /// Represents the source task identifier when setting an image to a view with extension methods.
  37. public enum Identifier {
  38. /// The underlying value type of source identifier.
  39. public typealias Value = UInt
  40. static private(set) var current: Value = 0
  41. static func next() -> Value {
  42. current += 1
  43. return current
  44. }
  45. }
  46. // MARK: Member Cases
  47. /// The target image should be got from network remotely. The associated `Resource`
  48. /// value defines detail information like image URL and cache key.
  49. case network(Resource)
  50. /// The target image should be provided in a data format. Normally, it can be an image
  51. /// from local storage or in any other encoding format (like Base64).
  52. case provider(ImageDataProvider)
  53. // MARK: Getting Properties
  54. /// The cache key defined for this source value.
  55. public var cacheKey: String {
  56. switch self {
  57. case .network(let resource): return resource.cacheKey
  58. case .provider(let provider): return provider.cacheKey
  59. }
  60. }
  61. /// The URL defined for this source value.
  62. ///
  63. /// For a `.network` source, it is the `downloadURL` of associated `Resource` instance.
  64. /// For a `.provider` value, it is always `nil`.
  65. public var url: URL? {
  66. switch self {
  67. case .network(let resource): return resource.downloadURL
  68. case .provider(let provider): return provider.contentURL
  69. }
  70. }
  71. }
  72. extension Source: Hashable {
  73. public static func == (lhs: Source, rhs: Source) -> Bool {
  74. switch (lhs, rhs) {
  75. case (.network(let r1), .network(let r2)):
  76. return r1.cacheKey == r2.cacheKey && r1.downloadURL == r2.downloadURL
  77. case (.provider(let p1), .provider(let p2)):
  78. return p1.cacheKey == p2.cacheKey && p1.contentURL == p2.contentURL
  79. case (.provider(_), .network(_)):
  80. return false
  81. case (.network(_), .provider(_)):
  82. return false
  83. }
  84. }
  85. public func hash(into hasher: inout Hasher) {
  86. switch self {
  87. case .network(let r):
  88. hasher.combine(r.cacheKey)
  89. hasher.combine(r.downloadURL)
  90. case .provider(let p):
  91. hasher.combine(p.cacheKey)
  92. hasher.combine(p.contentURL)
  93. }
  94. }
  95. }
  96. extension Source {
  97. var asResource: Resource? {
  98. guard case .network(let resource) = self else {
  99. return nil
  100. }
  101. return resource
  102. }
  103. }