Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. @IBOutlet weak var tableView: UITableView!
  2.  
  3. var posts = [Post]()
  4.  
  5.  
  6.  
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11.  
  12. loadPosts()
  13.  
  14. tableView.dataSource = self
  15. tableView.estimatedRowHeight = 375
  16. tableView.rowHeight = UITableView.automaticDimension
  17.  
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25. func loadPosts() {
  26.  
  27. Database.database().reference().child("posts").observe(.childAdded) { (snapshot: DataSnapshot) in
  28.  
  29. if let dict = snapshot.value as? [String: Any] {
  30.  
  31. let newPost = Post.transformPost(dict: dict)
  32. self.posts.append(newPost)
  33. self.tableView.reloadData()
  34.  
  35. }
  36.  
  37. }
  38.  
  39. }
  40.  
  41. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  42.  
  43. return posts.count
  44.  
  45. }
  46.  
  47.  
  48. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  49.  
  50. let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! HomeTableViewCell
  51. let post = posts[indexPath.row]
  52.  
  53. cell.post = post
  54.  
  55. return cell
  56.  
  57. }
  58.  
  59. @IBOutlet weak var profileImageView: UIImageView!
  60. @IBOutlet weak var usernameLabel: UILabel!
  61.  
  62. @IBOutlet weak var postImageView: UIImageView!
  63. @IBOutlet weak var captionLabel: UILabel!
  64.  
  65. @IBOutlet weak var cheerButton: UIButton!
  66. @IBOutlet weak var donateButton: UIButton!
  67. @IBOutlet weak var commentButton: UIButton!
  68.  
  69.  
  70.  
  71. var post: Post? {
  72. didSet {
  73. updateView()
  74. }
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. func updateView() {
  83.  
  84. captionLabel.text = post?.caption
  85.  
  86. if let photoUrlString = post?.photoUrl {
  87.  
  88. let photoUrl = URL(string: photoUrlString)
  89. postImageView.sd_setImage(with: photoUrl)
  90.  
  91. }
  92.  
  93. setUpUserInfo()
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. func setUpUserInfo() {
  103.  
  104. if let uid = post?.uid {
  105.  
  106. Database.database().reference().child("users").child(uid).observeSingleEvent(of: DataEventType.value, with: {
  107. snapshot in
  108.  
  109. if let dict = snapshot.value as? [String: Any] {
  110.  
  111. let user = User.transformUser(dict: dict)
  112. self.usernameLabel.text = user.username
  113.  
  114. if let photoUrlString = user.profileImageUrl {
  115.  
  116. let photoUrl = URL(string: photoUrlString)
  117. self.profileImageView.sd_setImage(with: photoUrl)
  118. }
  119.  
  120. }
  121.  
  122. })
  123.  
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131. override func awakeFromNib() {
  132. super.awakeFromNib()
  133.  
  134.  
  135.  
  136. usernameLabel.text = ""
  137. captionLabel.text = ""
  138.  
  139.  
  140. }
  141.  
  142.  
  143.  
  144. override func setSelected(_ selected: Bool, animated: Bool) {
  145. super.setSelected(selected, animated: animated)
  146.  
  147. }
  148.  
  149. var caption: String?
  150. var photoUrl: String?
  151. var uid: String?
  152.  
  153. static func transformPost(dict: [String: Any]) -> Post {
  154.  
  155. let post = Post()
  156. post.caption = dict["caption"] as? String
  157. post.photoUrl = dict["photoUrl"] as? String
  158. post.uid = dict["uid"] as? String
  159.  
  160. return post
  161.  
  162. }
  163.  
  164. var email: String?
  165. var profileImageUrl: String?
  166. var username: String?
  167.  
  168. static func transformUser(dict: [String: Any]) -> User {
  169.  
  170. let user = User()
  171. user.email = dict["email"] as? String
  172. user.profileImageUrl = dict["profileImageUrl"] as? String
  173. user.username = dict["username"] as? String
  174.  
  175. return user
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement