Skip to content

Instantly share code, notes, and snippets.

@vikdenic
Created October 2, 2017 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikdenic/a780e973b8e0e2a94ffe9b8dd0d92938 to your computer and use it in GitHub Desktop.
Save vikdenic/a780e973b8e0e2a94ffe9b8dd0d92938 to your computer and use it in GitHub Desktop.
//
// Allergen.swift
// SpokinModel
//
// Created by Kevin McQuown on 9/21/17.
// Copyright © 2017 Spokin. All rights reserved.
//
import Foundation
public class EmojiItem {
public var itemCategory: EmojiItemCategory!
public var colorCode: String?
public var color: UIColor?
public var styleCode: String?
public var isSelected = false
init(dict: [String: Any]) {
if let colorCode = dict["color"] as? String {
self.colorCode = colorCode
}
if let styleCode = dict["style"] as? String {
self.styleCode = styleCode
}
if let colorHexString = dict["colorHex"] as? String {
self.color = UIColor(hex: colorHexString)
}
}
}
public enum EmojiCategoryType: String {
case skinColor
case hairColor
case hairStyle
case eyeColor
case eyeShape
case facialHair
public func toInt() -> Int {
switch self {
case .skinColor: return 0
case .hairColor: return 1
case .hairStyle: return 2
case .eyeColor: return 3
case .eyeShape: return 4
case .facialHair: return 5
}
}
}
public class EmojiItemCategory {
public var name: String!
public var itemCategoryType: EmojiCategoryType! {
return EmojiCategoryType(rawValue: name)
}
public var displayName: String!
public var gender: Gender!
public var emojiItems = [EmojiItem]()
init(dict: [String: Any], genderString: String) {
self.name = dict["name"] as? String
self.gender = Gender(rawValue: genderString)
self.displayName = dict["displayName"] as? String
var setFirstItemAsSelected = false
for subItemDict in (dict["subItems"] as! [[String : Any]]) {
let emojiItem = EmojiItem(dict: subItemDict)
emojiItem.itemCategory = self
if setFirstItemAsSelected == false {
emojiItem.isSelected = true
setFirstItemAsSelected = true
}
emojiItems.append(emojiItem)
}
}
}
extension UIColor {
convenience init(hex: String) {
let scanner = Scanner(string: hex)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(
red: CGFloat(r) / 0xff,
green: CGFloat(g) / 0xff,
blue: CGFloat(b) / 0xff, alpha: 1
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment