Skip to content

Commit 53e77d0

Browse files
Merge pull request #158 from cuappdev/yana/allstops-retry-button
All Stop Retry Fixes
2 parents 7a318cb + cf54c9b commit 53e77d0

2 files changed

Lines changed: 99 additions & 21 deletions

File tree

TCAT/Controllers/AllStopsTableViewController.swift

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010
import DZNEmptyDataSet
1111

12-
protocol UnwindAllStopsTVCDelegate{
12+
protocol UnwindAllStopsTVCDelegate {
1313
func dismissSearchResultsVC(busStop: BusStop)
1414
}
1515

@@ -21,6 +21,7 @@ class AllStopsTableViewController: UITableViewController {
2121
var unwindAllStopsTVCDelegate: UnwindAllStopsTVCDelegate?
2222
var height: CGFloat?
2323
var currentChar: Character?
24+
var activityIndicator: UIActivityIndicatorView?
2425

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

3940
sortedKeys = Array(sectionIndexes.keys).sorted().filter({$0 != "#"})
41+
42+
// Adding "#" to keys for bus stops that start with a number
4043
if !allStops.isEmpty {
41-
// Adding "#" to keys for bus stops that start with a number
4244
sortedKeys.append("#")
4345
}
4446

4547
title = "All Stops"
4648
tableView.sectionIndexColor = .primaryTextColor
47-
tableView.register(BusStopCell.self, forCellReuseIdentifier: "BusStop")
49+
tableView.register(BusStopCell.self, forCellReuseIdentifier: Constants.Cells.busIdentifier)
4850
tableView.cellLayoutMarginsFollowReadableWidth = false
4951

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

5860
tableView.emptyDataSetSource = self
59-
//tableView.emptyDataSetDelegate = self
61+
tableView.emptyDataSetDelegate = self
6062
tableView.tableFooterView = UIView()
6163
// Set top of table view to align with scroll view
6264
tableView.contentOffset = .zero
6365

6466
}
6567

66-
override func didReceiveMemoryWarning() {
67-
super.didReceiveMemoryWarning()
68-
// Dispose of any resources that can be recreated.
69-
}
70-
7168
func sectionIndexesForBusStop() -> [String: [BusStop]] {
7269

7370
var sectionIndexDictionary: [String: [BusStop]] = [:]
@@ -101,15 +98,40 @@ class AllStopsTableViewController: UITableViewController {
10198
}
10299
}
103100

101+
// Adding "#" to section indexes for bus stops that start with a number
104102
if !allStops.isEmpty {
105-
// Adding "#" to section indexes for bus stops that start with a number
106103
sectionIndexDictionary["#"] = numberBusStops
107104
}
108105

109106
return sectionIndexDictionary
110107

111108
}
112109

110+
/// Retrieves the keys from the sectionIndexDictionary
111+
func sortedKeysForBusStops() -> [String] {
112+
// Don't include key '#'
113+
sortedKeys = Array(sectionIndexes.keys)
114+
.sorted()
115+
.filter { $0 != "#" }
116+
117+
if !allStops.isEmpty {
118+
// Adding "#" to keys for bus stops that start with a number
119+
sortedKeys.append("#")
120+
}
121+
122+
return sortedKeys
123+
}
124+
125+
func setUpTableOnRetry() {
126+
// Retry getting data from user defaults
127+
self.allStops = SearchTableViewManager.shared.getAllStops()
128+
// Set up table information
129+
self.sectionIndexes = self.sectionIndexesForBusStop()
130+
self.sortedKeys = self.sortedKeysForBusStops()
131+
132+
self.tableView.reloadData()
133+
}
134+
113135
// MARK: - Table view data source
114136

115137
override func numberOfSections(in tableView: UITableView) -> Int {
@@ -143,7 +165,7 @@ class AllStopsTableViewController: UITableViewController {
143165
}
144166

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

177-
// MARK: DZN EmptyDataSet Delegate
178-
extension AllStopsTableViewController: DZNEmptyDataSetSource {
199+
// MARK: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
200+
extension AllStopsTableViewController: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
201+
202+
func setUpActivityIndicator() {
203+
activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
204+
if let activityIndicator = activityIndicator {
205+
view.addSubview(activityIndicator)
206+
activityIndicator.snp.makeConstraints { (make) in
207+
make.centerX.equalToSuperview()
208+
make.centerY.equalToSuperview()
209+
}
210+
}
211+
}
179212

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

184217
func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
185-
let title = "Couldn't get stops 😟"
218+
if activityIndicator != nil {
219+
return nil
220+
}
221+
let title = "Couldn't Get Stops"
186222
let attrs = [NSAttributedString.Key.foregroundColor: UIColor.mediumGrayColor]
187223
return NSAttributedString(string: title, attributes: attrs)
188224
}
225+
226+
func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> NSAttributedString! {
227+
if activityIndicator != nil {
228+
return nil
229+
}
230+
let title = "Retry"
231+
let attrs = [NSAttributedString.Key.foregroundColor: UIColor.buttonColor]
232+
return NSAttributedString(string: title, attributes: attrs)
233+
}
234+
235+
func emptyDataSet(_ scrollView: UIScrollView!, didTap didTapButton: UIButton!) {
236+
setUpActivityIndicator()
237+
tableView.reloadData()
238+
activityIndicator?.startAnimating()
239+
retryNetwork { () -> Void in
240+
self.activityIndicator?.stopAnimating()
241+
self.activityIndicator = nil
242+
self.setUpTableOnRetry()
243+
}
244+
}
245+
246+
func retryNetwork(completion: @escaping () -> Void) {
247+
Network.getAllStops().perform(withSuccess: { stops in
248+
let allBusStops = stops.allStops
249+
if !allBusStops.isEmpty {
250+
// Only updating user defaults if retriving from network is successful
251+
let data = NSKeyedArchiver.archivedData(withRootObject: allBusStops)
252+
userDefaults.set(data, forKey: Constants.UserDefaults.allBusStops)
253+
}
254+
completion()
255+
}, failure: { error in
256+
print("AllStopsTableViewController.retryNetwork error:", error)
257+
completion()
258+
})
259+
}
260+
189261
}

TCAT/Utilities/SearchTableViewHelpers.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class SearchTableViewManager {
5959
private init() {}
6060
func getAllStops() -> [BusStop] {
6161
if let stops = allStops {
62-
return stops
62+
// Check if not empty so that an empty array isn't returned
63+
if !stops.isEmpty {
64+
return stops
65+
}
6366
}
6467
let stops = getAllBusStops()
6568
allStops = stops
@@ -69,10 +72,13 @@ class SearchTableViewManager {
6972
private func getAllBusStops() -> [BusStop] {
7073
if let allBusStops = userDefaults.value(forKey: Constants.UserDefaults.allBusStops) as? Data,
7174
var busStopArray = NSKeyedUnarchiver.unarchiveObject(with: allBusStops) as? [BusStop] {
72-
/// Creating "fake" bus stop to remove Google Places central Collegetown location choice
73-
let collegetownStop = BusStop(name: "Collegetown", lat: 42.442558, long: -76.485336)
74-
busStopArray.append(collegetownStop)
75-
return busStopArray
75+
// Check if empty so that an empty array isn't returned
76+
if !busStopArray.isEmpty {
77+
/// Creating "fake" bus stop to remove Google Places central Collegetown location choice
78+
let collegetownStop = BusStop(name: "Collegetown", lat: 42.442558, long: -76.485336)
79+
busStopArray.append(collegetownStop)
80+
return busStopArray
81+
}
7682
}
7783
return [BusStop]()
7884
}
@@ -258,7 +264,7 @@ class SearchTableViewManager {
258264
localizedTitle: item.name,
259265
localizedSubtitle: nil,
260266
icon: UIApplicationShortcutIcon(type: .location),
261-
userInfo: placeInfo as? [String : NSSecureCoding])
267+
userInfo: placeInfo as? [String: NSSecureCoding])
262268
shortcutItems.append(shortcutItem)
263269
}
264270
UIApplication.shared.shortcutItems = shortcutItems

0 commit comments

Comments
 (0)