Unverified Commit 031ac65e authored by Zach Knox's avatar Zach Knox
Browse files

added checks for filters within the main list view controller. The list view...

added checks for filters within the main list view controller. The list view controller now displays whatever is in shownFacilities, which is the filteredFacilities - anything from searching or favoriting

THE APP WILL SHOW NO FACILITIES UNTIL THE ACTUAL SORT FUNCTIONS ARE DONE FYI
parent 0f50f4f2
......@@ -7,6 +7,7 @@
//
import Foundation
import RealmSwift
//This may be a stupid way to handle filters, but it should work; at least for now
......@@ -23,10 +24,10 @@ class Filters {
//nothing to do here
}
func applyFiltersOnFacilities(_ facilities: [Facility]) -> [Facility] {
func applyFiltersOnFacilities(_ facilities: List<Facility>) -> List<Facility> {
//TODO: Add checks for onlyFromLocations and onlyFromCategories here before doing the rest
var specifiedFacilities = [Facility]()
let specifiedFacilities = List<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) {
......@@ -40,7 +41,7 @@ class Filters {
switch sortBy {
case .alphabetical:
if(openFirst) {
var returning: [Facility] = []
var returning = List<Facility>()
if(showOpen) {
returning += sortAlphabetically(open)
}
......@@ -60,12 +61,12 @@ class Filters {
return sortAlphabetically(closed)
}
else {
return []
return List<Facility>()
}
}
case .reverseAlphabetical:
if(openFirst) {
var returning: [Facility] = []
var returning = List<Facility>()
if(showOpen) {
returning += sortAlphabetically(open, reverse: true)
}
......@@ -85,12 +86,12 @@ class Filters {
return sortAlphabetically(closed, reverse: true)
}
else {
return []
return List<Facility>()
}
}
case .byLocation:
if(openFirst) {
var returning: [Facility] = []
var returning = List<Facility>()
if(showOpen) {
returning += sortByLocation(open)
}
......@@ -110,7 +111,7 @@ class Filters {
return sortByLocation(closed)
}
else {
return []
return List<Facility>()
}
}
}
......@@ -118,23 +119,23 @@ class Filters {
//TODO
// 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]) {
private func separateOpenAndClosed(_ facilities: List<Facility>) -> (open: List<Facility>, closed: List<Facility>) {
return ([], [])
return (List<Facility>(), List<Facility>())
}
//TODO
// Sorts items in the given Facility array by name alphabetically (reverse if told)
private func sortAlphabetically(_ facilities: [Facility], reverse: Bool = false) -> [Facility] {
private func sortAlphabetically(_ facilities: List<Facility>, reverse: Bool = false) -> List<Facility> {
return []
return List<Facility>()
}
//TODO
// Sorts Facilities by their given location's name, and within those sorts A->Z
private func sortByLocation(_ facilities: [Facility]) -> [Facility] {
private func sortByLocation(_ facilities: List<Facility>) -> List<Facility> {
return []
return List<Facility>()
}
func setShowOpen(_ to: Bool) -> Bool {
......
......@@ -22,13 +22,13 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let realm = try! Realm()
var facilitiesArray = List<Facility>()
// array of facilities that pass the current filters
var filteredFacilities = List<Facility>()
// List which actually pertains to what is shown
var shownFacilities = List<Facility>()
// passing in nil sets the search controller to be this controller
let searchController = UISearchController(searchResultsController: nil)
......@@ -63,12 +63,13 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
{
case 0:
showFavorites = false
filteredFacilities = facilitiesArray
shownFacilities = filteredFacilities
case 1:
showFavorites = true
filteredFacilities = filterFacilitiesForFavorites()
shownFacilities = filterFacilitiesForFavorites()
default:
showFavorites = false
shownFacilities = filteredFacilities
}
self.LocationsList.reloadData()
}
......@@ -83,7 +84,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
var favoriteFacilites = List<Facility>()
// add the facility to favorites list if it is a favorite
favoriteFacilites = facilitiesArray.filter({ (facility: Facility) -> Bool in
favoriteFacilites = filteredFacilities.filter({ (facility: Facility) -> Bool in
return Utilities.isFavoriteFacility(facility)
})
......@@ -242,12 +243,17 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
LocationsList.reloadData()
reloadWithFilters()
}
func reloadWithFilters() {
filteredFacilities = filters.applyFiltersOnFacilities(facilitiesArray)
shownFacilities = filteredFacilities
favoritesControlChanges(self)
LocationsList.reloadData()
}
func isSearchBarEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? true
......@@ -267,7 +273,8 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
*/
func filterFacilitiesForSearchText(_ searchText: String) -> List<Facility> {
var filtered: List<Facility>
/*
if showFavorites {
let favoriteFacilities = filterFacilitiesForFavorites()
......@@ -282,21 +289,30 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
return hasName || hasBuilding || hasCategory
})
}
} else {
filtered = facilitiesArray.filter({(facility: Facility) -> Bool in
let hasName = facility.facilityName.lowercased().contains(searchText.lowercased())
let hasBuilding = facility.facilityLocation?.building.lowercased().contains(searchText.lowercased()) ?? false
let hasCategory = facility.category?.categoryName.lowercased().contains(searchText.lowercased()) ?? false
return hasName || hasBuilding || hasCategory
})
}
*/
if searchText == "" {
filtered = shownFacilities
LocationsList.reloadData()
return shownFacilities
}
filtered = filteredFacilities.filter({(facility: Facility) -> Bool in
let hasName = facility.facilityName.lowercased().contains(searchText.lowercased())
let hasBuilding = facility.facilityLocation?.building.lowercased().contains(searchText.lowercased()) ?? false
let hasCategory = facility.category?.categoryName.lowercased().contains(searchText.lowercased()) ?? false
return hasName || hasBuilding || hasCategory
})
LocationsList.reloadData()
return filtered
}
/*
* Reloads data, either calling update() to attempt a download
* or simply pulling from the realm
*/
func refresh(_ sender: Any, forceUpdate: Bool = true) {
refreshControl.beginRefreshing()
if(forceUpdate) {
......@@ -322,7 +338,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
}
}
LocationsList.reloadData()
reloadWithFilters()
// Add locations and categories to filters
for f in facilitiesArray {
......@@ -336,6 +352,10 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
refreshControl.endRefreshing()
}
/*
* Attempts to update facilitiesArray from the network
* and place that new information into Realm
*/
func update(_ sender: Any) {
SRCTNetworkController.performDownload { (facilities) in
if(facilities == nil) {
......@@ -347,7 +367,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let lastUpdated = model.lastUpdated
self.facilitiesArray = facilitiesFromDB
self.LocationsList.reloadData()
self.reloadWithFilters()
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(lastUpdated)
}
else {
......@@ -363,7 +383,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
//defaults.set(facilities, forKey: "FacilitiesList")
let date = Date()
//defaults.set(date, forKey: "lastUpdatedList")
self.LocationsList.reloadData()
self.reloadWithFilters()
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
let model = FacilitiesModel()
model.facilities = facilities!
......@@ -409,7 +429,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isSearching() || showFavorites ? self.filteredFacilities.count : self.facilitiesArray.count
return shownFacilities.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
......@@ -428,16 +448,20 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let facility: Facility
let dataArray: [Facility]
//let dataArray: [Facility]
/*
// if something has been searched for, we want to use the filtered array as the data source
if isSearching() || showFavorites {
dataArray = placeOpenFacilitiesFirstInArray(filteredFacilities)
} else {
dataArray = placeOpenFacilitiesFirstInArray(facilitiesArray)
}
*/
facility = dataArray[indexPath.row]
facility = shownFacilities[indexPath.row]
cell.facility = facility
......@@ -486,7 +510,8 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
}
// Returns an array which has the open locations listed first
// Could be improved in the future because currently this means you're checking
// open status twice per cell
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment