<commit_before>//
//  OAuth2WebViewController.swift
//  OAuth2
//
//  Created by <NAME> on 7/15/14.
//  Copyright 2014 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit


/**
    A simple iOS web view controller that allows you to display the login/authorization screen.
 */
public class OAuth2WebViewController: UIViewController, UIWebViewDelegate
{
	/// Handle to the OAuth2 instance in play, only used for debug lugging at this time.
	var oauth: OAuth2?
	
	/// The URL to load on first show.
	public var startURL: NSURL? {
		didSet(oldURL) {
			if nil != startURL && nil == oldURL && isViewLoaded() {
				loadURL(startURL!)
			}
		}
	}
	
	/// The URL string to intercept and respond to.
	var interceptURLString: String? {
		didSet(oldURL) {
			if nil != interceptURLString {
				if let url = NSURL(string: interceptURLString!) {
					interceptComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: true)
				}
				else {
					oauth?.logIfVerbose("Failed to parse URL \(interceptURLString), discarding")
					interceptURLString = nil
				}
			}
			else {
				interceptComponents = nil
			}
		}
	}
	var interceptComponents: NSURLComponents?
	
	/// Closure called when the web view gets asked to load the redirect URL, specified in `interceptURLString`.
	var onIntercept: ((url: NSURL) -> Bool)?
	
	/// Called when the web view is about to be dismissed.
	var onWillDismiss: ((didCancel: Bool) -> Void)?
	
	var cancelButton: UIBarButtonItem?
	
	/// Our web view; implicitly unwrapped so do not attempt to use it unless isViewLoaded() returns true.
	var webView: UIWebView!
	
	var loadingView: UIView?
	
	init() {
		super.init(nibName: nil, bundle: nil)
	}
	
	required public init(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
	}
	
	
	// MARK: - View Handling
	
	override public func loadView() {
		edgesForExtendedLayout = .All
		extendedLayoutIncludesOpaqueBars = true
		automaticallyAdjustsScrollViewInsets = true
		
		super.loadView()
		view.backgroundColor = UIColor.whiteColor()
		
		cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "cancel:")
		navigationItem.rightBarButtonItem = cancelButton
		
		// create a web view
		webView = UIWebView()
		webView.setTranslatesAutoresizingMaskIntoConstraints(false)
		webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
		webView.delegate = self
		
		view.addSubview(webView)
		let views = ["web": webView]
		view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[web]|", options: nil, metrics: nil, views: views))
		view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[web]|", options: nil, metrics: nil, views: views))
	}
	
	override public func viewWillAppear(animated: Bool) {
		super.viewWillAppear(animated)
		
		if !webView.canGoBack {
			if nil != startURL {
				loadURL(startURL!)
			}
			else {
				webView.loadHTMLString("There is no `startURL`", baseURL: nil)
			}
		}
	}
	
	func showHideBackButton(show: Bool) {
		if show {
			let bb = UIBarButtonItem(barButtonSystemItem: .Rewind, target: self, action: "goBack:")
			navigationItem.leftBarButtonItem = bb
		}
		else {
			navigationItem.leftBarButtonItem = nil
		}
	}
	
	func showLoadingIndicator() {
		// TODO: implement<commit_msg>hide status bar<commit_after>//
//  OAuth2WebViewController.swift
//  OAuth2
//
//  Created by <NAME> on 7/15/14.
//  Copyright 2014 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit


/**
    A simple iOS web view controller that allows you to display the login/authorization screen.
 */
public class OAuth2WebViewController: UIViewController, UIWebViewDelegate
{
	/// Handle to the OAuth2 instance in play, only used for debug lugging at this time.
	var oauth: OAuth2?
	
	/// The URL to load on first show.
	public var startURL: NSURL? {
		didSet(oldURL) {
			if nil != startURL && nil == oldURL && isViewLoaded() {
				loadURL(startURL!)
			}
		}
	}
	
	/// The URL string to intercept and respond to.
	var interceptURLString: String? {
		didSet(oldURL) {
			if nil != interceptURLString {
				if let url = NSURL(string: interceptURLString!) {
					interceptComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: true)
				}
				else {
					oauth?.logIfVerbose("Failed to parse URL \(interceptURLString), discarding")
					interceptURLString = nil
				}
			}
			else {
				interceptComponents = nil
			}
		}
	}
	var interceptComponents: NSURLComponents?
	
	/// Closure called when the web view gets asked to load the redirect URL, specified in `interceptURLString`.
	var onIntercept: ((url: NSURL) -> Bool)?
	
	/// Called when the web view is about to be dismissed.
	var onWillDismiss: ((didCancel: Bool) -> Void)?
	
	var cancelButton: UIBarButtonItem?
	
	/// Our web view; implicitly unwrapped so do not attempt to use it unless isViewLoaded() returns true.
	var webView: UIWebView!
	
	var loadingView: UIView?
	
	init() {
		super.init(nibName: nil, bundle: nil)
	}
	
	required public init(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
	}
	
	
	// MARK: - View Handling
	
	override public func loadView() {
		edgesForExtendedLayout = .All
		extendedLayoutIncludesOpaqueBars = true
		automaticallyAdjustsScrollViewInsets = true
		
		super.loadView()
		view.backgroundColor = UIColor.whiteColor()
		
		cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "cancel:")
		navigationItem.rightBarButtonItem = cancelButton
		
		// create a web view
		webView = UIWebView()
		webView.setTranslatesAutoresizingMaskIntoConstraints(false)
		webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
		webView.delegate = self
		
		view.addSubview(webView)
		let views = ["web": webView]
		view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[web]|", options: nil, metrics: nil, views: views))
		view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[web]|", options: nil, metrics: nil, views: views))
	}
	
	override public func viewWillAppear(animated: Bool) {
		super.viewWillAppear(animated)
        UIApplication.sharedApplication().statusBarHidden = true
		
		if !webView.canGoBack {
			if nil != startURL {
				loadURL(startURL!)
			}
			else {
				webView.loadHTMLString("There is no `startURL`", baseURL: nil)
			}
		}
	}
    
    override public func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.sharedApplication().statusBarHidden = false
    }
	
	func showHideBackButton(show: Bool) {
		if show {
			let bb = UIBarButtonItem(barButtonSystemItem: .Rewind, target: self, action: "goBack:")
			navigationItem.leftBarButtonItem = bb
		}
		else {
			navigationItem.leftBarButtonItem = nil
		}
	}
	
	func showLoadingIndicator() {
		// TODO: implement
{'fraction_non_alphanumeric': 0.09937412185464299, 'fraction_numerical': 0.004598288414867799, 'mean_word_length': 3.669051878354204, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3310946', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2355, 'n_words': 723}