Placeholder.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. extension KFCrossPlatformImage: Placeholder {
  52. public func add(to imageView: KFCrossPlatformImageView) {
  53. imageView.image = self
  54. }
  55. public func remove(from imageView: KFCrossPlatformImageView) {
  56. imageView.image = nil
  57. }
  58. }
  59. /// Default implementation of an arbitrary view as a placeholder. The view will be
  60. /// added as a subview when adding and removed from its superview when removing.
  61. ///
  62. /// To use your customized View type as a placeholder, simply have it conform to
  63. /// `Placeholder` using an extension: `extension MyView: Placeholder {}`.
  64. @MainActor
  65. extension Placeholder where Self: KFCrossPlatformView {
  66. public func add(to imageView: KFCrossPlatformImageView) {
  67. imageView.addSubview(self)
  68. translatesAutoresizingMaskIntoConstraints = false
  69. centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
  70. centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
  71. heightAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
  72. widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
  73. }
  74. public func remove(from imageView: KFCrossPlatformImageView) {
  75. removeFromSuperview()
  76. }
  77. }
  78. #endif