ImageContext.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // ImageContext.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. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  30. extension KFImage {
  31. public class Context<HoldingView: KFImageHoldingView>: @unchecked Sendable where HoldingView: Sendable {
  32. private let propertyQueue = DispatchQueue(label: "com.onevcat.Kingfisher.KFImageContextPropertyQueue")
  33. let source: Source?
  34. var _options = KingfisherParsedOptionsInfo(
  35. KingfisherManager.shared.defaultOptions + [.loadDiskFileSynchronously]
  36. )
  37. var options: KingfisherParsedOptionsInfo {
  38. get { propertyQueue.sync { _options } }
  39. set { propertyQueue.sync { _options = newValue } }
  40. }
  41. var _configurations: [(HoldingView) -> HoldingView] = []
  42. var configurations: [(HoldingView) -> HoldingView] {
  43. get { propertyQueue.sync { _configurations } }
  44. set { propertyQueue.sync { _configurations = newValue } }
  45. }
  46. var _renderConfigurations: [(HoldingView.RenderingView) -> Void] = []
  47. var renderConfigurations: [(HoldingView.RenderingView) -> Void] {
  48. get { propertyQueue.sync { _renderConfigurations } }
  49. set { propertyQueue.sync { _renderConfigurations = newValue } }
  50. }
  51. var _contentConfiguration: ((HoldingView) -> AnyView)? = nil
  52. var contentConfiguration: ((HoldingView) -> AnyView)? {
  53. get { propertyQueue.sync { _contentConfiguration } }
  54. set { propertyQueue.sync { _contentConfiguration = newValue } }
  55. }
  56. var _cancelOnDisappear: Bool = false
  57. var cancelOnDisappear: Bool {
  58. get { propertyQueue.sync { _cancelOnDisappear } }
  59. set { propertyQueue.sync { _cancelOnDisappear = newValue } }
  60. }
  61. var _reducePriorityOnDisappear: Bool = false
  62. var reducePriorityOnDisappear: Bool {
  63. get { propertyQueue.sync { _reducePriorityOnDisappear } }
  64. set { propertyQueue.sync { _reducePriorityOnDisappear = newValue } }
  65. }
  66. var _placeholder: ((Progress) -> AnyView)? = nil
  67. var placeholder: ((Progress) -> AnyView)? {
  68. get { propertyQueue.sync { _placeholder } }
  69. set { propertyQueue.sync { _placeholder = newValue } }
  70. }
  71. var _failureView: (() -> AnyView)? = nil
  72. var failureView: (() -> AnyView)? {
  73. get { propertyQueue.sync { _failureView } }
  74. set { propertyQueue.sync { _failureView = newValue } }
  75. }
  76. var _startLoadingBeforeViewAppear: Bool = false
  77. var startLoadingBeforeViewAppear: Bool {
  78. get { propertyQueue.sync { _startLoadingBeforeViewAppear } }
  79. set { propertyQueue.sync { _startLoadingBeforeViewAppear = newValue } }
  80. }
  81. // SwiftUI transition support
  82. var _swiftUITransition: AnyTransition? = nil
  83. var swiftUITransition: AnyTransition? {
  84. get { propertyQueue.sync { _swiftUITransition } }
  85. set { propertyQueue.sync { _swiftUITransition = newValue } }
  86. }
  87. var _swiftUIAnimation: Animation? = nil
  88. var swiftUIAnimation: Animation? {
  89. get { propertyQueue.sync { _swiftUIAnimation } }
  90. set { propertyQueue.sync { _swiftUIAnimation = newValue } }
  91. }
  92. let onFailureDelegate = Delegate<KingfisherError, Void>()
  93. let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
  94. let onProgressDelegate = Delegate<(Int64, Int64), Void>()
  95. init(source: Source?) {
  96. self.source = source
  97. }
  98. func shouldApplyFade(cacheType: CacheType) -> Bool {
  99. options.forceTransition || cacheType == .none
  100. }
  101. func fadeTransitionDuration(cacheType: CacheType) -> TimeInterval? {
  102. shouldApplyFade(cacheType: cacheType)
  103. ? options.transition.fadeDuration
  104. : nil
  105. }
  106. }
  107. }
  108. extension ImageTransition {
  109. // Only for fade effect in SwiftUI.
  110. fileprivate var fadeDuration: TimeInterval? {
  111. switch self {
  112. case .fade(let duration):
  113. return duration
  114. default:
  115. return nil
  116. }
  117. }
  118. }
  119. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  120. extension KFImage.Context: Hashable {
  121. public static func == (lhs: KFImage.Context<HoldingView>, rhs: KFImage.Context<HoldingView>) -> Bool {
  122. lhs.source == rhs.source &&
  123. lhs.options.processor.identifier == rhs.options.processor.identifier
  124. }
  125. public func hash(into hasher: inout Hasher) {
  126. hasher.combine(source)
  127. hasher.combine(options.processor.identifier)
  128. }
  129. }
  130. #if !os(watchOS)
  131. @available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
  132. extension KFAnimatedImage {
  133. public typealias Context = KFImage.Context
  134. typealias ImageBinder = KFImage.ImageBinder
  135. }
  136. #endif
  137. #endif