KFImageProtocol.swift 5.3 KB

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