ImageContext.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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> {
  32. let source: Source?
  33. var options = KingfisherParsedOptionsInfo(
  34. KingfisherManager.shared.defaultOptions + [.loadDiskFileSynchronously]
  35. )
  36. var configurations: [(HoldingView) -> HoldingView] = []
  37. var renderConfigurations: [(HoldingView.RenderingView) -> Void] = []
  38. var contentConfiguration: ((HoldingView) -> AnyView)? = nil
  39. var cancelOnDisappear: Bool = false
  40. var placeholder: ((Progress) -> AnyView)? = nil
  41. let onFailureDelegate = Delegate<KingfisherError, Void>()
  42. let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
  43. let onProgressDelegate = Delegate<(Int64, Int64), Void>()
  44. var startLoadingBeforeViewAppear: Bool = false
  45. init(source: Source?) {
  46. self.source = source
  47. }
  48. func shouldApplyFade(cacheType: CacheType) -> Bool {
  49. options.forceTransition || cacheType == .none
  50. }
  51. func fadeTransitionDuration(cacheType: CacheType) -> TimeInterval? {
  52. shouldApplyFade(cacheType: cacheType)
  53. ? options.transition.fadeDuration
  54. : nil
  55. }
  56. }
  57. }
  58. extension ImageTransition {
  59. // Only for fade effect in SwiftUI.
  60. fileprivate var fadeDuration: TimeInterval? {
  61. switch self {
  62. case .fade(let duration):
  63. return duration
  64. default:
  65. return nil
  66. }
  67. }
  68. }
  69. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  70. extension KFImage.Context: Hashable {
  71. public static func == (lhs: KFImage.Context<HoldingView>, rhs: KFImage.Context<HoldingView>) -> Bool {
  72. lhs.source == rhs.source &&
  73. lhs.options.processor.identifier == rhs.options.processor.identifier
  74. }
  75. public func hash(into hasher: inout Hasher) {
  76. hasher.combine(source)
  77. hasher.combine(options.processor.identifier)
  78. }
  79. }
  80. #if canImport(UIKit) && !os(watchOS)
  81. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  82. extension KFAnimatedImage {
  83. public typealias Context = KFImage.Context
  84. typealias ImageBinder = KFImage.ImageBinder
  85. }
  86. #endif
  87. #endif