2
0

KFImageProtocol.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // KFImageProtocol.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2021/05/08.
  6. //
  7. // Copyright (c) 2021 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. #if canImport(SwiftUI) && canImport(Combine)
  27. import SwiftUI
  28. import Combine
  29. /// Represents a view that is compatible with Kingfisher in SwiftUI.
  30. ///
  31. /// As a framework user, you do not need to know the details of this protocol. As the public types, ``KFImage`` and
  32. /// ``KFAnimatedImage`` conform this type and should be used in your app to represent an image view with network and
  33. /// cache support in SwiftUI.
  34. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  35. public protocol KFImageProtocol: View, KFOptionSetter {
  36. associatedtype HoldingView: KFImageHoldingView
  37. var context: KFImage.Context<HoldingView> { get set }
  38. init(context: KFImage.Context<HoldingView>)
  39. }
  40. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  41. extension KFImageProtocol {
  42. public var body: some View {
  43. ZStack {
  44. KFImageRenderer<HoldingView>(
  45. context: context
  46. ).id(context)
  47. }
  48. }
  49. /// Creates an image view compatible with Kingfisher for loading an image from the provided `Source`.
  50. ///
  51. /// - Parameters:
  52. /// - source: The `Source` of the image that specifies where to load the target image.
  53. public init(source: Source?) {
  54. let context = KFImage.Context<HoldingView>(source: source)
  55. self.init(context: context)
  56. }
  57. /// Creates an image view compatible with Kingfisher for loading an image from the provided `URL`.
  58. ///
  59. /// - Parameters:
  60. /// - url: The `URL` defining the location from which to load the target image.
  61. public init(_ url: URL?) {
  62. self.init(source: url?.convertToSource())
  63. }
  64. /// Configures the current image with a `block` and returns another `Image` to use as the final content.
  65. ///
  66. /// This block will be lazily applied when creating the final `Image`.
  67. ///
  68. /// If multiple `configure` modifiers are added to the image, they will be evaluated in order.
  69. ///
  70. /// - Parameter block: The block that applies to the loaded image. The block should return an `Image` that is
  71. /// configured.
  72. /// - Returns: A ``KFImage`` or ``KFAnimatedImage`` view that configures the internal `Image` with the provided
  73. /// `block`.
  74. ///
  75. /// > If you want to configure the input image (which is usually an `Image` value) and use a non-`Image` value as
  76. /// > the configured result, use ``KFImageProtocol/contentConfigure(_:)`` instead.
  77. public func configure(_ block: @escaping (HoldingView) -> HoldingView) -> Self {
  78. context.configurations.append(block)
  79. return self
  80. }
  81. /// Configures the current image with a `block` and returns a `View` to use as the final content.
  82. ///
  83. /// This block will be lazily applied when creating the final `Image`.
  84. ///
  85. /// If multiple `contentConfigure` modifiers are added to the image, only the last one will be stored and used.
  86. ///
  87. /// - Parameter block: The block applies to the loaded image. The block should return a `View` that is configured.
  88. /// - Returns: A ``KFImage`` or ``KFAnimatedImage`` view that configures the internal `Image` with the provided
  89. /// `block`.
  90. public func contentConfigure<V: View>(_ block: @escaping (HoldingView) -> V) -> Self {
  91. context.contentConfiguration = { AnyView(block($0)) }
  92. return self
  93. }
  94. }
  95. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  96. public protocol KFImageHoldingView: View {
  97. associatedtype RenderingView
  98. static func created(from image: KFCrossPlatformImage?, context: KFImage.Context<Self>) -> Self
  99. }
  100. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  101. extension KFImageProtocol {
  102. public var options: KingfisherParsedOptionsInfo {
  103. get { context.options }
  104. nonmutating set { context.options = newValue }
  105. }
  106. public var onFailureDelegate: Delegate<KingfisherError, Void> { context.onFailureDelegate }
  107. public var onSuccessDelegate: Delegate<RetrieveImageResult, Void> { context.onSuccessDelegate }
  108. public var onProgressDelegate: Delegate<(Int64, Int64), Void> { context.onProgressDelegate }
  109. public var delegateObserver: AnyObject { context }
  110. }
  111. #endif