From 589baeb27187ae57956be52305cf0a80378c0711 Mon Sep 17 00:00:00 2001 From: Knut Inge Grosland <”hei@knutinge.com”> Date: Tue, 26 May 2015 22:22:43 +0100 Subject: [PATCH] fixes for urlstore folder and Window presenting --- AppReviews/ApplicationWindowController.swift | 4 +-- AppReviews/SearchViewController.swift | 2 +- ReviewManager/ReviewManager.swift | 35 +++++++++++--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/AppReviews/ApplicationWindowController.swift b/AppReviews/ApplicationWindowController.swift index e4e0b49..992da5b 100644 --- a/AppReviews/ApplicationWindowController.swift +++ b/AppReviews/ApplicationWindowController.swift @@ -72,8 +72,8 @@ extension ApplicationWindowController { searchViewController.delegate = self searchViewController.state = .Loading } - - window?.beginSheet(window!) { + + self.window?.beginSheet(window!) { (returnCode: NSModalResponse) in self.searchWindowController = nil diff --git a/AppReviews/SearchViewController.swift b/AppReviews/SearchViewController.swift index 87dd599..8507943 100644 --- a/AppReviews/SearchViewController.swift +++ b/AppReviews/SearchViewController.swift @@ -28,7 +28,7 @@ class SearchViewController: NSViewController { var delegate: SearchViewControllerDelegate? var state: SearchViewControllerState = .Idle { didSet { - switch state { + switch self.state { case .Idle: progressIndicator.stopAnimation(nil) case .Loading: diff --git a/ReviewManager/ReviewManager.swift b/ReviewManager/ReviewManager.swift index 557869e..de66ad6 100644 --- a/ReviewManager/ReviewManager.swift +++ b/ReviewManager/ReviewManager.swift @@ -65,28 +65,31 @@ final class ReviewManager { } func storeURL() -> NSURL { - let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String ?? "App Reviews" var error: NSError? = nil - // ApplicationSupportDirectory - - let applicationSupportDirectory = NSFileManager.defaultManager().URLForDirectory(.DesktopDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: &error) - let appDirectory = applicationSupportDirectory?.URLByAppendingPathComponent(appName, isDirectory: true) - - let success = NSFileManager.defaultManager().createDirectoryAtPath(appDirectory!.URLString, withIntermediateDirectories: true, attributes: nil, error: &error) + var failureReason = "There was an error creating or loading the application's saved data." - if error != nil { - println("createDirectoryAtPath: \(error?.localizedDescription)") + // Make sure the application files directory is there + let propertiesOpt = self.applicationDocumentsDirectory.resourceValuesForKeys([NSURLIsDirectoryKey], error: &error) + if let properties = propertiesOpt { + if !properties[NSURLIsDirectoryKey]!.boolValue { + failureReason = "Expected a folder to store application data, found a file \(self.applicationDocumentsDirectory.path)." + } + } else if error!.code == NSFileReadNoSuchFileError { + error = nil + NSFileManager.defaultManager().createDirectoryAtPath(self.applicationDocumentsDirectory.path!, withIntermediateDirectories: true, attributes: nil, error: &error) } - - let url = appDirectory?.URLByAppendingPathComponent(kSQLiteFileName) - if error != nil { - println("error storeURL: \(error?.localizedDescription)") - } - - return url! + return self.applicationDocumentsDirectory.URLByAppendingPathComponent(kSQLiteFileName) } + lazy var applicationDocumentsDirectory: NSURL = { + let urls = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask) + let appSupportURL = urls[urls.count - 1] as! NSURL + + let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String ?? "App Reviews" + return appSupportURL.URLByAppendingPathComponent(appName) + }() + func modelURL() -> NSURL { return NSBundle.mainBundle().URLForResource("AppReviews", withExtension: "momd")! }