Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // VetrinaTableViewController.swift
- // CSMApp
- //
- // Created by Federico Vivian on 18/01/18.
- // Copyright © 2018 Club Sommozzatori Mestre. All rights reserved.
- //
- import UIKit
- import Firebase
- import Kingfisher
- struct VetrinaStruct {
- let uid : String!
- let urluid : String!
- let nomeuid : String!
- let urlPhoto : String!
- let like : Int!
- let commenti : Int!
- let descrizione : String!
- let commentoUltimo : String!
- let uidUltimo : String!
- let urlUltimo : String!
- let nomeUltimo : String!
- let isLike : Bool!
- }
- class VetrinaTableViewController: UITableViewController {
- @IBOutlet weak var rightButton: UIBarButtonItem!
- var vetrina = [VetrinaStruct]()
- var storageRef : StorageReference! = nil
- var handle: UInt!
- var isLikeArr : Bool!
- let ref = Database.database().reference()
- @IBOutlet weak var emailTxt: UILabel!
- @IBOutlet weak var scadenzaVisitaTxt: UILabel!
- @IBOutlet weak var dataNascitaTxt: UILabel!
- @IBOutlet weak var notificheSel: UISwitch!
- @IBOutlet weak var ultimoBrevettoTxt: UILabel!
- @IBOutlet weak var nominativoTxt: UILabel!
- @IBAction func backButton(_ sender: Any) {
- let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
- let desController = mainStoryboard.instantiateViewController(withIdentifier: "HomeTableViewController") as! MainTableViewController
- let newFrontViewController = UINavigationController.init(rootViewController:desController)
- revealViewController()?.setMainViewController(newFrontViewController, animated: true)
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- ref.removeObserver(withHandle: handle)
- }
- @IBOutlet weak var photoProfilo: UIImageView!
- var headerView: UIView!
- var newHeaderLayer: CAShapeLayer!
- override func viewDidLoad() {
- super.viewDidLoad()
- rightButton.target = self.revealViewController()
- self.revealViewController()?.isRightPresentViewOnTop = false
- rightButton.action = #selector(PBRevealViewController.revealRightView)
- updateView()
- //MARK make navbar trasparent
- let bar:UINavigationBar! = self.navigationController?.navigationBar
- bar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
- bar.shadowImage = UIImage()
- }
- override func viewWillAppear(_ animated: Bool) {
- tableView.rowHeight = UITableViewAutomaticDimension
- tableView.estimatedRowHeight = 500
- if let user = Auth.auth().currentUser {
- useridIniziale = user
- vetrina.removeAll()
- leggiDati()
- // custom
- let label = UILabel()
- label.frame = CGRect(x: 10, y: -50, width: 250, height: 40)
- label.textColor = UIColor.white
- label.text = "Posta le tue Foto"
- label.font = UIFont.boldSystemFont(ofSize: 16)
- self.view.addSubview(label)
- // button
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: view.bounds.width - 55, y: -60, width: 44, height: 44)
- button.setImage(UIImage(named: "add_blue"), for: UIControlState())
- //button.tintColor = UIColor.white
- button.addTarget(self, action: #selector(stampa), for: .touchUpInside)
- view.addSubview(button)
- }
- }
- override func scrollViewDidScroll(_ scrollView: UIScrollView) {
- var offset = (scrollView.contentOffset.y / 150) + 1.5
- if offset > 1 {
- offset = 1
- let color = UIColor(red: 0, green: 0.5, blue: 1, alpha: offset - 0.5)
- self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
- self.navigationController?.navigationBar.backgroundColor = color
- UIApplication.shared.statusBarView?.backgroundColor = color
- } else {
- let color = UIColor(red: 0, green: 0.5, blue: 1, alpha: offset - 0.5)
- self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
- self.navigationController?.navigationBar.backgroundColor = color
- UIApplication.shared.statusBarView?.backgroundColor = color
- }
- setNewView()
- }
- func updateView() {
- tableView.backgroundColor = UIColor.white
- headerView = tableView.tableHeaderView
- tableView.tableHeaderView = nil
- tableView.rowHeight = UITableViewAutomaticDimension
- tableView.addSubview(headerView)
- newHeaderLayer = CAShapeLayer()
- newHeaderLayer.fillColor = UIColor.black.cgColor
- headerView.layer.mask = newHeaderLayer
- let newHeight = StretchyHeader1().headerHeight - StretchyHeader1().headerCut/2
- tableView.contentInset = UIEdgeInsets(top: newHeight, left: 0, bottom: 0, right: 0)
- tableView.contentOffset = CGPoint(x: 0, y: -newHeight)
- setNewView()
- }
- func setNewView(){
- let newHeight = StretchyHeader1().headerHeight - StretchyHeader1().headerCut/2
- var getHeaderFrame = CGRect(x: 0, y: -newHeight, width: tableView.bounds.width, height: StretchyHeader1().headerHeight)
- if tableView.contentOffset.y < newHeight {
- getHeaderFrame.origin.y = tableView.contentOffset.y
- getHeaderFrame.size.height = -tableView.contentOffset.y + StretchyHeader1().headerCut/2
- }
- headerView.frame = getHeaderFrame
- let cutDirection = UIBezierPath()
- cutDirection.move(to: CGPoint(x:0, y:0))
- cutDirection.addLine(to: CGPoint(x: getHeaderFrame.width, y:0))
- cutDirection.addLine(to: CGPoint(x: getHeaderFrame.width, y: getHeaderFrame.height ))
- cutDirection.addLine(to: CGPoint(x: 0, y: getHeaderFrame.height - StretchyHeader1().headerCut))
- newHeaderLayer.path = cutDirection.cgPath
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - Table view data source
- override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return UITableViewAutomaticDimension
- }
- @IBAction func unwindToViewController(segue: UIStoryboardSegue) {
- }
- func leggiDati(){
- let usersRef = ref.child("users")
- let likeRef = ref.child("Likes")
- let utente = useridIniziale?.uid
- handle = ref.child("Vetrina").observe(.value, with: { (snapshot) in
- // ref.child("Vetrina").observeSingleEvent(of: .value, with: { (snapshot) in
- let valore = snapshot.value as? NSDictionary
- if valore != nil {
- self.vetrina.removeAll()
- for (key, value) in valore! {
- guard let dict_value = value as? [String : Any] else {
- print("i dati di \(key) non sono un dizionario")
- return
- }
- let idVetrinaArr = key as? String ?? ""
- let uidArr = dict_value["uid"] as? String ?? ""
- let likeArr = dict_value["Like"] as? Int ?? 0
- let commentiArr = dict_value["Commenti"] as? Int ?? 0
- let URLPhotoArr = dict_value["URLPhoto"] as? String ?? ""
- let descrizioneArr = dict_value["Descrizione"] as? String ?? ""
- let commentoUltimoArr = dict_value["commentoUltimo"] as? String ?? ""
- let uidUltimoArr = dict_value["uidUltimo"] as? String ?? ""
- let nomeUltimoArr = dict_value["nomeUltimo"] as? String ?? ""
- let userRef = usersRef.child(uidArr)
- userRef.observeSingleEvent(of: .value, with: { (snap1) in
- let value = snap1.value as? NSDictionary
- let url1 = value!["imageDownloadURL"] as! String
- let nome1 = value!["Nome"] as! String
- let cognome1 = value!["Cognome"] as! String
- print(nome1 + " " + cognome1 + " " + uidArr + " " + url1)
- let userRef1 = usersRef.child(uidUltimoArr)
- userRef1.observeSingleEvent(of: .value, with: { (snap2) in
- let value = snap2.value as? NSDictionary
- let url2 = value!["imageDownloadURL"] as! String
- let userRef2 = likeRef.child(idVetrinaArr)
- userRef2.observeSingleEvent(of: .value, with: { (snap3) in
- if snap3.hasChild(utente!){
- self.isLikeArr = true
- } else {
- self.isLikeArr = false
- }
- self.vetrina.insert(VetrinaStruct(
- uid : uidArr,
- urluid : url1,
- nomeuid : cognome1 + " " + nome1,
- urlPhoto : URLPhotoArr,
- like : likeArr,
- commenti : commentiArr,
- descrizione : descrizioneArr,
- commentoUltimo : commentoUltimoArr,
- uidUltimo : uidUltimoArr,
- urlUltimo : url2,
- nomeUltimo : nomeUltimoArr,
- isLike : self.isLikeArr
- ), at: 0)
- self.vetrina.sort {
- $0.commenti > $1.commenti
- }
- self.tableView.reloadData()
- // self.reload(tableView: self.tableView)
- self.tableView.setNeedsLayout()
- self.tableView.layoutIfNeeded()
- })
- })
- })
- }
- } else {
- }
- }
- ) { (error) in
- print(error.localizedDescription)
- }
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return vetrina.count
- }
- /*
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 470.0
- }
- */
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableViewAutomaticDimension
- }
- override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableViewAutomaticDimension
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! VetrinaTableViewCell
- cell.uidImg.layer.cornerRadius = cell.uidImg.frame.size.width/2
- cell.uidImg.layer.borderWidth = 0
- cell.uidImg.clipsToBounds = true
- let resource = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urluid)!, cacheKey: vetrina[indexPath.row].urluid)
- let p = Bundle.main.path(forResource: "loading", ofType: "gif")
- let data = try! Data(contentsOf: URL(fileURLWithPath: p!))
- cell.uidImg.kf.indicatorType = .image(imageData: data)
- cell.uidImg.kf.setImage(with: resource)
- cell.nomeuid.text = vetrina[indexPath.row].nomeuid
- let resource1 = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urlPhoto)!, cacheKey: vetrina[indexPath.row].urlPhoto)
- let p1 = Bundle.main.path(forResource: "loading", ofType: "gif")
- let data1 = try! Data(contentsOf: URL(fileURLWithPath: p1!))
- cell.immagineVetrina.kf.indicatorType = .image(imageData: data1)
- cell.immagineVetrina.kf.setImage(with: resource1)
- cell.numeroLike.text = String(vetrina[indexPath.row].like)
- cell.numeroCommenti.text = String(vetrina[indexPath.row].commenti)
- cell.immagineUltimo.layer.cornerRadius = cell.immagineUltimo.frame.size.width/2
- cell.immagineUltimo.layer.borderWidth = 0
- cell.immagineUltimo.clipsToBounds = true
- let resource2 = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urlUltimo)!, cacheKey: vetrina[indexPath.row].urlUltimo)
- let p2 = Bundle.main.path(forResource: "loading", ofType: "gif")
- let data2 = try! Data(contentsOf: URL(fileURLWithPath: p2!))
- cell.immagineUltimo.kf.indicatorType = .image(imageData: data2)
- cell.immagineUltimo.kf.setImage(with: resource2)
- cell.ultimoLbl.layer.masksToBounds = true
- cell.ultimoLbl.layer.cornerRadius = 10
- cell.ultimoLbl.text = ""
- if vetrina[indexPath.row].isLike {
- cell.likeImg.image = UIImage(named: "LikeBlu")
- } else {
- cell.likeImg.image = UIImage(named: "LikeTras")
- }
- let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapping))
- cell.likeImg.isUserInteractionEnabled = true
- cell.likeImg.addGestureRecognizer(tap)
- cell.ultimoLbl.text = vetrina[indexPath.row].commentoUltimo
- return cell
- }
- @objc func tapping(){
- print("tapping")
- performSegue(withIdentifier: "segueLike", sender: self)
- }
- @objc func stampa() {
- performSegue(withIdentifier: "segueNuovoBrevetto", sender: self)
- }
- // MARK: - Navigation
- func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
- // Get Cell Label
- performSegue(withIdentifier: "segueBrevetto", sender: self)
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "segueBrevetto"
- {
- let destination = segue.destination as! UINavigationController
- let guest = destination.topViewController as! BrevettoDettaglioViewController
- let indexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
- let codice = self.vetrina[indexPath.row].uid
- guest.codicePass = codice!
- }
- }
- func reload(tableView: UITableView) {
- let contentOffset = tableView.contentOffset
- tableView.reloadData()
- tableView.layoutIfNeeded()
- tableView.layer.removeAllAnimations()
- tableView.setContentOffset(contentOffset, animated: false)
- }
- }
Add Comment
Please, Sign In to add comment