-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathTableViewController.swift
More file actions
95 lines (71 loc) · 2.97 KB
/
TableViewController.swift
File metadata and controls
95 lines (71 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// TableViewController.swift
// Example
//
// Created by Alexander Schuch on 16/05/17.
// Copyright © 2017 Alexander Schuch. All rights reserved.
//
import Foundation
import StatefulViewController
class TableViewController: UITableViewController, StatefulViewController {
fileprivate var dataArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Setup refresh control
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)
// Setup placeholder views
loadingView = LoadingView(frame: view.frame)
emptyView = EmptyView(frame: view.frame)
let failureView = ErrorView(frame: view.frame)
failureView.tapGestureRecognizer.addTarget(self, action: #selector(refresh))
errorView = failureView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupInitialViewState()
refresh()
}
@objc func refresh() {
if (lastState == .loading) { return }
startLoading {
print("completaion startLoading -> loadingState: \(self.currentState.rawValue)")
}
print("startLoading -> loadingState: \(self.lastState.rawValue)")
// Fake network call
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {
// Success
self.dataArray = ["Merlot", "Sauvignon Blanc", "Blaufränkisch", "Pinot Nior"]
self.tableView.reloadData()
self.endLoading(error: nil, completion: {
print("completion endLoading -> loadingState: \(self.currentState.rawValue)")
})
print("endLoading -> loadingState: \(self.lastState.rawValue)")
// Error
//self.endLoading(error: NSError(domain: "foo", code: -1, userInfo: nil))
// No Content
//self.endLoading(error: nil)
self.refreshControl?.endRefreshing()
}
}
}
extension TableViewController {
func hasContent() -> Bool {
return dataArray.count > 0
}
func handleErrorWhenContentAvailable(_ error: Error) {
let alertController = UIAlertController(title: "Ooops", message: "Something went wrong.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textCell", for: indexPath)
cell.textLabel?.text = dataArray[(indexPath as NSIndexPath).row]
return cell
}
}