-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCommentsViewController.swift
175 lines (139 loc) · 5.79 KB
/
CommentsViewController.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Foundation
import UIKit
import KsApi
import Library
import ReactiveSwift
import Result
import Prelude
internal final class CommentsViewController: UITableViewController {
fileprivate let viewModel: CommentsViewModelType = CommentsViewModel()
fileprivate let dataSource = CommentsDataSource()
// This button needs to store a strong reference so as to not get wiped when setting hidden state.
@IBOutlet fileprivate var commentBarButton: UIBarButtonItem!
fileprivate weak var loginToutViewController: UIViewController?
internal static func configuredWith(project: Project? = nil, update: Update? = nil)
-> CommentsViewController {
let vc = Storyboard.Comments.instantiate(CommentsViewController.self)
vc.viewModel.inputs.configureWith(project: project, update: update)
return vc
}
internal override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self.dataSource
NotificationCenter.default
.addObserver(forName: Notification.Name.ksr_sessionStarted, object: nil, queue: nil) { [weak self] _ in
self?.viewModel.inputs.userSessionStarted()
}
self.viewModel.inputs.viewDidLoad()
self.navigationItem.title = Strings.project_menu_buttons_comments()
if self.traitCollection.userInterfaceIdiom == .pad {
self.navigationItem.leftBarButtonItem = .close(self, selector: #selector(closeButtonTapped))
}
}
internal override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
internal override func bindStyles() {
super.bindStyles()
_ = self
|> baseTableControllerStyle(estimatedRowHeight: 200.0)
|> CommentsViewController.lens.view.backgroundColor .~ .white
_ = self.commentBarButton
|> UIBarButtonItem.lens.title %~ { _ in Strings.general_navigation_buttons_comment() }
|> UIBarButtonItem.lens.accessibilityHint %~ { _ in
Strings.accessibility_dashboard_buttons_post_update_hint()
}
}
// swiftlint:disable function_body_length
internal override func bindViewModel() {
self.viewModel.outputs.closeLoginTout
.observeForControllerAction()
.observeValues { [weak self] in
self?.loginToutViewController?.dismiss(animated: true, completion: nil)
}
self.viewModel.outputs.commentBarButtonVisible
.observeForUI()
.observeValues { [weak self] visible in
self?.navigationItem.rightBarButtonItem = visible ? self?.commentBarButton : nil
}
self.viewModel.outputs.commentsAreLoading
.observeForUI()
.observeValues { [weak self] in
$0 ? self?.refreshControl?.beginRefreshing() : self?.refreshControl?.endRefreshing()
}
self.viewModel.outputs.dataSource
.observeForUI()
.observeValues { [weak self] comments, project, user in
self?.dataSource.load(comments: comments, project: project, loggedInUser: user)
self?.tableView.reloadData()
}
self.viewModel.outputs.emptyStateVisible
.observeForControllerAction()
.observeValues { [weak self] project, update in
self?.dataSource.load(project: project, update: update)
self?.tableView.reloadData()
}
self.viewModel.outputs.openLoginTout
.observeForControllerAction()
.observeValues { [weak self] in self?.presentLoginTout() }
self.viewModel.outputs.presentPostCommentDialog
.observeForControllerAction()
.observeValues { [weak self] project, update in
self?.presentCommentDialog(project: project, update: update)
}
}
// swiftlint:enable function_body_length
override func tableView(_ tableView: UITableView,
willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
if let emptyCell = cell as? CommentsEmptyStateCell {
emptyCell.delegate = self
}
self.viewModel.inputs.willDisplayRow(self.dataSource.itemIndexAt(indexPath),
outOf: self.dataSource.numberOfItems())
}
internal func presentCommentDialog(project: Project, update: Update?) {
let dialog = CommentDialogViewController
.configuredWith(project: project, update: update, recipient: nil,
context: update == nil ? .projectComments : .updateComments)
dialog.delegate = self
let nav = UINavigationController(rootViewController: dialog)
nav.modalPresentationStyle = .formSheet
self.present(nav, animated: true, completion: nil)
}
internal func presentLoginTout() {
let login = LoginToutViewController.configuredWith(loginIntent: .generic)
let nav = UINavigationController(rootViewController: login)
nav.modalPresentationStyle = .formSheet
self.present(nav, animated: true, completion: nil)
}
@IBAction func commentButtonPressed() {
self.viewModel.inputs.commentButtonPressed()
}
@IBAction internal func refresh() {
self.viewModel.inputs.refresh()
}
@objc fileprivate func closeButtonTapped() {
self.dismiss(animated: true, completion: nil)
}
}
extension CommentsViewController: CommentDialogDelegate {
internal func commentDialogWantsDismissal(_ dialog: CommentDialogViewController) {
dialog.dismiss(animated: true, completion: nil)
}
internal func commentDialog(_ dialog: CommentDialogViewController, postedComment comment: Comment) {
self.viewModel.inputs.commentPosted(comment)
}
}
extension CommentsViewController: CommentsEmptyStateCellDelegate {
internal func commentEmptyStateCellGoBackToProject() {
_ = self.navigationController?.popViewController(animated: true)
}
internal func commentEmptyStateCellGoToCommentDialog() {
self.viewModel.inputs.commentButtonPressed()
}
internal func commentEmptyStateCellGoToLoginTout() {
self.viewModel.inputs.loginButtonPressed()
}
}