These are chat archives for correa-brian/milkshake

22nd
Jun 2016
Brian Correa
@correa-brian
Jun 22 2016 17:11
//
//  CTChatViewController.swift
//  chat-ios
//
//  Created by Brian Correa on 6/16/16.
//  Copyright © 2016 Velocity 360. All rights reserved.
//

import UIKit
import Firebase

class CTChatViewController: CTViewController, UITableViewDelegate, UITableViewDataSource {

    //Firebase Config:
    var firebase: FIRDatabaseReference! // establishes connection and maintains connection to DB
    var _refHandle: UInt!

    var place: CTPlace!
    var chatTable: UITableView!

    override func loadView(){
        let frame = UIScreen.mainScreen().bounds
        let view = UIView(frame: frame)
        view.backgroundColor = .grayColor()

        self.chatTable = UITableView(frame: frame, style: .Plain)
        self.chatTable.dataSource = self
        self.chatTable.delegate = self
        self.chatTable.registerClass(CTChatTableViewCell.classForCoder(), forCellReuseIdentifier: "cellId")
        view.addSubview(self.chatTable)

        self.view = view
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.firebase = FIRDatabase.database().reference() // initialize FB manager
    }

    override func viewWillAppear(animated: Bool) {
        print("viewWillAppear:")

        //Listen for new messages in the FB DB
        self._refHandle = self.firebase.child(self.place.id).observeEventType(.Value, withBlock: { (snapshot) -> Void in
            print("SNAPSHOT -- \(snapshot)") // prints everything
        })
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(CTChatTableViewCell.cellId, forIndexPath: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"
        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("didSelectRowAtIndexPath")

        //Push data to Firebase Database
        let post = ["from":"test", "message":"this is a test"]
        self.firebase.child(self.place.id).childByAutoId().setValue(post)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}