import UIKit import Parse private let reuseIdentifier = "Cell" class homeVC: UICollectionViewController { var refresher : UIRefreshControl! // size of page var page : Int = 12 // arrays to hold server information var uuidArray = [String]() var picArray = [PFFile]() // default func override func viewDidLoad() { super.viewDidLoad() // always vertical scroll self.collectionView?.alwaysBounceVertical = true // background color collectionView?.backgroundColor = .white // title at the top self.navigationItem.title = PFUser.current()?.username?.uppercased() // pull to refresh refresher = UIRefreshControl() refresher.addTarget(self, action: #selector(homeVC.refresh), for: UIControl.Event.valueChanged) collectionView?.addSubview(refresher) // receive notification from editVC NotificationCenter.default.addObserver(self, selector: #selector(homeVC.reload(_:)), name: NSNotification.Name(rawValue: "reload"), object: nil) // load posts func loadPosts() } // refreshing func @objc func refresh() { // reload posts loadPosts() // stop refresher animating refresher.endRefreshing() } // reloading func after received notification @objc func reload(_ notification:Notification) { collectionView?.reloadData() } // load posts func func loadPosts() { // request infomration from server let query = PFQuery(className: "posts") query.whereKey("username", equalTo: PFUser.current()!.username!) query.limit = page query.findObjectsInBackground (block: { (objects, error) -> Void in if error == nil { // clean up self.uuidArray.removeAll(keepingCapacity: false) self.picArray.removeAll(keepingCapacity: false) // find objects related to our request for object in objects! { // add found data to arrays (holders) self.uuidArray.append(object.value(forKey: "uuid") as! String) self.picArray.append(object.value(forKey: "pic") as! PFFile) } self.collectionView?.reloadData() } else { print(error!.localizedDescription) } }) } // load more while scrolling down override func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y >= scrollView.contentSize.height - self.view.frame.size.height { loadMore() } } // paging func loadMore() { // if there is more objects if page <= picArray.count { // increase page size page = page + 12 // load more posts let query = PFQuery(className: "posts") query.whereKey("username", equalTo: PFUser.current()!.username!) query.limit = page query.findObjectsInBackground(block: { (objects, error) -> Void in if error == nil { // clean up self.uuidArray.removeAll(keepingCapacity: false) self.picArray.removeAll(keepingCapacity: false) // find related objects for object in objects! { self.uuidArray.append(object.value(forKey: "uuid") as! String) self.picArray.append(object.value(forKey: "pic") as! PFFile) } self.collectionView?.reloadData() } else { print(error?.localizedDescription ?? String()) } }) } } // cell numb override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return picArray.count } // cell size func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { let size = CGSize(width: self.view.frame.size.width / 3, height: self.view.frame.size.width / 3) return size } // cell config override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // define cell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! pictureCell // get picture from the picArray picArray[indexPath.row].getDataInBackground { (data, error) -> Void in if error == nil { cell.picImg.image = UIImage(data: data!) } } return cell } // header config override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { // define header let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! headerView // STEP 1. Get user data // get users data with connections to collumns of PFuser class header.fullnameLbl.text = (PFUser.current()?.object(forKey: "fullname") as? String)?.uppercased() header.webTxt.text = PFUser.current()?.object(forKey: "web") as? String header.webTxt.sizeToFit() header.bioLbl.text = PFUser.current()?.object(forKey: "bio") as? String header.bioLbl.sizeToFit() let avaQuery = PFUser.current()?.object(forKey: "ava") as! PFFile avaQuery.getDataInBackground { (data, error) -> Void in header.avaImg.image = UIImage(data: data!) } header.button.setTitle("edit profile", for: UIControl.State()) // STEP 2. Count statistics // count total posts let posts = PFQuery(className: "posts") posts.whereKey("username", equalTo: PFUser.current()!.username!) posts.countObjectsInBackground (block: { (count, error) -> Void in if error == nil { header.posts.text = "\(count)" } }) // count total followers let followers = PFQuery(className: "follow") followers.whereKey("following", equalTo: PFUser.current()!.username!) followers.countObjectsInBackground (block: { (count, error) -> Void in if error == nil { header.followers.text = "\(count)" } }) // count total followings let followings = PFQuery(className: "follow") followings.whereKey("follower", equalTo: PFUser.current()!.username!) followings.countObjectsInBackground (block: { (count, error) -> Void in if error == nil { header.followings.text = "\(count)" } }) return header }