OSDN Git Service

コード整形
[kcd/KCD.git] / KCD / BookmarkManager.swift
1 //
2 //  BookmarkManager.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/22.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 fileprivate enum BookmarkMenuTag: Int {
12     
13     case bookmark = 5000
14     case separator = 9999
15 }
16
17 final class BookmarkManager: NSObject, NSMenuDelegate {
18     
19     private static let sharedInstance: BookmarkManager = BookmarkManager()
20     
21     class func shared() -> BookmarkManager {
22         
23         return sharedInstance
24     }
25     
26     private let bookmarksController: NSArrayController
27     
28     private override init() {
29         
30         bookmarksController = NSArrayController()
31         
32         super.init()
33         
34         bookmarksController.managedObjectContext = self.manageObjectContext
35         bookmarksController.entityName = Bookmark.entityName
36         let sort = NSSortDescriptor(key: "order", ascending: true)
37         bookmarksController.sortDescriptors = [sort]
38         
39         let mainMenu = NSApplication.shared().mainMenu
40         let bItem = mainMenu?.item(withTag: BookmarkMenuTag.bookmark.rawValue)
41         bookmarkMenu = bItem?.submenu
42         bookmarkMenu?.delegate = self
43         buildBookmarkMenu()
44     }
45     
46     private(set) var editorStore: BookmarkDataStore = BookmarkDataStore.oneTimeEditor()
47     private var bookmarkMenu: NSMenu!
48     var manageObjectContext = BookmarkDataStore.default.context
49     
50     var bookmarks: [Bookmark] {
51         
52         bookmarksController.fetch(nil)
53         
54         guard let items = bookmarksController.arrangedObjects as? [Bookmark]
55             else { return [] }
56         
57         return items
58     }
59     
60     func createNewBookmark() -> Bookmark? {
61         
62         guard let maxOrder = bookmarksController.value(forKeyPath: "arrangedObjects.@max.order") as? Int
63             else {
64                 print("BookmarkManager: Can no convert max order to Int")
65                 return nil
66         }
67         
68         guard let new = editorStore.createBookmark()
69             else {
70                 print("BookmarkManager: Can not insert BookMarkItem")
71                 return nil
72         }
73         
74         new.identifier = String(format: "B%@", arguments: [NSDate()])
75         new.order = maxOrder + 100
76         
77         DispatchQueue.main.asyncAfter(deadline: .now()) {
78             
79             self.editorStore.save()
80         }
81         
82         return new
83     }
84     
85     func menuNeedsUpdate(_ menu: NSMenu) {
86         
87         buildBookmarkMenu()
88     }
89     
90     private func buildBookmarkMenu() {
91         
92         // TODO: replace to forEach
93         for item in bookmarkMenu.items.reversed() {
94             
95             if item.tag == BookmarkMenuTag.separator.rawValue { break }
96             bookmarkMenu.removeItem(item)
97         }
98         
99         bookmarks.forEach {
100             
101             let item = NSMenuItem(title: $0.name,
102                                   action: #selector(ExternalBrowserWindowController.selectBookmark(_:)),
103                                   keyEquivalent: "")
104             item.representedObject = $0
105             bookmarkMenu.addItem(item)
106         }
107     }
108 }