123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- import Cocoa
- @objcMembers class KMDesignToken: NSObject {
-
- private static let sharedInstance = KMDesignToken()
- @objc class var shared: KMDesignToken {
- return sharedInstance
- }
- var jsonPaser: KMJSONParser?
- var finalDict: [String : Any] = [:]
- func parserExcel(withPath path: String) -> Void {
- let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "$metadata", ofType: "json")!))
- let jsonData = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
- let jsonDict = jsonData as! [String : [String]]
-
- jsonPaser = KMJSONParser.defaultJSONParser_JSONParser
- jsonPaser?.parseFilePaths = jsonDict["tokenSetOrder"]
- jsonPaser?.parseOutType = .ArrayObj
- jsonPaser?.delete = self
- jsonPaser?.parse()
- }
-
- func tokenUsesAction(withToken token: String) -> [String : Any] {
- return finalDict[token] as! [String : Any]
- }
-
-
- func tokenUsesModel(withToken token: String) -> KMDesignTokenValue {
- let tokenDict = tokenUsesAction(withToken: token)
- var model = KMDesignTokenValue.init()
- let allKey = tokenDict.keys
- if allKey.contains("sizing") {
- model.sizing = tokenDict["sizing"] as Any
- }
- if allKey.contains("height") {
- model.height = tokenDict["height"] as Any
- }
- if allKey.contains("width") {
- model.width = tokenDict["width"] as Any
- }
- if allKey.contains("spacing") {
- model.spacing = tokenDict["spacing"] as Any
- }
- if allKey.contains("verticalPadding") {
- model.verticalPadding = tokenDict["verticalPadding"] as Any
- }
- if allKey.contains("horizontalPadding") {
- model.horizontalPadding = tokenDict["horizontalPadding"] as Any
- }
- if allKey.contains("paddingTop") {
- model.paddingTop = tokenDict["paddingTop"] as Any
- }
- if allKey.contains("paddingRight") {
- model.paddingRight = tokenDict["paddingRight"] as Any
- }
- if allKey.contains("paddingBottom") {
- model.paddingBottom = tokenDict["paddingBottom"] as Any
- }
- if allKey.contains("paddingLeft") {
- model.paddingLeft = tokenDict["paddingLeft"] as Any
- }
- if allKey.contains("itemSpacing") {
- model.itemSpacing = tokenDict["itemSpacing"] as Any
- }
- if allKey.contains("fill") {
- model.fill = tokenDict["fill"] as Any
- }
- if allKey.contains("border") {
- model.border = tokenDict["border"] as Any
- }
- if allKey.contains("borderColor") {
- model.borderColor = tokenDict["borderColor"] as Any
- }
- if allKey.contains("borderTop") {
- model.borderTop = tokenDict["borderTop"] as Any
- }
- if allKey.contains("borderRight") {
- model.borderRight = tokenDict["borderRight"] as Any
- }
- if allKey.contains("borderBottom") {
- model.borderBottom = tokenDict["borderBottom"] as Any
- }
- if allKey.contains("borderLeft") {
- model.borderLeft = tokenDict["borderLeft"] as Any
- }
- if allKey.contains("borderRadius") {
- model.borderRadius = tokenDict["borderRadius"] as Any
- }
- if allKey.contains("borderRadiusTopLeft") {
- model.borderRadiusTopLeft = tokenDict["borderRadiusTopLeft"] as Any
- }
- if allKey.contains("borderRadiusTopRight") {
- model.borderRadiusTopRight = tokenDict["borderRadiusTopRight"] as Any
- }
- if allKey.contains("borderRadiusBottomRight") {
- model.borderRadiusBottomRight = tokenDict["borderRadiusBottomRight"] as Any
- }
- if allKey.contains("borderRadiusBottomLeft") {
- model.borderRadiusBottomLeft = tokenDict["borderRadiusBottomLeft"] as Any
- }
- if allKey.contains("borderWidth") {
- model.borderWidth = tokenDict["borderWidth"] as Any
- }
- if allKey.contains("borderWidthTop") {
- model.borderWidthTop = tokenDict["borderWidthTop"] as Any
- }
- if allKey.contains("borderWidthRight") {
- model.borderWidthRight = tokenDict["borderWidthRight"] as Any
- }
- if allKey.contains("borderWidthBottom") {
- model.borderWidthBottom = tokenDict["borderWidthBottom"] as Any
- }
- if allKey.contains("borderWidthLeft") {
- model.borderWidthLeft = tokenDict["borderWidthLeft"] as Any
- }
- if allKey.contains("boxShadow") {
- model.boxShadow = tokenDict["boxShadow"] as Any
- }
- if allKey.contains("typography") {
- model.typography = tokenDict["typography"] as Any
- }
- return model
- }
-
-
- func designTokenUsesAction(withToken token: String,
- sizing : Any = "",
- height : Any = "",
- width : Any = "",
- spacing : Any = "",
- verticalPadding : Any = "",
- horizontalPadding: Any = "",
- paddingTop : Any = "",
- paddingRight : Any = "",
- paddingBottom : Any = "",
- paddingLeft : Any = "",
- itemSpacing : Any = "",
- fill : Any = "",
- border : Any = "",
- borderColor : Any = "",
- borderRadius : Any = "",
- borderWidth : Any = "",
- boxShadow : Any = "",
- typography : Any = "") -> Any {
- if (sizing is String) &&
- (height is String) &&
- (width is String) &&
- (spacing is String) &&
- (verticalPadding is String) &&
- (horizontalPadding is String) &&
- (paddingTop is String) &&
- (paddingRight is String) &&
- (paddingBottom is String) &&
- (paddingLeft is String) &&
- (itemSpacing is String) &&
- (fill is String) &&
- (border is String) &&
- (borderColor is String) &&
- (borderRadius is String) &&
- (borderWidth is String) &&
- (boxShadow is String) &&
- (typography is String) {
- return tokenUsesModel(withToken: token)
- } else {
- let tokenDict = tokenUsesAction(withToken: token)
- let allKey = tokenDict.keys
- if allKey.contains("sizing") {
- let sizingNode = tokenDict["sizing"]
- if nodeIsEmpty(control: sizing, node: sizingNode as Any) {
- if sizingNodeUsesAction(control: sizing, node: sizingNode as Any) {
- KMPrint("sizing node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("height") ||
- allKey.contains("width") ||
- allKey.contains("spacing") ||
- allKey.contains("verticalPadding") ||
- allKey.contains("horizontalPadding") ||
- allKey.contains("paddingTop") ||
- allKey.contains("paddingRight") ||
- allKey.contains("paddingBottom") ||
- allKey.contains("paddingLeft") ||
- allKey.contains("itemSpacing") {
- var node: Any!
- var control: Any!
- if allKey.contains("height") {
- node = tokenDict["height"] as Any
- control = height
- } else if allKey.contains("width") {
- node = tokenDict["width"] as Any
- control = width
- } else if allKey.contains("spacing") {
- node = tokenDict["spacing"] as Any
- control = spacing
- } else if allKey.contains("verticalPadding") {
- node = tokenDict["verticalPadding"] as Any
- control = verticalPadding
- } else if allKey.contains("horizontalPadding") {
- node = tokenDict["horizontalPadding"] as Any
- control = horizontalPadding
- } else if allKey.contains("paddingTop") {
- node = tokenDict["paddingTop"] as Any
- control = paddingTop
- } else if allKey.contains("paddingRight") {
- node = tokenDict["paddingRight"] as Any
- control = paddingRight
- } else if allKey.contains("paddingBottom") {
- node = tokenDict["paddingBottom"] as Any
- control = paddingBottom
- } else if allKey.contains("paddingLeft") {
- node = tokenDict["paddingLeft"] as Any
- control = paddingLeft
- } else if allKey.contains("itemSpacing") {
- node = tokenDict["itemSpacing"] as Any
- control = itemSpacing
- }
- if nodeIsEmpty(control: control as Any, node: node as Any) {
- if layoutConstraintNodeUsesAction(control: control as Any, node: node as Any) {
- KMPrint("node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("fill") {
- let fillNode = tokenDict["fill"]
- if nodeIsEmpty(control: fill, node: fillNode as Any) {
- if fillNodeUsesAction(control: fill, node: fillNode as Any) {
- KMPrint("fill node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("border") {
- let borderNode = tokenDict["border"]
- if nodeIsEmpty(control: border, node: borderNode as Any) {
- if borderNodeUsesAction(control: border, node: borderNode as Any) {
- KMPrint("border node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("borderColor") {
- let borderColorNode = tokenDict["borderColor"]
- if nodeIsEmpty(control: borderColor, node: borderColorNode as Any) {
- if borderColorNodeUsesAction(control: borderColor, node: borderColorNode as Any) {
- KMPrint("borderColor node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("borderRadius") {
- let borderRadiusNode = tokenDict["borderRadius"]
- if nodeIsEmpty(control: borderRadius, node: borderRadiusNode as Any) {
- if borderRadiusNodeUsesAction(control: borderRadius, node: borderRadiusNode as Any) {
- KMPrint("borderRadius node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("borderWidth") {
- let borderWidthNode = tokenDict["borderWidth"]
- if nodeIsEmpty(control: borderWidth, node: borderWidthNode as Any) {
- if borderWidthNodeUsesAction(control: borderWidth, node: borderWidthNode as Any) {
- KMPrint("borderWidth node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("boxShadow") {
- let boxShadowNode = tokenDict["boxShadow"]
- if nodeIsEmpty(control: boxShadow, node: boxShadowNode as Any) {
- if boxShadowNodeUsesAction(control: boxShadow, node: boxShadowNode as Any) {
- KMPrint("boxShadow node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- if allKey.contains("typography") {
- let typographyNode = tokenDict["typography"]
- if nodeIsEmpty(control: typography, node: typographyNode as Any) {
- if typographyNodeUsesAction(control: typography, node: typographyNode as Any) {
- KMPrint("typography node user success")
- } else {
- return tokenUsesModel(withToken: token)
- }
- } else {
- return tokenUsesModel(withToken: token)
- }
- }
- }
- return tokenUsesModel(withToken: token)
- }
-
-
-
- func layoutConstraintNodeUsesAction(control: Any, node: Any) -> Bool {
- if control is NSLayoutConstraint {
- (control as! NSLayoutConstraint).constant = (node as! String).stringToCGFloat()
- return true
- }
- return false
- }
-
-
-
- func nodeIsEmpty(control: Any, node: Any) -> Bool {
- if (control is String) {
- return false
- } else {
- if (node is String) {
- if (node as! String == "") {
- return false
- } else {
- return true
- }
- } else if (node is [String : Any]) {
- return true
- }
- }
- return false
- }
- }
- extension KMDesignToken: KMJSONParserDelegate {
- func parser(_ parser: KMJSONParser, success responseObj: Any) {
- finalDict = responseObj as! [String : Any]
- }
- }
|