Kingfisher.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Kingfisher.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 16/9/14.
  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 Foundation
  27. import ImageIO
  28. #if os(macOS)
  29. import AppKit
  30. public typealias KFCrossPlatformImage = NSImage
  31. public typealias KFCrossPlatformView = NSView
  32. public typealias KFCrossPlatformColor = NSColor
  33. public typealias KFCrossPlatformImageView = NSImageView
  34. public typealias KFCrossPlatformButton = NSButton
  35. #else
  36. import UIKit
  37. public typealias KFCrossPlatformImage = UIImage
  38. public typealias KFCrossPlatformColor = UIColor
  39. #if !os(watchOS)
  40. public typealias KFCrossPlatformImageView = UIImageView
  41. public typealias KFCrossPlatformView = UIView
  42. public typealias KFCrossPlatformButton = UIButton
  43. #if canImport(TVUIKit)
  44. import TVUIKit
  45. #endif
  46. #if canImport(CarPlay) && !targetEnvironment(macCatalyst)
  47. import CarPlay
  48. #endif
  49. #else
  50. import WatchKit
  51. #endif
  52. #endif
  53. /// Wrapper for Kingfisher compatible types. This type provides an extension point for
  54. /// convenience methods in Kingfisher.
  55. public struct KingfisherWrapper<Base> {
  56. public let base: Base
  57. public init(_ base: Base) {
  58. self.base = base
  59. }
  60. }
  61. /// Represents an object type that is compatible with Kingfisher. You can use `kf` property to get a
  62. /// value in the namespace of Kingfisher.
  63. public protocol KingfisherCompatible: AnyObject { }
  64. /// Represents a value type that is compatible with Kingfisher. You can use `kf` property to get a
  65. /// value in the namespace of Kingfisher.
  66. public protocol KingfisherCompatibleValue {}
  67. extension KingfisherCompatible {
  68. /// Gets a namespace holder for Kingfisher compatible types.
  69. public var kf: KingfisherWrapper<Self> {
  70. get { return KingfisherWrapper(self) }
  71. set { }
  72. }
  73. }
  74. extension KingfisherCompatibleValue {
  75. /// Gets a namespace holder for Kingfisher compatible types.
  76. public var kf: KingfisherWrapper<Self> {
  77. get { return KingfisherWrapper(self) }
  78. set { }
  79. }
  80. }
  81. extension KFCrossPlatformImage: KingfisherCompatible { }
  82. #if !os(watchOS)
  83. extension KFCrossPlatformImageView: KingfisherCompatible { }
  84. extension KFCrossPlatformButton: KingfisherCompatible { }
  85. extension NSTextAttachment: KingfisherCompatible { }
  86. #else
  87. extension WKInterfaceImage: KingfisherCompatible { }
  88. #endif
  89. #if os(tvOS) && canImport(TVUIKit)
  90. @available(tvOS 12.0, *)
  91. extension TVMonogramView: KingfisherCompatible { }
  92. #endif
  93. #if canImport(CarPlay) && !targetEnvironment(macCatalyst)
  94. @available(iOS 14.0, *)
  95. extension CPListItem: KingfisherCompatible { }
  96. #endif