Filters.swift 5.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
//  Filters.swift
//  WhatsOpen
//
//  Created by Zach Knox on 5/7/17.
//  Copyright © 2017 SRCT. All rights reserved.
//

import Foundation

//This may be a stupid way to handle filters, but it should work; at least for now

Zach Knox's avatar
Zach Knox committed
13
class Filters {
14
15
16
	var showOpen = true
	var showClosed = true
	var sortBy = SortMethod.alphabetical
Zach Knox's avatar
Zach Knox committed
17
    var openFirst = true
Zach Knox's avatar
Zach Knox committed
18
	
Zach Knox's avatar
Zach Knox committed
19
20
	var onlyFromLocations = [String: Bool]() // Locations to show, could simply use Location objects instead if you wanted
	var onlyFromCategories = [String: Bool]() //same as above, but for Categories, not Locations
21
	
22
	init() {
Zach Knox's avatar
Zach Knox committed
23
        //nothing to do here
Zach Knox's avatar
Zach Knox committed
24
25
	}
	
Zach Knox's avatar
Zach Knox committed
26
    func applyFiltersOnFacilities(_ facilities: [Facility]) -> [Facility] {
27
28
		//TODO: Add checks for onlyFromLocations and onlyFromCategories here before doing the rest
		
29
30
31
32
33
34
35
36
37
		var specifiedFacilities = [Facility]()
		// facility must be within both a specified location and category
		for f in facilities {
			if(onlyFromLocations[(f.facilityLocation?.building)!] == true && onlyFromCategories[(f.category?.categoryName)!] == true) {
				specifiedFacilities.append(f)
			}
		}
		
		let (open, closed) = separateOpenAndClosed(specifiedFacilities)
Zach Knox's avatar
Zach Knox committed
38
        
39
        // This switch statement figures out what sort method is being used, and will sort accordingly
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
        switch sortBy {
        case .alphabetical:
            if(openFirst) {
                var returning: [Facility] = []
                if(showOpen) {
                    returning += sortAlphabetically(open)
                }
                if(showClosed) {
                    returning += sortAlphabetically(closed)
                }
                return returning
            }
            else {
                if(showOpen && showClosed) {
                    return sortAlphabetically(facilities)
                }
                else if(showOpen) {
                    return sortAlphabetically(open)
                }
                else if(showClosed) {
                    return sortAlphabetically(closed)
                }
                else {
                    return []
                }
            }
        case .reverseAlphabetical:
            if(openFirst) {
                var returning: [Facility] = []
                if(showOpen) {
                    returning += sortAlphabetically(open, reverse: true)
                }
                if(showClosed) {
                    returning += sortAlphabetically(closed, reverse: true)
                }
                return returning
            }
            else {
                if(showOpen && showClosed) {
                    return sortAlphabetically(facilities, reverse: true)
                }
                else if(showOpen) {
                    return sortAlphabetically(open, reverse: true)
                }
                else if(showClosed) {
                    return sortAlphabetically(closed, reverse: true)
                }
                else {
                    return []
                }
            }
        case .byLocation:
            if(openFirst) {
                var returning: [Facility] = []
                if(showOpen) {
                    returning += sortByLocation(open)
                }
                if(showClosed) {
                    returning += sortByLocation(closed)
                }
                return returning
            }
            else {
                if(showOpen && showClosed) {
                    return sortByLocation(facilities)
                }
                else if(showOpen) {
                    return sortByLocation(open)
                }
                else if(showClosed) {
                    return sortByLocation(closed)
                }
                else {
                    return []
                }
            }
        }
Zach Knox's avatar
Zach Knox committed
117
    }
118
119
	
	//TODO
Zach Knox's avatar
Zach Knox committed
120
121
122
123
124
    // Takes in array of Facilities, separates them into those open and closed, returning a tuple of 2 arrays
    private func separateOpenAndClosed(_ facilities: [Facility]) -> (open: [Facility], closed: [Facility]) {
        
        return ([], [])
    }
125
126
	
	//TODO
Zach Knox's avatar
Zach Knox committed
127
128
129
130
131
    // Sorts items in the given Facility array by name alphabetically (reverse if told)
    private func sortAlphabetically(_ facilities: [Facility], reverse: Bool = false) -> [Facility] {
        
        return []
    }
132
133
	
	//TODO
Zach Knox's avatar
Zach Knox committed
134
135
136
137
138
    // Sorts Facilities by their given location's name, and within those sorts A->Z
    private func sortByLocation(_ facilities: [Facility]) -> [Facility] {
        
        return []
    }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	
	func setShowOpen(_ to: Bool) -> Bool {
		showOpen = to
		return true
	}
	
	func setShowClosed(_ to: Bool) -> Bool {
		showClosed = to
		return true
	}
	
	func setOpenFirst(_ to: Bool) -> Bool {
		openFirst = to
		return true
	}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	
	func setCategory(_ category: String?, to: Bool) -> Bool{
		if(category != nil) {
			onlyFromCategories[category!] = to
			return true
		}
		else {
			return false
		}
	}
	
	func setLocation(_ location: String?, to: Bool) -> Bool{
		if(location != nil) {
			onlyFromLocations[location!] = to
			return true
		}
		else {
			return false
		}
	}
Zach Knox's avatar
Zach Knox committed
174
175
    
    
176
177
178
179
180
181
}

//Is this a viable way to do this?
enum SortMethod {
	case alphabetical //A -> Z
	case reverseAlphabetical //Z -> A
Zach Knox's avatar
Zach Knox committed
182
183
184
	case byLocation // A -> Z Locations, w/ A -> Z Facilities inside
    //case openLongest //Places things open longest on top; if only showing closed, shows opening soonest
	//case openShortest //Places things closing soonest on top; if only showing closed, shows opening furthest from now
185
	
186
187
188
189
	
	static var count = 3 // REMEMBER TO CHANGE THIS IF YOU ADD MORE CASES
	
	
190
191
	//We should figure out how we want to allow sorting
}