2
0

Placeholder.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Placeholder.swift
  3. // Kingfisher
  4. //
  5. // Created by Tieme van Veen on 28/08/2017.
  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. #if !os(watchOS)
  27. #if canImport(AppKit) && !targetEnvironment(macCatalyst)
  28. import AppKit
  29. #endif
  30. #if canImport(UIKit)
  31. import UIKit
  32. #endif
  33. /// Represents a placeholder type that could be set during loading as well as when loading is finished without
  34. /// getting an image.
  35. public protocol Placeholder {
  36. /// Called when the placeholder needs to be added to a given image view.
  37. ///
  38. /// To conform to ``Placeholder``, you implement this method and add your own placeholder view to the
  39. /// given `imageView`.
  40. ///
  41. /// - Parameter imageView: The image view where the placeholder should be added to.
  42. @MainActor func add(to imageView: KFCrossPlatformImageView)
  43. /// Called when the placeholder needs to be removed from a given image view.
  44. ///
  45. /// To conform to ``Placeholder``, you implement this method and remove your own placeholder view from the
  46. /// given `imageView`.
  47. ///
  48. /// - Parameter imageView: The image view where the placeholder is already added to and now should be removed from.
  49. @MainActor func remove(from imageView: KFCrossPlatformImageView)
  50. }
  51. @MainActor
  52. extension KFCrossPlatformImage: Placeholder {
  53. public func add(to imageView: KFCrossPlatformImageView) {
  54. imageView.image = self
  55. }
  56. public func remove(from imageView: KFCrossPlatformImageView) {
  57. imageView.image = nil
  58. }
  59. public func add(to base: any KingfisherHasImageComponent) {
  60. base.image = self
  61. }
  62. public func remove(from base: any KingfisherHasImageComponent) {
  63. base.image = nil
  64. }
  65. }
  66. /// Default implementation of an arbitrary view as a placeholder. The view will be
  67. /// added as a subview when adding and removed from its superview when removing.
  68. ///
  69. /// To use your customized View type as a placeholder, simply have it conform to
  70. /// `Placeholder` using an extension: `extension MyView: Placeholder {}`.
  71. @MainActor
  72. extension Placeholder where Self: KFCrossPlatformView {
  73. public func add(to imageView: KFCrossPlatformImageView) {
  74. imageView.addSubview(self)
  75. translatesAutoresizingMaskIntoConstraints = false
  76. centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
  77. centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
  78. heightAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
  79. widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
  80. }
  81. public func remove(from imageView: KFCrossPlatformImageView) {
  82. removeFromSuperview()
  83. }
  84. }
  85. #endif