-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedTableViewController.swift
269 lines (181 loc) · 9.61 KB
/
FeedTableViewController.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// FeedTableViewController.swift
// ParseStarterProject
//
// Created by Lakshmi sai nadh godena on 10/8/15.
// Copyright © 2015 Parse. All rights reserved.
//
import UIKit
import Parse
class FeedTableViewController: UITableViewController{
var messages = [String]()
var usernames = [String]()
var postedTimes = [String]()
var typeOfMessages = [String]()
var userids = [String]()
var profilePictures = [String:UIImage]()
let placeholderImage : UIImage = UIImage(named:"placeholder-hi.png")!
func swipeLeft(recognizer:UISwipeGestureRecognizer) {
self.performSegueWithIdentifier("userlist", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 132
let getFollowedUsersQuery = PFQuery(className: "followers")
getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
getFollowedUsersQuery.orderByDescending("createdAt")
getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
self.messages.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
// self.imageFiles.removeAll(keepCapacity: true)
self.postedTimes.removeAll(keepCapacity: true)
self.typeOfMessages.removeAll(keepCapacity: true)
self.profilePictures.removeAll(keepCapacity: true)
self.userids.removeAll(keepCapacity: true)
for object in objects {
let followedUser = object["following"] as! String
let query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.messages.append(object["message"] as! String)
let seconds:NSTimeInterval = NSDate().timeIntervalSinceDate(object.createdAt! as NSDate)
self.postedTimes.append(TimeConversion.timeElapsed(seconds))
self.typeOfMessages.append(object["typeOfMessage"] as! String)
self.userids.append(object["userId"] as! String)
let usernamesQuery = PFUser.query()
usernamesQuery?.whereKey("objectId", equalTo: object["userId"] as! String)
usernamesQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
print("success")
self.usernames.append(object["firstName"] as! String)
self.tableView.reloadData()
}
}
})
}
}
})
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return usernames.count
}
// override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//
// // let cell = self.tableView(self.tableView, cellForRowAtIndexPath: indexPath) as! Cell
//
//
//
// //cell.message.text = messages[indexPath.row]
// //cell.message.sizeToFit()
//
// return 222.0
//
// }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell
cell.username.text = usernames[indexPath.row]
print("Before",cell.message.frame.size.height)
cell.message.layoutIfNeeded()
cell.message.text = messages[indexPath.row]
print("After",cell.message.frame.size.height)
// cell.message.sizeToFit()
//
// let bounds = cell.bounds
//
// print(bounds.height+cell.message.bounds.height)
//
//
// cell.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width, height: bounds.height+cell.message.bounds.height)
let user = PFUser.query()
user!.whereKey("objectId", equalTo: self.userids[indexPath.row] as! String)
user?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
if let userPicture = object["profilePicture"] as? PFFile {
userPicture.getDataInBackgroundWithBlock({ (data, error) -> Void in
if error == nil {
cell.profilePicture.image = UIImage(data: data!)
cell.username.text = object["firstName"] as! String
} else {
cell.profilePicture.image = self.placeholderImage
cell.username.text = object["firstName"] as! String
}
})
} else {
cell.profilePicture.image = self.placeholderImage
cell.username.text = object["firstName"] as! String
}
}
}
})
//cell.profilePicture.image = profilePictures[usernames[indexPath.row]]
if(typeOfMessages[indexPath.row] == "High Alert") {
cell.typeOfMessage.image = UIImage(named: "rect_red.png")
}
else if (typeOfMessages[indexPath.row] == "Moderate") {
cell.typeOfMessage.image = UIImage(named: "rect_orange.png")
}
else {
cell.typeOfMessage.image = UIImage(named: "rect_blue.png")
}
cell.postedTime.text = postedTimes[indexPath.row]
print(cell.message.frame.size.height)
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}