RemoveAdvertViewController.swift 3 KB
//
//  RemoveAdvertViewController.swift
//  browser
//
//  Created by Artem Talko on 27.09.2023.
//

import UIKit

final class RemoveAdvertViewController: UIViewController {
    
    private let mainView = RemoveAdvertView()
    let data = StringConstants.removeAdvertTableViewData
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = ColorConstants.lightGray
        initViewController()
    }
    
    private func initViewController() {
        setupTableView()
        addTargets()
    }
    
    override func loadView() {
        view = mainView
    }
    
    init(){
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK: - Table View
extension RemoveAdvertViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "RemoveAdvertTableViewCell") as?
                RemoveAdvertTableViewCell else {
            return UITableViewCell() }

        cell.advantagesCellLabel.text = data[indexPath.row]
        cell.selectionStyle = .none

        return cell
    }
    
    private func setupTableView() {
        mainView.advantagesTableView.dataSource = self
        mainView.advantagesTableView.delegate = self
        mainView.advantagesTableView.register(RemoveAdvertTableViewCell.self, forCellReuseIdentifier: StringConstants.removeAdvertTableViewCell)
    }
}


//MARK: - Action
extension RemoveAdvertViewController {
    private func addTargets() {
        mainView.shieldView.addTarget(self, action:  #selector(shieldButtonTapped(_ :)), for: .touchUpInside)
        mainView.closeButton.addTarget(self, action: #selector(closeViewController(_ :)), for: .touchUpInside)
    }
    
    @objc
    private func shieldButtonTapped(_ sender: UIButton) {
        UIView.animate(withDuration: 0.2) {
            sender.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            sender.alpha = 0.7
        }
        let userDefaultsAdBlockerValue = CachingManager.shared.isAdBlocking
        if userDefaultsAdBlockerValue {
            mainView.shieldView.setImage(.shieldInactive, for: .normal)
            mainView.tapActionLabel.text = StringConstants.turnOff
            CachingManager.shared.isAdBlocking = false
        } else {
            mainView.shieldView.setImage(.shieldActive, for: .normal)
            mainView.tapActionLabel.text = StringConstants.turnOn
            CachingManager.shared.isAdBlocking = true
        }
        UIView.animate(withDuration: 0.2, delay: 0.2) {
            sender.transform = .identity
            sender.alpha = 1.0
        }
    }

    @objc
    private func closeViewController(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
}