ImageBinder.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // ImageBinder.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2019/06/27.
  6. //
  7. // Copyright (c) 2019 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. import Combine
  27. import SwiftUI
  28. import Kingfisher
  29. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  30. extension KFImage {
  31. /// Represents a binder for `KFImage`. It takes responsibility as an `ObjectBinding` and performs
  32. /// image downloading and progress reporting based on `KingfisherManager`.
  33. public class ImageBinder: ObservableObject {
  34. let source: Source?
  35. let options: KingfisherOptionsInfo?
  36. var downloadTask: DownloadTask?
  37. var loadingOrSuccessed: Bool = false
  38. let onFailureDelegate = Delegate<KingfisherError, Void>()
  39. let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
  40. let onProgressDelegate = Delegate<(Int64, Int64), Void>()
  41. @Published var image: KFCrossPlatformImage?
  42. // Only `.fade` is now supported.
  43. var fadeTransitionAnimation: Animation? {
  44. #if os(iOS) || os(tvOS)
  45. guard let options = (options.map { KingfisherParsedOptionsInfo($0) }) else {
  46. return nil
  47. }
  48. switch options.transition {
  49. case .fade(let duration):
  50. return .linear(duration: duration)
  51. default:
  52. return nil
  53. }
  54. #else
  55. return nil
  56. #endif
  57. }
  58. init(source: Source?, options: KingfisherOptionsInfo?) {
  59. self.source = source
  60. self.options = options
  61. self.image = nil
  62. }
  63. func start() {
  64. guard !loadingOrSuccessed else { return }
  65. loadingOrSuccessed = true
  66. guard let source = source else {
  67. DispatchQueue.main.async {
  68. self.onFailureDelegate.call(KingfisherError.imageSettingError(reason: .emptySource))
  69. }
  70. return
  71. }
  72. downloadTask = KingfisherManager.shared
  73. .retrieveImage(
  74. with: source,
  75. options: options,
  76. progressBlock: { size, total in
  77. self.onProgressDelegate.call((size, total))
  78. },
  79. completionHandler: { [weak self] result in
  80. guard let self = self else { return }
  81. self.downloadTask = nil
  82. switch result {
  83. case .success(let value):
  84. self.image = value.image
  85. DispatchQueue.main.async {
  86. self.onSuccessDelegate.call(value)
  87. }
  88. case .failure(let error):
  89. self.loadingOrSuccessed = false
  90. DispatchQueue.main.async {
  91. self.onFailureDelegate.call(error)
  92. }
  93. }
  94. })
  95. }
  96. /// Cancels the download task if it is in progress.
  97. public func cancel() {
  98. downloadTask?.cancel()
  99. }
  100. func setOnFailure(perform action: ((KingfisherError) -> Void)?) {
  101. onFailureDelegate.delegate(on: self) { _, error in
  102. action?(error)
  103. }
  104. }
  105. func setOnSuccess(perform action: ((RetrieveImageResult) -> Void)?) {
  106. onSuccessDelegate.delegate(on: self) { _, result in
  107. action?(result)
  108. }
  109. }
  110. func setOnProgress(perform action: ((Int64, Int64) -> Void)?) {
  111. onProgressDelegate.delegate(on: self) { _, result in
  112. action?(result.0, result.1)
  113. }
  114. }
  115. }
  116. }