Lists files in Documents directory, automatically updates when files are added or removed

This commit is contained in:
Riley Testut 2015-03-08 13:35:02 -07:00
parent aeed83fca5
commit 46bf220359
4 changed files with 183 additions and 3 deletions

View File

@ -7,6 +7,7 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
BF46894F1AAC46EF00A2586D /* DirectoryContentsDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF46894E1AAC46EF00A2586D /* DirectoryContentsDataSource.swift */; };
BFFA71DD1AAC406100EE9DD1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */; }; BFFA71DD1AAC406100EE9DD1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */; };
BFFA71DF1AAC406100EE9DD1 /* GamesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFA71DE1AAC406100EE9DD1 /* GamesViewController.swift */; }; BFFA71DF1AAC406100EE9DD1 /* GamesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFA71DE1AAC406100EE9DD1 /* GamesViewController.swift */; };
BFFA71E21AAC406100EE9DD1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFFA71E01AAC406100EE9DD1 /* Main.storyboard */; }; BFFA71E21AAC406100EE9DD1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFFA71E01AAC406100EE9DD1 /* Main.storyboard */; };
@ -15,6 +16,7 @@
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
BF46894E1AAC46EF00A2586D /* DirectoryContentsDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DirectoryContentsDataSource.swift; sourceTree = "<group>"; };
BFFA71D71AAC406100EE9DD1 /* Delta.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Delta.app; sourceTree = BUILT_PRODUCTS_DIR; }; BFFA71D71AAC406100EE9DD1 /* Delta.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Delta.app; sourceTree = BUILT_PRODUCTS_DIR; };
BFFA71DB1AAC406100EE9DD1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; BFFA71DB1AAC406100EE9DD1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@ -35,6 +37,15 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
BF46894D1AAC469800A2586D /* Game Selection */ = {
isa = PBXGroup;
children = (
BFFA71DE1AAC406100EE9DD1 /* GamesViewController.swift */,
BF46894E1AAC46EF00A2586D /* DirectoryContentsDataSource.swift */,
);
name = "Game Selection";
sourceTree = "<group>";
};
BFFA71CE1AAC406100EE9DD1 = { BFFA71CE1AAC406100EE9DD1 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -55,7 +66,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */, BFFA71DC1AAC406100EE9DD1 /* AppDelegate.swift */,
BFFA71DE1AAC406100EE9DD1 /* GamesViewController.swift */, BF46894D1AAC469800A2586D /* Game Selection */,
BFFA71E01AAC406100EE9DD1 /* Main.storyboard */, BFFA71E01AAC406100EE9DD1 /* Main.storyboard */,
BFFA71E31AAC406100EE9DD1 /* Images.xcassets */, BFFA71E31AAC406100EE9DD1 /* Images.xcassets */,
BFFA71E51AAC406100EE9DD1 /* LaunchScreen.xib */, BFFA71E51AAC406100EE9DD1 /* LaunchScreen.xib */,
@ -144,6 +155,7 @@
files = ( files = (
BFFA71DF1AAC406100EE9DD1 /* GamesViewController.swift in Sources */, BFFA71DF1AAC406100EE9DD1 /* GamesViewController.swift in Sources */,
BFFA71DD1AAC406100EE9DD1 /* AppDelegate.swift in Sources */, BFFA71DD1AAC406100EE9DD1 /* AppDelegate.swift in Sources */,
BF46894F1AAC46EF00A2586D /* DirectoryContentsDataSource.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -237,6 +249,7 @@
ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99; GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES; GCC_NO_COMMON_BLOCKS = YES;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNDECLARED_SELECTOR = YES;
@ -289,6 +302,7 @@
BFFA71F81AAC406100EE9DD1 /* Release */, BFFA71F81AAC406100EE9DD1 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
}; };
/* End XCConfigurationList section */ /* End XCConfigurationList section */
}; };

View File

@ -25,7 +25,7 @@
<!--Games--> <!--Games-->
<scene sceneID="JrP-w9-a3P"> <scene sceneID="JrP-w9-a3P">
<objects> <objects>
<tableViewController id="pG3-6q-BDc" sceneMemberID="viewController"> <tableViewController id="pG3-6q-BDc" customClass="GamesViewController" customModule="Delta" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="RlI-ct-T2T"> <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="RlI-ct-T2T">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/> <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>

View File

@ -0,0 +1,121 @@
//
// DirectoryContentsDataSource.swift
// Delta
//
// Created by Riley Testut on 3/8/15.
// Copyright (c) 2015 Riley Testut. All rights reserved.
//
import UIKit
import Foundation
public extension DirectoryContentsDataSource
{
func URLAtIndexPath(indexPath: NSIndexPath) -> NSURL
{
let URL = self.directoryContents[indexPath.row]
return URL
}
}
public class DirectoryContentsDataSource: NSObject
{
public let directoryURL: NSURL
public var tableViewCellIdentifier: String = "Cell"
public var contentsUpdateHandler: (Void -> Void)?
public var cellConfigurationBlock: ((UITableViewCell, NSIndexPath, NSURL) -> Void)?
private let fileDescriptor: Int32
private let directoryMonitorDispatchQueue: dispatch_queue_t
private let directoryMonitorDispatchSource: dispatch_source_t!
private var directoryContents: [NSURL]
required public init?(directoryURL: NSURL)
{
self.directoryURL = directoryURL
self.fileDescriptor = open(self.directoryURL.fileSystemRepresentation, O_EVTONLY)
self.directoryMonitorDispatchQueue = dispatch_queue_create("com.rileytestut.DirectoryContentsDataSource", DISPATCH_QUEUE_SERIAL)
self.directoryMonitorDispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(self.fileDescriptor), DISPATCH_VNODE_WRITE, self.directoryMonitorDispatchQueue)
self.directoryContents = [NSURL]()
super.init()
if self.fileDescriptor < 0
{
return nil
}
if self.directoryMonitorDispatchSource == nil
{
close(self.fileDescriptor);
return nil
}
dispatch_source_set_event_handler(self.directoryMonitorDispatchSource, {
self.didUpdateDirectoryContents()
});
dispatch_source_set_cancel_handler(self.directoryMonitorDispatchSource, {
close(self.fileDescriptor);
});
dispatch_resume(self.directoryMonitorDispatchSource);
self.didUpdateDirectoryContents()
}
deinit
{
if self.fileDescriptor >= 0
{
close(self.fileDescriptor);
}
if self.directoryMonitorDispatchSource != nil
{
dispatch_source_cancel(self.directoryMonitorDispatchSource);
}
}
}
private extension DirectoryContentsDataSource
{
func didUpdateDirectoryContents()
{
var error: NSError? = nil
var contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(self.directoryURL, includingPropertiesForKeys: nil, options:NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants | NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: &error) as! [NSURL]?
if let contents = contents
{
self.directoryContents = contents
}
else
{
println(error?.userInfo)
}
self.contentsUpdateHandler?()
}
}
extension DirectoryContentsDataSource: UITableViewDataSource
{
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.directoryContents.count
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let tableViewCell = tableView.dequeueReusableCellWithIdentifier(self.tableViewCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
self.cellConfigurationBlock?(tableViewCell, indexPath, self.URLAtIndexPath(indexPath))
return tableViewCell
}
}

View File

@ -10,11 +10,56 @@ import UIKit
class GamesViewController: UITableViewController class GamesViewController: UITableViewController
{ {
let directoryContentsDataSource: DirectoryContentsDataSource?
override init(style: UITableViewStyle)
{
let error: NSError? = nil;
let documentsDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
self.directoryContentsDataSource = DirectoryContentsDataSource(directoryURL: documentsDirectoryURL)
super.init(style: style)
}
required init(coder aDecoder: NSCoder)
{
let error: NSError? = nil;
let documentsDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
self.directoryContentsDataSource = DirectoryContentsDataSource(directoryURL: documentsDirectoryURL)
super.init(coder: aDecoder)
}
override func viewDidLoad() override func viewDidLoad()
{ {
super.viewDidLoad() super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.dataSource = self.directoryContentsDataSource
self.directoryContentsDataSource!.contentsUpdateHandler = {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
self.directoryContentsDataSource?.cellConfigurationBlock = { (cell, indexPath, URL) in
cell.textLabel?.text = URL.lastPathComponent
}
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
if self.directoryContentsDataSource == nil
{
let alertController = UIAlertController(title: "Games Directory Invalid", message: "Please ensure the current games directory exists, then restart the app.", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
} }
override func didReceiveMemoryWarning() override func didReceiveMemoryWarning()