Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.26 KB | None | 0 0
  1. //
  2. // ProfileViewController.swift
  3. // Evenz
  4. //
  5. // Created by Дмитрий Дурицкий on 7/6/19.
  6. // Copyright © 2019 Evenz. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ProfileViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
  12.  
  13. @IBOutlet weak var imageView: UIImageView!
  14. @IBOutlet weak var tableView: UITableView!
  15. @IBOutlet weak var fullNameLabel: UILabel!
  16.  
  17. @IBOutlet weak var suggestionsButton: UIButton!
  18. @IBOutlet weak var sugesstionsView: UIView!
  19.  
  20. @IBOutlet weak var likedButton: UIButton!
  21. @IBOutlet weak var likedView: UIView!
  22.  
  23. @IBOutlet weak var saveButton: UIButton!
  24. @IBOutlet weak var saveView: UIView!
  25.  
  26. @IBOutlet weak var ticketsButton: UIButton!
  27. @IBOutlet weak var ticketsView: UIView!
  28.  
  29. @IBOutlet weak var pastButton: UIButton!
  30. @IBOutlet weak var pastView: UIView!
  31.  
  32. @IBOutlet weak var commingSoonView: UIView!
  33. @IBOutlet weak var imageOfCommingSoon: UIImageView!
  34. @IBOutlet weak var infoLabel: UILabel!
  35.  
  36. var events = [Event]()
  37. var accountController = appDelegate().appScope.accountController
  38. var user = User()
  39. var dataSource = ProfileDataSource()
  40. let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
  41. var refreshToken = false
  42.  
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45.  
  46.  
  47. }
  48.  
  49. //MARK: Appearance methods
  50.  
  51. private func configureUserNavigationBar() {
  52. let rightButton = UIBarButtonItem(title: "ACCOUNT", style: .plain, target: self, action: #selector(showUserProfile))
  53. let font = UIFont(name: BoldTextFont, size: 11.0)
  54. rightButton.setTitleTextAttributes([NSAttributedStringKey.font: font!], for: .normal)
  55. navigationItem.rightBarButtonItem = rightButton
  56. }
  57.  
  58. @objc func showUserProfile() {
  59. let accountViewController = AccountViewController()
  60. accountViewController.accountController = appDelegate().appScope.accountController
  61. accountViewController.locationController = appDelegate().appScope.locationController
  62. accountViewController.screensController = appDelegate().appScope.screensController
  63. navigationController?.pushViewController(accountViewController, animated: true)
  64. }
  65.  
  66. override func viewWillAppear(_ animated: Bool) {
  67.  
  68.  
  69. imageView.layer.cornerRadius = imageView.frame.width/2
  70. imageView.layer.shadowColor = UIColor.black.cgColor
  71. imageView.layer.shadowOpacity = 1
  72. imageView.layer.shadowOffset = .zero
  73. imageView.layer.shadowRadius = 10
  74.  
  75. tableView.delegate = self
  76. tableView.dataSource = self
  77. tableView.separatorStyle = .none
  78. tableView.register(UINib(nibName: "SearchResultCell", bundle: nil), forCellReuseIdentifier: "SearchResultCell")
  79. configureUserNavigationBar()
  80.  
  81. suggestionsButton.titleLabel?.font = UIFont.scaleFontAccordingToDeviceSize(font: (suggestionsButton.titleLabel?.font)!)
  82. likedButton.titleLabel?.font = UIFont.scaleFontAccordingToDeviceSize(font: (likedButton.titleLabel?.font)!)
  83. saveButton.titleLabel?.font = UIFont.scaleFontAccordingToDeviceSize(font: (saveButton.titleLabel?.font)!)
  84. ticketsButton.titleLabel?.font = UIFont.scaleFontAccordingToDeviceSize(font: (ticketsButton.titleLabel?.font)!)
  85. pastButton.titleLabel?.font = UIFont.scaleFontAccordingToDeviceSize(font: (pastButton.titleLabel?.font)!)
  86. fullNameLabel.font = UIFont.scaleFontAccordingToDeviceSize(font: (fullNameLabel.font)!)
  87.  
  88.  
  89. activityIndicator.color = ColorManager.appRedColor()
  90. tableView.addSubview(activityIndicator)
  91. activityIndicator.center = tableView.center
  92.  
  93. setupTitleViewWithText(text: "My Events")
  94.  
  95. user = accountController.loadUser()!
  96. fullNameLabel.text = "\(user.firstName!) \(user.secondName!)"
  97. imageView.setImage(with: URL(string: user.imageString!))
  98.  
  99. if likedView.backgroundColor == .red {
  100. loadLikedEvents()
  101. } else if saveView.backgroundColor == .red {
  102. loadSavedEvents()
  103. }
  104. }
  105.  
  106. func changeButtonColor(button: UIButton, selectedView: UIView) {
  107. suggestionsButton.setTitleColor(.black, for: .normal)
  108. likedButton.setTitleColor(.black, for: .normal)
  109. saveButton.setTitleColor(.black, for: .normal)
  110. ticketsButton.setTitleColor(.black, for: .normal)
  111. pastButton.setTitleColor(.black, for: .normal)
  112.  
  113. sugesstionsView.backgroundColor = .white
  114. likedView.backgroundColor = .white
  115. saveView.backgroundColor = .white
  116. ticketsView.backgroundColor = .white
  117. pastView.backgroundColor = .white
  118.  
  119. selectedView.backgroundColor = .red
  120. button.setTitleColor(.red, for: .normal)
  121.  
  122. }
  123.  
  124. func clearTable() {
  125. events.removeAll()
  126. tableView.reloadData()
  127. }
  128.  
  129. //MARK: Actions
  130.  
  131. @IBAction func suggestionsAction(_ sender: Any) {
  132. clearTable()
  133. changeButtonColor(button: suggestionsButton, selectedView: sugesstionsView)
  134. commingSoonView.alpha = 1
  135. emptyState()
  136. }
  137.  
  138. @IBAction func likedAction(_ sender: Any) {
  139. commingSoonView.alpha = 0
  140. activityIndicator.startAnimating()
  141. clearTable()
  142. changeButtonColor(button: likedButton, selectedView: likedView)
  143. loadLikedEvents()
  144. }
  145.  
  146. func loadLikedEvents() {
  147. let request = LoadLikedRequest(pages: 0)
  148. request.resumeWithCompletionClosure { response in
  149. if response.errorInToken != nil {
  150. response.refreshAccessToken(dictionary: response.responseDictionary!, complition: {
  151. self.refreshToken = true
  152. self.update()
  153. })
  154. } else {
  155. self.refreshToken = false
  156. self.events = response.events
  157. self.emptyState()
  158. self.tableView.reloadData()
  159. self.activityIndicator.stopAnimating()
  160. }
  161. }
  162. }
  163.  
  164. func update() {
  165. if refreshToken {
  166. if likedView.backgroundColor == .red {
  167. loadLikedEvents()
  168. } else if saveView.backgroundColor == .red {
  169. loadSavedEvents()
  170. }
  171. }
  172. }
  173.  
  174. func loadSavedEvents() {
  175. let request = LoadSavedRequest(pages: 0)
  176. request.resumeWithCompletionClosure { response in
  177. if response.errorInToken != nil {
  178. response.refreshAccessToken(dictionary: response.responseDictionary!, complition: {
  179. self.refreshToken = true
  180. self.update()
  181. })
  182. } else {
  183. self.refreshToken = false
  184. self.events = response.events
  185. self.emptyState()
  186. self.tableView.reloadData()
  187. self.activityIndicator.stopAnimating()
  188. }
  189. }
  190. }
  191.  
  192. @IBAction func savedAction(_ sender: Any) {
  193. commingSoonView.alpha = 0
  194. activityIndicator.startAnimating()
  195. clearTable()
  196. changeButtonColor(button: saveButton, selectedView: saveView)
  197. loadSavedEvents()
  198. }
  199.  
  200. @IBAction func ticketsAction(_ sender: Any) {
  201. clearTable()
  202. changeButtonColor(button: ticketsButton, selectedView: ticketsView)
  203. commingSoonView.alpha = 1
  204. emptyState()
  205. }
  206.  
  207. @IBAction func pastsAction(_ sender: Any) {
  208. clearTable()
  209. changeButtonColor(button: pastButton, selectedView: pastView)
  210. commingSoonView.alpha = 1
  211. emptyState()
  212. }
  213.  
  214. func emptyState() {
  215. if events.isEmpty {
  216. UIView.animate(withDuration: 0.4) {
  217. self.commingSoonView.alpha = 1
  218. self.imageOfCommingSoon.image = UIImage(named: "Empty State")
  219. if self.likedView.backgroundColor == .red {
  220. self.infoLabel.text = "You have no liked events :( Like any event on event list you wish"
  221. } else if self.saveView.backgroundColor == .red {
  222. self.infoLabel.text = "You have no saved events :( Save any event on event list you wish"
  223. } else {
  224. self.imageOfCommingSoon.image = UIImage(named: "emptyState1")
  225. self.infoLabel.text = "Comming soon..."
  226. }
  227. }
  228. } else {
  229. commingSoonView.alpha = 0
  230. }
  231. }
  232.  
  233. //MARK: Delegates methods
  234.  
  235. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  236. return CGFloat.scaleAccordingToDeviceSize(maximumSize: 60)
  237. }
  238.  
  239. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  240. let event = events[indexPath.row]
  241. let mainStoryboard = UIStoryboard(name: "Details", bundle: nil)
  242. if let eventDetailsViewController = mainStoryboard.instantiateViewController(withIdentifier: "EventDetailsViewController") as? EventDetailsViewController {
  243. eventDetailsViewController.getEventDetails(id: event.id!)
  244. let transition = CATransition()
  245. transition.duration = 0.3
  246. transition.type = kCATransitionPush
  247. transition.subtype = kCATransitionFromRight
  248. transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
  249. view.window!.layer.add(transition, forKey: kCATransition)
  250. present(eventDetailsViewController, animated: false)
  251. }
  252. }
  253.  
  254. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  255.  
  256. return events.count
  257. }
  258.  
  259. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  260. let cell = tableView.dequeueReusableCell(withIdentifier: "SearchResultCell", for: indexPath) as! SearchResultCell
  261. let event = events[indexPath.row]
  262. cell.mainTextLabel.text = event.title
  263. cell.subLabel.text = event.startDateString
  264. return cell
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement