SubscriptionViewController.swift 3.43 KB
//
//  SubscriptionViewController.swift
//  browser
//
//  Created by Artem Talko on 24.11.2023.
//

import UIKit
import StoreKit


final class SubscriptionViewController: UIViewController {
    private let mainView = SubscriptionView()
    
    private let tableViewData: [String]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initViewController()
    }
    
    init() {
        self.tableViewData = StringConstants.removeAdvertTableViewData
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        view = mainView
    }
   
    private func initViewController() {
        addTargets()
        setupTableView()
    }
}

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

//MARK: Helpers
extension SubscriptionViewController {
    private func addTargets() {
        mainView.closeButton.addTarget(self, action: #selector(closeViewController(_ :)), for: .touchUpInside)
        mainView.subscribeButton.addTarget(self, action: #selector(buyAdblocker), for: .touchUpInside)
    }
    
    private func setupTableView() {
        mainView.advantagesTableView.dataSource = self
        mainView.advantagesTableView.delegate = self
        mainView.advantagesTableView.register(SubscriptionTableViewCell.self, forCellReuseIdentifier: SubscriptionTableViewCell.cellID)
    }
}


//MARK: Action
extension SubscriptionViewController {
    @objc
    private func closeViewController(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    @objc
    private func buyAdblocker(_ sender: UIButton) {
        guard let navigationController = presentingViewController as? NavigationViewController else {
            return
        }
        for viewController in navigationController.viewControllers {
            guard let removeAdvertViewController = viewController as? RemoveAdvertViewController else {
                return
            }
            Task {
                do {
                    if let adblockerProduct = removeAdvertViewController.storeKit.storeProducts.first(where: { $0.id == "com.browser.adblocker" }) {
                        if (try await removeAdvertViewController.storeKit.purchase(adblockerProduct)) != nil {
                            removeAdvertViewController.successfullPurchaseHandler()
                            
                            self.dismiss(animated: true)
                        } else {
                            print("Purchase cancelled or pending.")
                        }
                    } else {
                        print("Adblocker product not found.")
                    }
                } catch {
                    print("Failed to purchase Adblocker: \(error)")
                }
            }
        }
    }
}