ImageContext.swift 3.3 KB

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