ImageContext.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. let onFailureDelegate = Delegate<KingfisherError, Void>()
  82. let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
  83. let onProgressDelegate = Delegate<(Int64, Int64), Void>()
  84. init(source: Source?) {
  85. self.source = source
  86. }
  87. func shouldApplyFade(cacheType: CacheType) -> Bool {
  88. options.forceTransition || cacheType == .none
  89. }
  90. func fadeTransitionDuration(cacheType: CacheType) -> TimeInterval? {
  91. shouldApplyFade(cacheType: cacheType)
  92. ? options.transition.fadeDuration
  93. : nil
  94. }
  95. }
  96. }
  97. extension ImageTransition {
  98. // Only for fade effect in SwiftUI.
  99. fileprivate var fadeDuration: TimeInterval? {
  100. switch self {
  101. case .fade(let duration):
  102. return duration
  103. default:
  104. return nil
  105. }
  106. }
  107. }
  108. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  109. extension KFImage.Context: Hashable {
  110. public static func == (lhs: KFImage.Context<HoldingView>, rhs: KFImage.Context<HoldingView>) -> Bool {
  111. lhs.source == rhs.source &&
  112. lhs.options.processor.identifier == rhs.options.processor.identifier
  113. }
  114. public func hash(into hasher: inout Hasher) {
  115. hasher.combine(source)
  116. hasher.combine(options.processor.identifier)
  117. }
  118. }
  119. #if !os(watchOS)
  120. @available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
  121. extension KFAnimatedImage {
  122. public typealias Context = KFImage.Context
  123. typealias ImageBinder = KFImage.ImageBinder
  124. }
  125. #endif
  126. #endif