123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // KMAdvertisementModelTansition.swift
- // KMAdvertisement
- //
- // Created by lizhe on 2022/12/2.
- //
- import AppKit
- @objcMembers open class KMAdvertisementModelTransition: NSObject {
- //MARK: 模型转换
- //颜色模型转换
- // public static func transitionColor(color: KMAdvertisementModelItem.Color?, highlight:Bool) -> NSColor {
- // var result = NSColor.clear
- // guard let tempColor = color else { return result }
- //
- // if highlight {
- // let darkHex = tempColor.height?.dark?.hex
- // let darkAlpha = tempColor.height?.dark?.alpha
- // let universalHex = tempColor.height?.universal?.hex
- // let universalAlpha = tempColor.height?.universal?.alpha
- // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
- // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
- //
- // result = KMAdvertisementConfig.adapterDark(light: light , dark: dark) as! NSColor
- // } else {
- // let darkHex = tempColor.normal?.dark?.hex
- // let darkAlpha = tempColor.normal?.dark?.alpha
- // let universalHex = tempColor.normal?.universal?.hex
- // let universalAlpha = tempColor.normal?.universal?.alpha
- //
- // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
- // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
- // result = KMAdvertisementConfig.adapterDark(light: light, dark: dark) as! NSColor
- // }
- //
- // return result
- // }
-
- //图片转换
- public static func transitionImagePath(image: KMAdvertisementItemInfo.Image?, highlight:Bool) -> String {
- var result = ""
- guard let tempImage = image else { return result }
-
- if highlight {
- result = (KMAdvertisementConfig.isDarkModel() ? tempImage.hover_Dark : tempImage.hover_Light) ?? ""
- } else {
- result = (KMAdvertisementConfig.isDarkModel() ? tempImage.normal_Dark : tempImage.normal_Light) ?? ""
- }
-
- if result.count == 0 {
- let language = Language()
- language.en = image?.en
- language.zh_TW = image?.zh_TW
- language.zh_CN = image?.zh_CN
- result = KMAdvertisementModelTransition.transitionLanguage(langeuage: language)
- }
-
- return result
- }
-
- //多语转换
- @objc public static func transitionLanguage(langeuage: Language?) -> String {
- var result = ""
- let string = KMAdvertisementModelTransition.getLanguageType()
- if string == "zh-Hans-CN" {
- result = langeuage?.zh_CN ?? ""
- } else if string == "zh-Hans-TW" ||
- string == "zh-Hant-CN"{
- result = langeuage?.zh_TW ?? ""
- } else {
- result = langeuage?.en ?? ""
- }
- return result
- }
-
- public static func getLanguageType() -> String {
- #if os(iOS)
- let def = UserDefaults.standard
- let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
- let chooseLanguage = allLanguages.first
- return chooseLanguage ?? "en"
- #elseif os(OSX)
- // let locale = NSLocale.current.languageCode
- // let chooseLanguage = Locale.preferredLanguages[0]
- let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
- let chooseLanguage: String = allLanguages.first ?? "en"
- return chooseLanguage
- #endif
- }
-
- @objc public static func sendFireBaseEvent(firebase: firebaseInfo?) -> Void {
- guard let firebaseInfo = firebase else {
- return
- }
- guard let fireBaseEvent = firebaseInfo.event else {
- return
- }
- guard let firebasePropertyKey = firebaseInfo.propertyKey else {
- return
- }
- guard let firebasePropertyValue = firebaseInfo.propertyValue else {
- return
- }
- FMTrackEventManager.defaultManager.trackEvent(event: fireBaseEvent, withProperties: [firebasePropertyKey : firebasePropertyValue])
- }
- }
- extension NSColor {
- convenience init(hex: String) {
- var hexString = hex
- if hex.starts(with: "#") {
- hexString = String(hex.dropFirst())
- }
- if let ui64 = UInt64(hexString, radix: 16) {
- self.init(hex: Int(ui64))
- } else {
- self.init(hex: 0) // <--- black
- }
- // or using ternaries as requested
- // let hexString = hex.starts(with: "#") ? String(hex.dropFirst()) : hex
- // let ui64 = UInt64(hexString, radix: 16)
- // self.init(hex: ui64 != nil ? Int(ui64!) : 0)
- }
-
- convenience init(hex: Int) {
- let components = (
- R: CGFloat((hex >> 16) & 0xff) / 255,
- G: CGFloat((hex >> 08) & 0xff) / 255,
- B: CGFloat((hex >> 00) & 0xff) / 255
- )
- self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
- }
- // func toHex(alpha: Bool = false) -> String? {
- // guard let components = cgColor.components, components.count >= 3 else {
- // return nil
- // }
- //
- // let r = Float(components[0])
- // let g = Float(components[1])
- // let b = Float(components[2])
- // var a = Float(1.0)
- //
- // if components.count >= 4 {
- // a = Float(components[3])
- // }
- //
- // if alpha {
- // return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
- // } else {
- // return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
- // }
- // }
- }
|