KMAdvertisementModelTransition.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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" || string == "zh-Hans" {
  33. result = langeuage?.zh_CN ?? ""
  34. } else if string == "zh-Hans-TW" ||
  35. string == "zh-Hant-CN" ||
  36. string == "zh-Hant" {
  37. result = langeuage?.zh_TW ?? ""
  38. } else {
  39. result = langeuage?.en ?? ""
  40. }
  41. return result
  42. }
  43. public static func getLanguageType() -> String {
  44. #if os(iOS)
  45. let def = UserDefaults.standard
  46. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  47. let chooseLanguage = allLanguages.first
  48. return chooseLanguage ?? "en"
  49. #elseif os(OSX)
  50. let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  51. let chooseLanguage: String = allLanguages.first ?? "en"
  52. return chooseLanguage
  53. #endif
  54. }
  55. @objc public static func sendFireBaseEvent(firebase: firebaseInfo?) -> Void {
  56. guard let firebaseInfo = firebase else {
  57. return
  58. }
  59. guard let fireBaseEvent = firebaseInfo.event else {
  60. return
  61. }
  62. guard let firebasePropertyKey = firebaseInfo.propertyKey else {
  63. return
  64. }
  65. guard let firebasePropertyValue = firebaseInfo.propertyValue else {
  66. return
  67. }
  68. FMTrackEventManager.defaultManager.trackEvent(event: fireBaseEvent, withProperties: [firebasePropertyKey : firebasePropertyValue])
  69. }
  70. }
  71. extension NSColor {
  72. convenience init(hex: String) {
  73. var hexString = hex
  74. if hex.starts(with: "#") {
  75. hexString = String(hex.dropFirst())
  76. }
  77. if let ui64 = UInt64(hexString, radix: 16) {
  78. self.init(hex: Int(ui64))
  79. } else {
  80. self.init(hex: 0) // <--- black
  81. }
  82. }
  83. convenience init(hex: Int) {
  84. let components = (
  85. R: CGFloat((hex >> 16) & 0xff) / 255,
  86. G: CGFloat((hex >> 08) & 0xff) / 255,
  87. B: CGFloat((hex >> 00) & 0xff) / 255
  88. )
  89. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  90. }
  91. }