Repository navigation
Expand file tree
/
Copy pathViewController.swift
More file actions
97 lines (73 loc) · 3.15 KB
/
Copy pathViewController.swift
File metadata and controls
97 lines (73 loc) · 3.15 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
96
97
//
// ViewController.swift
// Example
//
// Created by Alexander Schuch on 30/07/14.
// Copyright (c) 2014 Alexander Schuch. All rights reserved.
//
import UIKit
import StatefulViewController
class ViewController: UIViewController, StatefulViewController {
fileprivate var dataArray = [String]()
private let refreshControl = UIRefreshControl()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Setup refresh control
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
tableView.addSubview(refreshControl)
// 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()
}
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 ViewController {
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 ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
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
}
}