Filters.swift 4.26 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
	
19
	init() {
Zach Knox's avatar
Zach Knox committed
20
        //nothing to do here
Zach Knox's avatar
Zach Knox committed
21
22
	}
	
Zach Knox's avatar
Zach Knox committed
23
    func applyFiltersOnFacilities(_ facilities: [Facility]) -> [Facility] {
24
        let (open, closed) = separateOpenAndClosed(facilities)
Zach Knox's avatar
Zach Knox committed
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
        // I imagine that a function per enum may be more useful than this switch statement of copied code, but whatever
        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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    }
    
    // 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 ([], [])
    }
    
    // Sorts items in the given Facility array by name alphabetically (reverse if told)
    private func sortAlphabetically(_ facilities: [Facility], reverse: Bool = false) -> [Facility] {
        
        return []
    }
    
    // Sorts Facilities by their given location's name, and within those sorts A->Z
    private func sortByLocation(_ facilities: [Facility]) -> [Facility] {
        
        return []
    }
    
    
    
    
127
128
129
130
131
132
}

//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
133
134
135
	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
136
137
138
	
	//We should figure out how we want to allow sorting
}