Commit 1054e885 authored by Zach Knox's avatar Zach Knox
Browse files

Merge branch '3-favorites' into 'superfluousness'

Favorites

See merge request !16
parents 8f9d6f0b bd2c5197
......@@ -64,3 +64,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
.DS_Store
......@@ -247,6 +247,68 @@ class Utilities: NSObject {
static func isMainSchedule(facility: Facility) -> Bool {
return facility.mainSchedule != nil
}
//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
}
// return if the facility's name is in the list of favorites
return favoriteStrings!.contains { (favorite: String) -> Bool in
return facility.facilityName == favorite
}
}
/**
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
}
else {
let defaults = UserDefaults.standard
var favoriteStrings = defaults.array(forKey: "favorites") as! [String]?
if(favoriteStrings == nil) {
favoriteStrings = []
}
favoriteStrings?.append(facility.facilityName)
defaults.set(favoriteStrings, forKey: "favorites")
return true
}
}
/**
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
var favoriteStrings = defaults.array(forKey: "favorites") as! [String]
let removing = favoriteStrings.index(of: facility.facilityName)
favoriteStrings.remove(at: removing!)
defaults.set(favoriteStrings, forKey: "favorites")
return true
}
else {
return false
}
}
}
......
......@@ -26,9 +26,11 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
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()
......@@ -53,26 +55,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
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 {
......@@ -109,7 +129,6 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let tapped = self.LocationsList.cellForItem(at: indexPath!) as! FacilityCollectionViewCell
destination!.facility = tapped.facility
self.presentDetailView(destination!)
}
}
......@@ -212,14 +231,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
}
func refresh(_ sender: Any, forceUpdate: Bool = true) {
......@@ -324,7 +373,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isSearching() ? self.filteredFacilities.count : self.facilitiesArray.count
return isSearching() || showFavorites ? self.filteredFacilities.count : self.facilitiesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
......@@ -346,7 +395,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
let dataArray: [Facility]
// if something has been searched for, we want to use the filtered array as the data source
if isSearching() {
if isSearching() || showFavorites {
dataArray = placeOpenFacilitiesFirstInArray(filteredFacilities)
} else {
dataArray = placeOpenFacilitiesFirstInArray(facilitiesArray)
......@@ -396,7 +445,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
return placeOpenFacilitiesFirstInArray(facilitiesArray)
}
else {
return [] //TODO - INCOMPLETE
return placeOpenFacilitiesFirstInArray(filteredFacilities)
}
......@@ -492,7 +541,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
extension FacilitiesListViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text ?? ""
filterFacilitiesForSearchText(searchText)
filteredFacilities = filterFacilitiesForSearchText(searchText)
}
}
......
......@@ -14,22 +14,57 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
@IBOutlet var PlaceLabel: UILabel!
@IBOutlet var OpenLabel: UILabel!
@IBOutlet var CategoryLabel: UILabel!
@IBOutlet var OpenTimesList: UITableView!
@IBOutlet var detailStackView: UIStackView!
@IBOutlet var favoritesButton: UIButton!
var facility: Facility!
override var previewActionItems: [UIPreviewActionItem] {
let favoritePreviewItem = UIPreviewAction(title: "Mark as Favorite", style: UIPreviewActionStyle.default, handler: markAsFavoritePreviewAction)
var title: String
if(Utilities.isFavoriteFacility(facility)) {
title = "Remove from Favorites"
}
else {
title = "Add to Favorites"
}
let favoritePreviewItem = UIPreviewAction(title: title, style: UIPreviewActionStyle.default, handler: markAsFavoritePreviewAction)
return [favoritePreviewItem]
}
/**
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)) { // if the facility is a favorite
Utilities.removeFavoriteFacility(facility) // remove it from favorites
}
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)
favoritesButton.titleLabel?.text = "Remove from Favorites"
}
else {
favoritesButton.setTitle("Add to Favorites", for: .normal)
favoritesButton.titleLabel?.text = "Add to Favorites"
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
var facility: Facility!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
......@@ -54,6 +89,12 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
OpenLabel.backgroundColor = UIColor.red
}
setFavoriteButtonText()
favoritesButton.tintColor = UIColor.white
favoritesButton.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
favoritesButton.layer.cornerRadius = 10
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
......@@ -101,6 +142,11 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
}
func markAsFavoritePreviewAction(_ sendingAction: UIPreviewAction, sender: UIViewController) {
if(Utilities.isFavoriteFacility(facility)) {
Utilities.removeFavoriteFacility(facility)
}
else {
Utilities.addFavoriteFacility(facility)
}
}
}
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