<commit_before>//
//  AssetGeneratorViewController.swift
//  XCAssetGenerator
//
//  Created by Bader on 9/12/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Cocoa

protocol ScriptParametersDelegate {
    func scriptParametersChanged(controller: AssetGeneratorViewController)
}

class AssetGeneratorViewController: NSViewController {
  
    var parametersDelegate: ScriptParametersDelegate?
    
    var scriptController: ScriptController
    var fileDropController: FileDropViewController!
    var projectToolbarController: ProjectToolbarController!
    
    private var timer: NSTimer = NSTimer()
    
    required init?(coder: NSCoder) {
        scriptController = ScriptController()
        super.init(coder: coder)
    }
    
    
    // We have to set this as soon as possible. Hacky as heck but MVC isnt helping right now.
    func setRecentListDropdown(list: ProgressPopUpButton) {
        self.projectToolbarController = ProjectToolbarController(recentList: list)
        self.projectToolbarController.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // TODO: Find better way to connect containerController to local var. sigh.
        // self.fileDropController = self.childViewControllers.first! as FileDropViewController
    }
    
    
    override func viewDidAppear() {
        super.viewDidAppear()
        self.scriptController.sourceDelegate        = self.fileDropController
        self.scriptController.progressDelegate      = self
        self.scriptController.destinationDelegate   = self.projectToolbarController
    }
    
    func recentlyUsedProjectsDropdownListChanged(sender: ProgressPopUpButton) {
        self.projectToolbarController.recentProjectsListChanged(sender)
    }
    
    func browseButtonPressed() {
        self.projectToolbarController.browseButtonPressed()
    }
    
    func generateButtonPressed(#generateAssets: Bool, args: [AnyObject]?) {
        var options : [ScriptOptions]? = [ScriptOptions]()
        
        if generateAssets {
            options?.insert(ScriptOptions.GenerateMissingAssets, atIndex: 0)
        }
        self.scriptController.executeScript(options)
    }
    
    func canExecuteScript() -> Bool {
        return self.scriptController.canExecuteScript()
    }

    // MARK: - Segues functions
    // Is this better?
    override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "embeddedContainer" {
            self.fileDropController = segue.destinationController as FileDropViewController
            self.fileDropController.delegate = self
            // self.scriptController.sourceDelegate = self.fileDropController
        }
    }
    
}


// MARK:- ProjectToolbar Delegate
extension AssetGeneratorViewController: ProjectToolbarDelegate {
    
    func projectToolbarDidChangeProject(project: XCProject?) {
//        println("projectToolbarDidChangeProject: \(project)")
        if let p = project {
            self.parametersDelegate?.scriptParametersChanged(self)
            if !p.hasValidAssetsPath() {
                println("ERROR: THE DESTINATION PATH CONTAINS A DODO")
                println("REASON: SELECTED PROJECT DOES NOT CONTAIN A VALID XCASSETS PATH")
//                let alert = NSAlert()
//                alert.messageText = "Project does not contain any .xcassets folder\n What the fuck man?"
//                alert.beginSheetModalForWindow(self.view.superview!.window!, completionHandler: { (response :NSModalResponse) -> Void in
//                    println(response)
//                })
            }
        }
    }
}


// MARK: - FileDropController Delegate
extension AssetGeneratorViewController: FileDropControllerDelegate {
    
    func fileDropControllerDidRemoveSourcePath(controller: FileDropViewController, removedPath: String) {
        self.parametersDelegate?.scriptParametersChanged(self)
    }
    
    func fileDropControllerDidSetSourcePath(controller: FileDropViewController, path: Path, previousPath: String?) {
        if PathValidator.directoryContainsInvalidCharacters(path: path, options: nil) {
            println("WARNING: THE SOURCE PATH CONTAINS DODO. I REPEAT, THE SOURCE PATH CONTAINS A DODO")
            println("REASON: FOUND A SUBDIRECTORY WHICH CONTAINS A DOT..... DOT..DOT..")
        }
        
        self.parametersDelegate?.scriptParametersChanged(self)
    }
}<commit_msg>Fix bug where projectDidChange delegate doesnt propogate properly when deleted<commit_after>//
//  AssetGeneratorViewController.swift
//  XCAssetGenerator
//
//  Created by Bader on 9/12/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Cocoa

protocol ScriptParametersDelegate {
    func scriptParametersChanged(controller: AssetGeneratorViewController)
}

class AssetGeneratorViewController: NSViewController {
  
    var parametersDelegate: ScriptParametersDelegate?
    
    var scriptController: ScriptController
    var fileDropController: FileDropViewController!
    var projectToolbarController: ProjectToolbarController!
    
    private var timer: NSTimer = NSTimer()
    
    required init?(coder: NSCoder) {
        scriptController = ScriptController()
        super.init(coder: coder)
    }
    
    
    // We have to set this as soon as possible. Hacky as heck but MVC isnt helping right now.
    func setRecentListDropdown(list: ProgressPopUpButton) {
        self.projectToolbarController = ProjectToolbarController(recentList: list)
        self.projectToolbarController.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // TODO: Find better way to connect containerController to local var. sigh.
        // self.fileDropController = self.childViewControllers.first! as FileDropViewController
    }
    
    
    override func viewDidAppear() {
        super.viewDidAppear()
        self.scriptController.sourceDelegate        = self.fileDropController
        self.scriptController.progressDelegate      = self
        self.scriptController.destinationDelegate   = self.projectToolbarController
    }
    
    func recentlyUsedProjectsDropdownListChanged(sender: ProgressPopUpButton) {
        self.projectToolbarController.recentProjectsListChanged(sender)
    }
    
    func browseButtonPressed() {
        self.projectToolbarController.browseButtonPressed()
    }
    
    func generateButtonPressed(#generateAssets: Bool, args: [AnyObject]?) {
        var options : [ScriptOptions]? = [ScriptOptions]()
        
        if generateAssets {
            options?.insert(ScriptOptions.GenerateMissingAssets, atIndex: 0)
        }
        self.scriptController.executeScript(options)
    }
    
    func canExecuteScript() -> Bool {
        return self.scriptController.canExecuteScript()
    }

    // MARK: - Segues functions
    // Is this better?
    override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "embeddedContainer" {
            self.fileDropController = segue.destinationController as FileDropViewController
            self.fileDropController.delegate = self
            // self.scriptController.sourceDelegate = self.fileDropController
        }
    }
    
}


// MARK:- ProjectToolbar Delegate
extension AssetGeneratorViewController: ProjectToolbarDelegate {
    
    func projectToolbarDidChangeProject(project: XCProject?) {
        if let p = project {
            if !p.hasValidAssetsPath() {
                println("ERROR: THE DESTINATION PATH CONTAINS A DODO")
                println("REASON: SELECTED PROJECT DOES NOT CONTAIN A VALID XCASSETS PATH")
            }
        }
        
        self.parametersDelegate?.scriptParametersChanged(self)
    }
}


// MARK: - FileDropController Delegate
extension AssetGeneratorViewController: FileDropControllerDelegate {
    
    func fileDropControllerDidRemoveSourcePath(controller: FileDropViewController, removedPath: String) {
        self.parametersDelegate?.scriptParametersChanged(self)
    }
    
    func fileDropControllerDidSetSourcePath(controller: FileDropViewController, path: Path, previousPath: String?) {
        if PathValidator.directoryContainsInvalidCharacters(path: path, options: nil) {
            println("WARNING: THE SOURCE PATH CONTAINS DODO. I REPEAT, THE SOURCE PATH CONTAINS A DODO")
            println("REASON: FOUND A SUBDIRECTORY WHICH CONTAINS A DOT..... DOT..DOT..")
        }
        
        self.parametersDelegate?.scriptParametersChanged(self)
    }
}
{'fraction_non_alphanumeric': 0.0776255707762557, 'fraction_numerical': 0.002341646177262616, 'mean_word_length': 2.9309710078232856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2269010', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2169, 'n_words': 503}