<commit_before>//
//  ShaderDocumentBrowserViewController.swift
//  MetalToy
//
//  Created by minimoog on 10/17/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import UIKit

class ShaderDocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        
        allowsDocumentCreation = true
        allowsPickingMultipleItems = false
    }
    
    // --- Create new document ---
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController,
                         didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
        print("Creating new document")
        
        let doc = ShaderDocument()
        let url = doc.fileURL
        
        //save to temp
        doc.save(to: url, for: .forCreating) { (saveSuccess) in
            guard saveSuccess else {
                print("Cannot create new document")
                
                importHandler(nil, .none)
                return
            }
            
            // close document
            
            doc.close(completionHandler: { (closeSuccess) in
                guard closeSuccess else {
                    print("Cannot create/close new document")
                    
                    importHandler(nil, .none)
                    return
                }
                
                importHandler(url, .move)
            })
        }
    }

    func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
        print("importing document from \(sourceURL.path) to \(destinationURL.path)")
        
        presentDocument(at: destinationURL)
    }
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
        let alert = UIAlertController(title: "Unable to import document",
                                      message: "error: \(String(describing: error?.localizedDescription))",
                                      preferredStyle: .alert)
        
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        
        alert.addAction(action)
        
        controller.present(alert, animated: true, completion: nil)
    }
    
    // user selected a document
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
        assert(controller.allowsPickingMultipleItems == false)
        assert(!documentURLs.isEmpty)
        assert(documentURLs.count <= 1)
        
        guard let url = documentURLs.first else {
            fatalError(" no url? ")
        }
        
        presentDocument(at: url)
    }
    
    func presentDocument(at url: URL) {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        
        let tempController = storyBoard.instantiateViewController(withIdentifier: "EditorViewController")
        
        guard let editorViewController = tempController as? EditorViewController else {
            fatalError("cannot cast to editorViewController")
        }
        
        //load editor view
        editorViewController.loadViewIfNeeded()
        
        let doc = ShaderDocument(fileURL: url)
        
        editorViewController.document = doc
        
        doc.open { [weak self] (success) in
            guard success else {
                fatalError("Unable to open shader file")
            }
            
            self?.present(editorViewController, animated: true, completion: nil)
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}<commit_msg>Invoke EditorNavigationController instead EditorViewController in  Doc Browser controller.<commit_after>//
//  ShaderDocumentBrowserViewController.swift
//  MetalToy
//
//  Created by minimoog on 10/17/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import UIKit

class ShaderDocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        
        allowsDocumentCreation = true
        allowsPickingMultipleItems = false
    }
    
    // --- Create new document ---
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController,
                         didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
        print("Creating new document")
        
        let doc = ShaderDocument()
        let url = doc.fileURL
        
        //save to temp
        doc.save(to: url, for: .forCreating) { (saveSuccess) in
            guard saveSuccess else {
                print("Cannot create new document")
                
                importHandler(nil, .none)
                return
            }
            
            // close document
            
            doc.close(completionHandler: { (closeSuccess) in
                guard closeSuccess else {
                    print("Cannot create/close new document")
                    
                    importHandler(nil, .none)
                    return
                }
                
                importHandler(url, .move)
            })
        }
    }

    func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
        print("importing document from \(sourceURL.path) to \(destinationURL.path)")
        
        presentDocument(at: destinationURL)
    }
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
        let alert = UIAlertController(title: "Unable to import document",
                                      message: "error: \(String(describing: error?.localizedDescription))",
                                      preferredStyle: .alert)
        
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        
        alert.addAction(action)
        
        controller.present(alert, animated: true, completion: nil)
    }
    
    // user selected a document
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
        assert(controller.allowsPickingMultipleItems == false)
        assert(!documentURLs.isEmpty)
        assert(documentURLs.count <= 1)
        
        guard let url = documentURLs.first else {
            fatalError(" no url? ")
        }
        
        presentDocument(at: url)
    }
    
    func presentDocument(at url: URL) {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        
        let tempController = storyBoard.instantiateViewController(withIdentifier: "EditorNavigationController")
        
        guard let editorNavigationController = tempController as? EditorNavigationController else {
            fatalError("cannot cast to EditorViewController")
        }
        
        //load editor view
        editorNavigationController.loadViewIfNeeded()
        
        let doc = ShaderDocument(fileURL: url)
        
        editorNavigationController.document = doc
        
        doc.open { [weak self] (success) in
            guard success else {
                fatalError("Unable to open shader file")
            }
            
            self?.present(editorNavigationController, animated: true, completion: nil)
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
{'fraction_non_alphanumeric': 0.07675778918467231, 'fraction_numerical': 0.002626238510206518, 'mean_word_length': 1.9232379623168179, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5005778', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1936, 'n_words': 531}