/
UserListViewController.swift
238 lines (177 loc) · 7.61 KB
/
UserListViewController.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// UserListViewController.swift
// Emojr
//
// Created by Joseph Pecoraro on 6/7/16.
// Copyright © 2016 Joseph Pecoraro. All rights reserved.
//
import UIKit
class UserListViewController: UIViewController {
var tableView = UITableView()
var allUsers = [UserData]()
var shownUsers = [UserData]()
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(self.refreshData), for: UIControlEvents.valueChanged)
return refreshControl
}()
lazy var noDataView: NoDataView = {
let view = NoDataView.instanceFromNib()
view.translatesAutoresizingMaskIntoConstraints = false
view.messageLabel.text = self.noDataMessage
return view
}()
var noDataMessage = "No users here!"
let networkFacade = NetworkFacade()
let followManager = FollowUserManager()
override func viewDidLoad() {
super.viewDidLoad()
layoutTableView()
self.navigationItem.rightBarButtonItem = rightBarButtonItem()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
refreshData()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
}
func rightBarButtonItem() -> UIBarButtonItem? {
return UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(navigateToAddBySearch))
}
func layoutTableView() {
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.constrainToEdges(of: view)
tableView.addSubview(refreshControl)
tableView.tableFooterView = UIView(frame: .zero)
tableView.delegate = self;
tableView.dataSource = self;
tableView.register(UINib(nibName: "UserTableViewCell", bundle:nil), forCellReuseIdentifier: UserCellIdentifier)
}
@objc func refreshData() {
refreshControl.beginRefreshing()
networkFacade.getUsers(completionBlock: userResponseHandler)
}
func userResponseHandler(_ error: Error?, _ list: [UserData]?) {
if let error = error {
log.error(error)
refreshControl.endRefreshing()
} else {
self.allUsers = list ?? []
self.updateShownUsers()
}
}
func updateShownUsers() {
shownUsers = allUsers.filter { $0.username != User.sharedInstance.username! }
tableView.reloadData()
refreshControl.endRefreshing()
if shownUsers.count == 0 {
displayNoDataView()
} else {
removeNoDataView()
}
}
@objc func navigateToAddBySearch() {
self.navigationController?.pushViewController(AddUsersViewController(), animated: true)
}
func askToFollowUser(_ user: UserData) {
followManager.startFollowing(user: user, completionBlock: followUserResultBlock)
}
func askToStopFollowingUser(_ user: UserData) {
followManager.askToStopFollowingUser(user, presentingViewController: self, completionBlock: followUserResultBlock)
}
func followUserResultBlock(success: Bool) {
tableView.reloadData()
}
func displayNoDataView() {
self.view.addSubview(noDataView)
noDataView.constrainToEdges(of: view)
}
func removeNoDataView() {
noDataView.removeFromSuperview()
}
@IBAction func closeView(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
}
}
extension UserListViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let user = shownUsers[indexPath.row];
if user.username! != User.sharedInstance.username! {
showActionSheet(for: user)
}
}
func showActionSheet(for user: UserData) {
let actionSheet = UIAlertController(title: "What would you like to do?",
message: nil,
preferredStyle: .actionSheet)
var followActionTitle: String?
var followActionBlock: ((UIAlertAction) -> Void)?
if User.sharedInstance.isFollowing(user: user) {
followActionTitle = "Unfollow \(user.username!)?"
followActionBlock = { action in
self.askToStopFollowingUser(user)
}
} else {
followActionTitle = "Follow \(user.username!)?"
followActionBlock = { action in
self.askToFollowUser(user)
}
}
let followAction = UIAlertAction(title: followActionTitle,
style: .default,
handler: followActionBlock)
actionSheet.addAction(followAction)
let viewUserPageAction = UIAlertAction(title: "View \(user.username!)'s Feed", style: .default) { (action: UIAlertAction) in
let userTimelineVC = TimelineViewController(with: .user(user: user))
userTimelineVC.user = user
self.navigationController?.pushViewController(userTimelineVC, animated: true)
}
actionSheet.addAction(viewUserPageAction)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheet.addAction(cancel)
self.present(actionSheet, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
if (User.sharedInstance.id! == shownUsers[indexPath.row].id!) {
return false
}
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let user = shownUsers[indexPath.row]
return followManager.editActionFor(user: user, presentingViewController: self, completionBlock: followUserResultBlock)
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let user = shownUsers[indexPath.row]
return User.sharedInstance.id != user.id
}
}
extension UserListViewController : UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.shownUsers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: UserCellIdentifier) as! UserTableViewCell;
let user = shownUsers[indexPath.row]
cell.usernameLabel.text = user.username
if User.sharedInstance.isFollowing(user: user) {
cell.emojiFollowingLabel.isHidden = false
}
else {
cell.emojiFollowingLabel.isHidden = true
}
return cell;
}
}