KMAdvertisementModelTransition.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. FMTrackEventManager.defaultManager.trackEvent(event: fireBaseEvent, withProperties: [firebasePropertyKey : firebasePropertyValue])
  97. }
  98. }
  99. extension NSColor {
  100. convenience init(hex: String) {
  101. var hexString = hex
  102. if hex.starts(with: "#") {
  103. hexString = String(hex.dropFirst())
  104. }
  105. if let ui64 = UInt64(hexString, radix: 16) {
  106. self.init(hex: Int(ui64))
  107. } else {
  108. self.init(hex: 0) // <--- black
  109. }
  110. // or using ternaries as requested
  111. // let hexString = hex.starts(with: "#") ? String(hex.dropFirst()) : hex
  112. // let ui64 = UInt64(hexString, radix: 16)
  113. // self.init(hex: ui64 != nil ? Int(ui64!) : 0)
  114. }
  115. convenience init(hex: Int) {
  116. let components = (
  117. R: CGFloat((hex >> 16) & 0xff) / 255,
  118. G: CGFloat((hex >> 08) & 0xff) / 255,
  119. B: CGFloat((hex >> 00) & 0xff) / 255
  120. )
  121. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  122. }
  123. // func toHex(alpha: Bool = false) -> String? {
  124. // guard let components = cgColor.components, components.count >= 3 else {
  125. // return nil
  126. // }
  127. //
  128. // let r = Float(components[0])
  129. // let g = Float(components[1])
  130. // let b = Float(components[2])
  131. // var a = Float(1.0)
  132. //
  133. // if components.count >= 4 {
  134. // a = Float(components[3])
  135. // }
  136. //
  137. // if alpha {
  138. // return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  139. // } else {
  140. // return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  141. // }
  142. // }
  143. }