ImageContext.swift 5.3 KB

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