5 // Created by Hori,Masaki on 2015/05/27.
6 // Copyright (c) 2015年 Hori,Masaki. All rights reserved.
9 #import "HMBookmarkManager.h"
11 const NSUInteger kBookmarkMenuItemTag = 5000;
12 const NSUInteger kSeparatorItemTag = 9999;
15 static HMBookmarkManager *sharedInstance = nil;
16 static NSMenu *bookmarkMenu = nil;
19 @interface HMBookmarkManager () <NSMenuDelegate>
21 @property (strong, nonatomic) NSArrayController *bookmarksController;
23 @property (readonly) HMBookmarkDataStore *editorStore;
26 @implementation HMBookmarkManager
30 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
31 [nc addObserver:[self sharedManager]
32 selector:@selector(applicationDidFinishLaunching:)
33 name:NSApplicationDidFinishLaunchingNotification
37 + (instancetype)sharedManager
39 if(sharedInstance) return sharedInstance;
41 sharedInstance = [self new];
42 return sharedInstance;
49 _bookmarksController = [[NSArrayController alloc] initWithContent:nil];
50 _bookmarksController.managedObjectContext = self.manageObjectContext;
51 _bookmarksController.entityName = @"Bookmark";
53 NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"order"
55 _bookmarksController.sortDescriptors = @[sortDesc];
58 [NSTimer scheduledTimerWithTimeInterval:0.1
60 selector:@selector(storeData:)
68 - (void)applicationDidFinishLaunching:(NSNotification *)notification
70 NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
71 NSMenuItem *bookmarkMenuItem = [mainMenu itemWithTag:kBookmarkMenuItemTag];
72 bookmarkMenu = [bookmarkMenuItem submenu];
74 [bookmarkMenu setDelegate:self];
76 [self buildBookmarkMenu];
78 - (void)menuNeedsUpdate:(NSMenu*)menu
80 [self buildBookmarkMenu];
82 - (void)buildBookmarkMenu
84 NSInteger itemNum = bookmarkMenu.numberOfItems;
85 for(NSInteger i = itemNum - 1; i > 0; i--) {
86 NSMenuItem *item = [bookmarkMenu itemAtIndex:i];
87 if([item tag] == kSeparatorItemTag) break;
88 [bookmarkMenu removeItemAtIndex:i];
91 NSArray *bookmarksArray = self.bookmarks;
92 NSInteger bookmarkNum = bookmarksArray.count;
93 for(NSInteger i = 0; i < bookmarkNum; i++) {
94 HMBookmarkItem *item = bookmarksArray[i];
95 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:item.name
96 action:@selector(selectBookmark:)
98 newItem.representedObject = item;
99 [bookmarkMenu addItem:newItem];
103 - (NSManagedObjectContext *)manageObjectContext
105 return [[HMBookmarkDataStore defaultManager] managedObjectContext];
107 - (HMBookmarkDataStore *)editorStore
109 static HMBookmarkDataStore *_store = nil;
110 if(_store) return _store;
112 _store = [HMBookmarkDataStore oneTimeEditor];
115 - (void)storeData:(id)timer
117 HMBookmarkDataStore *store = self.editorStore;
118 NSManagedObjectContext *context = store.managedObjectContext;
119 if(context.hasChanges) {
120 [store saveAction:nil];
123 - (HMBookmarkItem *)createNewBookmark
125 NSNumber *maxOrder = [self.bookmarksController valueForKeyPath:@"arrangedObjects.@max.order"];
127 HMBookmarkItem *object = [NSEntityDescription insertNewObjectForEntityForName:@"Bookmark"
128 inManagedObjectContext:self.editorStore.managedObjectContext];
129 object.identifier = [NSString stringWithFormat:@"HMBM%@", [NSDate date]];
130 object.order = @(maxOrder.integerValue + 100);
135 - (NSArray *)bookmarks
137 [self.bookmarksController fetch:nil];
138 NSArray *array = self.bookmarksController.arrangedObjects;