<commit_before>//
//  AuthenticationFlow.swift
//  PartyUP
//
//  Created by <NAME> on 2016-04-25.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import SCLAlertView

typealias AuthenticationFlowCompletion = (AuthenticationManager, Bool) -> Void

class AuthenticationFlow {

	private(set) var isFlowing = false
    
    func addAction(action: AuthenticationFlowCompletion) {
        completers.append(action)
    }
    
    func setPutoffs(putoffs: [String]) {
        self.putoffs = putoffs
    }
    
    func startOnController(controller: UIViewController) -> AuthenticationFlow {
        if !isFlowing {
            isFlowing = true
            beginOnController(controller)
        }
        return self
    }
    
    func stop() {
        if let leader = leader {
            leader.close()
            AuthenticationFlow.flow = nil
        }
    }

	private func beginOnController(controller: UIViewController) {
        let alert = SCLAlertView(appearance: SCLAlertView.SCLAppearance(showCloseButton: false, shouldAutoDismiss: false))
        let inLabel = NSLocalizedString("Log in with", comment: "Login service button label")
        let outLabel = NSLocalizedString("Log out of", comment: "Logout service button label")
        for auth in manager.authentics {
            let label = "  " + (auth.isLoggedIn ? outLabel : inLabel) + " \(auth.name)"
            let button = alert.addButton(label, backgroundColor: auth.color) {
                [unowned alert] in alert.hideView()
                self.manager.loginToProvider(auth, fromViewController: controller)
            }
            button.setImage(auth.logo, forState: .Normal)
        }
        alert.addButton(NSLocalizedString("Read Terms of Service", comment: "Terms alert full terms action")) { UIApplication.sharedApplication().openURL(NSURL(string: "terms.html", relativeToURL: PartyUpConstants.PartyUpWebsite)!)
        }
        
        var off = putoffs.generate()
        if let putoff = off.next() {
            putoffButton = alert.addButton(putoff) { [weak self] in
                if let put = off.next() {
                    self?.putoffButton?.setTitle(put, forState: .Normal)
                } else {
					self?.end(true)
                }
            }
        }
        
        let file = NSBundle.mainBundle().pathForResource("Conduct", ofType: "txt")
        let message: String? = file.flatMap { try? String.init(contentsOfFile: $0) }
        leader = alert.showNotice(NSLocalizedString("Log in", comment: "Login Title"),
                                  subTitle: message!,
                                  colorStyle: 0xf77e56)
	}

	private func end(cancelled: Bool = false) {
        stop()
        self.completers.forEach { $0(self.manager, cancelled) }
	}

    init() {
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AuthenticationFlow.observeAuthenticationNotification(_:)), name: AuthenticationManager.AuthenticationStatusChangeNotification, object: manager)
	}<commit_msg>Work around the background colour getting unset on button interactions in SCLAlertView.<commit_after>//
//  AuthenticationFlow.swift
//  PartyUP
//
//  Created by <NAME> on 2016-04-25.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import SCLAlertView

typealias AuthenticationFlowCompletion = (AuthenticationManager, Bool) -> Void

class AuthenticationFlow {

	private(set) var isFlowing = false
    
    func addAction(action: AuthenticationFlowCompletion) {
        completers.append(action)
    }
    
    func setPutoffs(putoffs: [String]) {
        self.putoffs = putoffs
    }
    
    func startOnController(controller: UIViewController) -> AuthenticationFlow {
        if !isFlowing {
            isFlowing = true
            beginOnController(controller)
        }
        return self
    }
    
    func stop() {
        if let leader = leader {
            leader.close()
            AuthenticationFlow.flow = nil
        }
    }

	private func beginOnController(controller: UIViewController) {
        let themeColor = UIColor(r: 247, g: 126, b: 86, alpha: 255)
        let alert = SCLAlertView(appearance: SCLAlertView.SCLAppearance(showCloseButton: false, shouldAutoDismiss: false))
        let inLabel = NSLocalizedString("Log in with", comment: "Login service button label")
        let outLabel = NSLocalizedString("Log out of", comment: "Logout service button label")
        for auth in manager.authentics {
            let label = "  " + (auth.isLoggedIn ? outLabel : inLabel) + " \(auth.name)"
            let button = alert.addButton(label, backgroundColor: auth.color) {
                [unowned alert] in alert.hideView()
                self.manager.loginToProvider(auth, fromViewController: controller)
            }
            button.setImage(auth.logo, forState: .Normal)
        }
        alert.addButton(NSLocalizedString("Read Terms of Service", comment: "Terms alert full terms action"), backgroundColor: themeColor) { UIApplication.sharedApplication().openURL(NSURL(string: "terms.html", relativeToURL: PartyUpConstants.PartyUpWebsite)!)
        }
        
        var off = putoffs.generate()
        if let putoff = off.next() {
            putoffButton = alert.addButton(putoff, backgroundColor: themeColor) { [weak self] in
                if let put = off.next() {
                    self?.putoffButton?.setTitle(put, forState: .Normal)
                } else {
					self?.end(true)
                }
            }
        }
        
        let file = NSBundle.mainBundle().pathForResource("Conduct", ofType: "txt")
        let message: String? = file.flatMap { try? String.init(contentsOfFile: $0) }
        leader = alert.showNotice(NSLocalizedString("Log in", comment: "Login Title"),
                                  subTitle: message!,
                                  colorStyle: 0xf77e56)
	}

	private func end(cancelled: Bool = false) {
        stop()
        self.completers.forEach { $0(self.manager, cancelled) }
	}

    init() {
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AuthenticationFlow.observeAuthenticationNotification(_:)), name: AuthenticationManager.AuthenticationStatusChangeNotification, object: manager)
	}
{'fraction_non_alphanumeric': 0.10038180082723512, 'fraction_numerical': 0.0077951002227171495, 'mean_word_length': 2.4773230088495577, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5373240', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1660, 'n_words': 403}