Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. @IBOutlet weak var chatsTableView: UITableView!
  2.  
  3. var recentChats: [NSDictionary] = []
  4. var filteredChats: [NSDictionary] = []
  5.  
  6. var recentListener: ListenerRegistration!
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10. navigationController?.navigationBar.prefersLargeTitles = true
  11. loadRecentChats()
  12. }
  13. //MARK IBActions
  14. @IBAction func newChatBtnPressed(_ sender: Any) {
  15. let userVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "userTableView") as! YsersTableVC
  16.  
  17. self.navigationController?.pushViewController(userVC, animated: true)
  18.  
  19. }
  20. //TableViewDataSource
  21.  
  22. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  23. print ("here is the (recentChats.count)")
  24. return recentChats.count
  25. }
  26.  
  27. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  28.  
  29. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RecentChatTableViewCell
  30.  
  31. let recent = recentChats[indexPath.row]
  32.  
  33. cell.generateCell(recentChat: recent, indexPath: indexPath)
  34.  
  35. return cell
  36. }
  37.  
  38. //MARK: LoadRecentChats
  39.  
  40. func loadRecentChats() {
  41. recentListener = reference(.Recent).whereField(kUSERID, isEqualTo: FUser.currentId()).addSnapshotListener({ (snapshot, error) in
  42.  
  43. guard let snapshot = snapshot else { return }
  44. print ("snapshot count is (snapshot.count)")
  45. print ("(snapshot)")
  46.  
  47. self.recentChats = []
  48.  
  49. if !snapshot.isEmpty {
  50.  
  51. let sorted = ((dictionaryFromSnapshots(snapshots: snapshot.documents)) as NSArray).sortedArray(using: [NSSortDescriptor(key: kDATE, ascending: false)]) as! [NSDictionary]
  52.  
  53. for recent in sorted {
  54.  
  55. if recent[kLASTMESSAGE] as! String != "" && recent[kCHATROOMID] != nil && recent[kRECENTID] != nil {
  56.  
  57. self.recentChats.append(recent)
  58. }
  59. }
  60. self.chatsTableView.reloadData()
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement