105 lines
3.4 KiB
Swift
105 lines
3.4 KiB
Swift
//
|
||
// ViewController.swift
|
||
// LightshowManager
|
||
//
|
||
// Created by Quentin Millardet on 20/04/2024.
|
||
//
|
||
|
||
import UIKit
|
||
import os
|
||
|
||
class ViewController: UIViewController, UIDocumentPickerDelegate {
|
||
|
||
var startingDirectory : URL?
|
||
var model : LightshowModel?
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
model = (UIApplication.shared.delegate as! AppDelegate).model
|
||
// Do any additional setup after loading the view.
|
||
}
|
||
|
||
|
||
@IBAction func selectFolder(_ sender: Any) {
|
||
print("ici")
|
||
// Create a document picker for directories.
|
||
let documentPicker =
|
||
UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
|
||
documentPicker.delegate = self
|
||
|
||
|
||
// Set the initial directory.
|
||
documentPicker.directoryURL = startingDirectory
|
||
|
||
|
||
// Present the document picker.
|
||
present(documentPicker, animated: true, completion: nil)
|
||
}
|
||
|
||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
|
||
// Start accessing a security-scoped resource.
|
||
guard url.startAccessingSecurityScopedResource() else {
|
||
// Handle the failure here.
|
||
return
|
||
}
|
||
|
||
|
||
// Make sure you release the security-scoped resource when you finish.
|
||
defer { url.stopAccessingSecurityScopedResource() }
|
||
|
||
|
||
// Use file coordination for reading and writing any of the URL’s content.
|
||
var error: NSError? = nil
|
||
|
||
var teslacamFolder = false;
|
||
NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { (url) in
|
||
|
||
let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
|
||
|
||
// Get an enumerator for the directory's content.
|
||
guard let fileList =
|
||
FileManager.default.enumerator(at: url, includingPropertiesForKeys: keys) else {
|
||
Swift.debugPrint("*** Unable to access the contents of \(url.path) ***\n")
|
||
return
|
||
}
|
||
|
||
model?.resetStundent()
|
||
|
||
for case let file as URL in fileList {
|
||
|
||
if (file.lastPathComponent.lowercased().contains("teslacam")){
|
||
print("ici")
|
||
teslacamFolder = true
|
||
}
|
||
|
||
|
||
|
||
let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "network")
|
||
logger.debug("chosen file: \(file.relativeString)")
|
||
|
||
let filename = file.lastPathComponent;
|
||
model?.setStudentAtIndex(model?.getStudentCount() ?? 0, withdraw: filename )
|
||
|
||
// Start accessing the content's security-scoped URL.
|
||
guard file.startAccessingSecurityScopedResource() else {
|
||
logger.error("Not accessing Security Scoped Ressource")
|
||
// Handle the failure here.
|
||
continue
|
||
}
|
||
|
||
|
||
// Do something with the file here.
|
||
// Swift.debugPrint("chosen file: \(file.lastPathComponent)")
|
||
|
||
// Make sure you release the security-scoped resource when you finish.
|
||
file.stopAccessingSecurityScopedResource()
|
||
}
|
||
}
|
||
|
||
(UIApplication.shared.delegate as! AppDelegate).teslacamFolder = teslacamFolder
|
||
}
|
||
|
||
|
||
}
|
||
|