<commit_before>//
//  CaturlogWindowViewModel.swift
//  Caturlog
//
//  Created by <NAME> on 7/22/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import Cocoa
import CoreData

class CaturlogWindowViewModel {
    
    let itemEntityController: NSArrayController
    
    init() {
        let moc = (NSApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
        itemEntityController = NSArrayController(content: nil)
        itemEntityController.managedObjectContext = moc
        itemEntityController.bind("managedObjectContext", toObject: moc, withKeyPath: "self", options: nil)
        itemEntityController.entityName = "Item"
        itemEntityController.automaticallyPreparesContent = true
        itemEntityController.avoidsEmptySelection = true
        itemEntityController.preservesSelection = true
        itemEntityController.selectsInsertedObjects = true
        itemEntityController.clearsFilterPredicateOnInsertion = true
        itemEntityController.usesLazyFetching = false
        itemEntityController.fetch(nil)        
    }
        
    func omnibarTokensChanged(newTokens: Array<String>) {
        let (urls,tags) = urlsAndTagsFromTokens(newTokens)

        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices
        
        if(urls.count > 0) {
            // Add URLs and bind them to tags; use an empty predicate
            if let user = appDel.caturlogServices.user.getCurrentUser()? {
                for url in urls {
                    services.filer.storeAsItem(url, completion: { (item: Item) -> () in 
                        for tag in tags {
                            // Apply the tag for the URL
                            services.tagger.addTag(tag, contentID: item.contentID, user:user)
                        }                    
                    })
                }
            }
        }
        else if (tags.count > 0){
            // Create and search with a predicate
            var i = 0
            var predicateString = ""
            for tag in tags {
                if(i > 0) {
                    predicateString += " AND "
                }
                predicateString += "ANY userItemTags.tag.name BEGINSWITH '\(tag)'"
                i++
            }
            itemEntityController.fetchPredicate = NSPredicate(format: predicateString)
            itemEntityController.fetch(nil)<commit_msg>If a search for tags produces zero results, default to showing all results. I needed to put it in an async block because that slight delay gives the search a chance to return before the arrangedObjects count is checked.<commit_after>//
//  CaturlogWindowViewModel.swift
//  Caturlog
//
//  Created by <NAME> on 7/22/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import Cocoa
import CoreData

class CaturlogWindowViewModel {
    
    let itemEntityController: NSArrayController
    
    init() {
        let moc = (NSApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
        itemEntityController = NSArrayController(content: nil)
        itemEntityController.managedObjectContext = moc
        itemEntityController.bind("managedObjectContext", toObject: moc, withKeyPath: "self", options: nil)
        itemEntityController.entityName = "Item"
        itemEntityController.automaticallyPreparesContent = true
        itemEntityController.avoidsEmptySelection = true
        itemEntityController.preservesSelection = true
        itemEntityController.selectsInsertedObjects = true
        itemEntityController.clearsFilterPredicateOnInsertion = true
        itemEntityController.usesLazyFetching = false
        itemEntityController.fetch(nil)        
    }
        
    func omnibarTokensChanged(newTokens: Array<String>) {
        let (urls,tags) = urlsAndTagsFromTokens(newTokens)

        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices
        
        if(urls.count > 0) {
            // Add URLs and bind them to tags; use an empty predicate
            if let user = appDel.caturlogServices.user.getCurrentUser()? {
                for url in urls {
                    services.filer.storeAsItem(url, completion: { (item: Item) -> () in 
                        for tag in tags {
                            // Apply the tag for the URL
                            services.tagger.addTag(tag, contentID: item.contentID, user:user)
                        }                    
                    })
                }
            }
        }
        else if (tags.count > 0){
            // Create and search with a predicate
            var i = 0
            var predicateString = ""
            for tag in tags {
                if(i > 0) {
                    predicateString += " AND "
                }
                predicateString += "ANY userItemTags.tag.name BEGINSWITH '\(tag)'"
                i++
            }
            itemEntityController.fetchPredicate = NSPredicate(format: predicateString)
            itemEntityController.fetch(nil)
            dispatch_async(dispatch_get_main_queue(), {
                // If there are no matching entities, revert to showing all
                if(self.itemEntityController.arrangedObjects.count == 0) {
                    self.itemEntityController.fetchPredicate = nil
                    self.itemEntityController.fetch(nil)                
                }
            })
{'fraction_non_alphanumeric': 0.07464028776978417, 'fraction_numerical': 0.004856115107913669, 'mean_word_length': 1.891835673426937, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2394127', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1352, 'n_words': 345}