You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.6 KiB

  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 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. // `ImageDataProvider` does not provide a URL. All it cares is how to get the data back.
  69. case .provider(_): return nil
  70. }
  71. }
  72. }
  73. extension Source {
  74. var asResource: Resource? {
  75. guard case .network(let resource) = self else {
  76. return nil
  77. }
  78. return resource
  79. }
  80. var asProvider: ImageDataProvider? {
  81. guard case .provider(let provider) = self else {
  82. return nil
  83. }
  84. return provider
  85. }
  86. }