Advertisement
ChandanAppdesk

dsdsf

Feb 7th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. //
  2. // FeedViewController.swift
  3. // lookThePart
  4. //
  5. // Created by Latheeshwarraj Mohanraj on 4/30/20.
  6. // Copyright © 2020 Look The Part. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import MBProgressHUD
  12. class FeedViewController: UIViewController {
  13.  
  14. @IBOutlet var feedsCollectionView: UICollectionView!
  15. @IBOutlet var errorLabel: UILabel!
  16. @IBOutlet var errorPhoto: UIImageView!
  17. var posts:[PostShortDetail] = [] {
  18. didSet {
  19. self.feedsCollectionView.reloadData()
  20. }
  21. }
  22.  
  23. var totalPages: Int = .zero
  24. var currentPages: Int = .zero
  25. var isPaginating: Bool = false {
  26. didSet {
  27. feedsCollectionView.reloadData()
  28. }
  29. }
  30.  
  31. var resetToTop = false
  32.  
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. setCustomStyles()
  36. self.setupCollectionView()
  37. loadPosts(fetchPageNo: 1, pagination: true)
  38. setCustomStyles()
  39. hideNavbarBorder()
  40. updateDailyVisitApi()
  41.  
  42. let refreshControl = UIRefreshControl()
  43. refreshControl.addTarget(self, action: #selector(doSomething), for: .valueChanged)
  44.  
  45. // this is the replacement of implementing: "collectionView.addSubview(refreshControl)"
  46. feedsCollectionView.refreshControl = refreshControl
  47. }
  48.  
  49. @objc func doSomething(refreshControl: UIRefreshControl) {
  50. self.posts = []
  51. self.currentPages = 1
  52. self.isPaginating = false
  53. loadPosts(fetchPageNo: 1, pagination: true)
  54. // somewhere in your code you might need to call:
  55. refreshControl.endRefreshing() //At some point you could end refreshing.
  56. }
  57.  
  58.  
  59. func updateDailyVisitApi(){
  60. let endpoint = EndPoints.activeUser + "?id=\(Defaults().getLoginCredentials()?.username ?? Constants.emptyString)"
  61. LTPAPIs.shared.request(endpoint).response { response in
  62. if response.response!.statusCode == 200 {
  63. // User daily visit registered
  64. } else {
  65. // User daily visit registered failed
  66. }
  67. }
  68. }
  69. @objc func characterImageTapped(tapGestureRecognizer: CustomTapGesture){
  70. print("Character image tap")
  71. let vc = self.storyboard!.instantiateViewController(withIdentifier: "CharacterViewController") as! CharacterViewController
  72. vc.entertainmentName = tapGestureRecognizer.entertainmentName
  73. vc.entertainmentId = tapGestureRecognizer.entertainmentId
  74. vc.characterId = tapGestureRecognizer.characterId
  75. self.navigationController?.pushViewController(vc, animated: true)
  76. // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
  77. // self.navigationController?.pushViewController(vc, animated: true)
  78. }
  79.  
  80. @objc func characterNameTapped(tapGestureRecognizer: CustomTapGesture){
  81. print("Character name tap")
  82. let vc = self.storyboard!.instantiateViewController(withIdentifier: "CharacterViewController") as! CharacterViewController
  83. vc.entertainmentName = tapGestureRecognizer.entertainmentName
  84. vc.entertainmentId = tapGestureRecognizer.entertainmentId
  85. vc.characterId = tapGestureRecognizer.characterId
  86. self.navigationController?.pushViewController(vc, animated: true)
  87. // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
  88. // self.navigationController?.pushViewController(vc, animated: true)
  89. }
  90.  
  91. @objc func postTapped(tapGestureRecognizer: CustomTapGesture){
  92. print(" post tap")
  93. let vc = self.storyboard!.instantiateViewController(withIdentifier: "PostDetailViewController") as! PostDetailViewController
  94. let post = tapGestureRecognizer.post!
  95. vc.postId = String(post.postID)
  96. vc.characterTitle = post.characterName ?? ""
  97. vc.characterId = String(post.characterID ?? 0)
  98. vc.entertainmentId = tapGestureRecognizer.entertainmentId
  99. vc.characterThubmnailUrl = post.characterThumbnail ?? ""
  100. self.navigationController?.pushViewController(vc, animated: true)
  101. // let vc = self.storyboard!.instantiateViewController(withIdentifier: "TrendingProductsController") as! TrendingProductsController
  102. // self.navigationController?.pushViewController(vc, animated: true)
  103. }
  104.  
  105. var isLoading = false
  106. func loadPosts(fetchPageNo page: Int, pagination: Bool = false) {
  107. if isLoading { return }
  108. isLoading = true
  109. if pagination {
  110. self.isPaginating = true
  111. self.feedsCollectionView.reloadData()
  112. }
  113. let fullEndPoint = "\(EndPoints.getFeaturedPosts)?page=\(page)"
  114. LTPAPIs.shared.request(fullEndPoint, isLTPEndpoint: true, method: .get, encoding: URLEncoding.default).responseDecodable(of: CustomPosts.self) { response in
  115. switch response.result {
  116. case let .success(data):
  117. self.currentPages = data.currentPage ?? .zero
  118. self.totalPages = data.totalPages ?? .zero
  119. self.posts += data.data ?? []
  120. print("This is chandan gaurav")
  121. // dump(data)
  122. if self.posts.count > 0 {
  123. self.errorLabel.isHidden = true
  124. self.errorPhoto.isHidden = true
  125. } else {
  126. self.errorLabel.isHidden = false
  127. self.errorPhoto.isHidden = false
  128. }
  129. self.setupCollectionView()
  130. if pagination {
  131. self.isPaginating = false
  132. self.feedsCollectionView.reloadData()
  133. }
  134. self.isLoading = false
  135. case let .failure(error):
  136. self.isLoading = false
  137. Helper.showAlert(viewController: self, title: "Oops", message: "Unexpected Error")
  138. }
  139. }
  140. }
  141.  
  142. func setupCollectionView(){
  143. feedsCollectionView.delegate = self
  144. feedsCollectionView.dataSource = self
  145. feedsCollectionView.register(UINib(nibName: "FeedCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "FeedCollectionViewCell")
  146. feedsCollectionView.register(IndicatorFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: IndicatorFooter.id)
  147. }
  148.  
  149. func setCustomStyles(){
  150. let title = NSMutableAttributedString(string: "Look the Part")
  151. title.addAttribute(
  152. .font,
  153. value: UIFont(name: "Didot-Italic", size: 26)!,
  154. range: NSRange(location: 0, length: 4))
  155. title.addAttribute(
  156. .font,
  157. value: UIFont(name: "Didot-Italic", size: 20)!,
  158. range: NSRange(location: 5, length: 3))
  159. title.addAttribute(
  160. .font,
  161. value: UIFont(name: "Didot-Italic", size: 26)!,
  162. range: NSRange(location: 9, length: 4))
  163. //navigationItem.title = title
  164. let titleLbl = UILabel()
  165. //
  166. // let attributes: [NSAttributedString.Key: Any] = [NSAttributedStringKey.font: UIFont(name: "Noteworthy-Bold", size: 30)!, NSAttributedStringKey.foregroundColor: titleLblColor]
  167.  
  168. titleLbl.attributedText = title
  169. titleLbl.sizeToFit()
  170. self.navigationItem.titleView = titleLbl
  171. navigationController?.navigationBar.tintColor = UIColor.black
  172. // navigationController?.navigationBar.titleTextAttributes =
  173. // [NSAttributedString.Key.foregroundColor: UIColor.black,
  174. // NSAttributedString.Key.font: UIFont(name: "Didot-Italic", size: 24)!]
  175. navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
  176. }
  177. }
  178.  
  179. extension FeedViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  180. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  181. print("I HAVE \(posts.count) Posts")
  182. return posts.count
  183. }
  184.  
  185. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  186. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedCollectionViewCell", for: indexPath) as! FeedCollectionViewCell
  187. let width = (collectionView.bounds.width)
  188. print("CHARACTER THUMBNAIL = \(String(describing: posts[indexPath.row].characterThumbnail?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))) COUNT: \(indexPath.row)")
  189. cell.viewHeight.constant = (width*1.25)+60
  190. cell.viewWidth.constant = (collectionView.bounds.width)
  191. cell.characterName.text = posts[indexPath.row].characterName
  192. cell.tvShow.text = posts[indexPath.row].entertainmentName ?? ""
  193. cell.characterThumbnail.sd_setImage(with: URL(string: posts[indexPath.row].characterThumbnail?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""), placeholderImage: UIImage(named: "missing_circle"))
  194. //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"))
  195. cell.progressBar.startAnimating()
  196. cell.progressBar.isHidden = true
  197. 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
  198.  
  199. cell.progressBar.stopAnimating()
  200. cell.progressBar.isHidden = true
  201. })
  202. //progressBar
  203. cell.characterThumbnail.layer.masksToBounds = true
  204. cell.characterThumbnail.layer.cornerRadius = cell.characterThumbnail.bounds.width / 2
  205.  
  206. let profileNameTap = CustomTapGesture(target: self, action:
  207. #selector(characterImageTapped(tapGestureRecognizer:)))
  208. profileNameTap.characterId = String(posts[indexPath.row].characterID ?? 0)
  209. profileNameTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
  210. cell.profileButton.isUserInteractionEnabled = true
  211. cell.profileButton.addGestureRecognizer(profileNameTap)
  212.  
  213. let characterImageTap = CustomTapGesture(target: self, action:
  214. #selector(characterImageTapped(tapGestureRecognizer:)))
  215. characterImageTap.characterId = String(posts[indexPath.row].characterID ?? 0)
  216. characterImageTap.entertainmentName = posts[indexPath.row].entertainmentName ?? ""
  217. characterImageTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
  218. cell.characterThumbnail.isUserInteractionEnabled = true
  219. cell.characterThumbnail.addGestureRecognizer(characterImageTap)
  220.  
  221. let characterNameTap = CustomTapGesture(target: self, action: #selector(characterNameTapped(tapGestureRecognizer:)))
  222. characterImageTap.entertainmentName = posts[indexPath.row].entertainmentName ?? ""
  223. characterNameTap.characterId = String(posts[indexPath.row].characterID ?? 0)
  224. characterNameTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
  225. cell.characterName.isUserInteractionEnabled = true
  226. cell.characterName.addGestureRecognizer(characterNameTap)
  227.  
  228. let postTap = CustomTapGesture(target: self, action: #selector(postTapped(tapGestureRecognizer:)))
  229. postTap.post = posts[indexPath.row]
  230. postTap.entertainmentId = String(describing: posts[indexPath.row].entertainmentID)
  231. cell.postImage.isUserInteractionEnabled = true
  232. cell.postImage.addGestureRecognizer(postTap)
  233.  
  234.  
  235. return cell
  236. }
  237.  
  238. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  239. let width = (collectionView.bounds.width)
  240. return CGSize(width: width, height: (width*1.25)+60)
  241. }
  242.  
  243. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  244. // let vc = self.storyboard!.instantiateViewController(withIdentifier: "PostDetailViewController") as! PostDetailViewController
  245. // let post = posts[indexPath.row]
  246. // vc.postId = String(post.postID)
  247. // vc.characterTitle = post.characterName ?? ""
  248. // vc.characterId = String(post.characterID ?? 0)
  249. // vc.characterThubmnailUrl = post.characterThumbnail ?? ""
  250. // self.navigationController?.pushViewController(vc, animated: true)
  251. }
  252. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  253. if kind == UICollectionView.elementKindSectionFooter {
  254. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: IndicatorFooter.id, for: indexPath) as! IndicatorFooter
  255. return footer
  256. }
  257. return UICollectionReusableView()
  258. }
  259. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  260. return isPaginating ? .init(width: collectionView.bounds.width, height: 90) : .zero
  261. }
  262. }
  263.  
  264. extension FeedViewController: UIScrollViewDelegate {
  265. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  266. let position = scrollView.contentOffset.y
  267. if position > feedsCollectionView.contentSize.height - 100 - scrollView.frame.size.height {
  268. if self.isPaginating { return }
  269. if self.totalPages == self.currentPages { return }
  270. currentPages = currentPages + 1
  271. self.loadPosts(fetchPageNo: currentPages, pagination: true)
  272. }
  273. }
  274. }
  275.  
  276. class CustomTapGesture: UITapGestureRecognizer {
  277. var characterId = String()
  278. var entertainmentName = String()
  279. var entertainmentId = String()
  280. var postId = String()
  281. var post: PostShortDetail? = nil
  282. }
  283.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement