<commit_before>//
//  TodayViewController.swift
//  Widget
//
//  Created by Max on 27/12/15.
//
//

import UIKit
import NotificationCenter

extension String {
	
	var urlEncode: NSString? {
		get {
			return stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet())
		}
	}
}

class TodayViewController: UIViewController, NCWidgetProviding {
	
	private let kLastUsedPlaylistKey = "Last Used Playlist";
	private let kSharedVerbsKey = "Shared Verbs";
	
	private var lastUpdated: NSDate?
	private var infinitif: String?
	private var tense: String?
	private var lastUsedPlaylist: String?
	private var isQuizMode = false
	
	@IBOutlet var label: UILabel?
	
    override func viewDidLoad() {
        super.viewDidLoad()
		self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "viewDidSelected:"))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	func viewDidSelected(gesture: UIGestureRecognizer) {
		if (self.infinitif != nil) {
			if (self.isQuizMode && self.lastUsedPlaylist != nil && self.tense != nil) {
				self.lastUpdated = nil
				let url = "iverb://quiz/\(self.lastUsedPlaylist!.urlEncode!)/\(self.infinitif!)#\(self.tense!)"
				self.extensionContext?.openURL(NSURL(string: url)!, completionHandler: nil)
			} else {
				let url = "iverb://verb#\(self.infinitif!)"
				self.extensionContext?.openURL(NSURL(string: url)!, completionHandler: nil)
			}
		}
	}
	
    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
		
		if (lastUpdated != nil && lastUpdated!.timeIntervalSinceNow < 10 * 60) {
			completionHandler(NCUpdateResult.NoData)
		}
		
		srand(UInt32(time(nil)))
		let v = rand()
		self.isQuizMode = ((v % 2) == 1)
		
		let sharedDefaults = NSUserDefaults(suiteName: "group.lisacintosh.iverb")
		let dict = sharedDefaults?.dictionaryForKey(kSharedVerbsKey)
		let index = dict?.startIndex.advancedBy(Int(rand() % Int32(dict!.keys.count)))
		let (key, value) = dict![index!]
		self.infinitif = key
		let comps = (value as! String).componentsSeparatedByString("|")
		
		var string = "To \(comps[0]), \(comps[1]), \(comps[2])\n"
		
		if (self.isQuizMode) {
			let playlistName = sharedDefaults?.stringForKey(kLastUsedPlaylistKey)
			// Ignore non-user playlist that starts and ends with "__"
			self.lastUsedPlaylist = (playlistName != nil && !playlistName!.hasPrefix("_") && !playlistName!.hasSuffix("_")) ? playlistName : nil
			
			if (self.lastUsedPlaylist != nil) {
				let index = Int(rand() % 2)
				self.tense = [ "past", "past-participle" ][index]
				string = "To \(comps[0]), "<commit_msg>[fix] Fixing widget<commit_after>//
//  TodayViewController.swift
//  Widget
//
//  Created by Max on 27/12/15.
//
//

import UIKit
import NotificationCenter

extension String {
	
	var urlEncode: NSString? {
		get {
			return stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet())
		}
	}
}

class TodayViewController: UIViewController, NCWidgetProviding {
	
	private let kLastUsedPlaylistKey = "Last Used Playlist";
	private let kSharedVerbsKey = "Shared Verbs";
	
	private var lastUpdated: NSDate?
	private var infinitif: String?
	private var tense: String?
	private var lastUsedPlaylist: String?
	private var isQuizMode = false
	
	@IBOutlet var label: UILabel?
	
    override func viewDidLoad() {
        super.viewDidLoad()
		self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "viewDidSelected:"))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	func viewDidSelected(gesture: UIGestureRecognizer) {
		if (self.infinitif != nil) {
			if (self.isQuizMode && self.lastUsedPlaylist != nil && self.tense != nil) {
				self.lastUpdated = nil
				let url = "iverb://quiz/\(self.lastUsedPlaylist!.urlEncode!)/\(self.infinitif!)#\(self.tense!)"
				self.extensionContext?.openURL(NSURL(string: url)!, completionHandler: nil)
			} else {
				let url = "iverb://verb#\(self.infinitif!)"
				self.extensionContext?.openURL(NSURL(string: url)!, completionHandler: nil)
			}
		}
	}
	
    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
		
		if (lastUpdated != nil && lastUpdated!.timeIntervalSinceNow < 10 * 60) {
			completionHandler(NCUpdateResult.NoData)
			return
		}
		
		let sharedDefaults = NSUserDefaults(suiteName: "group.lisacintosh.iverb")
		let dict = sharedDefaults?.dictionaryForKey(kSharedVerbsKey)!
		if (dict == nil || dict!.keys.count == 0) {
			completionHandler(NCUpdateResult.NoData)
			return
		}
		
		srand(UInt32(time(nil)))
		let v = rand()
		self.isQuizMode = ((v % 2) == 1)
		
		let index = dict!.startIndex.advancedBy(Int(rand() % Int32(dict!.keys.count)))
		let (key, value) = dict![index]
		self.infinitif = key
		let comps = (value as! String).componentsSeparatedByString("|")
		if (comps.count < 3) {
			completionHandler(NCUpdateResult.NoData)
			return
		}
		
		var string = "To \(comps[0]), \(comps[1]), \(comps[2])\n"
		
		if (self.isQuizMode) {
			let playlistName = sharedDefaults!.stringForKey(kLastUsedPlaylistKey)
			// Ignore non-user playlist that starts and ends with "__"
			self.lastUsedPlaylist = (playlistName != nil && !playlistName!.hasPrefix("_") && !playlistName!.hasSuffix("_")) ? playlistName : nil
			
			if (self.lastUsedPlaylist != nil) {
				let index = Int(rand() % 2)
				self.tense = [ "past", "past-participle" ][index]
				string = "To \(comps[0]), "
{'fraction_non_alphanumeric': 0.13318004952246199, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 4.008857395925598, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4417685', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1820, 'n_words': 393}