Skip to content

Commit

Permalink
Merge pull request #158 from cuappdev/yana/allstops-retry-button
Browse files Browse the repository at this point in the history
All Stop Retry Fixes
  • Loading branch information
mattbarker016 authored Sep 27, 2018
2 parents 7a318cb + cf54c9b commit 53e77d0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 21 deletions.
102 changes: 87 additions & 15 deletions TCAT/Controllers/AllStopsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import DZNEmptyDataSet

protocol UnwindAllStopsTVCDelegate{
protocol UnwindAllStopsTVCDelegate {
func dismissSearchResultsVC(busStop: BusStop)
}

Expand All @@ -21,6 +21,7 @@ class AllStopsTableViewController: UITableViewController {
var unwindAllStopsTVCDelegate: UnwindAllStopsTVCDelegate?
var height: CGFloat?
var currentChar: Character?
var activityIndicator: UIActivityIndicatorView?

override func viewWillLayoutSubviews() {
if let y = navigationController?.navigationBar.frame.maxY {
Expand All @@ -37,14 +38,15 @@ class AllStopsTableViewController: UITableViewController {
sectionIndexes = sectionIndexesForBusStop()

sortedKeys = Array(sectionIndexes.keys).sorted().filter({$0 != "#"})

// Adding "#" to keys for bus stops that start with a number
if !allStops.isEmpty {
// Adding "#" to keys for bus stops that start with a number
sortedKeys.append("#")
}

title = "All Stops"
tableView.sectionIndexColor = .primaryTextColor
tableView.register(BusStopCell.self, forCellReuseIdentifier: "BusStop")
tableView.register(BusStopCell.self, forCellReuseIdentifier: Constants.Cells.busIdentifier)
tableView.cellLayoutMarginsFollowReadableWidth = false

if #available(iOS 11.0, *) {
Expand All @@ -56,18 +58,13 @@ class AllStopsTableViewController: UITableViewController {
}

tableView.emptyDataSetSource = self
//tableView.emptyDataSetDelegate = self
tableView.emptyDataSetDelegate = self
tableView.tableFooterView = UIView()
// Set top of table view to align with scroll view
tableView.contentOffset = .zero

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func sectionIndexesForBusStop() -> [String: [BusStop]] {

var sectionIndexDictionary: [String: [BusStop]] = [:]
Expand Down Expand Up @@ -101,15 +98,40 @@ class AllStopsTableViewController: UITableViewController {
}
}

// Adding "#" to section indexes for bus stops that start with a number
if !allStops.isEmpty {
// Adding "#" to section indexes for bus stops that start with a number
sectionIndexDictionary["#"] = numberBusStops
}

return sectionIndexDictionary

}

/// Retrieves the keys from the sectionIndexDictionary
func sortedKeysForBusStops() -> [String] {
// Don't include key '#'
sortedKeys = Array(sectionIndexes.keys)
.sorted()
.filter { $0 != "#" }

if !allStops.isEmpty {
// Adding "#" to keys for bus stops that start with a number
sortedKeys.append("#")
}

return sortedKeys
}

func setUpTableOnRetry() {
// Retry getting data from user defaults
self.allStops = SearchTableViewManager.shared.getAllStops()
// Set up table information
self.sectionIndexes = self.sectionIndexesForBusStop()
self.sortedKeys = self.sortedKeysForBusStops()

self.tableView.reloadData()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
Expand Down Expand Up @@ -143,7 +165,7 @@ class AllStopsTableViewController: UITableViewController {
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BusStop", for: indexPath) as! BusStopCell
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.Cells.busIdentifier, for: indexPath) as! BusStopCell
let section = sectionIndexes[sortedKeys[indexPath.section]]
cell.textLabel?.text = section?[indexPath.row].name
return cell
Expand Down Expand Up @@ -174,16 +196,66 @@ class AllStopsTableViewController: UITableViewController {
}
}

// MARK: DZN EmptyDataSet Delegate
extension AllStopsTableViewController: DZNEmptyDataSetSource {
// MARK: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
extension AllStopsTableViewController: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {

func setUpActivityIndicator() {
activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
if let activityIndicator = activityIndicator {
view.addSubview(activityIndicator)
activityIndicator.snp.makeConstraints { (make) in
make.centerX.equalToSuperview()
make.centerY.equalToSuperview()
}
}
}

func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
return #imageLiteral(resourceName: "emptyPin")
return activityIndicator != nil ? nil : #imageLiteral(resourceName: "emptyPin")
}

func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let title = "Couldn't get stops 😟"
if activityIndicator != nil {
return nil
}
let title = "Couldn't Get Stops"
let attrs = [NSAttributedString.Key.foregroundColor: UIColor.mediumGrayColor]
return NSAttributedString(string: title, attributes: attrs)
}

func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> NSAttributedString! {
if activityIndicator != nil {
return nil
}
let title = "Retry"
let attrs = [NSAttributedString.Key.foregroundColor: UIColor.buttonColor]
return NSAttributedString(string: title, attributes: attrs)
}

func emptyDataSet(_ scrollView: UIScrollView!, didTap didTapButton: UIButton!) {
setUpActivityIndicator()
tableView.reloadData()
activityIndicator?.startAnimating()
retryNetwork { () -> Void in
self.activityIndicator?.stopAnimating()
self.activityIndicator = nil
self.setUpTableOnRetry()
}
}

func retryNetwork(completion: @escaping () -> Void) {
Network.getAllStops().perform(withSuccess: { stops in
let allBusStops = stops.allStops
if !allBusStops.isEmpty {
// Only updating user defaults if retriving from network is successful
let data = NSKeyedArchiver.archivedData(withRootObject: allBusStops)
userDefaults.set(data, forKey: Constants.UserDefaults.allBusStops)
}
completion()
}, failure: { error in
print("AllStopsTableViewController.retryNetwork error:", error)
completion()
})
}

}
18 changes: 12 additions & 6 deletions TCAT/Utilities/SearchTableViewHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class SearchTableViewManager {
private init() {}
func getAllStops() -> [BusStop] {
if let stops = allStops {
return stops
// Check if not empty so that an empty array isn't returned
if !stops.isEmpty {
return stops
}
}
let stops = getAllBusStops()
allStops = stops
Expand All @@ -69,10 +72,13 @@ class SearchTableViewManager {
private func getAllBusStops() -> [BusStop] {
if let allBusStops = userDefaults.value(forKey: Constants.UserDefaults.allBusStops) as? Data,
var busStopArray = NSKeyedUnarchiver.unarchiveObject(with: allBusStops) as? [BusStop] {
/// Creating "fake" bus stop to remove Google Places central Collegetown location choice
let collegetownStop = BusStop(name: "Collegetown", lat: 42.442558, long: -76.485336)
busStopArray.append(collegetownStop)
return busStopArray
// Check if empty so that an empty array isn't returned
if !busStopArray.isEmpty {
/// Creating "fake" bus stop to remove Google Places central Collegetown location choice
let collegetownStop = BusStop(name: "Collegetown", lat: 42.442558, long: -76.485336)
busStopArray.append(collegetownStop)
return busStopArray
}
}
return [BusStop]()
}
Expand Down Expand Up @@ -258,7 +264,7 @@ class SearchTableViewManager {
localizedTitle: item.name,
localizedSubtitle: nil,
icon: UIApplicationShortcutIcon(type: .location),
userInfo: placeInfo as? [String : NSSecureCoding])
userInfo: placeInfo as? [String: NSSecureCoding])
shortcutItems.append(shortcutItem)
}
UIApplication.shared.shortcutItems = shortcutItems
Expand Down

0 comments on commit 53e77d0

Please sign in to comment.