Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ProfileTableViewController.swift
- // Shop
- //
- // Created by Bogdan Filippov on 15/04/2019.
- // Copyright © 2019 Bogdan Filippov. All rights reserved.
- //
- import UIKit
- class ProfileTableViewController: UITableViewController {
- var updateCollection: UpdateCollection?
- var activeCells: [CellMode] = [] {
- didSet {
- tableView.reloadData()
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- setupActiveOrders()
- activeOrdersRequest()
- profileRequest()
- userCarsRequest()
- paymentMethodsRequest()
- updateCollection?.onUpdate()
- }
- func setupActiveOrders() {
- var activeCells: [CellMode] = []
- if UserStorage.shared.havePassword ?? false {
- activeCells += [.profile, .editProfile]
- } else {
- activeCells += [.registration]
- }
- activeCells += [/*.userBonusCard,*/ .activeOrders, .paymentMethods, .cars, .favorite, .address, /*.notifications,*/ .exit]
- if self.activeCells.count != activeCells.count {
- self.activeCells = activeCells
- }
- }
- // MARK: - Other
- func reloadTableViewCells() {
- tableView.beginUpdates()
- tableView.reloadRows(at: tableView.indexPathsForVisibleRows ?? [], with: .none)
- tableView.endUpdates()
- }
- // MARK: - API Requests
- func activeOrdersRequest() {
- OrderService.shared.orders(page: 0, itemsPerPage: 20) { (response, error, code) in
- guard let orders = response?.orders else {
- self.presentServiceErrorAlert(error: error)
- return
- }
- OrderCDManager.shared.save(orders: orders)
- self.setupActiveOrders()
- }
- }
- func profileRequest() {
- ProfileService.shared.profile { (response, error, code) in
- guard let response = response else {
- self.presentServiceErrorAlert(error: error)
- return
- }
- UserStorage.shared.save(user: response)
- self.reloadTableViewCells()
- }
- }
- func userCarsRequest() {
- UserCarsService.shared.cars { (response, error, code) in
- guard let response = response, code == 200 else {
- self.presentServiceErrorAlert(error: error)
- return
- }
- CarCDManager.shared.save(cars: response)
- }
- }
- func paymentMethodsRequest() {
- guard UserStorage.shared.isAuthTokenAvailable else {
- return
- }
- PaymentMethodService.shared.get { (response, error, code) in
- guard let response = response else {
- self.presentServiceErrorAlert(error: error)
- return
- }
- PaymentMethodCDManager.shared.save(paymentMethods: response)
- }
- }
- func logoutRequest() {
- AuthService.shared.logout { (response, error, code) in
- AppConfiguration.current.clearUserData()
- self.tabBarController?.selectedIndex = 0
- }
- }
- // MARK: - Alerts
- func presentLogoutAlert() {
- let alertController = UIAlertController(title: "Выход", message: "Вы уверены что хотите выйти из своего аккаунта?", preferredStyle: .alert)
- alertController.addAction(UIAlertAction(title: "Выйти", style: .destructive, handler: { (_) in
- self.logoutRequest()
- }))
- alertController.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
- present(alertController, animated: true, completion: nil)
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return activeCells.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let activeCell = activeCells[indexPath.row]
- let cell = tableView.dequeueReusableCell(withIdentifier: activeCell.rawValue, for: indexPath)
- switch activeCell {
- case .registration:
- let cell = cell as! ProfileRegistrationTableViewCell
- return cell
- case .profile:
- let cell = cell as! UserProfileTableViewCell
- return cell
- case .editProfile:
- let cell = cell
- return cell
- case .userBonusCard:
- let cell = cell
- return cell
- case .activeOrders:
- let cell = cell as! ProfileActiveOrdersTableViewCell
- return cell
- case .paymentMethods:
- let cell = cell as! ProfileUserPaymentMethodsTableViewCell
- return cell
- case .cars:
- let cell = cell as! ProfileCarsTableViewCell
- return cell
- case .favorite:
- let cell = cell
- cell.tintColor = .black
- cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
- return cell
- case .address:
- let cell = cell
- cell.tintColor = .black
- cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
- return cell
- case .notifications:
- let cell = cell
- cell.tintColor = .black
- cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
- return cell
- case .exit:
- let cell = cell
- cell.tintColor = .white
- cell.accessoryView = UIImageView(image: UIImage(named: "profile_detailCell")?.withRenderingMode(.alwaysTemplate))
- return cell
- }
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let cell = tableView.cellForRow(at: indexPath)
- cell?.setSelected(false, animated: true)
- if cell?.reuseIdentifier == "exit" {
- presentLogoutAlert()
- }
- if cell?.reuseIdentifier == "address" {
- let vc = ProfileControllers.shared.deliveryAddress
- navigationController?.pushViewController(vc, animated: true)
- }
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if let vc = segue.destination as? PickpointTableViewController {
- vc.isFavorites = true
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement