//

//  SignupViewController.swift

//  FreeTalk

//

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

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

//


import UIKit

import Firebase


class SignupViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {

    

    @IBOutlet var imageView: UIImageView!

    

    @IBOutlet var email: UITextField!

    @IBOutlet var name: UITextField!

    

    @IBOutlet var password: UITextField!

   

    @IBOutlet var signup: UIButton!

    @IBOutlet var cancel: UIButton!

    

    let remoteConfig = RemoteConfig.remoteConfig()

    var color: String?


    override func viewDidLoad() {

        super.viewDidLoad()


        let statusBar = UIView()

        self.view.addSubview(statusBar)

        statusBar.snp.makeConstraints {(m) in

            m.right.top.left.equalTo(self.view)

            m.height.equalTo(20)

        }

        color = remoteConfig["splash_background"].stringValue

        

        statusBar.backgroundColor = UIColor(hex: color!)

        

        

        imageView.isUserInteractionEnabled = true

        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePicker)))

        

        

        signup.backgroundColor = UIColor(hex: color!)

        cancel.backgroundColor = UIColor(hex: color!)

        

        signup.addTarget(self, action: #selector(signupEvent), for: .touchUpInside)

        cancel.addTarget(self, action: #selector(cancelevent), for: .touchUpInside)

        // Do any additional setup after loading the view.

    }

    @objc func imagePicker(){

        

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self

        imagePicker.allowsEditing = true

        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

        

        self.present(imagePicker, animated: true, completion: nil)

    }

    

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){

        imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage

        

        dismiss(animated: true, completion: nil)

    }

    

    @objc func signupEvent(){

        Auth.auth().createUser(withEmail: email.text!, password: password.text!) {(user, err) in

            let uid = user?.uid

            

            let image = UIImageJPEGRepresentation(self.imageView.image!, 0.1)

            

            Storage.storage().reference().child("userImages").child(uid!).putData(image!, metadata: nil, completion: { (data, error) in

             

                let imageUrl = data?.downloadURL()?.absoluteString

                let values = ["userName":self.name.text!,"profileImageUrl":imageUrl]

                

                Database.database().reference().child("users").child(uid!).setValue(values, withCompletionBlock:

                    { (err, ref) in

                        if(err==nil){

                            self.cancelevent()

                        }

                })

            })

        }

    }

    

    @objc func cancelevent(){

        self.dismiss(animated: true, completion: nil)

    }

    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.

    }

    */


}








//

//  MainViewController.swift -> PeopleViewController.swift

//  FreeTalk

//

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

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

//


import UIKit

import SnapKit

import Firebase


class PeopleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var array :  [UserModel] = []

    var tableview : UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()


        tableview = UITableView()

        tableview.delegate = self

        tableview.dataSource = self

        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

        view.addSubview(tableview)

        tableview.snp.makeConstraints { (m) in

            m.top.equalTo(view)

            m.bottom.left.right.equalTo(view)

        }

        

            Database.database().reference().child("users").observe(DataEventType.value, with: { (snapshot) in

                

            

                self.array.removeAll()

                

                for child in snapshot.children {

                    let fchild = child as! DataSnapshot

                    let userModel = UserModel()

                    

                    userModel.setValuesForKeys(fchild.value as! [String : Any])

                    self.array.append(userModel)

                    

                }

                

                DispatchQueue.main.async {

                    self.tableview.reloadData();

                }

                

        })

            

        

        

        // Do any additional setup after loading the view.

    }


    

    

    

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

        return array.count

    }


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

        

        let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for :indexPath)

        

        let imageview = UIImageView()

        cell.addSubview(imageview)

        imageview.snp.makeConstraints{(m) in

            m.centerY.equalTo(cell)

            m.left.equalTo(cell).offset(10)

            m.height.width.equalTo(50)

        }

        

        URLSession.shared.dataTask(with: URL(string: array[indexPath.row].profileImageUrl!)!) { (data, response, err) in

            

            DispatchQueue.main.async {

                imageview.image = UIImage(data: data!)

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

                imageview.clipsToBounds = true

            }

        }.resume()

        

        let label = UILabel()

        cell.addSubview(label)

        label.snp.makeConstraints{ (m) in

            m.centerY.equalTo(cell)

            m.left.equalTo(imageview.snp.right).offset(20)

        }

        

        label.text = array[indexPath.row].userName

        

        return cell

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 70

    }

    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let view = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController")

        self.navigationController?.pushViewController(view!, animated: true)

    }


    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.

    }

    */


}









+ Recent posts