OSDN Git Service

分散していたスクリーンショットディレクトリを表すプロパティを集約した
authormasakih <masakih@users.sourceforge.jp>
Tue, 14 Aug 2018 10:17:50 +0000 (19:17 +0900)
committermasakih <masakih@users.sourceforge.jp>
Tue, 14 Aug 2018 10:17:50 +0000 (19:17 +0900)
KCD/AppDelegate.swift
KCD/ApplicationDirecroriesExtension.swift
KCD/GameViewController.swift
KCD/PreferencePanelController.swift
KCD/ScreenshotListViewController.swift
KCD/ScreenshotListWindowController.swift

index 4393984..b484861 100644 (file)
@@ -37,23 +37,6 @@ final class AppDelegate: NSObject {
     private var updaters: [() -> Void] = []
     private var didLoadedMainMenu = false
     
-    var screenShotSaveDirectory: String {
-        
-        get {
-            
-            return UserDefaults.standard[.screenShotSaveDirectory] ?? ApplicationDirecrories.pictures.path
-        }
-        set {
-            
-            UserDefaults.standard[.screenShotSaveDirectory] = newValue
-        }
-    }
-    
-    var screenShotSaveDirectoryURL: URL {
-        
-        return URL(fileURLWithPath: screenShotSaveDirectory)
-    }
-    
     @objc dynamic var monospaceSystemFont11: NSFont {
         
         return NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular)
index cbab409..27b0e5a 100644 (file)
@@ -8,33 +8,23 @@
 
 extension ApplicationDirecrories {
     
-    static let screenshotSaveDirectoryURL: URL = {
+    var screenshotSaveDirectoryURL: URL {
         
-        let parentURL = URL(fileURLWithPath: AppDelegate.shared.screenShotSaveDirectory)
+        let parentURL = URL(fileURLWithPath: screenShotSaveDirectory)
         let url = parentURL.appendingPathComponent(localizedAppName())
-        let fm = FileManager.default
-        var isDir: ObjCBool = false
         
-        do {
-            
-            if !fm.fileExists(atPath: url.path, isDirectory: &isDir) {
-                
-                try fm.createDirectory(at: url, withIntermediateDirectories: false)
-                
-            } else if !isDir.boolValue {
-                
-                print("\(url) is regular file, not direcory.")
-                
-                return parentURL
-            }
-            
-        } catch {
-            
-            print("Can not create screenshot save directory.")
-            
-            return parentURL
-        }
+        guard checkDirectory(url, create: true) else { return parentURL }
         
         return url
-    }()
+    }
+    
+    var screenShotSaveDirectory: String {
+        
+        return UserDefaults.standard[.screenShotSaveDirectory] ?? ApplicationDirecrories.pictures.path
+    }
+    
+    func setScreenshotDirectory(_ newValue: String) {
+        
+        UserDefaults.standard[.screenShotSaveDirectory] = newValue
+    }
 }
index 6ca6066..a87dc7b 100644 (file)
@@ -128,7 +128,7 @@ final class GameViewController: NSViewController {
         
         webView.cacheDisplay(in: frame, to: rep)
         
-        ScreenshotRegister(ApplicationDirecrories.screenshotSaveDirectoryURL)
+        ScreenshotRegister(ApplicationDirecrories.shared.screenshotSaveDirectoryURL)
             .registerScreenshot(rep, name: localizedAppName())
     }
     
@@ -191,7 +191,7 @@ final class GameViewController: NSViewController {
             rep.size = NSSize(width: 800, height: 480)
         }
         
-        ScreenshotRegister(ApplicationDirecrories.screenshotSaveDirectoryURL)
+        ScreenshotRegister(ApplicationDirecrories.shared.screenshotSaveDirectoryURL)
             .registerScreenshot(rep, name: localizedAppName())
     }
     
index 5e6ab21..5dd6790 100644 (file)
@@ -40,10 +40,10 @@ final class PreferencePanelController: NSWindowController {
     
     private(set) var screenShotSaveDirectory: String {
         
-        get { return AppDelegate.shared.screenShotSaveDirectory }
+        get { return ApplicationDirecrories.shared.screenShotSaveDirectory }
         set {
             
-            AppDelegate.shared.screenShotSaveDirectory = newValue
+            ApplicationDirecrories.shared.setScreenshotDirectory(newValue)
             
             let index = screenShotSaveDirectoryPopUp
                 .indexOfItem(withTag: ScreenshotSaveDirectoryPopupMenuItemTag.saveDirectory.rawValue)
@@ -67,7 +67,7 @@ final class PreferencePanelController: NSWindowController {
         
         super.windowDidLoad()
         
-        screenShotSaveDirectory = AppDelegate.shared.screenShotSaveDirectory
+        screenShotSaveDirectory = ApplicationDirecrories.shared.screenShotSaveDirectory
         
         guard let window = window else {
             
index 1a5bc1e..09f080c 100644 (file)
@@ -62,36 +62,6 @@ final class ScreenshotListViewController: NSViewController {
         return name
     }
     
-    private var screenshotSaveDirectoryURL: URL {
-        
-        let parentURL = URL(fileURLWithPath: AppDelegate.shared.screenShotSaveDirectory)
-        let url = parentURL.appendingPathComponent(dirName)
-        let fm = FileManager.default
-        var isDir: ObjCBool = false
-        
-        do {
-            
-            if !fm.fileExists(atPath: url.path, isDirectory: &isDir) {
-                
-                try fm.createDirectory(at: url, withIntermediateDirectories: false)
-                
-            } else if !isDir.boolValue {
-                
-                Logger.shared.log("\(url) is regular file, not direcory.")
-                
-                return parentURL
-            }
-            
-        } catch {
-            
-            Logger.shared.log("Can not create screenshot save directory.")
-            
-            return parentURL
-        }
-        
-        return url
-    }
-    
     var indexPathsOfItemsBeingDragged: Set<IndexPath>?
     
     // MARK: - Function
@@ -212,7 +182,8 @@ final class ScreenshotListViewController: NSViewController {
         
         Future<[ScreenshotInformation]> {
             
-            ScreenshotLoader(self.screenshotSaveDirectoryURL).merge(screenshots: [])
+            ScreenshotLoader(ApplicationDirecrories.shared.screenshotSaveDirectoryURL)
+                .merge(screenshots: [])
             
             }
             .onSuccess { screenshots in
index 610eef9..ed6a717 100644 (file)
@@ -74,7 +74,7 @@ final class ScreenshotListWindowController: NSWindowController {
     
     func registerScreenshot(_ image: NSBitmapImageRep) {
         
-        ScreenshotRegister(ApplicationDirecrories.screenshotSaveDirectoryURL)
+        ScreenshotRegister(ApplicationDirecrories.shared.screenshotSaveDirectoryURL)
             .registerScreenshot(image, name: localizedAppName())
     }
     
@@ -89,7 +89,7 @@ final class ScreenshotListWindowController: NSWindowController {
                 .flatMap { NSBitmapImageRep(data: $0) }
                 .map {
                     
-                    ScreenshotRegister(ApplicationDirecrories.screenshotSaveDirectoryURL)
+                    ScreenshotRegister(ApplicationDirecrories.shared.screenshotSaveDirectoryURL)
                         .registerScreenshot($0, name: localizedAppName())
             }
         }