KMAdvertisementModelTransition.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 transitionImagePath(image: KMAdvertisementItemInfo.Image?, highlight:Bool) -> String {
  12. var result = ""
  13. guard let tempImage = image else { return result }
  14. if highlight {
  15. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.hover_Dark : tempImage.hover_Light) ?? ""
  16. } else {
  17. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.normal_Dark : tempImage.normal_Light) ?? ""
  18. }
  19. if result.count == 0 {
  20. let language = Language()
  21. language.en = image?.en
  22. language.zh_TW = image?.zh_TW
  23. language.zh_CN = image?.zh_CN
  24. result = KMAdvertisementModelTransition.transitionLanguage(langeuage: language)
  25. }
  26. return result
  27. }
  28. //多语转换
  29. @objc public static func transitionLanguage(langeuage: Language?) -> String {
  30. var result = ""
  31. // let string = KMAdvertisementModelTransition.getLanguageType()
  32. // if string == "zh-Hans-CN" {
  33. // result = langeuage?.zh_CN ?? ""
  34. // } else if string == "zh-Hans-TW" {
  35. let string = KMAdvertisementModelTransition.getLanguageType()
  36. if string == "zh-Hans-CN" || string == "zh-Hans" {
  37. result = langeuage?.zh_CN ?? ""
  38. } else if string == "zh-Hans-TW" ||
  39. string == "zh-Hant-CN" ||
  40. string == "zh-Hant" {
  41. result = langeuage?.zh_TW ?? ""
  42. } else {
  43. result = langeuage?.en ?? ""
  44. }
  45. return result
  46. }
  47. public static func getLanguageType() -> String {
  48. #if os(iOS)
  49. let def = UserDefaults.standard
  50. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  51. let chooseLanguage = allLanguages.first
  52. return chooseLanguage ?? "en"
  53. #elseif os(OSX)
  54. let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  55. let chooseLanguage: String = allLanguages.first ?? "en"
  56. return chooseLanguage
  57. #endif
  58. }
  59. @objc public static func sendFireBaseEvent(firebase: firebaseInfo?) -> Void {
  60. guard let firebaseInfo = firebase else {
  61. return
  62. }
  63. guard let fireBaseEvent = firebaseInfo.event else {
  64. return
  65. }
  66. guard let firebasePropertyKey = firebaseInfo.propertyKey else {
  67. return
  68. }
  69. guard let firebasePropertyValue = firebaseInfo.propertyValue else {
  70. return
  71. }
  72. FMTrackEventManager.defaultManager.trackEvent(event: fireBaseEvent, withProperties: [firebasePropertyKey : firebasePropertyValue])
  73. }
  74. }
  75. extension NSColor {
  76. convenience init(hex: String) {
  77. var hexString = hex
  78. if hex.starts(with: "#") {
  79. hexString = String(hex.dropFirst())
  80. }
  81. if let ui64 = UInt64(hexString, radix: 16) {
  82. self.init(hex: Int(ui64))
  83. } else {
  84. self.init(hex: 0) // <--- black
  85. }
  86. }
  87. convenience init(hex: Int) {
  88. let components = (
  89. R: CGFloat((hex >> 16) & 0xff) / 255,
  90. G: CGFloat((hex >> 08) & 0xff) / 255,
  91. B: CGFloat((hex >> 00) & 0xff) / 255
  92. )
  93. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  94. }
  95. }