ImageContext.swift 3.5 KB

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