Commit 0794102d authored by Zac Wood's avatar Zac Wood
Browse files

Fixes the search controller not displaying results correctly when favorites are selected.

parent 20a2bf68
......@@ -64,3 +64,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
.DS_Store
......@@ -250,22 +250,30 @@ class Utilities: NSObject {
//MARK - Favorite facilities
/**
Checks if a facility is a favorite.
- returns:
true if the facility is a favorite, false if it isn't
*/
static func isFavoriteFacility(_ facility: Facility) -> Bool {
let defaults = UserDefaults.standard
let favoriteStrings = defaults.array(forKey: "favorites") as! [String]?
if( favoriteStrings == nil ) {
return false
}
for str in favoriteStrings! {
// Search through realm DB for facilities w/ matching title
// return true if found
if( facility.facilityName == str ) {
return true
}
// return if the facility's name is in the list of favorites
return favoriteStrings!.contains { (favorite: String) -> Bool in
return facility.facilityName == favorite
}
return false
}
/**
Adds a facility to the UserDefault's favorites list.
- returns:
true if the facility was added correctly, false if the facility is already a favorite.
*/
static func addFavoriteFacility(_ facility: Facility) -> Bool {
if(isFavoriteFacility(facility)) {
return false
......@@ -282,6 +290,12 @@ class Utilities: NSObject {
}
}
/**
Removes a facility from the UserDefault's favorites list.
- returns:
true if the facility was removed correctly, false if the facility is not a favorite.
*/
static func removeFavoriteFacility(_ facility: Facility) -> Bool {
if(isFavoriteFacility(facility)) {
let defaults = UserDefaults.standard
......
......@@ -12,10 +12,13 @@ import RealmSwift
class FacilitiesListViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIViewControllerPreviewingDelegate {
// array of all facilities
var facilitiesArray = List<Facility>()
// array of facilities that pass the current filters
var filteredFacilities = List<Facility>()
// passing in nil sets the controller to be this controller
// passing in nil sets the search controller to be this controller
let searchController = UISearchController(searchResultsController: nil)
var filters = Filters()
......@@ -40,45 +43,44 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
var showFavorites = false
@IBOutlet var LastUpdatedLabel: UIBarButtonItem!
let refreshControl = UIRefreshControl()
@IBAction func favoritesControlChanges(_ sender: Any) {
switch (self.favoritesControl.selectedSegmentIndex)
switch self.favoritesControl.selectedSegmentIndex
{
case 0:
showFavorites = false
filteredFacilities = facilitiesArray
case 1:
showFavorites = true
filteredFacilities = List<Facility>()
let defaults = UserDefaults.standard
let favoriteStrings = defaults.array(forKey: "favorites") as! [String]?
if(favoriteStrings == nil) {
return
}
else {
for facility in facilitiesArray {
for str in favoriteStrings! {
if(facility.facilityName == str) {
filteredFacilities.append(facility)
break;
}
}
}
}
showFavorites = true
filteredFacilities = filterFacilitiesForFavorites()
default:
showFavorites = false
}
self.LocationsList.reloadData()
}
let refreshControl = UIRefreshControl()
/**
Get all of the facilities that are favorited.
- returns:
List of facilities that are favorited
*/
func filterFacilitiesForFavorites() -> List<Facility> {
var favoriteFacilites = List<Facility>()
// add the facility to favorites list if it is a favorite
favoriteFacilites = facilitiesArray.filter({ (facility: Facility) -> Bool in
return Utilities.isFavoriteFacility(facility)
})
return favoriteFacilites
}
override func viewWillLayoutSubviews() {
LocationsListLayout.itemSize.width = getCellWidth()
LocationsListLayout.invalidateLayout()
}
func getCellWidth() -> CGFloat {
......@@ -115,7 +117,6 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let tapped = self.LocationsList.cellForItem(at: indexPath!) as! FacilityCollectionViewCell
destination!.facility = tapped.facility
self.presentDetailView(destination!)
}
}
......@@ -206,14 +207,44 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
return searchController.isActive && !isSearchBarEmpty()
}
func filterFacilitiesForSearchText(_ searchText: String) {
filteredFacilities = 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
})
/**
Filters facilities based on the text inputted into the search controller.
- parameters:
- searchText: text used to filter the facilities.
- returns:
List of filtered facilities. Facilities whose names, buildings, or categories match the search text are included.
*/
func filterFacilitiesForSearchText(_ searchText: String) -> List<Facility> {
var filtered: List<Facility>
if showFavorites {
let favoriteFacilities = filterFacilitiesForFavorites()
if searchText == "" { // if the search text is empty, just return the favorites.
filtered = favoriteFacilities
} else {
filtered = favoriteFacilities.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
})
}
} 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
})
}
LocationsList.reloadData()
return filtered
}
@objc func refresh(_ sender: Any) {
......@@ -316,7 +347,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
return placeOpenFacilitiesFirstInArray(facilitiesArray)
}
else {
return [] //TODO - INCOMPLETE
return placeOpenFacilitiesFirstInArray(filteredFacilities)
}
......@@ -412,7 +443,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
extension FacilitiesListViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text ?? ""
filterFacilitiesForSearchText(searchText)
filteredFacilities = filterFacilitiesForSearchText(searchText)
}
}
......@@ -14,8 +14,10 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
@IBOutlet var PlaceLabel: UILabel!
@IBOutlet var OpenLabel: UILabel!
@IBOutlet var CategoryLabel: UILabel!
@IBOutlet var OpenTimesList: UITableView!
@IBOutlet var favoritesButton: UIButton!
var facility: Facility!
override var previewActionItems: [UIPreviewActionItem] {
var title: String
......@@ -29,16 +31,25 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
return [favoritePreviewItem]
}
@IBOutlet var favoritesButton: UIButton!
/**
Favorites button touch handler
Adds a facility to favorites if it isn't a favorite,
removes from favorites if it is a favorite.
*/
@IBAction func setFavButton(_ sender: Any) {
if(Utilities.isFavoriteFacility(facility)) {
Utilities.removeFavoriteFacility(facility)
if(Utilities.isFavoriteFacility(facility)) { // if the facility is a favorite
Utilities.removeFavoriteFacility(facility) // remove it from favorites
}
else {
else { // else add it to favorites
Utilities.addFavoriteFacility(facility)
}
setFavoriteButtonText()
}
/**
Change the favorite button text depending on if the facility is a favorite
*/
func setFavoriteButtonText() {
if(Utilities.isFavoriteFacility(facility)) {
favoritesButton.setTitle("Remove from Favorites", for: .normal)
......@@ -54,8 +65,6 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
return .lightContent
}
var facility: Facility!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
......
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