forked from asuc-octo/berkeley-mobile-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiningData.swift
166 lines (129 loc) · 3.85 KB
/
DiningData.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* DiningData.swift
*
* This file defines all the data types related to dining.
* Note: DiningHall and Item are declared as classes, not structs to avoid copying potentially large data.
*/
import Foundation
import UIKit
/**
* `MealType` is one of four:
* - Breakfast
* - Lunch
* - Dinner
* - Late night
*/
enum MealType: String
{
case
breakfast = "breakfast",
lunch = "lunch",
limited_lunch = "limited_lunch",
dinner = "dinner",
limited_dinner = "limited_dinner"
static let allValues = [breakfast, lunch, limited_lunch, dinner, limited_dinner]
var name: String {
return self.rawValue.capitalized
}
}
/// Represents a single meal slot with a `DiningMenu` and open hours as `DateRange`.
struct MealShift
{
var menu: DiningMenu
let hours: DateRange?
init(menu: DiningMenu, hours: DateRange?)
{
self.menu = menu
self.hours = hours
}
var isOpen: Bool
{
return !menu.isEmpty && (hours?.isActive ?? false)
}
}
typealias MealMap = [MealType : MealShift]
/**
* `DiningHall` represents a single physical dining location.
* Each hall contains the `DiningMenu`, Open & Close times for every `MealType`.
*/
class DiningHall: Resource
{
var image: UIImage?
static func displayName(pluralized: Bool) -> String
{
return "Dining Hall" + (pluralized ? "s" : "")
}
static var dataSource: ResourceDataSource.Type? = DiningDataSource.self
static var detailProvider: ResourceDetailProvider.Type? = DiningHallViewController.self
let name: String
let imageURL: URL?
let meals: MealMap
var isFavorited: Bool = false
init(name: String, imageLink: String?, shifts: MealMap)
{
self.name = name
self.imageURL = URL(string: imageLink ?? "")
self.meals = shifts
}
/// Returns whether the hall is currently open.
var isOpen: Bool
{
return meals.reduce(false){ $0 || $1.value.isOpen }
}
/// Returns the DiningMenu for the given MealType.
func menuForType(_ type: MealType) -> DiningMenu
{
return meals[type]!.menu
}
}
class CafeClass: Resource
{
var image: UIImage?
static func displayName(pluralized: Bool) -> String
{
return "Cafe" + (pluralized ? "s" : "")
}
static var dataSource: ResourceDataSource.Type? = CafeDataSource.self
static var detailProvider: ResourceDetailProvider.Type? = DiningHallViewController.self
let name: String
let imageURL: URL?
let meals: MealMap
var isFavorited: Bool = false
init(name: String, imageLink: String?, shifts: MealMap)
{
self.name = name
self.imageURL = URL(string: imageLink ?? "")
self.meals = shifts
}
/// Returns whether the hall is currently open.
var isOpen: Bool
{
return meals.reduce(false){ $0 || $1.value.isOpen }
}
/// Returns the DiningMenu for the given MealType.
func menuForType(_ type: MealType) -> DiningMenu
{
return meals[type]!.menu
}
}
/// DiningMenu is a typealias for an array of DiningItems.
typealias DiningMenu = Array<DiningItem>
/**
* `DiningItem` represents a single dish/item.
* Contains a refernce to the `DiningHall` and `MealType` of where and when this item is served.
*/
class DiningItem: Favorable
{
let name: String
let mealType: MealType
var isFavorited: Bool
// additional information regarding food such as allergens or alternative options
let restrictions: [String]
init(name: String, type: MealType, favorited: Bool = false, restrictions: [String])
{
self.name = name
self.mealType = type
self.isFavorited = favorited
self.restrictions = restrictions
}
}