ImageContext.swift 3.6 KB

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