-
Notifications
You must be signed in to change notification settings - Fork 1
/
FeedViewController.swift
208 lines (160 loc) · 6.64 KB
/
FeedViewController.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
//
// FeedViewController.swift
// Chasm
//
// Created by Nick Melkadze on 10/27/22.
// Updated by Georgina Woo on 10/28/22.
import UIKit
import AlamofireImage
import Parse
import MessageInputBar
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MessageInputBarDelegate {
let commentBar = MessageInputBar()
var showsCommentBar = false
var posts = [PFObject]() //post of arrays, an empty array
var numberOfPosts: Int!
var selectedPost: PFObject!
let myRefreshControl = UIRefreshControl()
/*
PFUser.logOut()
let main = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = main.instantiateViewController(withIdentifier: "LoginViewController")
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let delegate = windowScene.delegate as? SceneDelegate else { return }
delegate.window?.rootViewController = loginViewController
*/
override var inputAccessoryView: UIView?
{
return commentBar
}
override var canBecomeFirstResponder: Bool
{
return showsCommentBar
}
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
commentBar.inputTextView.placeholder = "Comment on this rock..."
commentBar.sendButton.title = "Post"
commentBar.delegate = self
tableView.delegate = self
tableView.dataSource = self
tableView.keyboardDismissMode = .interactive
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(keyboardWillBeHidden(note:)), name: UIResponder.keyboardWillHideNotification, object: nil)
myRefreshControl.addTarget(self, action: #selector(viewDidAppear), for: .valueChanged)
tableView.refreshControl = myRefreshControl
// Do any additional setup after loading the view.
}
@objc func keyboardWillBeHidden(note: Notification)
{
commentBar.inputTextView.text = nil
showsCommentBar = false
becomeFirstResponder()
}
override func viewDidAppear(_ animated: Bool) {
numberOfPosts = 20
super.viewDidAppear(animated)
let query = PFQuery(className: "Posts")
query.includeKeys(["author", "comment", "comment.author"])
query.limit = numberOfPosts
query.order(byDescending : "createdAt")
query.findObjectsInBackground{ (posts, error) in
if posts != nil {
self.posts = posts!
self.tableView.reloadData()
self.myRefreshControl.endRefreshing()
}
}
}
func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
// Create the comment
let comment = PFObject(className: "Comments")
comment["text"] = text
comment["post"] = selectedPost
comment["author"] = PFUser.current()!
selectedPost.add(comment, forKey: "comment")
selectedPost.saveInBackground{ (success, error) in
if success{
print("Comment saved")
} else {
print("Error saving comment")
}
}
tableView.reloadData()
// Clear and dismiss the input bar
commentBar.inputTextView.text = nil
showsCommentBar = false
becomeFirstResponder()
commentBar.inputTextView.resignFirstResponder()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let post = posts[section]
let comments = (post["comment"] as? [PFObject]) ?? []
return comments.count + 2
}
func numberOfSections(in tableView: UITableView) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.section]
let comments = (post["comment"] as? [PFObject]) ?? []
if indexPath.row == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! PostCell
let user = post["author"] as! PFUser
cell.usernameLabel.text = user.username
cell.captionLabel.text = post["caption"] as? String
let imageFile = post["image"] as! PFFileObject
let urlString = imageFile.url!
let url = URL(string: urlString)!
cell.photoView.af.setImage(withURL: url)
//cell.setFavorite(post[indexPath.row]["favorited"] as! Bool)
return cell
}
/*
else if indexPath.row > captions.count
{
print("got here", indexPath.row, captions.count)
let caption = tableView.dequeueReusableCell(withIdentifier: "CaptionCell") as! CaptionCell
let user = post["author"] as! PFUser
caption.usernameLabel.text = user.username
caption.captionLabel.text = post["caption"] as? String
return caption
}
*/
else if indexPath.row <= comments.count
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell") as! CommentCell
let comment = comments[indexPath.row - 1]
cell.commentLabel.text = comment["text"] as? String
let user = comment["author"] as! PFUser
cell.nameLabel.text = user.username
return cell
}
else
{
let cell = tableView.dequeueReusableCell(withIdentifier: "AddCommentCell")!
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let post = posts[indexPath.section]
let comments = (post["comment"] as? [PFObject]) ?? []
if indexPath.row == comments.count + 1
{
showsCommentBar = true
becomeFirstResponder()
commentBar.inputTextView.becomeFirstResponder()
selectedPost = post
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}