iTSangar
3/22/2016 - 9:08 PM

Model.swift

//
//  Model.swift
//
//  Created by iTSangar on 11/5/15.
//  Copyright © 2015 iTSangar. All rights reserved.
//

import UIKit
import ObjectMapper
import SwiftDate

//**************************************
//MARK: - Error Map
//**************************************
class Error: Mappable {
  var error: Bool!
  var message: String!
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    error     <- map["error"]
    message   <- map["message"]
  }
}

//**************************************
//MARK: - User Map
//**************************************
class User: NSObject, NSCoding, Mappable {
  var id: Int!
  var name: String!
  var image: String?
  var email: String!
  var key: String!
  var active: Bool?
  var pwd_default: Bool!
  var created_at: String!
  
  override init() {}
  
  required init?(_ map: Map) {
    super.init()
    mapping(map)
  }
  
  required init(coder aDecoder: NSCoder) {
    super.init()
    id = aDecoder.decodeObjectForKey("id") as! Int
    name = aDecoder.decodeObjectForKey("name") as! String
    image = aDecoder.decodeObjectForKey("image") as? String
    email = aDecoder.decodeObjectForKey("email") as! String
    key = aDecoder.decodeObjectForKey("key") as! String
    active = aDecoder.decodeObjectForKey("active") as? Bool
    pwd_default = aDecoder.decodeObjectForKey("default") as! Bool
    created_at = aDecoder.decodeObjectForKey("created") as! String
  }
  
  func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(id, forKey: "id")
    aCoder.encodeObject(name, forKey: "name")
    aCoder.encodeObject(image, forKey: "image")
    aCoder.encodeObject(email, forKey: "email")
    aCoder.encodeObject(key, forKey: "key")
    aCoder.encodeObject(active, forKey: "active")
    aCoder.encodeObject(pwd_default, forKey: "default")
    aCoder.encodeObject(created_at, forKey: "created")
  }
  
  func mapping(map: Map) {
    id            <- map["id"]
    name          <- map["name"]
    image         <- map["image"]
    email         <- map["email"]
    key           <- map["api_key"]
    active        <- map["active"]
    pwd_default   <- map["password_default"]
    created_at    <- map["created_at"]
  }
}

//**************************************
//MARK: - Forum Map
//**************************************
class Forum: Mappable {
  var id: Int!
  var title: String!
  var category: String!
  var created_by: String!
  var created_by_image: String?
  var created_at: NSDate!
  var count_replies: Int?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id                <- map["id"]
    title             <- map["title"]
    category          <- map["category"]
    created_by        <- map["created_by"]
    created_by_image  <- map["created_by_image"]
    created_at        <- (map["created_at"], DateFormatter())
    count_replies     <- map["count_replies"]
  }
}

class Forums: Mappable {
  var forums: [Forum]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    forums    <- map["rows"]
  }
}

//**************************************
//MARK: - Calendar Map
//**************************************
enum Option: String {
  case Sim = "sim"
  case Nao = "não"
  case Talvez = "talvez"
}

class Calendar: Mappable {
  var id: Int!
  var title: String!
  var start: NSDate!
  var end: NSDate!
  var event_hour: String?
  var status: Bool!
  var created_at: NSDate!
  var total_responses: Int?
  var comments: Int?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id              <- map["id"]
    title           <- map["title"]
    start           <- (map["start"], DateFormatter())
    end             <- (map["end"], DateFormatter())
    event_hour      <- map["event_hour"]
    status          <- map["status"]
    created_at      <- (map["created_at"], DateFormatter())
    total_responses <- map["total_responses"]
    comments        <- map["comments"]
  }
}

class Calendars: Mappable {
  var calendars: [Calendar]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    calendars    <- map["rows"]
  }
}

//**************************************
//MARK: - Survey Map
//**************************************
class Survey: Mappable {
  var id: Int!
  var question: String!
  var status: Bool!
  var created_at: NSDate!
  var finished_at: NSDate!
  var total_options: Int!
  var total_votes: Int?
  var winner: String?
  var winner_id: Int?
  var comments: Int?
  var options: [Options]?
  var votes: [Votes]?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id              <- map["id"]
    question        <- map["question"]
    status          <- map["status"]
    created_at      <- (map["created_at"], DateFormatter())
    finished_at     <- (map["finished_at"], DateFormatter())
    total_options   <- map["total_options"]
    total_votes     <- map["total_votes"]
    winner          <- map["winner"]
    winner_id       <- map["winner_id"]
    comments        <- map["comments"]
    options         <- map["options"]
    votes           <- map["votes"]
  }
}

class Surveys: Mappable {
  var surveys: [Survey]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    surveys    <- map["rows"]
  }
}

class Options: Mappable {
  var id: Int!
  var text: String!
  var percent: String?
  var voted: Int?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id          <- map["id"]
    text        <- map["text"]
    percent     <- map["percent"]
    voted       <- map["voted"]
  }
}

class Votes: Mappable {
  var user: String!
  var user_id: Int!
  var user_image: String?
  var option: Int!
  var voted_at: NSDate!
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    user         <- map["user"]
    user_id      <- map["user_id"]
    user_image   <- map["user_image"]
    option       <- map["option"]
    voted_at     <- (map["voted_at"], DateFormatter())
  }
}

//**************************************
//MARK: - Reply Map
//**************************************
class Reply: Mappable {
  var id: Int!
  var text: String!
  var created_at: NSDate!
  var created_by: String!
  var created_by_id: Int!
  var created_by_image: String?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id                <- map["id"]
    text              <- map["text"]
    created_at        <- (map["created_at"], DateFormatter())
    created_by        <- map["created_by"]
    created_by_id     <- map["created_by_id"]
    created_by_image  <- map["created_by_image"]
  }
}

class Replies: Mappable {
  var replies: [Reply]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    replies    <- map["rows"]
  }
}

//**************************************
//MARK: - News Map
//**************************************
class News: Mappable {
  var id: Int!
  var title: String!
  var date: NSDate!
  var image: String?
  var link: String?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id        <- map["id"]
    title     <- map["title"]
    date      <- (map["date"], DateFormatter())
    image     <- map["image"]
    link      <- map["link"]
  }
}

//**************************************
//MARK: - Facebook Map
//**************************************
enum FBStatus: String {
  case Share = "shared_story"
  case Photo = "added_photos"
}

class Facebook: Mappable {
  var id: String!
  var title: String?
  var date: NSDate!
  var user_name: String?
  var image: String?
  var link: String?
  var status: FBStatus!
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id          <- map["id"]
    title       <- map["title"]
    date        <- (map["date"], DateFormatter())
    user_name   <- map["user_name"]
    image       <- map["image"]
    link        <- map["link"]
    status      <- map["status"]
  }
}

//**************************************
//MARK: - Twitter Map
//**************************************
class Twitter: Mappable {
  var id: String!
  var title: String?
  var date: NSDate!
  var user_name: String?
  var user_name_screen: String?
  var user_image: String?
  var link: String?
  
  required init?(_ map: Map) {}
  
  func mapping(map: Map) {
    id                  <- map["id"]
    title               <- map["title"]
    date                <- (map["date"], DateFormatter())
    user_name           <- map["user_name"]
    user_name_screen    <- map["user_name_screen"]
    user_image          <- map["user_image"]
    link                <- map["link"]
  }
}

//**************************************
//MARK: - Timeline Map
//**************************************
enum Type: Int {
  case Forum = 1
  case Calendar = 2
  case Survey = 3
  case News = 4
  case Facebook = 5
  case Twitter = 6
}

class TTypes: Mappable {
  var type: Type?
  var name: String!
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    type    <- map["type"]
    name    <- map["type_name"]
  }
}

class TimelineType: Mappable {
  var types: [TTypes]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    types    <- map["rows"]
  }
}

class Timeline: Mappable {
  var rows: [AnyObject]?
  
  required init?(_ map: Map) {
  }
  
  func mapping(map: Map) {
    rows    <- map["rows"]
  }
}

//**************************************
//MARK: - Format Date
//**************************************
public class DateFormatter: TransformType {
  public typealias Object = NSDate
  public typealias JSON = String
  
  public func transformFromJSON(value: AnyObject?) -> NSDate? {
    if let dateString = value as? String {
      return dateString.toDate(DateFormat.Custom("yyyy/MM/dd HH:mm:ss"))
    }
    return nil
  }
  
  public func transformToJSON(value: NSDate?) -> String? {
    if let date = value {
      return date.toString(DateFormat.Custom("yyyy/MM/dd HH:mm:ss"))
    }
    return nil
  }
}