KFImageProtocol.swift 5.3 KB

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