Guest User

Untitled

a guest
Mar 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.67 KB | None | 0 0
  1. //
  2. // VetrinaTableViewController.swift
  3. // CSMApp
  4. //
  5. // Created by Federico Vivian on 18/01/18.
  6. // Copyright © 2018 Club Sommozzatori Mestre. All rights reserved.
  7. //
  8.  
  9.  
  10.  
  11.  
  12.  
  13. import UIKit
  14. import Firebase
  15. import Kingfisher
  16.  
  17. struct VetrinaStruct {
  18. let uid : String!
  19. let urluid : String!
  20. let nomeuid : String!
  21. let urlPhoto : String!
  22. let like : Int!
  23. let commenti : Int!
  24. let descrizione : String!
  25. let commentoUltimo : String!
  26. let uidUltimo : String!
  27. let urlUltimo : String!
  28. let nomeUltimo : String!
  29. let isLike : Bool!
  30.  
  31. }
  32.  
  33. class VetrinaTableViewController: UITableViewController {
  34.  
  35.  
  36. @IBOutlet weak var rightButton: UIBarButtonItem!
  37.  
  38. var vetrina = [VetrinaStruct]()
  39. var storageRef : StorageReference! = nil
  40. var handle: UInt!
  41. var isLikeArr : Bool!
  42. let ref = Database.database().reference()
  43.  
  44.  
  45.  
  46. @IBOutlet weak var emailTxt: UILabel!
  47. @IBOutlet weak var scadenzaVisitaTxt: UILabel!
  48. @IBOutlet weak var dataNascitaTxt: UILabel!
  49. @IBOutlet weak var notificheSel: UISwitch!
  50. @IBOutlet weak var ultimoBrevettoTxt: UILabel!
  51. @IBOutlet weak var nominativoTxt: UILabel!
  52.  
  53.  
  54. @IBAction func backButton(_ sender: Any) {
  55. let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  56. let desController = mainStoryboard.instantiateViewController(withIdentifier: "HomeTableViewController") as! MainTableViewController
  57. let newFrontViewController = UINavigationController.init(rootViewController:desController)
  58. revealViewController()?.setMainViewController(newFrontViewController, animated: true)
  59. }
  60.  
  61. override func viewDidDisappear(_ animated: Bool) {
  62. super.viewDidDisappear(animated)
  63. ref.removeObserver(withHandle: handle)
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. @IBOutlet weak var photoProfilo: UIImageView!
  72.  
  73. var headerView: UIView!
  74. var newHeaderLayer: CAShapeLayer!
  75.  
  76.  
  77. override func viewDidLoad() {
  78.  
  79.  
  80.  
  81. super.viewDidLoad()
  82.  
  83.  
  84. rightButton.target = self.revealViewController()
  85. self.revealViewController()?.isRightPresentViewOnTop = false
  86. rightButton.action = #selector(PBRevealViewController.revealRightView)
  87.  
  88. updateView()
  89.  
  90. //MARK make navbar trasparent
  91. let bar:UINavigationBar! = self.navigationController?.navigationBar
  92. bar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
  93. bar.shadowImage = UIImage()
  94.  
  95. }
  96.  
  97. override func viewWillAppear(_ animated: Bool) {
  98.  
  99. tableView.rowHeight = UITableViewAutomaticDimension
  100. tableView.estimatedRowHeight = 500
  101.  
  102. if let user = Auth.auth().currentUser {
  103.  
  104. useridIniziale = user
  105.  
  106. vetrina.removeAll()
  107.  
  108. leggiDati()
  109.  
  110. // custom
  111. let label = UILabel()
  112. label.frame = CGRect(x: 10, y: -50, width: 250, height: 40)
  113. label.textColor = UIColor.white
  114. label.text = "Posta le tue Foto"
  115. label.font = UIFont.boldSystemFont(ofSize: 16)
  116. self.view.addSubview(label)
  117.  
  118. // button
  119. let button = UIButton(type: .custom)
  120. button.frame = CGRect(x: view.bounds.width - 55, y: -60, width: 44, height: 44)
  121. button.setImage(UIImage(named: "add_blue"), for: UIControlState())
  122. //button.tintColor = UIColor.white
  123. button.addTarget(self, action: #selector(stampa), for: .touchUpInside)
  124. view.addSubview(button)
  125.  
  126.  
  127. }
  128. }
  129.  
  130.  
  131. override func scrollViewDidScroll(_ scrollView: UIScrollView) {
  132. var offset = (scrollView.contentOffset.y / 150) + 1.5
  133.  
  134. if offset > 1 {
  135. offset = 1
  136. let color = UIColor(red: 0, green: 0.5, blue: 1, alpha: offset - 0.5)
  137. self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
  138. self.navigationController?.navigationBar.backgroundColor = color
  139. UIApplication.shared.statusBarView?.backgroundColor = color
  140.  
  141. } else {
  142. let color = UIColor(red: 0, green: 0.5, blue: 1, alpha: offset - 0.5)
  143. self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
  144. self.navigationController?.navigationBar.backgroundColor = color
  145. UIApplication.shared.statusBarView?.backgroundColor = color
  146. }
  147.  
  148.  
  149. setNewView()
  150. }
  151.  
  152.  
  153. func updateView() {
  154. tableView.backgroundColor = UIColor.white
  155. headerView = tableView.tableHeaderView
  156. tableView.tableHeaderView = nil
  157. tableView.rowHeight = UITableViewAutomaticDimension
  158. tableView.addSubview(headerView)
  159.  
  160. newHeaderLayer = CAShapeLayer()
  161. newHeaderLayer.fillColor = UIColor.black.cgColor
  162. headerView.layer.mask = newHeaderLayer
  163. let newHeight = StretchyHeader1().headerHeight - StretchyHeader1().headerCut/2
  164.  
  165. tableView.contentInset = UIEdgeInsets(top: newHeight, left: 0, bottom: 0, right: 0)
  166. tableView.contentOffset = CGPoint(x: 0, y: -newHeight)
  167. setNewView()
  168. }
  169.  
  170. func setNewView(){
  171. let newHeight = StretchyHeader1().headerHeight - StretchyHeader1().headerCut/2
  172. var getHeaderFrame = CGRect(x: 0, y: -newHeight, width: tableView.bounds.width, height: StretchyHeader1().headerHeight)
  173. if tableView.contentOffset.y < newHeight {
  174. getHeaderFrame.origin.y = tableView.contentOffset.y
  175. getHeaderFrame.size.height = -tableView.contentOffset.y + StretchyHeader1().headerCut/2
  176. }
  177. headerView.frame = getHeaderFrame
  178. let cutDirection = UIBezierPath()
  179. cutDirection.move(to: CGPoint(x:0, y:0))
  180. cutDirection.addLine(to: CGPoint(x: getHeaderFrame.width, y:0))
  181. cutDirection.addLine(to: CGPoint(x: getHeaderFrame.width, y: getHeaderFrame.height ))
  182. cutDirection.addLine(to: CGPoint(x: 0, y: getHeaderFrame.height - StretchyHeader1().headerCut))
  183. newHeaderLayer.path = cutDirection.cgPath
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190. override func didReceiveMemoryWarning() {
  191. super.didReceiveMemoryWarning()
  192. // Dispose of any resources that can be recreated.
  193. }
  194.  
  195. // MARK: - Table view data source
  196. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  197. return UITableViewAutomaticDimension
  198. }
  199.  
  200. @IBAction func unwindToViewController(segue: UIStoryboardSegue) {
  201.  
  202.  
  203. }
  204.  
  205. func leggiDati(){
  206.  
  207. let usersRef = ref.child("users")
  208. let likeRef = ref.child("Likes")
  209. let utente = useridIniziale?.uid
  210.  
  211. handle = ref.child("Vetrina").observe(.value, with: { (snapshot) in
  212. // ref.child("Vetrina").observeSingleEvent(of: .value, with: { (snapshot) in
  213.  
  214. let valore = snapshot.value as? NSDictionary
  215. if valore != nil {
  216.  
  217. self.vetrina.removeAll()
  218.  
  219. for (key, value) in valore! {
  220. guard let dict_value = value as? [String : Any] else {
  221. print("i dati di \(key) non sono un dizionario")
  222. return
  223. }
  224. let idVetrinaArr = key as? String ?? ""
  225. let uidArr = dict_value["uid"] as? String ?? ""
  226. let likeArr = dict_value["Like"] as? Int ?? 0
  227. let commentiArr = dict_value["Commenti"] as? Int ?? 0
  228. let URLPhotoArr = dict_value["URLPhoto"] as? String ?? ""
  229. let descrizioneArr = dict_value["Descrizione"] as? String ?? ""
  230. let commentoUltimoArr = dict_value["commentoUltimo"] as? String ?? ""
  231. let uidUltimoArr = dict_value["uidUltimo"] as? String ?? ""
  232.  
  233. let nomeUltimoArr = dict_value["nomeUltimo"] as? String ?? ""
  234. let userRef = usersRef.child(uidArr)
  235. userRef.observeSingleEvent(of: .value, with: { (snap1) in
  236. let value = snap1.value as? NSDictionary
  237. let url1 = value!["imageDownloadURL"] as! String
  238. let nome1 = value!["Nome"] as! String
  239. let cognome1 = value!["Cognome"] as! String
  240. print(nome1 + " " + cognome1 + " " + uidArr + " " + url1)
  241. let userRef1 = usersRef.child(uidUltimoArr)
  242. userRef1.observeSingleEvent(of: .value, with: { (snap2) in
  243. let value = snap2.value as? NSDictionary
  244. let url2 = value!["imageDownloadURL"] as! String
  245. let userRef2 = likeRef.child(idVetrinaArr)
  246. userRef2.observeSingleEvent(of: .value, with: { (snap3) in
  247. if snap3.hasChild(utente!){
  248. self.isLikeArr = true
  249. } else {
  250. self.isLikeArr = false
  251. }
  252. self.vetrina.insert(VetrinaStruct(
  253. uid : uidArr,
  254. urluid : url1,
  255. nomeuid : cognome1 + " " + nome1,
  256. urlPhoto : URLPhotoArr,
  257. like : likeArr,
  258. commenti : commentiArr,
  259. descrizione : descrizioneArr,
  260. commentoUltimo : commentoUltimoArr,
  261. uidUltimo : uidUltimoArr,
  262. urlUltimo : url2,
  263. nomeUltimo : nomeUltimoArr,
  264. isLike : self.isLikeArr
  265. ), at: 0)
  266. self.vetrina.sort {
  267. $0.commenti > $1.commenti
  268. }
  269. self.tableView.reloadData()
  270. // self.reload(tableView: self.tableView)
  271. self.tableView.setNeedsLayout()
  272. self.tableView.layoutIfNeeded()
  273.  
  274. })
  275. })
  276. })
  277.  
  278.  
  279. }
  280.  
  281. } else {
  282.  
  283. }
  284.  
  285. }
  286. ) { (error) in
  287. print(error.localizedDescription)
  288. }
  289. }
  290.  
  291.  
  292.  
  293.  
  294. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  295. // #warning Incomplete implementation, return the number of rows
  296.  
  297.  
  298. return vetrina.count
  299.  
  300. }
  301.  
  302.  
  303.  
  304.  
  305. /*
  306. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  307. return 470.0
  308. }
  309. */
  310.  
  311. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  312.  
  313.  
  314. return UITableViewAutomaticDimension
  315. }
  316.  
  317. override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  318.  
  319. return UITableViewAutomaticDimension
  320. }
  321.  
  322.  
  323. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  324. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! VetrinaTableViewCell
  325.  
  326.  
  327. cell.uidImg.layer.cornerRadius = cell.uidImg.frame.size.width/2
  328. cell.uidImg.layer.borderWidth = 0
  329. cell.uidImg.clipsToBounds = true
  330.  
  331. let resource = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urluid)!, cacheKey: vetrina[indexPath.row].urluid)
  332. let p = Bundle.main.path(forResource: "loading", ofType: "gif")
  333. let data = try! Data(contentsOf: URL(fileURLWithPath: p!))
  334. cell.uidImg.kf.indicatorType = .image(imageData: data)
  335. cell.uidImg.kf.setImage(with: resource)
  336.  
  337. cell.nomeuid.text = vetrina[indexPath.row].nomeuid
  338.  
  339. let resource1 = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urlPhoto)!, cacheKey: vetrina[indexPath.row].urlPhoto)
  340. let p1 = Bundle.main.path(forResource: "loading", ofType: "gif")
  341. let data1 = try! Data(contentsOf: URL(fileURLWithPath: p1!))
  342. cell.immagineVetrina.kf.indicatorType = .image(imageData: data1)
  343. cell.immagineVetrina.kf.setImage(with: resource1)
  344.  
  345.  
  346. cell.numeroLike.text = String(vetrina[indexPath.row].like)
  347. cell.numeroCommenti.text = String(vetrina[indexPath.row].commenti)
  348.  
  349. cell.immagineUltimo.layer.cornerRadius = cell.immagineUltimo.frame.size.width/2
  350. cell.immagineUltimo.layer.borderWidth = 0
  351. cell.immagineUltimo.clipsToBounds = true
  352.  
  353. let resource2 = ImageResource(downloadURL: URL.init(string: vetrina[indexPath.row].urlUltimo)!, cacheKey: vetrina[indexPath.row].urlUltimo)
  354. let p2 = Bundle.main.path(forResource: "loading", ofType: "gif")
  355. let data2 = try! Data(contentsOf: URL(fileURLWithPath: p2!))
  356. cell.immagineUltimo.kf.indicatorType = .image(imageData: data2)
  357. cell.immagineUltimo.kf.setImage(with: resource2)
  358. cell.ultimoLbl.layer.masksToBounds = true
  359. cell.ultimoLbl.layer.cornerRadius = 10
  360. cell.ultimoLbl.text = ""
  361.  
  362. if vetrina[indexPath.row].isLike {
  363. cell.likeImg.image = UIImage(named: "LikeBlu")
  364. } else {
  365. cell.likeImg.image = UIImage(named: "LikeTras")
  366. }
  367. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapping))
  368. cell.likeImg.isUserInteractionEnabled = true
  369. cell.likeImg.addGestureRecognizer(tap)
  370.  
  371. cell.ultimoLbl.text = vetrina[indexPath.row].commentoUltimo
  372.  
  373.  
  374. return cell
  375. }
  376. @objc func tapping(){
  377. print("tapping")
  378. performSegue(withIdentifier: "segueLike", sender: self)
  379. }
  380. @objc func stampa() {
  381.  
  382. performSegue(withIdentifier: "segueNuovoBrevetto", sender: self)
  383. }
  384. // MARK: - Navigation
  385.  
  386.  
  387. func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  388.  
  389.  
  390. // Get Cell Label
  391.  
  392. performSegue(withIdentifier: "segueBrevetto", sender: self)
  393. }
  394.  
  395.  
  396. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  397.  
  398. if segue.identifier == "segueBrevetto"
  399. {
  400.  
  401. let destination = segue.destination as! UINavigationController
  402. let guest = destination.topViewController as! BrevettoDettaglioViewController
  403. let indexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
  404. let codice = self.vetrina[indexPath.row].uid
  405. guest.codicePass = codice!
  406. }
  407. }
  408.  
  409. func reload(tableView: UITableView) {
  410.  
  411. let contentOffset = tableView.contentOffset
  412. tableView.reloadData()
  413. tableView.layoutIfNeeded()
  414. tableView.layer.removeAllAnimations()
  415. tableView.setContentOffset(contentOffset, animated: false)
  416.  
  417. }
  418.  
  419.  
  420.  
  421. }
Add Comment
Please, Sign In to add comment