Advertisement
Guest User

TVC

a guest
Nov 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.08 KB | None | 0 0
  1. //
  2. //  ProfileTableViewController.swift
  3. //  Shop
  4. //
  5. //  Created by Bogdan Filippov on 15/04/2019.
  6. //  Copyright © 2019 Bogdan Filippov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ProfileTableViewController: UITableViewController {
  12.    
  13.     var updateCollection: UpdateCollection?
  14.    
  15.     var activeCells: [CellMode] = [] {
  16.         didSet {
  17.             tableView.reloadData()
  18.         }
  19.     }
  20.  
  21.     override func viewDidLoad() {
  22.         super.viewDidLoad()
  23.        
  24.     }
  25.    
  26.     override func viewWillAppear(_ animated: Bool) {
  27.         super.viewWillAppear(animated)
  28.         setupActiveOrders()
  29.         activeOrdersRequest()
  30.         profileRequest()
  31.         userCarsRequest()
  32.         paymentMethodsRequest()
  33.         updateCollection?.onUpdate()
  34.     }
  35.    
  36.     func setupActiveOrders() {
  37.         var activeCells: [CellMode] = []
  38.        
  39.         if UserStorage.shared.havePassword ?? false {
  40.             activeCells += [.profile, .editProfile]
  41.         } else {
  42.             activeCells += [.registration]
  43.         }
  44.         activeCells += [/*.userBonusCard,*/ .activeOrders, .paymentMethods, .cars, .favorite, .address, /*.notifications,*/ .exit]
  45.        
  46.         if self.activeCells.count != activeCells.count {
  47.             self.activeCells = activeCells
  48.         }
  49.     }
  50.    
  51.     // MARK: - Other
  52.    
  53.     func reloadTableViewCells() {
  54.         tableView.beginUpdates()
  55.         tableView.reloadRows(at: tableView.indexPathsForVisibleRows ?? [], with: .none)
  56.         tableView.endUpdates()
  57.     }
  58.    
  59.     // MARK: - API Requests
  60.    
  61.     func activeOrdersRequest() {
  62.         OrderService.shared.orders(page: 0, itemsPerPage: 20) { (response, error, code) in
  63.             guard let orders = response?.orders else {
  64.                 self.presentServiceErrorAlert(error: error)
  65.                 return
  66.             }
  67.            
  68.             OrderCDManager.shared.save(orders: orders)
  69.             self.setupActiveOrders()
  70.         }
  71.     }
  72.    
  73.     func profileRequest() {
  74.         ProfileService.shared.profile { (response, error, code) in
  75.             guard let response = response else {
  76.                 self.presentServiceErrorAlert(error: error)
  77.                 return
  78.             }
  79.            
  80.             UserStorage.shared.save(user: response)
  81.             self.reloadTableViewCells()
  82.         }
  83.     }
  84.    
  85.     func userCarsRequest() {
  86.         UserCarsService.shared.cars { (response, error, code) in
  87.             guard let response = response, code == 200 else {
  88.                 self.presentServiceErrorAlert(error: error)
  89.                 return
  90.             }
  91.            
  92.             CarCDManager.shared.save(cars: response)
  93.         }
  94.     }
  95.    
  96.     func paymentMethodsRequest() {
  97.         guard UserStorage.shared.isAuthTokenAvailable else {
  98.             return
  99.         }
  100.        
  101.         PaymentMethodService.shared.get { (response, error, code) in
  102.             guard let response = response else {
  103.                 self.presentServiceErrorAlert(error: error)
  104.                 return
  105.             }
  106.            
  107.             PaymentMethodCDManager.shared.save(paymentMethods: response)
  108.         }
  109.     }
  110.    
  111.     func logoutRequest() {
  112.         AuthService.shared.logout { (response, error, code) in
  113.             AppConfiguration.current.clearUserData()
  114.             self.tabBarController?.selectedIndex = 0
  115.         }
  116.     }
  117.    
  118.     // MARK: - Alerts
  119.    
  120.     func presentLogoutAlert() {
  121.         let alertController = UIAlertController(title: "Выход", message: "Вы уверены что хотите выйти из своего аккаунта?", preferredStyle: .alert)
  122.        
  123.         alertController.addAction(UIAlertAction(title: "Выйти", style: .destructive, handler: { (_) in
  124.             self.logoutRequest()
  125.         }))
  126.         alertController.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
  127.        
  128.         present(alertController, animated: true, completion: nil)
  129.     }
  130.  
  131.     // MARK: - Table view data source
  132.    
  133.     override func numberOfSections(in tableView: UITableView) -> Int {
  134.         // #warning Incomplete implementation, return the number of sections
  135.         return 1
  136.     }
  137.    
  138.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  139.         // #warning Incomplete implementation, return the number of rows
  140.         return activeCells.count
  141.     }
  142.    
  143.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  144.         let activeCell = activeCells[indexPath.row]
  145.         let cell = tableView.dequeueReusableCell(withIdentifier: activeCell.rawValue, for: indexPath)
  146.  
  147.         switch activeCell {
  148.         case .registration:
  149.             let cell = cell as! ProfileRegistrationTableViewCell
  150.             return cell
  151.         case .profile:
  152.             let cell = cell as! UserProfileTableViewCell
  153.             return cell
  154.         case .editProfile:
  155.             let cell = cell
  156.             return cell
  157.         case .userBonusCard:
  158.             let cell = cell
  159.             return cell
  160.         case .activeOrders:
  161.             let cell = cell as! ProfileActiveOrdersTableViewCell
  162.             return cell
  163.         case .paymentMethods:
  164.             let cell = cell as! ProfileUserPaymentMethodsTableViewCell
  165.             return cell
  166.         case .cars:
  167.             let cell = cell as! ProfileCarsTableViewCell
  168.             return cell
  169.         case .favorite:
  170.             let cell = cell
  171.             cell.tintColor = .black
  172.             cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
  173.             return cell
  174.         case .address:
  175.             let cell = cell
  176.             cell.tintColor = .black
  177.             cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
  178.             return cell
  179.         case .notifications:
  180.             let cell = cell
  181.             cell.tintColor = .black
  182.             cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
  183.             return cell
  184.         case .exit:
  185.             let cell = cell
  186.             cell.tintColor = .white
  187.             cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
  188.             return cell
  189.         }
  190.     }
  191.    
  192.     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  193.         let cell = tableView.cellForRow(at: indexPath)
  194.         cell?.setSelected(false, animated: true)
  195.        
  196.         if cell?.reuseIdentifier == "exit" {
  197.             presentLogoutAlert()
  198.         }
  199.        
  200.         if cell?.reuseIdentifier == "address" {
  201.             let vc = ProfileControllers.shared.deliveryAddress
  202.             navigationController?.pushViewController(vc, animated: true)
  203.         }
  204.     }
  205.    
  206.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  207.         if let vc = segue.destination as? PickpointTableViewController {
  208.             vc.isFavorites = true
  209.         }
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement