SettingsViewController.swift 3.79 KB
//
//  SettingsViewController.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 08.06.2022.
//

import UIKit

class SettingsViewController: UIViewController {
    var mainView = SettingsView()
    var networkManager = AuthNetworkManager()
    
    let settingsModel = SettingsModel.allCases
    
    override func loadView() {
        view = mainView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initViewController()
    }
    
    private func initViewController() {
        mainView.settingsTableView.delegate = self
        mainView.settingsTableView.dataSource = self
        
        mainView.settingsTableView.registerReusableCell(DisplayNameCell.self)
        mainView.settingsTableView.registerReusableCell(SettingsSwitchCell.self)
        mainView.settingsTableView.registerReusableCell(SettingsTableViewCell.self)
        
        mainView.backButton.addTarget(self, action: #selector(didShowHomeVC), for: .touchUpInside)
        mainView.logoutButton.addTarget(self, action: #selector(didLogoutAndShowVerificationVC), for: .touchUpInside)
    }
}

extension SettingsViewController {
    @objc private func didShowHomeVC() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc private func didLogoutAndShowVerificationVC() {
        let vc = VerificationViewController()
        navigationController?.pushViewController(vc, animated: true)
        networking()
    }
    
    func networking() {
        guard let logoutUrl = URL(string: Constants.APIRoutes.logoutURL.urlString) else { return }
        guard let logoutModel = logoutModelFilling() else { return }
        networkManager.logoutRequest(logoutUrl, model: logoutModel) { response in
            print("5️⃣❇️\(response.message ?? "Data not received!")❇️")
        } fail: { error in
            print("5️⃣❗️\(error)❗️")
        }
    }
    
    func logoutModelFilling() -> LogoutRequestModel? {
        guard let deviceUUID = UIDevice.current.identifierForVendor?.uuidString else {
            return nil
        }
        return LogoutRequestModel(deviceUUID: deviceUUID)
    }
}

//MARK: tableView delegate and datasource
extension SettingsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingsModel.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let row = indexPath.row
        let model: SettingsModel = settingsModel[row]
        
        switch model {
        case .displayName:
            let cell: DisplayNameCell = tableView.dequeueReusableCell(for: indexPath)
            cell.titleLabel.text = model.getTitle()
            cell.descriptionLabel.text = model.getDescription()
            cell.settingsIcon.image = model.getIcon()
            return cell
        case .doNotDisturb, .ringtone:
            let cell: SettingsSwitchCell = tableView.dequeueReusableCell(for: indexPath)
            cell.titleLabel.text = model.getTitle()
            cell.descriptionLabel.text = model.getDescription()
            cell.settingsIcon.image = model.getIcon()
            return cell
        case .editResidents, .widgetLock:
            let cell: SettingsTableViewCell = tableView.dequeueReusableCell(for: indexPath)
            cell.titleLabel.text = model.getTitle()
            cell.descriptionLabel.text = model.getDescription()
            cell.settingsIcon.image = model.getIcon()
            return cell
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 3 {
            let vc = ChooseApartmentsViewController()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}