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

import UIKit

class SettingsViewController: UIViewController {
    var mainView = SettingsView()
    var networkManager = AuthService()
    
    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)
    }
}
//MARK: - Delegate
extension SettingsViewController {
    @objc private func didShowHomeVC() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc private func didLogoutAndShowVerificationVC() {
        networking()
        let vc = VerificationViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func networking() {
        guard let logoutModel = logoutModelFilling() else { return }
        AuthService.share.logoutRequest(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)
        }
    }
}