ImageContext.swift 5.0 KB

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