Skip to content

Commit 39ea74f

Browse files
Merge pull request #149 from cuappdev/yana/allstops-retry-button
Allstops retry button
2 parents f54ace4 + a0ade9e commit 39ea74f

7 files changed

Lines changed: 173 additions & 186 deletions

TCAT.xcodeproj/project.pbxproj

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,6 @@
458458
449A7C851D80D0E80019300C /* Sources */,
459459
449A7C861D80D0E80019300C /* Frameworks */,
460460
449A7C871D80D0E80019300C /* Resources */,
461-
046C7FE1354D5CA231D4B0DB /* [CP] Embed Pods Frameworks */,
462-
46E366F31C5FDE5AE2F1856C /* [CP] Copy Pods Resources */,
463461
);
464462
buildRules = (
465463
);
@@ -479,8 +477,6 @@
479477
449A7C901D80D0E80019300C /* Sources */,
480478
449A7C911D80D0E80019300C /* Frameworks */,
481479
449A7C921D80D0E80019300C /* Resources */,
482-
D7D6F3AD37AAA12DFFBEE2FD /* [CP] Embed Pods Frameworks */,
483-
53F5B40EB43DEC59D709CAAF /* [CP] Copy Pods Resources */,
484480
);
485481
buildRules = (
486482
);
@@ -579,21 +575,6 @@
579575
/* End PBXResourcesBuildPhase section */
580576

581577
/* Begin PBXShellScriptBuildPhase section */
582-
046C7FE1354D5CA231D4B0DB /* [CP] Embed Pods Frameworks */ = {
583-
isa = PBXShellScriptBuildPhase;
584-
buildActionMask = 2147483647;
585-
files = (
586-
);
587-
inputPaths = (
588-
);
589-
name = "[CP] Embed Pods Frameworks";
590-
outputPaths = (
591-
);
592-
runOnlyForDeploymentPostprocessing = 0;
593-
shellPath = /bin/sh;
594-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCATTests/Pods-TCATTests-frameworks.sh\"\n";
595-
showEnvVarsInLog = 0;
596-
};
597578
0DD807D4971138217FA4C33E /* [CP] Check Pods Manifest.lock */ = {
598579
isa = PBXShellScriptBuildPhase;
599580
buildActionMask = 2147483647;
@@ -632,7 +613,7 @@
632613
files = (
633614
);
634615
inputPaths = (
635-
"${SRCROOT}/Pods/Target Support Files/Pods-TCAT/Pods-TCAT-resources.sh",
616+
"${PODS_ROOT}/Target Support Files/Pods-TCAT/Pods-TCAT-resources.sh",
636617
"${PODS_ROOT}/GoogleMaps/Maps/Frameworks/GoogleMaps.framework/Resources/GoogleMaps.bundle",
637618
"${PODS_ROOT}/GooglePlaces/Frameworks/GooglePlaces.framework/Resources/GooglePlaces.bundle",
638619
);
@@ -643,7 +624,7 @@
643624
);
644625
runOnlyForDeploymentPostprocessing = 0;
645626
shellPath = /bin/sh;
646-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCAT/Pods-TCAT-resources.sh\"\n";
627+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TCAT/Pods-TCAT-resources.sh\"\n";
647628
showEnvVarsInLog = 0;
648629
};
649630
2E3D0C0A5BDEC0DF37BFA6ED /* [CP] Check Pods Manifest.lock */ = {
@@ -664,50 +645,19 @@
664645
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
665646
showEnvVarsInLog = 0;
666647
};
667-
46E366F31C5FDE5AE2F1856C /* [CP] Copy Pods Resources */ = {
668-
isa = PBXShellScriptBuildPhase;
669-
buildActionMask = 2147483647;
670-
files = (
671-
);
672-
inputPaths = (
673-
);
674-
name = "[CP] Copy Pods Resources";
675-
outputPaths = (
676-
);
677-
runOnlyForDeploymentPostprocessing = 0;
678-
shellPath = /bin/sh;
679-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCATTests/Pods-TCATTests-resources.sh\"\n";
680-
showEnvVarsInLog = 0;
681-
};
682-
53F5B40EB43DEC59D709CAAF /* [CP] Copy Pods Resources */ = {
683-
isa = PBXShellScriptBuildPhase;
684-
buildActionMask = 2147483647;
685-
files = (
686-
);
687-
inputPaths = (
688-
);
689-
name = "[CP] Copy Pods Resources";
690-
outputPaths = (
691-
);
692-
runOnlyForDeploymentPostprocessing = 0;
693-
shellPath = /bin/sh;
694-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCATUITests/Pods-TCATUITests-resources.sh\"\n";
695-
showEnvVarsInLog = 0;
696-
};
697648
7C01627F386C89E7F1995831 /* [CP] Embed Pods Frameworks */ = {
698649
isa = PBXShellScriptBuildPhase;
699650
buildActionMask = 2147483647;
700651
files = (
701652
);
702653
inputPaths = (
703-
"${SRCROOT}/Pods/Target Support Files/Pods-TCAT/Pods-TCAT-frameworks.sh",
654+
"${PODS_ROOT}/Target Support Files/Pods-TCAT/Pods-TCAT-frameworks.sh",
704655
"${BUILT_PRODUCTS_DIR}/Alamofire/Alamofire.framework",
705656
"${BUILT_PRODUCTS_DIR}/DZNEmptyDataSet/DZNEmptyDataSet.framework",
706657
"${BUILT_PRODUCTS_DIR}/Fuzzywuzzy_swift/Fuzzywuzzy_swift.framework",
707658
"${BUILT_PRODUCTS_DIR}/GoogleUtilities/GoogleUtilities.framework",
708659
"${BUILT_PRODUCTS_DIR}/MarqueeLabel/MarqueeLabel.framework",
709660
"${BUILT_PRODUCTS_DIR}/NotificationBannerSwift/NotificationBannerSwift.framework",
710-
"${BUILT_PRODUCTS_DIR}/Pages/Pages.framework",
711661
"${BUILT_PRODUCTS_DIR}/Presentation/Presentation.framework",
712662
"${BUILT_PRODUCTS_DIR}/Pulley/Pulley.framework",
713663
"${BUILT_PRODUCTS_DIR}/SnapKit/SnapKit.framework",
@@ -724,7 +674,6 @@
724674
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleUtilities.framework",
725675
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MarqueeLabel.framework",
726676
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/NotificationBannerSwift.framework",
727-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Pages.framework",
728677
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Presentation.framework",
729678
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Pulley.framework",
730679
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SnapKit.framework",
@@ -735,7 +684,7 @@
735684
);
736685
runOnlyForDeploymentPostprocessing = 0;
737686
shellPath = /bin/sh;
738-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCAT/Pods-TCAT-frameworks.sh\"\n";
687+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TCAT/Pods-TCAT-frameworks.sh\"\n";
739688
showEnvVarsInLog = 0;
740689
};
741690
8F523521CC9B937AEFB99FF9 /* [CP] Check Pods Manifest.lock */ = {
@@ -756,21 +705,6 @@
756705
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
757706
showEnvVarsInLog = 0;
758707
};
759-
D7D6F3AD37AAA12DFFBEE2FD /* [CP] Embed Pods Frameworks */ = {
760-
isa = PBXShellScriptBuildPhase;
761-
buildActionMask = 2147483647;
762-
files = (
763-
);
764-
inputPaths = (
765-
);
766-
name = "[CP] Embed Pods Frameworks";
767-
outputPaths = (
768-
);
769-
runOnlyForDeploymentPostprocessing = 0;
770-
shellPath = /bin/sh;
771-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-TCATUITests/Pods-TCATUITests-frameworks.sh\"\n";
772-
showEnvVarsInLog = 0;
773-
};
774708
E3FCA3C71FAA5AFB0069BD0F /* Fabric Configuration */ = {
775709
isa = PBXShellScriptBuildPhase;
776710
buildActionMask = 12;

TCAT/AppDelegate.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
133133
Network.getAllStops().perform(withSuccess: { stops in
134134
let allBusStops = stops.allStops
135135
if allBusStops.isEmpty {
136-
let title = "Couldn't Fetch Bus Stops"
137-
let message = "The app will continue trying on launch. You can continue to use the app as normal."
138-
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
139-
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
140-
UIApplication.shared.keyWindow?.presentInApp(alertController)
136+
self.handleGetAllStopsError()
141137
} else {
142138
let data = NSKeyedArchiver.archivedData(withRootObject: allBusStops)
143139
self.userDefaults.set(data, forKey: Constants.UserDefaults.allBusStops)
144140
}
145141
}, failure: { error in
146142
print("getBusStops error:", error)
143+
self.handleGetAllStopsError()
147144
})
148145
}
149146

147+
/// Present an alert indicating bus stops weren't fetched.
148+
func handleGetAllStopsError() {
149+
let title = "Couldn't Fetch Bus Stops"
150+
let message = "The app will continue trying on launch. You can continue to use the app as normal."
151+
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
152+
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
153+
UIApplication.shared.keyWindow?.presentInApp(alertController)
154+
}
155+
150156
}
151157

152158
extension UIWindow {

TCAT/Controllers/AllStopsTableViewController.swift

Lines changed: 83 additions & 18 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 {
@@ -35,12 +36,7 @@ class AllStopsTableViewController: UITableViewController {
3536

3637
super.viewDidLoad()
3738
sectionIndexes = sectionIndexesForBusStop()
38-
39-
sortedKeys = Array(sectionIndexes.keys).sorted().filter({$0 != "#"})
40-
if !allStops.isEmpty {
41-
// Adding "#" to keys for bus stops that start with a number
42-
sortedKeys.append("#")
43-
}
39+
sortedKeys = sortedKeysForBusStops()
4440

4541
title = "All Stops"
4642
tableView.sectionIndexColor = .primaryTextColor
@@ -56,18 +52,15 @@ class AllStopsTableViewController: UITableViewController {
5652
}
5753

5854
tableView.emptyDataSetSource = self
59-
//tableView.emptyDataSetDelegate = self
55+
tableView.emptyDataSetDelegate = self
6056
tableView.tableFooterView = UIView()
6157
// Set top of table view to align with scroll view
6258
tableView.contentOffset = .zero
63-
6459
}
6560

66-
override func didReceiveMemoryWarning() {
67-
super.didReceiveMemoryWarning()
68-
// Dispose of any resources that can be recreated.
69-
}
61+
// MARK: TableView DataSource
7062

63+
/// Retrieves the section indexes for the bus stops
7164
func sectionIndexesForBusStop() -> [String: [BusStop]] {
7265

7366
var sectionIndexDictionary: [String: [BusStop]] = [:]
@@ -107,10 +100,32 @@ class AllStopsTableViewController: UITableViewController {
107100
}
108101

109102
return sectionIndexDictionary
103+
}
104+
105+
/// Retrieves the keys from the sectionIndexDictionary
106+
func sortedKeysForBusStops() -> [String] {
107+
// Don't include key '#'
108+
sortedKeys = Array(sectionIndexes.keys)
109+
.sorted()
110+
.filter { $0 != "#" }
110111

112+
if !allStops.isEmpty {
113+
// Adding "#" to keys for bus stops that start with a number
114+
sortedKeys.append("#")
115+
}
116+
117+
return sortedKeys
111118
}
112119

113-
// MARK: - Table view data source
120+
func setUpTableOnRetry() {
121+
// Retry getting data from user defaults
122+
self.allStops = SearchTableViewManager.shared.getAllStops()
123+
// Set up table information
124+
self.sectionIndexes = self.sectionIndexesForBusStop()
125+
self.sortedKeys = self.sortedKeysForBusStops()
126+
127+
self.tableView.reloadData()
128+
}
114129

115130
override func numberOfSections(in tableView: UITableView) -> Int {
116131
return sectionIndexes.count
@@ -174,16 +189,66 @@ class AllStopsTableViewController: UITableViewController {
174189
}
175190
}
176191

177-
// MARK: DZN EmptyDataSet Delegate
178-
extension AllStopsTableViewController: DZNEmptyDataSetSource {
192+
// MARK: DZNEmptyDataSet
193+
extension AllStopsTableViewController: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
194+
func setUpActivityIndicator() {
195+
activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
196+
if let activityIndicator = activityIndicator {
197+
view.addSubview(activityIndicator)
198+
activityIndicator.snp.makeConstraints { (make) in
199+
make.centerX.equalToSuperview()
200+
make.centerY.equalToSuperview()
201+
}
202+
}
203+
}
179204

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

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

0 commit comments

Comments
 (0)