Guest User

Untitled

a guest
Jun 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. enum PostType: String {
  2.  
  3. case image = "image"
  4. case gif = "gif"
  5. case video = "video"
  6. case text = "text"
  7. case link = "link"
  8. case audio = "audio"
  9. case poll = "poll"
  10. case chat = "chat"
  11. case quote = "quote"
  12. }
  13.  
  14. func loadPosts() {
  15. activityIndicator.startAnimating()
  16. Api.Feed.observeFeedPosts(withUserId: Api.Users.CURRENT_USER!.uid) {
  17. post in
  18. guard let userId = post.userUid else { return }
  19. self.fetchUser(uid: userId, completed: {
  20. self.posts.insert(post, at: 0)
  21. self.activityIndicator.stopAnimating()
  22. self.collectionView.reloadData()
  23. })
  24. }
  25. Api.Feed.observeFeedRemoved(withUserId: Api.Users.CURRENT_USER!.uid) { (post) in
  26.  
  27. self.posts = self.posts.filter { $0.id != post.id } // removed all array elements matching the key
  28. self.users = self.users.filter { $0.id != post.userUid }
  29. self.collectionView.reloadData()
  30. }
  31. }
  32.  
  33. func fetchUser(uid: String, completed: @escaping () -> Void ) {
  34. Api.Users.observeUsersShort(withId: uid) {
  35. user in
  36. self.users.insert(user, at: 0)
  37. completed()
  38. }
  39. }
  40.  
  41. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  42. return posts.count
  43. }
  44.  
  45. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  46. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellName.postTextCVC, for: indexPath) as! PostTextCVC
  47. let user = users[indexPath.row]
  48. let post = posts[indexPath.row]
  49. cell.delegatePostTextCVC = self
  50. cell.user = user
  51. cell.dashboardVC = self
  52. cell.post = post
  53. return cell
  54. }
Add Comment
Please, Sign In to add comment