Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Firebase
- class NotesTableViewController: UITableViewController, SaveNoteDelegate {
- var ref: DatabaseReference!
- var notes = [Notes]()
- var currentUserName = "Here comes the username"
- override func viewDidLoad() {
- super.viewDidLoad()
- title = currentUserName
- }
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return notes.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! NotesTableViewCell
- cell.userNameLabel.text = notes[indexPath.row].name
- cell.userImage.image = notes[indexPath.row].userImage
- return cell
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "notesToWrite", let destination = segue.destination as? WriteViewController {
- destination.delegate = self
- }
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 250
- }
- func saveNote(note: Notes) {
- notes.append(note)
- let storageRef = Storage.storage().reference()
- let imagesRef = storageRef.child("PushImageAttachment")
- let fileName = "pushImage.jpg"
- let spaceRef = imagesRef.child(fileName)
- if let data = UIImageJPEGRepresentation(note.userImage, 0.6) {
- spaceRef.putData(data, metadata: nil, completion: { [unowned self] (metadata, error) in
- if error != nil {
- print(error?.localizedDescription)
- return
- }
- if let imgURL = metadata?.downloadURL()?.absoluteString {
- self.handleNoteSave(note: note, imageURL: imgURL)
- }
- })
- }
- }
- func handleNoteSave(note: Notes, imageURL: String) {
- ref = Database.database().reference()
- let noteToSave = ["name" : currentUserName, "message": note.name, "imageURL": imageURL] as [String: Any]
- ref.child("users").child(currentUserName).setValue(noteToSave) { (error, dataRef) in
- if error != nil {
- print("error occured", error?.localizedDescription ?? "")
- } else {
- DispatchQueue.main.async {
- self.tableView.reloadData()
- }
- print("success")
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment