Model.swift 19.4 KB
Newer Older
1
2
3
// This file was generated by json2swift. https://github.com/ijoshsmith/json2swift

import Foundation
4
import RealmSwift
Eyad Hasan's avatar
Eyad Hasan committed
5
import ObjectMapper
Zach Knox's avatar
Zach Knox committed
6
import ObjectMapper_Realm
7
8
9
//
// MARK: - Data Model
//
10

Zach Knox's avatar
Zach Knox committed
11
enum Day: Int {
12
13
14
15
16
17
18
    case Monday = 0
    case Tuesday = 1
    case Wednesday = 2
    case Thursday = 3
    case Friday = 4
    case Saturday = 5
    case Sunday = 6
19

20
    // Add functions here later if we need them
Zach Knox's avatar
Zach Knox committed
21
22
}

Sgangele21's avatar
Sgangele21 committed
23
class Facility: Object, MapContext, Mappable {
Zach Knox's avatar
Zach Knox committed
24
25
    @objc dynamic var slug = ""
    @objc dynamic var facilityName = ""
Zach Knox's avatar
Zach Knox committed
26
27
    @objc dynamic var facilityLocation: Locations? = Locations()
    @objc dynamic var category: Categories? = Categories()
28
	var facilityTags: List<FacilityTag>?  = List<FacilityTag>()
Zach Knox's avatar
Zach Knox committed
29
30
    @objc dynamic var mainSchedule: Schedule? = Schedule()
	var specialSchedules: List<Schedule>? = List<Schedule>()
31
32
33
34
35
	var labels: List<FacilityTag>? = List<FacilityTag>()
	@objc dynamic var tapingoURL = ""
	@objc dynamic var note = ""
	@objc dynamic var logoURL = ""
	
Eyad Hasan's avatar
Eyad Hasan committed
36
37
38
39
40
41
42
43
    required convenience init?(map: Map) {
        self.init()
    }
    func mapping(map: Map){
        slug <- map["slug"]
        facilityName <- map["facility_name"]
        facilityLocation <- map["facility_location"]
        category <- map["facility_category"]
44
        facilityTags <- (map["facility_product_tags"], TagTransform())
Eyad Hasan's avatar
Eyad Hasan committed
45
        mainSchedule <- map["main_schedule"]
Zach Knox's avatar
Zach Knox committed
46
        specialSchedules <- (map["special_schedules"], ListTransform<Schedule>())
47
48
49
50
51
52
		labels <- (map["facility_labels"], TagTransform())
		tapingoURL <- map["tapingo_url"]
		note <- map["note"]
		logoURL <- map["logo"]
		
		
Eyad Hasan's avatar
Eyad Hasan committed
53
    }
54

Eyad Hasan's avatar
Eyad Hasan committed
55
56
}

Sgangele21's avatar
Sgangele21 committed
57
class Locations: Object, Mappable {
Zach Knox's avatar
Zach Knox committed
58
59
60
61
62
63
64
    @objc dynamic var id = 0
    @objc dynamic var created = ""
    @objc dynamic var lastmodified = ""
    @objc dynamic var building = ""
    @objc dynamic var address = ""
    @objc dynamic var campus = ""
    @objc dynamic var onCampus = false
Jesse Scearce's avatar
Jesse Scearce committed
65
    @objc dynamic var abbreviation = ""
Zach Knox's avatar
Zach Knox committed
66
	@objc dynamic var coordinates: Coordinates? = Coordinates()
67

Eyad Hasan's avatar
Eyad Hasan committed
68
    required convenience init?(map: Map){
69
		self.init()
Eyad Hasan's avatar
Eyad Hasan committed
70
    }
71
72
	
	func mapping(map: Map){
Eyad Hasan's avatar
Eyad Hasan committed
73
74
75
76
77
78
79
        id <- map["id"]
        created <- map["created"]
        lastmodified <- map["modified"]
        building <- map["building"]
        address <- map["address"]
        campus <- map["campus_region"]
        onCampus <- map["on_campus"]
Jesse Scearce's avatar
Jesse Scearce committed
80
        abbreviation <- map["friendly_building"]
Zach Knox's avatar
Zach Knox committed
81
		coordinates <- map["coordinate_location"]
Eyad Hasan's avatar
Eyad Hasan committed
82
    }
83
84
85
86
87
88
89
90
91
92
93
94
	
	func equals(_ another: Locations) -> Bool {
		if  self.building == another.building &&
		    self.address == another.address &&
		    self.campus == another.campus &&
			self.onCampus == another.onCampus {
			return true
		}
		else {
			return false
		}
	}
95

Eyad Hasan's avatar
Eyad Hasan committed
96
97
}

Zach Knox's avatar
Zach Knox committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class Coordinates: Object, Mappable {
	var coords: List<Double>? = List<Double>()
	@objc dynamic var type = ""
	
	required convenience init?(map: Map){
		self.init()
	}
	
	func mapping(map: Map) {
		coords <- (map["coordinates"], CoordTransform())
		type <- map["type"]
	}
}

Sgangele21's avatar
Sgangele21 committed
112
class Categories: Object, Mappable {
Zach Knox's avatar
Zach Knox committed
113
114
115
116
    @objc dynamic var id = 0
    @objc dynamic var created = ""
    @objc dynamic var modified = ""
    @objc dynamic var categoryName = ""
117

Eyad Hasan's avatar
Eyad Hasan committed
118
119
120
121
122
123
124
125
126
    required convenience init?(map: Map){
        self.init()
    }
    func mapping(map: Map){
        id <- map["id"]
        created <- map["created"]
        modified <- map["modified"]
        categoryName <- map["name"]
    }
127
128
129
130
	
	func equals(_ another: Categories) -> Bool {
		return another.categoryName == self.categoryName
	}
131

Eyad Hasan's avatar
Eyad Hasan committed
132
}
Eyad Hasan's avatar
Eyad Hasan committed
133

134
135
136
137
138
139
140
141
142
143
144
145
class FacilityTag: Object, Mappable {
    @objc dynamic var tag = ""
	
	
	required convenience init?(map: Map){
		self.init()
	}
	
	func mapping(map: Map) {
		tag <- map["facility_product_tags"]
		tag <- map["facility_labels"]
	}
146

147
	
Eyad Hasan's avatar
Eyad Hasan committed
148
149
}

Zach Knox's avatar
Zach Knox committed
150
class Schedule: Object, Mappable {
Zach Knox's avatar
Zach Knox committed
151
    @objc dynamic var id = 0
Eyad Hasan's avatar
Eyad Hasan committed
152
    var openTimes = List<OpenTimes>()
Zach Knox's avatar
Zach Knox committed
153
154
155
156
157
    @objc dynamic var lastModified = ""
    @objc dynamic var name = ""
    @objc dynamic var validStart = ""
    @objc dynamic var validEnd = ""
    @objc dynamic var twentyFourHours = false
158
159


Eyad Hasan's avatar
Eyad Hasan committed
160
161
162
    required convenience init?(map: Map){
        self.init()
    }
163

Eyad Hasan's avatar
Eyad Hasan committed
164
165
    func mapping(map: Map){
        id <- map["id"]
166
167
168
169
170
171
172
173
        // This is a way around mapping to a list object
        var openTimesList: [OpenTimes]?
        openTimesList <- map["open_times"]
        if let openTimesList = openTimesList {
            for openTime in openTimesList {
                self.openTimes.append(openTime)
            }
        }
Eyad Hasan's avatar
Eyad Hasan committed
174
175
176
177
178
179
        lastModified <- map["modified"]
        name <- map["name"]
        validStart <- map["valid_start"]
        validEnd <- map["valid_end"]
        twentyFourHours <- map["twenty_four_hours"]
    }
Eyad Hasan's avatar
Eyad Hasan committed
180
}
181

Sgangele21's avatar
Sgangele21 committed
182
class SpecialSchedule: Object, Mappable {
183

Sgangele21's avatar
Sgangele21 committed
184
185
186
    convenience required init?(map: Map) {
        self.init()
    }
187

Zach Knox's avatar
Zach Knox committed
188
    @objc dynamic var id = 0
Sgangele21's avatar
Sgangele21 committed
189
    var openTimes = List<OpenTimes>()
Zach Knox's avatar
Zach Knox committed
190
191
192
193
194
    @objc dynamic var lastModified = ""
    @objc dynamic var name = ""
    @objc dynamic var validStart = ""
    @objc dynamic var validEnd = ""
    @objc dynamic var twentyFourHours = false
195

Sgangele21's avatar
Sgangele21 committed
196
197
    func mapping(map: Map){
        id <- map["id"]
198
199
200
201
202
203
204
205
        // This is a way around mapping to a list object
        var openTimesList: [OpenTimes]?
        openTimesList <- map["open_times"]
        if let openTimesList = openTimesList {
            for openTime in openTimesList {
                self.openTimes.append(openTime)
            }
        }
Sgangele21's avatar
Sgangele21 committed
206
207
208
209
210
211
        lastModified <- map["modified"]
        name <- map["name"]
        validStart <- map["valid_start"]
        validEnd <- map["valid_end"]
        twentyFourHours <- map["twenty_four_hours"]
    }
212

213
214
215
}


Sgangele21's avatar
Sgangele21 committed
216
class OpenTimes: Object, Mappable {
Zach Knox's avatar
Zach Knox committed
217
218
219
220
221
222
    @objc dynamic var schedule = 0
    @objc dynamic var lastModified = ""
    @objc dynamic var startDay = 0
    @objc dynamic var endDay = 0
    @objc dynamic var startTime = ""
    @objc dynamic var endTime = ""
223

Sgangele21's avatar
Sgangele21 committed
224
225
226
    convenience required init?(map: Map) {
        self.init()
    }
227

Sgangele21's avatar
Sgangele21 committed
228
    func mapping(map: Map){
229
230
        schedule <- map["schedule"]
        lastModified <- map["modified"]
Sgangele21's avatar
Sgangele21 committed
231
232
233
234
235
        startDay <- map["start_day"]
        endDay <- map["end_day"]
        startTime <- map["start_time"]
        endTime <- map["end_time"]
    }
236

Eyad Hasan's avatar
Eyad Hasan committed
237
}
Eyad Hasan's avatar
Eyad Hasan committed
238

Zach Knox's avatar
Zach Knox committed
239
class Alert: Object, MapContext, Mappable {
Zach Knox's avatar
Zach Knox committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	@objc dynamic var id = 0
	@objc dynamic var created = ""
	@objc dynamic var lastModified = ""
	@objc dynamic var urgency = ""
	@objc dynamic var message = ""
	@objc dynamic var startDate = ""
	@objc dynamic var endDate = ""
	
	convenience required init?(map: Map) {
		self.init()
	}
	
	func mapping(map: Map){
		id <- map["id"]
		created <- map["created"]
		lastModified <- map["modified"]
		urgency <- map["urgency_tag"]
		message <- map["message"]
		startDate <- map["start_datetime"]
		endDate <- map["end_datetime"]
	}
}

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Updated for Swift 4, based on https://gist.github.com/Jerrot/fe233a94c5427a4ec29b but I removed the generics sorry code reuse
class TagTransform : TransformType {
	typealias Object = List<FacilityTag>
	typealias JSON = [String]
	
	func transformFromJSON(_ value: Any?) -> List<FacilityTag>? {
		let result = List<FacilityTag>()
		if let tempArr = value as! [String]? {
			for entry in tempArr {
				let tag = FacilityTag()
				tag.tag = entry
				result.append(tag)
			}
		}
		return result
	}
	
	func transformToJSON(_ value: List<FacilityTag>?) -> [String]? {
		if (value!.count > 0) {
			var result = [String]()
			for entry in value! {
				result.append(entry.tag)
			}
			return result
		}
		return nil
	}
}

Zach Knox's avatar
Zach Knox committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
class CoordTransform: TransformType {
	typealias Object = List<Double>
	typealias JSON = [String]
	
	func transformFromJSON(_ value: Any?) -> List<Double>? {
		let result = List<Double>()
		if let tempArr = value as! [Double]? {
			var count = 0
			for entry in tempArr {
				result.insert(entry, at: count)
				count += 1
			}
		}
		return result
	}
	
	func transformToJSON(_ value: List<Double>?) -> [String]? {
		if (value!.count > 0) {
			var result = [String]()
			for entry in value! {
				result.append("\(entry)")
			}
			return result
		}
		return nil
	}
	
}
320
321


Eyad Hasan's avatar
Eyad Hasan committed
322
/**struct OpenTimes: CreatableFromJSON { // TODO: Rename this struct
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    let endDay: Int
    let endTime: Date
    let id: Int
    let lastModified: Date
    let schedule: Int
    let startDay: Int
    let startTime: Date
    init(endDay: Int, endTime: Date, id: Int, lastModified: Date, schedule: Int, startDay: Int, startTime: Date) {
        self.endDay = endDay
        self.endTime = endTime
        self.id = id
        self.lastModified = lastModified
        self.schedule = schedule
        self.startDay = startDay
        self.startTime = startTime
    }
    init?(json: [String: Any]) {
        guard let endDay = json["end_day"] as? Int else { return nil }
        guard let endTime = Date(json: json, key: "end_time", format: "HH:mm:ss") else { return nil }
        guard let id = json["id"] as? Int else { return nil }
        guard let lastModified = Date(json: json, key: "last_modified", format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") else { return nil }
        guard let schedule = json["schedule"] as? Int else { return nil }
        guard let startDay = json["start_day"] as? Int else { return nil }
        guard let startTime = Date(json: json, key: "start_time", format: "HH:mm:ss") else { return nil }
        self.init(endDay: endDay, endTime: endTime, id: id, lastModified: lastModified, schedule: schedule, startDay: startDay, startTime: startTime)
    }
}


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
struct Facility: CreatableFromJSON { // TODO: Rename this struct
    let category: Any?
    let id: Int
    let lastModified: Date
    let location: String
    let mainSchedule: MainSchedule
    let name: String
    let specialSchedules: SpecialSchedules?
    init(category: Any?, id: Int, lastModified: Date, location: String, mainSchedule: MainSchedule, name: String, specialSchedules: SpecialSchedules?) {
        self.category = category
        self.id = id
        self.lastModified = lastModified
        self.location = location
        self.mainSchedule = mainSchedule
        self.name = name
        self.specialSchedules = specialSchedules
    }
    init?(json: [String: Any]) {
        guard let id = json["id"] as? Int else { return nil }
        guard let lastModified = Date(json: json, key: "last_modified", format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") else { return nil }
        guard let location = json["location"] as? String else { return nil }
        guard let mainSchedule = MainSchedule(json: json, key: "main_schedule") else { return nil }
        guard let name = json["name"] as? String else { return nil }
        let specialSchedules = SpecialSchedules(json: json, key: "special_schedules")
        let category = json["category"] as? [Any?]
        self.init(category: category, id: id, lastModified: lastModified, location: location, mainSchedule: mainSchedule, name: name, specialSchedules: specialSchedules)
    }
    struct MainSchedule: CreatableFromJSON { // TODO: Rename this struct
        let id: Int
        let lastModified: Date
        let name: String
        let openTimes: [OpenTimes]
        let validEnd: Date
        let validStart: Date
        init(id: Int, lastModified: Date, name: String, openTimes: [OpenTimes], validEnd: Date, validStart: Date) {
            self.id = id
            self.lastModified = lastModified
            self.name = name
            self.openTimes = openTimes
            self.validEnd = validEnd
            self.validStart = validStart
        }
        init?(json: [String: Any]) {
            guard let id = json["id"] as? Int else { return nil }
            guard let lastModified = Date(json: json, key: "last_modified", format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") else { return nil }
            guard let name = json["name"] as? String else { return nil }
            guard let openTimes = OpenTimes.createRequiredInstances(from: json, arrayKey: "open_times") else { return nil }
            guard let validEnd = Date(json: json, key: "valid_end", format: "yyyy-MM-dd") else { return nil }
            guard let validStart = Date(json: json, key: "valid_start", format: "yyyy-MM-dd") else { return nil }
            self.init(id: id, lastModified: lastModified, name: name, openTimes: openTimes, validEnd: validEnd, validStart: validStart)
        }
403

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    }
    struct SpecialSchedules: CreatableFromJSON { // TODO: Rename this struct
        let id: Int
        let lastModified: Date
        let name: String
        let openTimes: [OpenTimes]
        let validEnd: Date
        let validStart: Date
        init(id: Int, lastModified: Date, name: String, openTimes: [OpenTimes], validEnd: Date, validStart: Date) {
            self.id = id
            self.lastModified = lastModified
            self.name = name
            self.openTimes = openTimes
            self.validEnd = validEnd
            self.validStart = validStart
        }
        init?(json: [String: Any]) {
            guard let id = json["id"] as? Int else { return nil }
            guard let lastModified = Date(json: json, key: "last_modified", format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") else { return nil }
            guard let name = json["name"] as? String else { return nil }
            guard let openTimes = OpenTimes.createRequiredInstances(from: json, arrayKey: "open_times") else { return nil }
            guard let validEnd = Date(json: json, key: "valid_end", format: "yyyy-MM-dd") else { return nil }
            guard let validStart = Date(json: json, key: "valid_start", format: "yyyy-MM-dd") else { return nil }
            self.init(id: id, lastModified: lastModified, name: name, openTimes: openTimes, validEnd: validEnd, validStart: validStart)
        }
    }
}
Eyad Hasan's avatar
Eyad Hasan committed
431
**/
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// MARK: - JSON Utilities
//
/// Adopted by a type that can be instantiated from JSON data.
protocol CreatableFromJSON {
    /// Attempts to configure a new instance of the conforming type with values from a JSON dictionary.
    init?(json: [String: Any])
}

extension CreatableFromJSON {
    /// Attempts to configure a new instance using a JSON dictionary selected by the `key` argument.
    init?(json: [String: Any], key: String) {
        guard let jsonDictionary = json[key] as? [String: Any] else { return nil }
        self.init(json: jsonDictionary)
    }
447

448
449
450
451
452
453
    /// Attempts to produce an array of instances of the conforming type based on an array in the JSON dictionary.
    /// - Returns: `nil` if the JSON array is missing or if there is an invalid/null element in the JSON array.
    static func createRequiredInstances(from json: [String: Any], arrayKey: String) -> [Self]? {
        guard let jsonDictionaries = json[arrayKey] as? [[String: Any]] else { return nil }
        return createRequiredInstances(from: jsonDictionaries)
    }
454

455
456
457
458
459
460
461
462
463
464
    /// Attempts to produce an array of instances of the conforming type based on an array of JSON dictionaries.
    /// - Returns: `nil` if there is an invalid/null element in the JSON array.
    static func createRequiredInstances(from jsonDictionaries: [[String: Any]]) -> [Self]? {
        var array = [Self]()
        for jsonDictionary in jsonDictionaries {
            guard let instance = Self.init(json: jsonDictionary) else { return nil }
            array.append(instance)
        }
        return array
    }
465

466
467
468
469
470
471
    /// Attempts to produce an array of instances of the conforming type, or `nil`, based on an array in the JSON dictionary.
    /// - Returns: `nil` if the JSON array is missing, or an array with `nil` for each invalid/null element in the JSON array.
    static func createOptionalInstances(from json: [String: Any], arrayKey: String) -> [Self?]? {
        guard let array = json[arrayKey] as? [Any] else { return nil }
        return createOptionalInstances(from: array)
    }
472

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    /// Attempts to produce an array of instances of the conforming type, or `nil`, based on an array.
    /// - Returns: An array of instances of the conforming type and `nil` for each invalid/null element in the source array.
    static func createOptionalInstances(from array: [Any]) -> [Self?] {
        return array.map { item in
            if let jsonDictionary = item as? [String: Any] {
                return Self.init(json: jsonDictionary)
            }
            else {
                return nil
            }
        }
    }
}

extension Date {
    // Date formatters are cached because they are expensive to create. All cache access is performed on a serial queue.
    private static let cacheQueue = DispatchQueue(label: "DateFormatterCacheQueue")
    private static var formatterCache = [String: DateFormatter]()
    private static func dateFormatter(with format: String) -> DateFormatter {
        if let formatter = formatterCache[format] { return formatter }
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.calendar = Calendar(identifier: .gregorian)
497
        formatter.timeZone = TimeZone(secondsFromGMT: NSTimeZone.local.secondsFromGMT())! // changed to be the user's local time.
498
499
500
        formatterCache[format] = formatter
        return formatter
    }
501

502
503
504
505
506
    static func parse(string: String, format: String) -> Date? {
        var formatter: DateFormatter!
        cacheQueue.sync { formatter = dateFormatter(with: format) }
        return formatter.date(from: string)
    }
507

508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
    init?(json: [String: Any], key: String, format: String) {
        guard let string = json[key] as? String else { return nil }
        guard let date = Date.parse(string: string, format: format) else { return nil }
        self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate)
    }
}

extension URL {
    init?(json: [String: Any], key: String) {
        guard let string = json[key] as? String else { return nil }
        self.init(string: string)
    }
}

extension Double {
    init?(json: [String: Any], key: String) {
        // Explicitly unboxing the number allows an integer to be converted to a double,
        // which is needed when a JSON attribute value can have either representation.
        guard let nsNumber = json[key] as? NSNumber else { return nil }
        self.init(_: nsNumber.doubleValue)
    }
}

extension Array where Element: NSNumber {
    // Convert integers to doubles, for example [1, 2.0] becomes [1.0, 2.0]
    // This is necessary because ([1, 2.0] as? [Double]) yields nil.
    func toDoubleArray() -> [Double] {
        return map { $0.doubleValue }
    }
}

extension Array where Element: CustomStringConvertible {
    func toDateArray(withFormat format: String) -> [Date]? {
        var dateArray = [Date]()
        for string in self {
            guard let date = Date.parse(string: String(describing: string), format: format) else { return nil }
            dateArray.append(date)
        }
        return dateArray
    }
548

549
550
551
    func toURLArray() -> [URL]? {
        var urlArray = [URL]()
        for string in self {
552
553
            guard let url = URL(string: String(describing: string)) else { return nil }
            urlArray.append(url)
554
555
556
557
558
559
560
561
562
        }
        return urlArray
    }
}

extension Array where Element: Any {
    func toOptionalValueArray<Value>() -> [Value?] {
        return map { ($0 is NSNull) ? nil : ($0 as? Value) }
    }
563

564
565
566
567
568
569
    func toOptionalDateArray(withFormat format: String) -> [Date?] {
        return map { item in
            guard let string = item as? String else { return nil }
            return Date.parse(string: string, format: format)
        }
    }
570

571
572
573
574
575
576
    func toOptionalDoubleArray() -> [Double?] {
        return map { item in
            guard let nsNumber = item as? NSNumber else { return nil }
            return nsNumber.doubleValue
        }
    }
577

578
579
580
581
582
583
584
    func toOptionalURLArray() -> [URL?] {
        return map { item in
            guard let string = item as? String else { return nil }
            return URL(string: string)
        }
    }
}