ResidentsViewController.swift 3.59 KB
//
//  ResidentsViewController.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 14.06.2022.
//

import UIKit

class ResidentsViewController: UIViewController {
    var mainView = ResidentsView()
    var myData: [ResidentModel] = []
    
    let addResidentsVC = AddResidentsViewController()
    let networkManager = NetworkManager()
    
    override func loadView() {
        view = mainView
    }
    
    override func viewDidLoad() {
        initViewController()
        networking()
    }
    func modelFilling() -> RequestModel? {
        guard let deviceUUID = UIDevice.current.identifierForVendor?.uuidString else {
            return nil
        }
        guard let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
            return nil
        }
        return RequestModel(deviceUUID: deviceUUID,
                                 manufacturer: "Apple",
                                 model: UIDevice.current.model,
                                 platform: UIDevice.current.systemName,
                                 osVersion: UIDevice.current.systemVersion,
                                 appVersion: appVersion)
        
        
    }
    func networking() {
        guard let url = URL(string: Constant.URL.rawValue) else { return }
        guard let model = modelFilling() else { return }
        networkManager.authorizationRequest(url, model: model) { responseModel in
            <#code#>
        } fail: { <#Error#> in
            <#code#>
        }

        
        print("")
        
    }
    func initViewController() {
        mainView.tableView.delegate = self
        mainView.tableView.dataSource = self
        mainView.tableView.register(ResidentTableViewCell.self, forCellReuseIdentifier: ResidentTableViewCell.id)
        
        mainView.backButton.addTarget(self, action: #selector(didShowChooseApartVC), for: .touchUpInside)
        mainView.addResidentsButton.addTarget(self, action: #selector(willShowAddResidentVC), for: .touchUpInside)
    }
    
    @objc func didShowChooseApartVC() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc func willShowAddResidentVC() {
        addResidentsVC.modalPresentationStyle = .overCurrentContext
        addResidentsVC.modalTransitionStyle = .crossDissolve
        addResidentsVC.delegate = self
        present(addResidentsVC, animated: true)
    }
}

//MARK: - TableView delegate & data source
extension ResidentsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        addResidentsVC.savedResidents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ResidentTableViewCell.id, for: indexPath) as? ResidentTableViewCell else { return UITableViewCell() }
        //        cell.configureWithItem(doors[indexPath.row])
        cell.serialNumberLabel.text = "\(indexPath.row + 1)"
        cell.nameLabel.text = myData[indexPath.row].name
        cell.phoneNumberLabel.text = myData[indexPath.row].mobileNumber
        cell.managerButton.isSelected = myData[indexPath.row].manager
        cell.hiddenButton.isSelected = myData[indexPath.row].hidden
        cell.heartButton.isSelected = myData[indexPath.row].heart
        return cell
    }
}
//MARK: - Protocol delegate
extension ResidentsViewController: ResidentSavable {
    func save(_ data: [ResidentModel]) {
        self.myData = data
        mainView.tableView.reloadData()
    }
}