OSDN Git Service

不要となっていたプロパティを削除
[kcd/KCD.git] / KCD / ScreenshotLoader.swift
1 //
2 //  ScreenshotLoader.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/11/05.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class ScreenshotLoader {
12     
13     let url: URL
14     
15     init(_ url: URL) {
16         
17         self.url = url
18     }
19     
20     func merge(screenshots: [ScreenshotInformation]) -> [ScreenshotInformation] {
21         
22         let urls = screenshotURLs()
23         
24         // なくなっているものを削除
25         let itemWithoutDeleting = screenshots.filter { urls.contains($0.url) }
26         
27         // 新しいものを追加
28         let newItems = urls
29             .filter { url in !itemWithoutDeleting.contains(where: { url == $0.url }) }
30             .map { ScreenshotInformation(url: $0) }
31         
32         return itemWithoutDeleting + newItems
33     }
34     
35     private func screenshotURLs() -> [URL] {
36         
37         guard let files = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) else {
38             
39             Logger.shared.log("can not read list of screenshot directory")
40             
41             return []
42         }
43         
44         return files.filter(isPicture)
45     }
46     
47     private func isPicture(_ url: URL) -> Bool {
48                 
49         guard let r = try? url.resourceValues(forKeys: [.typeIdentifierKey]) else {
50             
51             return false
52         }
53         guard let type = r.typeIdentifier else {
54             
55             return false
56         }
57         
58         return NSImage.imageTypes.contains(type)
59     }
60 }