KMAdvertisementModelTransition.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // KMAdvertisementModelTansition.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/12/2.
  6. //
  7. import AppKit
  8. @objcMembers open class KMAdvertisementModelTransition: NSObject {
  9. //MARK: 模型转换
  10. //颜色模型转换
  11. // public static func transitionColor(color: KMAdvertisementModelItem.Color?, highlight:Bool) -> NSColor {
  12. // var result = NSColor.clear
  13. // guard let tempColor = color else { return result }
  14. //
  15. // if highlight {
  16. // let darkHex = tempColor.height?.dark?.hex
  17. // let darkAlpha = tempColor.height?.dark?.alpha
  18. // let universalHex = tempColor.height?.universal?.hex
  19. // let universalAlpha = tempColor.height?.universal?.alpha
  20. // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  21. // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  22. //
  23. // result = KMAdvertisementConfig.adapterDark(light: light , dark: dark) as! NSColor
  24. // } else {
  25. // let darkHex = tempColor.normal?.dark?.hex
  26. // let darkAlpha = tempColor.normal?.dark?.alpha
  27. // let universalHex = tempColor.normal?.universal?.hex
  28. // let universalAlpha = tempColor.normal?.universal?.alpha
  29. //
  30. // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  31. // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  32. // result = KMAdvertisementConfig.adapterDark(light: light, dark: dark) as! NSColor
  33. // }
  34. //
  35. // return result
  36. // }
  37. //图片转换
  38. public static func transitionImagePath(image: KMAdvertisementItemInfo.Image?, highlight:Bool) -> String {
  39. var result = ""
  40. guard let tempImage = image else { return result }
  41. if highlight {
  42. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.hover_Dark : tempImage.hover_Light) ?? ""
  43. } else {
  44. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.normal_Dark : tempImage.normal_Light) ?? ""
  45. }
  46. if result.count == 0 {
  47. let language = Language()
  48. language.en = image?.en
  49. language.zh_TW = image?.zh_TW
  50. language.zh_CN = image?.zh_CN
  51. result = KMAdvertisementModelTransition.transitionLanguage(langeuage: language)
  52. }
  53. return result
  54. }
  55. //多语转换
  56. @objc public static func transitionLanguage(langeuage: Language?) -> String {
  57. var result = ""
  58. let string = KMAdvertisementModelTransition.getLanguageType()
  59. if string == "zh-Hans-CN" {
  60. result = langeuage?.zh_CN ?? ""
  61. } else if string == "zh-Hans-TW" ||
  62. string == "zh-Hant-CN"{
  63. result = langeuage?.zh_TW ?? ""
  64. } else {
  65. result = langeuage?.en ?? ""
  66. }
  67. return result
  68. }
  69. public static func getLanguageType() -> String {
  70. #if os(iOS)
  71. let def = UserDefaults.standard
  72. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  73. let chooseLanguage = allLanguages.first
  74. return chooseLanguage ?? "en"
  75. #elseif os(OSX)
  76. // let locale = NSLocale.current.languageCode
  77. // let chooseLanguage = Locale.preferredLanguages[0]
  78. let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  79. let chooseLanguage: String = allLanguages.first ?? "en"
  80. return chooseLanguage
  81. #endif
  82. }
  83. @objc public static func sendFireBaseEvent(firebase: firebaseInfo?) -> Void {
  84. guard let firebaseInfo = firebase else {
  85. return
  86. }
  87. guard let fireBaseEvent = firebaseInfo.event else {
  88. return
  89. }
  90. guard let firebasePropertyKey = firebaseInfo.propertyKey else {
  91. return
  92. }
  93. guard let firebasePropertyValue = firebaseInfo.propertyValue else {
  94. return
  95. }
  96. }
  97. }
  98. extension NSColor {
  99. convenience init(hex: String) {
  100. var hexString = hex
  101. if hex.starts(with: "#") {
  102. hexString = String(hex.dropFirst())
  103. }
  104. if let ui64 = UInt64(hexString, radix: 16) {
  105. self.init(hex: Int(ui64))
  106. } else {
  107. self.init(hex: 0) // <--- black
  108. }
  109. // or using ternaries as requested
  110. // let hexString = hex.starts(with: "#") ? String(hex.dropFirst()) : hex
  111. // let ui64 = UInt64(hexString, radix: 16)
  112. // self.init(hex: ui64 != nil ? Int(ui64!) : 0)
  113. }
  114. convenience init(hex: Int) {
  115. let components = (
  116. R: CGFloat((hex >> 16) & 0xff) / 255,
  117. G: CGFloat((hex >> 08) & 0xff) / 255,
  118. B: CGFloat((hex >> 00) & 0xff) / 255
  119. )
  120. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  121. }
  122. // func toHex(alpha: Bool = false) -> String? {
  123. // guard let components = cgColor.components, components.count >= 3 else {
  124. // return nil
  125. // }
  126. //
  127. // let r = Float(components[0])
  128. // let g = Float(components[1])
  129. // let b = Float(components[2])
  130. // var a = Float(1.0)
  131. //
  132. // if components.count >= 4 {
  133. // a = Float(components[3])
  134. // }
  135. //
  136. // if alpha {
  137. // return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  138. // } else {
  139. // return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  140. // }
  141. // }
  142. }