diff --git a/examples/Sample/Sample/Base.lproj/Main.storyboard b/examples/Sample/Sample/Base.lproj/Main.storyboard index e6136bb6d2ecfacd0ae47f724364147c29dd8044..9e3617ccace37d1a019feb04a9b256df3af6d803 100644 --- a/examples/Sample/Sample/Base.lproj/Main.storyboard +++ b/examples/Sample/Sample/Base.lproj/Main.storyboard @@ -42,9 +42,21 @@ + + + + + + + + + + + + - + @@ -68,6 +80,8 @@ + + diff --git a/examples/Sample/Sample/FileBrowser.swift b/examples/Sample/Sample/FileBrowser.swift index 4b0c319827bbbf771c5b6a46e6822b9c3ff8c260..e47ef37e22b03d064972e32f986a9c51676d6641 100644 --- a/examples/Sample/Sample/FileBrowser.swift +++ b/examples/Sample/Sample/FileBrowser.swift @@ -7,52 +7,68 @@ // import UIKit +import Zip -class FileBrowser: UIViewController { +class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate { + // IBOutlets @IBOutlet weak var tableView: UITableView! - @IBOutlet weak var selectionCounter: UIBarButtonItem! + @IBOutlet weak var zipButton: UIBarButtonItem! + @IBOutlet weak var unzipButton: UIBarButtonItem! let fileManager = NSFileManager.defaultManager() - var path: NSURL? { didSet { - if let filePath = path { - var tempFiles = [String]() - do { - self.title = filePath.lastPathComponent - tempFiles = try self.fileManager.contentsOfDirectoryAtPath(filePath.path!) - } catch { - if path == "/System" { - tempFiles = ["Library"] - } - if path == "/Library" { - tempFiles = ["Preferences"] - } - if path == "/var" { - tempFiles = ["mobile"] - } - if path == "/usr" { - tempFiles = ["lib", "libexec", "bin"] - } - - } - self.files = tempFiles.sort(){$0 < $1} - } + updateFiles() } } + var files = [String]() + var selectedFiles = [String]() + + //MARK: Lifecycle + override func viewDidLoad() { if self.path == nil { let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL self.path = documentsUrl } + updateSelection() + } + + //MARK: File manager + + func updateFiles() { + if let filePath = path { + var tempFiles = [String]() + do { + self.title = filePath.lastPathComponent + tempFiles = try self.fileManager.contentsOfDirectoryAtPath(filePath.path!) + } catch { + if path == "/System" { + tempFiles = ["Library"] + } + if path == "/Library" { + tempFiles = ["Preferences"] + } + if path == "/var" { + tempFiles = ["mobile"] + } + if path == "/usr" { + tempFiles = ["lib", "libexec", "bin"] + } + } + self.files = tempFiles.sort(){$0 < $1} + tableView.reloadData() + } } + //MARK: UITableView Data Source and Delegate + func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } @@ -70,7 +86,9 @@ class FileBrowser: UIViewController { guard let path = path else { return cell } - let newPath = path.URLByAppendingPathComponent(files[indexPath.row]).path! + cell.selectionStyle = .None + let filePath = files[indexPath.row] + let newPath = path.URLByAppendingPathComponent(filePath).path! var isDirectory: ObjCBool = false fileManager.fileExistsAtPath(newPath, isDirectory: &isDirectory) cell.textLabel?.text = files[indexPath.row] @@ -80,12 +98,69 @@ class FileBrowser: UIViewController { else { cell.imageView?.image = UIImage(named: "File") } + cell.backgroundColor = (selectedFiles.contains(filePath)) ? UIColor(white: 0.9, alpha: 1.0):UIColor.whiteColor() return cell } - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) + func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + let filePath = files[indexPath.row] + if let index = selectedFiles.indexOf(filePath) where selectedFiles.contains(filePath) { + selectedFiles.removeAtIndex(index) + } + else { + selectedFiles.append(filePath) + } + updateSelection() + } + + func updateSelection() { + tableView.reloadData() + selectionCounter.title = "\(selectedFiles.count) Selected" + + zipButton.enabled = (selectedFiles.count > 0) + if (selectedFiles.count == 1) { + let filePath = selectedFiles.first + let pathExtension = path!.URLByAppendingPathComponent(filePath!).pathExtension + if pathExtension == "zip" { + unzipButton.enabled = true + } + else { + unzipButton.enabled = false + } + } + else { + unzipButton.enabled = false + } } + //MARK: Actions + + @IBAction func unzipSelection(sender: AnyObject) { + let filePath = selectedFiles.first + let pathURL = path!.URLByAppendingPathComponent(filePath!) + do { + try Zip().quickUnzipFile(pathURL) + self.selectedFiles.removeAll() + updateSelection() + updateFiles() + } catch { + print("ERROR") + } + } + + @IBAction func zipSelection(sender: AnyObject) { + var urlPaths = [NSURL]() + for filePath in selectedFiles { + urlPaths.append(path!.URLByAppendingPathComponent(filePath)) + } + do { + try Zip().quickZipFiles(urlPaths, fileName: "Archive") + self.selectedFiles.removeAll() + updateSelection() + updateFiles() + } catch { + print("ERROR") + } + } }