Unverified Commit 8f58e445 authored by Zach Knox's avatar Zach Knox
Browse files

Implemented favorites

Currently can only add/remove via 3D touch action
parent ddc5142b
......@@ -249,33 +249,34 @@ class Utilities: NSObject {
}
//MARK - Favorite facilities
static func getFavorites() -> [Facility] {
let defaults = UserDefaults.standard
let favoriteStrings = defaults.array(forKey: "favorites") as! [String]
for var str in favoriteStrings {
// Search through realm DB for facilities w/ matching titles
}
return []
}
static func isFavoriteFacility(_ facility: Facility) -> Bool {
let defaults = UserDefaults.standard
let favoriteStrings = defaults.array(forKey: "favorites") as! [String]
for var str in favoriteStrings! {
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 false
}
static func addFavoriteFacility(_ facility: Facility) -> Bool {
if(isFavoriteFacility(facility)) {
return false
}
else {
let defaults = UserDefaults.standard
var favoriteStrings = defaults.array(forKey: "favorites") as! [String]
favoriteStrings.append(facility.facilityName)
var favoriteStrings = defaults.array(forKey: "favorites") as! [String]?
if(favoriteStrings == nil) {
favoriteStrings = []
}
favoriteStrings?.append(facility.facilityName)
defaults.set(favoriteStrings, forKey: "favorites")
return true
}
......@@ -288,6 +289,7 @@ class Utilities: NSObject {
let removing = favoriteStrings.index(of: facility.facilityName)
favoriteStrings.remove(at: removing!)
defaults.set(favoriteStrings, forKey: "favorites")
return true
}
else {
return false
......
......@@ -46,8 +46,27 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
{
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;
}
}
}
}
default:
showFavorites = false
}
......@@ -225,7 +244,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 {
......@@ -247,7 +266,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)
......
......@@ -20,7 +20,14 @@ class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITab
@IBOutlet var detailStackView: UIStackView!
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]
}
......@@ -101,6 +108,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