//

//  ChatModel.swift

//  FreeTalk

//

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

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

//


import UIKit


class ChatModel: NSObject {

    


    public var users: Dictionary<String,Bool> = [:] // 채팅방에 참여한 사람들

    public var comments: Dictionary<String,Comment> = [:] // 채팅방의 대화내용

    

    public class Comment{

        public var uid: String?

        public var message: String?

    }

}




//

//  ChatViewController.swift

//  FreeTalk

//

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

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

//


import UIKit

import Firebase


class ChatViewController: UIViewController {


    @IBOutlet var sendButton: UIButton!

    @IBOutlet var textfield_message: UITextField!

    

    var uid: String?

    var chatRoomUid: String?

    

    

    public var destinationUid: String? // 나중에 내가 채팅할 대상의 uid

    override func viewDidLoad() {

        super.viewDidLoad()

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

        sendButton.addTarget(self, action: #selector(createRoom), for: .touchUpInside)

        checkChatRoom()

        // Do any additional setup after loading the view.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    @objc func createRoom(){

        let createRoomInfo : Dictionary<String,Any> = [ "users" : [

            uid!: true,

            destinationUid! : true

            ]

        ]

        

        if(chatRoomUid == nil){

        Database.database().reference().child("chatrooms").childByAutoId().setValue(createRoomInfo)

        }else{

            let value: Dictionary<String,Any> = [

                "comments":[

                    "uid" : uid!,

                    "messaage" : textfield_message.text!

                ]

            ]

            Database.database().reference().child("chatrooms").child(chatRoomUid!).child("comments").childByAutoId().setValue(value)

        }

    }

    

    func checkChatRoom(){

        

        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.chatRoomUid = item.key

            }

        })

    }


    /*

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

    }

    */


}





+ Recent posts