-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleUIViewControllerFromStoryboard.swift
124 lines (93 loc) · 3.7 KB
/
ExampleUIViewControllerFromStoryboard.swift
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright © 2020 Lukas Kukacka. All rights reserved.
//
import UIKit
class ExampleUIViewControllerFromStoryboard: UIViewController {
/// Storyboard does not allow better dependency injection.
/// Without using Storybpard, this should be `let` injected in `init`
var viewModel: ExampleViewModel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var acitivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var reloadNavigationItem: UIBarButtonItem!
@IBOutlet weak var resetNavigationItem: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "UIViewController example"
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.dataSource = self
self.setUpObservers()
// Start loading view model immediatelly
if self.viewModel.state == .empty {
self.viewModel.receive(.reload)
}
self.setUpViews(forState: self.viewModel.state)
}
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)
/// Bit of hack for `UIViewControllerRepresentable`
/// See https://stackoverflow.com/a/59317657
/// There is some glitch in the animation, but that is not concern of this demo
parent?.navigationItem.title = self.title
parent?.navigationItem.rightBarButtonItems = self.navigationItem.rightBarButtonItems
}
}
// MARK: Observers
extension ExampleUIViewControllerFromStoryboard {
func setUpObservers() {
// React to state changes.
// Here similarly to SwiftUI, we configure view according to state
self.viewModel.onStateChanged = { [weak self] _, newState in
self?.setUpViews(forState: newState)
}
}
}
// MARK: User actions
extension ExampleUIViewControllerFromStoryboard {
@IBAction func reload(_ sender: Any) {
self.viewModel.receive(.reload)
}
@IBAction func reset(_ sender: Any) {
self.viewModel.receive(.reset)
}
}
// MARK: UITableViewDataSource
extension ExampleUIViewControllerFromStoryboard: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.viewModel.state.values?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let value = self.viewModel.state.values![indexPath.row]
cell.textLabel?.text = value
return cell
}
}
// MARK: Private
private extension ExampleUIViewControllerFromStoryboard {
func setUpViews(forState state: ExampleViewModel.State) {
self.resetViews()
switch state {
case .empty:
self.label.text = "No content\nUse the Reload ↻ button"
case .loading:
self.label.text = "Loading..."
self.acitivityIndicator.startAnimating()
case .error(let error):
let errorDescription = (error as NSError).localizedDescription
self.label.text = "Something went wrong\n\(errorDescription)"
case .loaded:
self.tableView.reloadData()
self.tableView.isHidden = false
}
self.label.isHidden = (self.label.text == nil)
self.reloadNavigationItem.isEnabled = self.viewModel.canReload
self.resetNavigationItem.isEnabled = self.viewModel.canReset
}
func resetViews() {
self.tableView.isHidden = true
self.acitivityIndicator.stopAnimating()
self.label.isHidden = true
self.label.text = nil
}
}