ImageBinder.swift 4.4 KB

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