Source.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Source.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/17.
  6. //
  7. // Copyright (c) 2018 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 the source task identifier when setting an image to a view with extension methods.
  28. public enum SourceIdentifier {
  29. /// The underlying value type of source identifier.
  30. public typealias Value = UInt
  31. static var current: Value = 0
  32. static func next() -> Value {
  33. current += 1
  34. return current
  35. }
  36. }
  37. /// Represents an image setting source for Kingfisher method.
  38. ///
  39. /// A `Source` value indicates the way how the target image can be retrieved and cached.
  40. ///
  41. /// - network: The target image should be got from network remotely. The associated `Resource`
  42. /// value defines detail information like image URL and cache key.
  43. /// - provider: The target image should be provided in a data format. Normally, it can be an image
  44. /// from local storage or in any other encoding format (like Base64).
  45. public enum Source {
  46. /// The target image should be got from network remotely. The associated `Resource`
  47. /// value defines detail information like image URL and cache key.
  48. case network(Resource)
  49. /// The target image should be provided in a data format. Normally, it can be an image
  50. /// from local storage or in any other encoding format (like Base64).
  51. case provider(ImageDataProvider)
  52. public var cacheKey: String {
  53. switch self {
  54. case .network(let resource): return resource.cacheKey
  55. case .provider(let provider): return provider.cacheKey
  56. }
  57. }
  58. public var url: URL? {
  59. switch self {
  60. case .network(let resource): return resource.downloadURL
  61. // `ImageDataProvider` does not provide a URL. All it cares is how to get the data back.
  62. case .provider(_): return nil
  63. }
  64. }
  65. }