<commit_before>//
//  AppCoordinator.swift
//  MyWeight
//
//  Created by Diogo on 09/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import StoreKit

public class AppCoordinator {

    let navigationController: UINavigationController
    let massService: MassRepository = MassService()

    public init(with navigationController: UINavigationController)
    {
        navigationController.isNavigationBarHidden = true
        self.navigationController = navigationController
    }

    public func start()
    {
        let controller = ListViewController(with: massService)
        controller.delegate = self
        navigationController.pushViewController(controller, animated: true)
    }

    public func extensionRequestedAuthorization() {
        massService.requestAuthorizationForExtension()
    }

    let modalTransitionController = ModalTransition()

    func startAdd(last mass: Mass?)
    {
        let addViewController = AddViewController(with: massService,
                                                  startMass: mass ?? Mass())
        addViewController.delegate = self

        addViewController.modalPresentationStyle = .custom
        addViewController.transitioningDelegate = modalTransitionController

        self.navigationController.present(addViewController,
                                          animated: true)
    }

    func startAuthorizationRequest()
    {
        let viewController = AuthorizationRequestViewController(with: massService)
        viewController.delegate = self
        self.navigationController.present(viewController,
                                          animated: true)
    }

    func startAuthorizationDenied()
    {
        let viewController = AccessDeniedViewController()
        viewController.delegate = self
        self.navigationController.present(viewController,
                                          animated: true)
    }

    func askForReview() {
        if #available(iOS 10.3, *) {
            let key = "<KEY>"
            let defaults = UserDefaults.standard
            let times = defaults.integer(forKey: key) + 1
            defaults.set(times, forKey: key)
            if times % 10 == 0, let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                SKStoreReviewController.requestReview(in: scene)
            }
        }
    }
}

extension AppCoordinator: ListViewControllerDelegate {

    public func didTapAddMeasure(last mass: Mass?)
    {
        switch massService.authorizationStatus {
        case .authorized:
            startAdd(last: mass)
        case .notDetermined:
            startAuthorizationRequest()
        case .denied:
            startAuthorizationDenied()
        }
    }

    public func failedToDeleteMass() {
        let action = AlertViewModel
            .Action(title: Localization.alertOkButton)
            { $0.presentingViewController?.dismiss(animated: true) }
        let viewMoodel =
            AlertViewModel(title: Localization.alertFailToDeleteTitle,
                           message: Localization.alertFailToDeleteMessage,
                           actions: [action])
        let alert = UIAlertController(with: viewMoodel)

        navigationController.present(alert, animated: true)
    }

}

extension AppCoordinator: AddViewControllerDelegate {

    public func didEnd(on viewController: AddViewController) {
        viewController.dismiss(animated: true, completion: nil)
        askForReview()
    }

}
<commit_msg>Prevent presentation in a background thread<commit_after>//
//  AppCoordinator.swift
//  MyWeight
//
//  Created by Diogo on 09/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import StoreKit

public class AppCoordinator {

    let navigationController: UINavigationController
    let massService: MassRepository = MassService()

    public init(with navigationController: UINavigationController)
    {
        navigationController.isNavigationBarHidden = true
        self.navigationController = navigationController
    }

    public func start()
    {
        let controller = ListViewController(with: massService)
        controller.delegate = self
        navigationController.pushViewController(controller, animated: true)
    }

    public func extensionRequestedAuthorization() {
        massService.requestAuthorizationForExtension()
    }

    let modalTransitionController = ModalTransition()

    func startAdd(last mass: Mass?)
    {
        let addViewController = AddViewController(with: massService,
                                                  startMass: mass ?? Mass())
        addViewController.delegate = self

        addViewController.modalPresentationStyle = .custom
        addViewController.transitioningDelegate = modalTransitionController

        self.navigationController.present(addViewController,
                                          animated: true)
    }

    func startAuthorizationRequest()
    {
        let viewController = AuthorizationRequestViewController(with: massService)
        viewController.delegate = self
        self.navigationController.present(viewController,
                                          animated: true)
    }

    func startAuthorizationDenied()
    {
        let viewController = AccessDeniedViewController()
        viewController.delegate = self
        self.navigationController.present(viewController,
                                          animated: true)
    }

    func askForReview() {
        if #available(iOS 10.3, *) {
            let key = "<KEY>"
            let defaults = UserDefaults.standard
            let times = defaults.integer(forKey: key) + 1
            defaults.set(times, forKey: key)
            if times % 10 == 0, let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                SKStoreReviewController.requestReview(in: scene)
            }
        }
    }
}

extension AppCoordinator: ListViewControllerDelegate {

    public func didTapAddMeasure(last mass: Mass?)
    {
        switch massService.authorizationStatus {
        case .authorized:
            startAdd(last: mass)
        case .notDetermined:
            startAuthorizationRequest()
        case .denied:
            startAuthorizationDenied()
        }
    }

    public func failedToDeleteMass() {
        let action = AlertViewModel
            .Action(title: Localization.alertOkButton)
            { $0.presentingViewController?.dismiss(animated: true) }
        let viewModel =
            AlertViewModel(title: Localization.alertFailToDeleteTitle,
                           message: Localization.alertFailToDeleteMessage,
                           actions: [action])
        let alert = UIAlertController(with: viewModel)

        DispatchQueue.main.async {
            self.navigationController.present(alert, animated: true)
        }
    }

}

extension AppCoordinator: AddViewControllerDelegate {

    public func didEnd(on viewController: AddViewController) {
        viewController.dismiss(animated: true, completion: nil)
        askForReview()
    }

}

{'fraction_non_alphanumeric': 0.07176966292134832, 'fraction_numerical': 0.0050561797752808986, 'mean_word_length': 2.2515981735159816, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '639538', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1626, 'n_words': 376}