KMAdvertisementModelTransition.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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?.zh_TW
  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 languageConfig = KMAdvertisementManager.manager.configuration.language
  59. // if languageConfig != .unknow {
  60. // if languageConfig == .en {
  61. // result = langeuage?.en ?? ""
  62. // } else if languageConfig == .zhCN {
  63. // result = langeuage?.zh_CN ?? ""
  64. // } else if languageConfig == .zhTW {
  65. // result = langeuage?.zh_TW ?? ""
  66. // }
  67. // } else {
  68. let string = KMAdvertisementModelTransition.getLanguageType()
  69. if string == "zh-Hans-CN" {
  70. result = langeuage?.zh_CN ?? ""
  71. } else if string == "zh-Hans-TW" {
  72. result = langeuage?.zh_TW ?? ""
  73. } else {
  74. result = langeuage?.en ?? ""
  75. }
  76. // }
  77. return result
  78. }
  79. public static func getLanguageType() -> String {
  80. #if os(iOS)
  81. let def = UserDefaults.standard
  82. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  83. let chooseLanguage = allLanguages.first
  84. return chooseLanguage ?? "en"
  85. #elseif os(OSX)
  86. // let locale = NSLocale.current.languageCode
  87. // let chooseLanguage = Locale.preferredLanguages[0]
  88. let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  89. let chooseLanguage: String = allLanguages.first ?? "en"
  90. return chooseLanguage
  91. #endif
  92. }
  93. @objc public static func sendFireBaseEvent(firebase: firebaseInfo?) -> Void {
  94. guard let firebaseInfo = firebase else {
  95. return
  96. }
  97. guard let fireBaseEvent = firebaseInfo.event else {
  98. return
  99. }
  100. guard let firebasePropertyKey = firebaseInfo.propertyKey else {
  101. return
  102. }
  103. guard let firebasePropertyValue = firebaseInfo.propertyValue else {
  104. return
  105. }
  106. FMTrackEventManager.defaultManager.trackEvent(event: fireBaseEvent, withProperties: [firebasePropertyKey : firebasePropertyValue])
  107. }
  108. }
  109. extension NSColor {
  110. convenience init(hex: String) {
  111. var hexString = hex
  112. if hex.starts(with: "#") {
  113. hexString = String(hex.dropFirst())
  114. }
  115. if let ui64 = UInt64(hexString, radix: 16) {
  116. self.init(hex: Int(ui64))
  117. } else {
  118. self.init(hex: 0) // <--- black
  119. }
  120. // or using ternaries as requested
  121. // let hexString = hex.starts(with: "#") ? String(hex.dropFirst()) : hex
  122. // let ui64 = UInt64(hexString, radix: 16)
  123. // self.init(hex: ui64 != nil ? Int(ui64!) : 0)
  124. }
  125. convenience init(hex: Int) {
  126. let components = (
  127. R: CGFloat((hex >> 16) & 0xff) / 255,
  128. G: CGFloat((hex >> 08) & 0xff) / 255,
  129. B: CGFloat((hex >> 00) & 0xff) / 255
  130. )
  131. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  132. }
  133. // func toHex(alpha: Bool = false) -> String? {
  134. // guard let components = cgColor.components, components.count >= 3 else {
  135. // return nil
  136. // }
  137. //
  138. // let r = Float(components[0])
  139. // let g = Float(components[1])
  140. // let b = Float(components[2])
  141. // var a = Float(1.0)
  142. //
  143. // if components.count >= 4 {
  144. // a = Float(components[3])
  145. // }
  146. //
  147. // if alpha {
  148. // return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  149. // } else {
  150. // return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  151. // }
  152. // }
  153. }