Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FeedViewController.swift
- // lookThePart
- //
- // Created by Latheeshwarraj Mohanraj on 4/30/20.
- // Copyright © 2020 Look The Part. All rights reserved.
- //
- import UIKit
- import Alamofire
- import MBProgressHUD
- class FeedViewController: UIViewController {
- @IBOutlet var feedsCollectionView: UICollectionView!
- @IBOutlet var errorLabel: UILabel!
- @IBOutlet var errorPhoto: UIImageView!
- var posts:[PostShortDetail] = [] {
- didSet {
- self.feedsCollectionView.reloadData()
- }
- }
- var totalPages: Int = .zero
- var currentPages: Int = .zero
- var isPaginating: Bool = false {
- didSet {
- feedsCollectionView.reloadData()
- }
- }
- var resetToTop = false
- override func viewDidLoad() {
- super.viewDidLoad()
- setCustomStyles()
- self.setupCollectionView()
- loadPosts(fetchPageNo: 1, pagination: true)
- setCustomStyles()
- hideNavbarBorder()
- updateDailyVisitApi()
- let refreshControl = UIRefreshControl()
- refreshControl.addTarget(self, action: #selector(doSomething), for: .valueChanged)
- // this is the replacement of implementing: "collectionView.addSubview(refreshControl)"
- feedsCollectionView.refreshControl = refreshControl
- }
- @objc func doSomething(refreshControl: UIRefreshControl) {
- self.posts = []
- self.currentPages = 1
- self.isPaginating = false
- loadPosts(fetchPageNo: 1, pagination: true)
- // somewhere in your code you might need to call:
- refreshControl.endRefreshing() //At some point you could end refreshing.
- }
- func updateDailyVisitApi(){
- let endpoint = EndPoints.activeUser + "?id=\(Defaults().getLoginCredentials()?.username ?? Constants.emptyString)"
- LTPAPIs.shared.request(endpoint).response { response in
- if response.response!.statusCode == 200 {
- // User daily visit registered
- } else {
- // User daily visit registered failed
- }
- }
- }
- @objc func characterImageTapped(tapGestureRecognizer: CustomTapGesture){
- print("Character image tap")
- let vc = self.storyboard!.instantiateViewController(withIdentifier: "CharacterViewController") as! CharacterViewController
- vc.entertainmentName = tapGestureRecognizer.entertainmentName
- vc.entertainmentId = tapGestureRecognizer.entertainmentId
- vc.characterId = tapGestureRecognizer.characterId
- self.navigationController?.pushViewController(vc, animated: true)
- // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
- // self.navigationController?.pushViewController(vc, animated: true)
- }
- @objc func characterNameTapped(tapGestureRecognizer: CustomTapGesture){
- print("Character name tap")
- let vc = self.storyboard!.instantiateViewController(withIdentifier: "CharacterViewController") as! CharacterViewController
- vc.entertainmentName = tapGestureRecognizer.entertainmentName
- vc.entertainmentId = tapGestureRecognizer.entertainmentId
- vc.characterId = tapGestureRecognizer.characterId
- self.navigationController?.pushViewController(vc, animated: true)
- // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
- // self.navigationController?.pushViewController(vc, animated: true)
- }
- @objc func postTapped(tapGestureRecognizer: CustomTapGesture){
- print(" post tap")
- let vc = self.storyboard!.instantiateViewController(withIdentifier: "PostDetailViewController") as! PostDetailViewController
- let post = tapGestureRecognizer.post!
- vc.postId = String(post.postID)
- vc.characterTitle = post.characterName ?? ""
- vc.characterId = String(post.characterID ?? 0)
- vc.entertainmentId = tapGestureRecognizer.entertainmentId
- vc.characterThubmnailUrl = post.characterThumbnail ?? ""
- self.navigationController?.pushViewController(vc, animated: true)
- // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
- // self.navigationController?.pushViewController(vc, animated: true)
- }
- var isLoading = false
- func loadPosts(fetchPageNo page: Int, pagination: Bool = false) {
- if isLoading { return }
- isLoading = true
- if pagination {
- self.isPaginating = true
- self.feedsCollectionView.reloadData()
- }
- let fullEndPoint = "\(EndPoints.getFeaturedPosts)?page=\(page)"
- LTPAPIs.shared.request(fullEndPoint, isLTPEndpoint: true, method: .get, encoding: URLEncoding.default).responseDecodable(of: CustomPosts.self) { response in
- switch response.result {
- case let .success(data):
- self.currentPages = data.currentPage ?? .zero
- self.totalPages = data.totalPages ?? .zero
- self.posts += data.data ?? []
- print("This is chandan gaurav")
- // dump(data)
- if self.posts.count > 0 {
- self.errorLabel.isHidden = true
- self.errorPhoto.isHidden = true
- } else {
- self.errorLabel.isHidden = false
- self.errorPhoto.isHidden = false
- }
- self.setupCollectionView()
- if pagination {
- self.isPaginating = false
- self.feedsCollectionView.reloadData()
- }
- self.isLoading = false
- case let .failure(error):
- self.isLoading = false
- Helper.showAlert(viewController: self, title: "Oops", message: "Unexpected Error")
- }
- }
- }
- func setupCollectionView(){
- feedsCollectionView.delegate = self
- feedsCollectionView.dataSource = self
- feedsCollectionView.register(UINib(nibName: "FeedCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "FeedCollectionViewCell")
- feedsCollectionView.register(IndicatorFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: IndicatorFooter.id)
- }
- func setCustomStyles(){
- let title = NSMutableAttributedString(string: "Look the Part")
- title.addAttribute(
- .font,
- value: UIFont(name: "Didot-Italic", size: 26)!,
- range: NSRange(location: 0, length: 4))
- title.addAttribute(
- .font,
- value: UIFont(name: "Didot-Italic", size: 20)!,
- range: NSRange(location: 5, length: 3))
- title.addAttribute(
- .font,
- value: UIFont(name: "Didot-Italic", size: 26)!,
- range: NSRange(location: 9, length: 4))
- //navigationItem.title = title
- let titleLbl = UILabel()
- //
- // let attributes: [NSAttributedString.Key: Any] = [NSAttributedStringKey.font: UIFont(name: "Noteworthy-Bold", size: 30)!, NSAttributedStringKey.foregroundColor: titleLblColor]
- titleLbl.attributedText = title
- titleLbl.sizeToFit()
- self.navigationItem.titleView = titleLbl
- navigationController?.navigationBar.tintColor = UIColor.black
- // navigationController?.navigationBar.titleTextAttributes =
- // [NSAttributedString.Key.foregroundColor: UIColor.black,
- // NSAttributedString.Key.font: UIFont(name: "Didot-Italic", size: 24)!]
- navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
- }
- }
- extension FeedViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- print("I HAVE \(posts.count) Posts")
- return posts.count
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedCollectionViewCell", for: indexPath) as! FeedCollectionViewCell
- let width = (collectionView.bounds.width)
- print("CHARACTER THUMBNAIL = \(String(describing: posts[indexPath.row].characterThumbnail?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))) COUNT: \(indexPath.row)")
- cell.viewHeight.constant = (width*1.25)+60
- cell.viewWidth.constant = (collectionView.bounds.width)
- cell.characterName.text = posts[indexPath.row].characterName
- cell.tvShow.text = posts[indexPath.row].entertainmentName ?? ""
- cell.characterThumbnail.sd_setImage(with: URL(string: posts[indexPath.row].characterThumbnail?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""), placeholderImage: UIImage(named: "missing_circle"))
- //cell.postImage.sd_setImage(with: URL(string: posts[indexPath.row].postThumbnailCropped?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? posts[indexPath.row].postThumbnail.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "missing_vertical"))
- cell.progressBar.startAnimating()
- cell.progressBar.isHidden = true
- cell.postImage.sd_setImage(with: URL(string: posts[indexPath.row].postThumbnailCropped?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? posts[indexPath.row].postThumbnail.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "missing_vertical"), completed: { (image, error, cacheType, imageURL) in
- cell.progressBar.stopAnimating()
- cell.progressBar.isHidden = true
- })
- //progressBar
- cell.characterThumbnail.layer.masksToBounds = true
- cell.characterThumbnail.layer.cornerRadius = cell.characterThumbnail.bounds.width / 2
- let profileNameTap = CustomTapGesture(target: self, action:
- #selector(characterImageTapped(tapGestureRecognizer:)))
- profileNameTap.characterId = String(posts[indexPath.row].characterID ?? 0)
- profileNameTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
- cell.profileButton.isUserInteractionEnabled = true
- cell.profileButton.addGestureRecognizer(profileNameTap)
- let characterImageTap = CustomTapGesture(target: self, action:
- #selector(characterImageTapped(tapGestureRecognizer:)))
- characterImageTap.characterId = String(posts[indexPath.row].characterID ?? 0)
- characterImageTap.entertainmentName = posts[indexPath.row].entertainmentName ?? ""
- characterImageTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
- cell.characterThumbnail.isUserInteractionEnabled = true
- cell.characterThumbnail.addGestureRecognizer(characterImageTap)
- let characterNameTap = CustomTapGesture(target: self, action: #selector(characterNameTapped(tapGestureRecognizer:)))
- characterImageTap.entertainmentName = posts[indexPath.row].entertainmentName ?? ""
- characterNameTap.characterId = String(posts[indexPath.row].characterID ?? 0)
- characterNameTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
- cell.characterName.isUserInteractionEnabled = true
- cell.characterName.addGestureRecognizer(characterNameTap)
- let postTap = CustomTapGesture(target: self, action: #selector(postTapped(tapGestureRecognizer:)))
- postTap.post = posts[indexPath.row]
- postTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
- cell.postImage.isUserInteractionEnabled = true
- cell.postImage.addGestureRecognizer(postTap)
- return cell
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- let width = (collectionView.bounds.width)
- return CGSize(width: width, height: (width*1.25)+60)
- }
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- // let vc = self.storyboard!.instantiateViewController(withIdentifier: "PostDetailViewController") as! PostDetailViewController
- // let post = posts[indexPath.row]
- // vc.postId = String(post.postID)
- // vc.characterTitle = post.characterName ?? ""
- // vc.characterId = String(post.characterID ?? 0)
- // vc.characterThubmnailUrl = post.characterThumbnail ?? ""
- // self.navigationController?.pushViewController(vc, animated: true)
- }
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
- if kind == UICollectionView.elementKindSectionFooter {
- let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: IndicatorFooter.id, for: indexPath) as! IndicatorFooter
- return footer
- }
- return UICollectionReusableView()
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
- return isPaginating ? .init(width: collectionView.bounds.width, height: 90) : .zero
- }
- }
- extension FeedViewController: UIScrollViewDelegate {
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- let position = scrollView.contentOffset.y
- if position > feedsCollectionView.contentSize.height - 100 - scrollView.frame.size.height {
- if self.isPaginating { return }
- if self.totalPages == self.currentPages { return }
- currentPages = currentPages + 1
- self.loadPosts(fetchPageNo: currentPages, pagination: true)
- }
- }
- }
- class CustomTapGesture: UITapGestureRecognizer {
- var characterId = String()
- var entertainmentName = String()
- var entertainmentId = String()
- var postId = String()
- var post: PostShortDetail? = nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement