SettingViewController.swift 4.88 KB
//
//  SettingController.swift
//  browser
//
//  Created by Artem Talko on 26.09.2023.
//

import UIKit
import MessageUI
import StoreKit

final class SettingViewController: UIViewController {
    
    private let mainView = SettingView()
    private let settingTableViewData = StringConstants.settingViewControllerTableViewData
    
    init() {
        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()
        
        view.backgroundColor = .white
        initViewController()
    }
    
    private func initViewController() {
        navigationController?.isNavigationBarHidden = false
        
        setupSettingTableView()
        addTargets()
    }
}


//MARK: SettingTableView
extension SettingViewController: UITableViewDelegate, UITableViewDataSource  {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingTableViewData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: SettingTableViewCell.cellId, for: indexPath) as? SettingTableViewCell
        else {
            return UITableViewCell()
        }
        let info = settingTableViewData[indexPath.item]
        cell.settingTableViewLabel.text = info
        cell.selectionStyle = .none
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let index = indexPath.row
        cellPresshandler(index)
    }
}


//MARK: - Helpers
extension SettingViewController {
    private func setupSettingTableView() {
        mainView.settingTableView.delegate = self
        mainView.settingTableView.dataSource = self
        mainView.settingTableView.register(SettingTableViewCell.self, forCellReuseIdentifier: SettingTableViewCell.cellId)
    }
    
    private func setupMessage() {
        guard MFMailComposeViewController.canSendMail() else {
            return
        }
        let composer = MFMailComposeViewController()
        composer.mailComposeDelegate = self
        composer.setToRecipients(["creattechllc@gmail.com"])
        composer.setSubject("Browser")
        present(composer, animated: true, completion: nil)
    }
    
    private func cellPresshandler(_ index: Int) {
        switch index {
        case 0:
            let privacyViewController = PrivacyViewController()
            presentViewController(privacyViewController)
        case 1:
            let termsViewController = TermsViewController()
            presentViewController(termsViewController)
        case 2:
            shareButtonPressed()
        case 3:
            if let appStoreURL = URL(string: StringConstants.appStoreUrl) {
                if UIApplication.shared.canOpenURL(appStoreURL) {
                    UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil)
                }
            }
        case 4:
            setupMessage()
        case 5:
            Task {
                try? await SubscriptionManager.shared.restorePurchase()
            }
        default:
            break
        }
    }
    
    private func addTargets() {
        mainView.backButton.addTarget(self, action: #selector(closeViewController(_ :)), for: .touchUpInside)
    }
    
    private func shareButtonPressed() {
        let items = [URL(string: StringConstants.appURL)]
        let shareView = UIActivityViewController(activityItems: items as [Any], applicationActivities: nil)
        
        present(shareView, animated: true)
    }
    
    private func presentViewController(_ viewController: UIViewController) {
        viewController.modalPresentationStyle = .fullScreen
        
        present(viewController, animated: true)
    }
}

//MARK: - Action
extension SettingViewController {
    @objc
    private func closeViewController(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
}

//MARK: Mail helper
extension SettingViewController: MFMailComposeViewControllerDelegate {
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        if let _ = error {
            controller.dismiss(animated: true)
        }
        switch result {
        case .cancelled:
            print("cancelled")
        case .saved:
            print("saved")
        case .sent:
            print("send")
        case .failed:
            print("fail")
        @unknown default:
            print("error!")
        }
        
        controller.dismiss(animated: true)
    }
}