CPListItem+Kingfisher.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // CPListItem+Kingfisher.swift
  3. // Kingfisher
  4. //
  5. // Created by Wayne Hartman on 2021-08-29.
  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. #if canImport(CarPlay) && !targetEnvironment(macCatalyst)
  27. import CarPlay
  28. @available(iOS 14.0, *)
  29. @MainActor
  30. extension KingfisherWrapper where Base: CPListItem {
  31. // MARK: Setting Image
  32. /// Sets an image to the image view with a source.
  33. ///
  34. /// - Parameters:
  35. /// - source: The `Source` object contains information about the image.
  36. /// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
  37. /// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
  38. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  39. /// `expectedContentLength`, this block will not be called.
  40. /// - completionHandler: Called when the image retrieved and set finished.
  41. /// - Returns: A task represents the image downloading.
  42. ///
  43. /// - Note:
  44. ///
  45. /// Internally, this method will use `KingfisherManager` to get the requested source
  46. /// Since this method will perform UI changes, you must call it from the main thread.
  47. /// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
  48. ///
  49. @discardableResult
  50. public func setImage(
  51. with source: Source?,
  52. placeholder: KFCrossPlatformImage? = nil,
  53. options: KingfisherOptionsInfo? = nil,
  54. progressBlock: DownloadProgressBlock? = nil,
  55. completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
  56. {
  57. let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? []))
  58. return setImage(
  59. with: source,
  60. placeholder: placeholder,
  61. parsedOptions: options,
  62. progressBlock: progressBlock,
  63. completionHandler: completionHandler
  64. )
  65. }
  66. /// Sets an image to the image view with a requested resource.
  67. ///
  68. /// - Parameters:
  69. /// - resource: The `Resource` object contains information about the image.
  70. /// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
  71. /// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
  72. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  73. /// `expectedContentLength`, this block will not be called.
  74. /// - completionHandler: Called when the image retrieved and set finished.
  75. /// - Returns: A task represents the image downloading.
  76. ///
  77. /// - Note:
  78. ///
  79. /// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
  80. /// or network. Since this method will perform UI changes, you must call it from the main thread.
  81. /// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
  82. ///
  83. @discardableResult
  84. public func setImage(
  85. with resource: (any Resource)?,
  86. placeholder: KFCrossPlatformImage? = nil,
  87. options: KingfisherOptionsInfo? = nil,
  88. progressBlock: DownloadProgressBlock? = nil,
  89. completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
  90. {
  91. return setImage(
  92. with: resource?.convertToSource(),
  93. placeholder: placeholder,
  94. options: options,
  95. progressBlock: progressBlock,
  96. completionHandler: completionHandler)
  97. }
  98. func setImage(
  99. with source: Source?,
  100. placeholder: KFCrossPlatformImage? = nil,
  101. parsedOptions: KingfisherParsedOptionsInfo,
  102. progressBlock: DownloadProgressBlock? = nil,
  103. completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
  104. {
  105. var mutatingSelf = self
  106. return setImage(
  107. with: source,
  108. imageAccessor: ImagePropertyAccessor(
  109. setImage: { image, _ in
  110. /**
  111. * In iOS SDK 14.0-14.4 the image param was non-`nil`. The SDK changed in 14.5
  112. * to allow `nil`. The compiler version 5.4 was introduced in this same SDK,
  113. * which allows >=14.5 SDK to set a `nil` image. This compile check allows
  114. * newer SDK users to set the image to `nil`, while still allowing older SDK
  115. * users to compile the framework.
  116. */
  117. #if compiler(>=5.4)
  118. self.base.setImage(image)
  119. #else
  120. if let image = image {
  121. self.base.setImage(image)
  122. }
  123. #endif
  124. },
  125. getImage: {
  126. self.base.image
  127. }
  128. ),
  129. taskAccessor: TaskPropertyAccessor(
  130. setTaskIdentifier: { mutatingSelf.taskIdentifier = $0 },
  131. getTaskIdentifier: { mutatingSelf.taskIdentifier },
  132. setTask: { mutatingSelf.imageTask = $0 }
  133. ),
  134. placeholder: placeholder,
  135. parsedOptions: parsedOptions,
  136. progressBlock: progressBlock,
  137. completionHandler: completionHandler
  138. )
  139. }
  140. // MARK: Cancelling Image
  141. /// Cancel the image download task bounded to the image view if it is running.
  142. /// Nothing will happen if the downloading has already finished.
  143. public func cancelDownloadTask() {
  144. imageTask?.cancel()
  145. }
  146. }
  147. @MainActor private var taskIdentifierKey: Void?
  148. @MainActor private var imageTaskKey: Void?
  149. // MARK: Properties
  150. @MainActor
  151. extension KingfisherWrapper where Base: CPListItem {
  152. public private(set) var taskIdentifier: Source.Identifier.Value? {
  153. get {
  154. let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
  155. return box?.value
  156. }
  157. set {
  158. let box = newValue.map { Box($0) }
  159. setRetainedAssociatedObject(base, &taskIdentifierKey, box)
  160. }
  161. }
  162. private var imageTask: DownloadTask? {
  163. get { return getAssociatedObject(base, &imageTaskKey) }
  164. set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
  165. }
  166. }
  167. #endif