KMAdvertisementModelTransition.swift 6.0 KB

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