PayloadViewController.swift 3 KB
//
//  PayloadViewController.swift
//  browser
//
//  Created by Artem Talko on 25.09.2023.
//

import UIKit

final class PayloadViewController: UIViewController {
    private let mainView = PayloadView()
    
    private let payloadTableViewData: [String]
    
    init() {
        payloadTableViewData = StringConstants.payloadViewControllerData
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        view = mainView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initViewController()
    }
    
    private func initViewController() {
        navigationController?.isNavigationBarHidden = true
        
        setupTableView()
        addTargets()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        NotificationCenter.default.removeObserver(self)
    }
}

extension PayloadViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return payloadTableViewData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: AdvantagesTableViewCell.cellID) as?
                AdvantagesTableViewCell else {
            return UITableViewCell()
        }
        cell.advantagesCellLabel.text = payloadTableViewData[indexPath.row]
        
        return cell
    }
}


//MARK: - Action
extension PayloadViewController {
    @objc
    private func getStartedButtonTapped(_ sender: UIButton) {
        navigationController?.popViewController(animated: false)
    }
    
    @objc
    private func textPressed(_ sender: UITapGestureRecognizer) {
        let termsViewController = TermsViewController()
        termsViewController.modalPresentationStyle = .fullScreen
        
        present(termsViewController, animated: true)
    }
}

//MARK: Helpers
extension PayloadViewController {
    private func addTargets() {
        mainView.getStartedButton.addTarget(self, action:  #selector(getStartedButtonTapped(_ :)), for: .touchUpInside)
        mainView.privacyLabelView.isUserInteractionEnabled = true
        mainView.privacyLabelView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(textPressed(_:))))
        
        GestureSetup()
    }
    
    private func GestureSetup() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(textPressed(_ :)))
        mainView.infoLabel.addGestureRecognizer(tapGesture)
    }
    
    private func setupTableView() {
        mainView.advantagesTableView.dataSource = self
        mainView.advantagesTableView.delegate = self
        mainView.advantagesTableView.register(AdvantagesTableViewCell.self, forCellReuseIdentifier: AdvantagesTableViewCell.cellID)
    }
}