<commit_before>//
//  TodayViewController.swift
//  TodayExtensionMac
//
//  Created by <NAME> on 6/12/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import NotificationCenter

class TodayViewController: NSViewController, NCWidgetProviding, NCWidgetListViewDelegate {

    @IBOutlet var listViewController: NCWidgetListViewController!
    
    // MARK: - NSViewController

    override var nibName: String? {
        return "TodayViewController"
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set up the widget list view controller.
        // The contents property should contain an object for each row in the list.
        self.listViewController.contents = []
        NSUserDefaults.groupUserDefaults().favoriteStopIds = [11776, 10308]

    }
    
    func onUpdateFavorites(result: Failable<[FavoriteStopViewModel], BusError>, completionHandler: NCUpdateResult -> Void) {
        switch result {
        case .Success(let models):
            listViewController.contents = models.map({ Box(value: $0) })
            completionHandler(.NewData)
        case .Error(let error):
            // show an error view or something?
            listViewController.contents = []
            completionHandler(.Failed)
            print(error)
            break
        }
    }

    // MARK: - NCWidgetProviding

    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
        // Refresh the widget's contents in preparation for a snapshot.
        // Call the completion handler block after the widget's contents have been
        // refreshed. Pass NCUpdateResultNoData to indicate that nothing has changed
        // or NCUpdateResultNewData to indicate that there is new data since the
        // last invocation of this method.
        
        let cachedStops = NSUserDefaults.groupUserDefaults()
            .cachedFavoriteStops
            .flatMap({ toFavoriteStopViewModel($0, fallbackToGrayColor: false) })
        listViewController.contents = cachedStops.map({ Box(value: $0) })
        completionHandler(.NewData)
        CorvallisBusFavoritesManager.favoriteStops(updateCache: true, fallbackToGrayColor: false, limitResults: false)
                                    .startOnMainThread { self.onUpdateFavorites($0, completionHandler: completionHandler) }
    }

    func widgetMarginInsetsForProposedMarginInsets(defaultMarginInset: NSEdgeInsets) -> NSEdgeInsets {
        return NSEdgeInsetsZero
    }

    var widgetAllowsEditing: Bool {
        // Return true to indicate that the widget supports editing of content and
        // that the list view should be allowed to enter an edit mode.
        return true
    }

    // MARK: - NCWidgetListViewDelegate

    func widgetList(list: NCWidgetListViewController!, viewControllerForRow row: Int) -> NSViewController! {
        // Return a new view controller subclass for displaying an item of widget
        // content. The NCWidgetListViewController will set the representedObject
        // of this view controller to one of the objects in its contents array.
        return ListRowViewController()
    }

    func widgetList(list: NCWidgetListViewController!, shouldReorderRow row: Int) -> Bool {
        // Return true to allow the item to be reordered in the list by the user.
        return false
    }

    func widgetList(list: NCWidgetListViewController!, shouldRemoveRow row: Int) -> Bool {
        // Return true to allow the item to be removed from the list by the user.
        return true
    }

    func widgetList(list: NCWidgetListViewController!, didRemoveRow row: Int) {
        // The user has removed an item from the list.
        let defaults = NSUserDefaults.groupUserDefaults()
        defaults.favoriteStopIds.removeAtIndex(row)
    }

}
<commit_msg>Allow deleting rows from favorites list<commit_after>//
//  TodayViewController.swift
//  TodayExtensionMac
//
//  Created by <NAME> on 6/12/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import NotificationCenter

class TodayViewController: NSViewController, NCWidgetProviding, NCWidgetListViewDelegate {
    @IBOutlet var listViewController: NCWidgetListViewController!
    
    // MARK: - NSViewController

    override var nibName: String? {
        return "TodayViewController"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        listViewController.showsAddButtonWhenEditing = false
        listViewController.contents = []
        NSUserDefaults.groupUserDefaults().favoriteStopIds = [11776, 10308]

    }
    
    func onUpdateFavorites(result: Failable<[FavoriteStopViewModel], BusError>, completionHandler: NCUpdateResult -> Void) {
        switch result {
        case .Success(let models):
            listViewController.contents = models.map({ Box(value: $0) })
            completionHandler(.NewData)
        case .Error(let error):
            // show an error view or something?
            listViewController.contents = []
            completionHandler(.Failed)
            print(error)
            break
        }
    }

    // MARK: - NCWidgetProviding

    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
        // Refresh the widget's contents in preparation for a snapshot.
        // Call the completion handler block after the widget's contents have been
        // refreshed. Pass NCUpdateResultNoData to indicate that nothing has changed
        // or NCUpdateResultNewData to indicate that there is new data since the
        // last invocation of this method.
        
        let cachedStops = NSUserDefaults.groupUserDefaults()
            .cachedFavoriteStops
            .flatMap({ toFavoriteStopViewModel($0, fallbackToGrayColor: false) })
        listViewController.contents = cachedStops.map({ Box(value: $0) })
        completionHandler(.NewData)
        CorvallisBusFavoritesManager.favoriteStops(updateCache: true, fallbackToGrayColor: false, limitResults: false)
                                    .startOnMainThread { self.onUpdateFavorites($0, completionHandler: completionHandler) }
    }

    func widgetMarginInsetsForProposedMarginInsets(defaultMarginInset: NSEdgeInsets) -> NSEdgeInsets {
        return NSEdgeInsetsZero
    }

    var widgetAllowsEditing: Bool {
        // Return true to indicate that the widget supports editing of content and
        // that the list view should be allowed to enter an edit mode.
        return true
    }
    
    func widgetDidBeginEditing() {
        listViewController.editing = true
    }
    
    func widgetDidEndEditing() {
        listViewController.editing = false
    }

    // MARK: - NCWidgetListViewDelegate

    func widgetList(list: NCWidgetListViewController!, viewControllerForRow row: Int) -> NSViewController! {
        // Return a new view controller subclass for displaying an item of widget
        // content. The NCWidgetListViewController will set the representedObject
        // of this view controller to one of the objects in its contents array.
        return ListRowViewController()
    }

    func widgetList(list: NCWidgetListViewController!, shouldReorderRow row: Int) -> Bool {
        // Return true to allow the item to be reordered in the list by the user.
        return false
    }

    func widgetList(list: NCWidgetListViewController!, shouldRemoveRow row: Int) -> Bool {
        // Return true to allow the item to be removed from the list by the user.
        return true
    }

    func widgetList(list: NCWidgetListViewController!, didRemoveRow row: Int) {
        // The user has removed an item from the list.
        let defaults = NSUserDefaults.groupUserDefaults()
        defaults.favoriteStopIds.removeAtIndex(row)
    }

}

{'fraction_non_alphanumeric': 0.07363298757843514, 'fraction_numerical': 0.005890639006274811, 'mean_word_length': 2.6909262759924384, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6602730', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1921, 'n_words': 621}