채팅방에서 상대방 이름과 마지막 메세지 출력되도록 추가



//

//  ChatRoomsViewController.swift

//  FreeTalk

//

//  Created by stayfoolish on 2018. 9. 16..

//  Copyright © 2018년 stayfoolish. All rights reserved.

//




import UIKit

import Firebase


class ChatRoomsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!

    

    var uid : String!

    var chatrooms : [ChatModel]! = []

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        self.uid = Auth.auth().currentUser?.uid

        self.getChatroomsList()

        // Do any additional setup after loading the view.

    }

    

    func getChatroomsList(){

        

        Database.database().reference().child("chatrooms").queryOrdered(byChild: "users/"+uid).queryEqual(toValue: true).observeSingleEvent(of: DataEventType.value, with: {(datasnapshot) in

            

            for item in datasnapshot.children.allObjects as! [DataSnapshot]{

                self.chatrooms.removeAll()

                if let chatroomdic = item.value as? [String:AnyObject]{

                    let chatModel = ChatModel(JSON: chatroomdic)

                    self.chatrooms.append(chatModel!)

                }

                

            }

            self.tableview.reloadData()

            

            

        })

        

        

        

    }

    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.chatrooms.count

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell", for:indexPath) as! CustomCell

        

        var destinationUid: String?

        

        for item in chatrooms[indexPath.row].users{

            if(item.key != self.uid){

                destinationUid = item.key

            }

        }

        Database.database().reference().child("users").child(destinationUid!).observeSingleEvent(of: DataEventType.value, with: { (datasnapshot) in

            

            let userModel = UserModel()

            userModel.setValuesForKeys(datasnapshot.value as! [String:AnyObject])

            

            cell.label_title.text = userModel.userName

            let url = URL(string: userModel.profileImageUrl!)

            URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, err) in

                

                DispatchQueue.main.sync {

                    cell.imageview.image = UIImage(data: data!)

                    cell.imageview.layer.cornerRadius = cell.imageview.frame.width/2

                    cell.imageview.layer.masksToBounds = true

                }

            }).resume()

            

            let lastMessagekey = self.chatrooms[indexPath.row].comments.keys.sorted(){$0>$1}

            cell.label_lastmessage.text = self.chatrooms[indexPath.row].comments[lastMessagekey[0]]?.message

            

        })

        

        return cell

        

        

    }

    override func viewDidAppear(_ animated: Bool) {

        viewDidLoad()

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    

    /*

     // 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.destinationViewController.

     // Pass the selected object to the new view controller.

     }

     */

    

}


class CustomCell: UITableViewCell {

    

    @IBOutlet weak var label_lastmessage: UILabel!

    @IBOutlet weak var imageview: UIImageView!

    @IBOutlet weak var label_title: UILabel!

}











+ Recent posts