OSDN Git Service

ブックマークをCoreDataで管理するようにした
authormasakih <masakih@users.sourceforge.jp>
Sat, 30 May 2015 06:36:55 +0000 (15:36 +0900)
committermasakih <masakih@users.sourceforge.jp>
Sat, 30 May 2015 06:36:55 +0000 (15:36 +0900)
KCD.xcodeproj/project.pbxproj
KCD/Bookmark.xcdatamodeld/Bookmark.xcdatamodel/contents [new file with mode: 0644]
KCD/HMBookMarkItem.h
KCD/HMBookmarkDataStore.h [new file with mode: 0644]
KCD/HMBookmarkDataStore.m [new file with mode: 0644]
KCD/HMBookmarkItem.m
KCD/HMBookmarkManager.h
KCD/HMBookmarkManager.m
KCD/HMExternalBrowserWindowController.h
KCD/HMExternalBrowserWindowController.m

index eeaa7ee..09ae343 100644 (file)
@@ -59,6 +59,8 @@
                F494DC6D18CC054800AE97E4 /* HMNyukyoDockStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = F494DC6C18CC054800AE97E4 /* HMNyukyoDockStatus.m */; };
                F49546261B1355EC00E0D8D0 /* HMBookmarkItem.m in Sources */ = {isa = PBXBuildFile; fileRef = F49546251B1355EC00E0D8D0 /* HMBookmarkItem.m */; };
                F495462B1B14CBA600E0D8D0 /* HMBookmarkManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F495462A1B14CBA600E0D8D0 /* HMBookmarkManager.m */; };
+               F495462E1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F495462C1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld */; };
+               F49546311B18991300E0D8D0 /* HMBookmarkDataStore.m in Sources */ = {isa = PBXBuildFile; fileRef = F49546301B18991300E0D8D0 /* HMBookmarkDataStore.m */; };
                F49828FC1902A226000D6BBE /* HMStrokeTextFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F49828FB1902A226000D6BBE /* HMStrokeTextFieldCell.m */; };
                F4A2AF521A074DCF0024BD9E /* HMScreenshotListWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A2AF501A074DCF0024BD9E /* HMScreenshotListWindowController.m */; };
                F4A2AF541A074DE40024BD9E /* HMScreenshotListWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4A2AF561A074DE40024BD9E /* HMScreenshotListWindowController.xib */; };
                F49546251B1355EC00E0D8D0 /* HMBookmarkItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMBookmarkItem.m; sourceTree = "<group>"; };
                F49546291B14CBA600E0D8D0 /* HMBookmarkManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMBookmarkManager.h; sourceTree = "<group>"; };
                F495462A1B14CBA600E0D8D0 /* HMBookmarkManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMBookmarkManager.m; sourceTree = "<group>"; };
+               F495462D1B1894DD00E0D8D0 /* Bookmark.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Bookmark.xcdatamodel; sourceTree = "<group>"; };
+               F495462F1B18991300E0D8D0 /* HMBookmarkDataStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMBookmarkDataStore.h; sourceTree = "<group>"; };
+               F49546301B18991300E0D8D0 /* HMBookmarkDataStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMBookmarkDataStore.m; sourceTree = "<group>"; };
                F49828FA1902A226000D6BBE /* HMStrokeTextFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMStrokeTextFieldCell.h; sourceTree = "<group>"; };
                F49828FB1902A226000D6BBE /* HMStrokeTextFieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMStrokeTextFieldCell.m; sourceTree = "<group>"; };
                F4A2AF4F1A074DCF0024BD9E /* HMScreenshotListWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMScreenshotListWindowController.h; sourceTree = "<group>"; };
                                F4AA05F9192B92C700844688 /* Temporary.xcdatamodeld */,
                                F4AA05FC192B932E00844688 /* HMTemporaryDataStore.h */,
                                F4AA05FD192B932E00844688 /* HMTemporaryDataStore.m */,
+                               F495462C1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld */,
+                               F495462F1B18991300E0D8D0 /* HMBookmarkDataStore.h */,
+                               F49546301B18991300E0D8D0 /* HMBookmarkDataStore.m */,
                        );
                        name = CoreData;
                        sourceTree = "<group>";
                                F4B3DC5218F150CD0011B4FC /* HMScreenshotWindowController.m in Sources */,
                                F426802E194D0FFD00B2C27F /* HMMemberShip3Command.m in Sources */,
                                F4B37EF319FBBBED002F371A /* HMRemodelSlotCommand.m in Sources */,
+                               F495462E1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld in Sources */,
                                F4B414FB18B1FB6C00468DAF /* HMMasterMapCellCommand.m in Sources */,
                                F4B3DC2E18E07B680011B4FC /* HMHistoryWindowController.m in Sources */,
                                F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */,
                                F4B1E30819C73B6A00805879 /* HMSuppliesCell.m in Sources */,
                                F4A3AD8E19BB220000FDBF80 /* HMMaskInfomation.m in Sources */,
                                F4D6BCBA1AA32BEA00742FEB /* HMFleetViewController.m in Sources */,
+                               F49546311B18991300E0D8D0 /* HMBookmarkDataStore.m in Sources */,
                                F4E7802E18DC3AF00011BC4C /* LocalData.xcdatamodeld in Sources */,
                                F4D6BCAD1AA2A60900742FEB /* HMSokuTransformer.m in Sources */,
                                F4BEE4731AA5E34600979752 /* HMSlotItemFrameView.m in Sources */,
                        sourceTree = "<group>";
                        versionGroupType = wrapper.xcdatamodel;
                };
+               F495462C1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld */ = {
+                       isa = XCVersionGroup;
+                       children = (
+                               F495462D1B1894DD00E0D8D0 /* Bookmark.xcdatamodel */,
+                       );
+                       currentVersion = F495462D1B1894DD00E0D8D0 /* Bookmark.xcdatamodel */;
+                       path = Bookmark.xcdatamodeld;
+                       sourceTree = "<group>";
+                       versionGroupType = wrapper.xcdatamodel;
+               };
                F4AA05F9192B92C700844688 /* Temporary.xcdatamodeld */ = {
                        isa = XCVersionGroup;
                        children = (
diff --git a/KCD/Bookmark.xcdatamodeld/Bookmark.xcdatamodel/contents b/KCD/Bookmark.xcdatamodeld/Bookmark.xcdatamodel/contents
new file mode 100644 (file)
index 0000000..64091e5
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="7701" systemVersion="14D136" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
+    <entity name="Bookmark" representedClassName="HMBookmarkItem" syncable="YES">
+        <attribute name="canResize" attributeType="Boolean" syncable="YES"/>
+        <attribute name="canScroll" attributeType="Boolean" syncable="YES"/>
+        <attribute name="contentVisibleRectString" attributeType="String" syncable="YES"/>
+        <attribute name="identifier" attributeType="String" indexed="YES" syncable="YES"/>
+        <attribute name="name" attributeType="String" syncable="YES"/>
+        <attribute name="order" attributeType="Integer 32" defaultValueString="100000" syncable="YES"/>
+        <attribute name="scrollDelayValue" optional="YES" attributeType="Float" defaultValueString="0.0" syncable="YES"/>
+        <attribute name="urlString" attributeType="String" syncable="YES"/>
+        <attribute name="windowContentSizeString" attributeType="String" syncable="YES"/>
+    </entity>
+    <elements>
+        <element name="Bookmark" positionX="-63" positionY="-18" width="128" height="180"/>
+    </elements>
+</model>
\ No newline at end of file
index dbff004..00005a8 100644 (file)
@@ -8,7 +8,7 @@
 
 #import <Foundation/Foundation.h>
 
-@interface HMBookmarkItem : NSObject <NSCoding>
+@interface HMBookmarkItem : NSManagedObject
 
 @property (copy, nonatomic) NSString *identifier;
 @property (copy, nonatomic) NSString *name;
@@ -18,6 +18,8 @@
 @property BOOL canResize;
 @property BOOL canScroll;
 
+@property (strong, nonatomic) NSNumber *order;
+
 /// contentVisibleRectに移動するまでの遅延時間
 @property NSTimeInterval scrollDelay;
 
diff --git a/KCD/HMBookmarkDataStore.h b/KCD/HMBookmarkDataStore.h
new file mode 100644 (file)
index 0000000..107ef12
--- /dev/null
@@ -0,0 +1,13 @@
+//
+//  HMBookmarkDataStore.h
+//  KCD
+//
+//  Created by Hori,Masaki on 2015/05/29.
+//  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMCoreDataManager.h"
+
+@interface HMBookmarkDataStore : HMCoreDataManager
+
+@end
diff --git a/KCD/HMBookmarkDataStore.m b/KCD/HMBookmarkDataStore.m
new file mode 100644 (file)
index 0000000..9b2271d
--- /dev/null
@@ -0,0 +1,46 @@
+//
+//  HMBookmarkDataStore.m
+//  KCD
+//
+//  Created by Hori,Masaki on 2015/05/29.
+//  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMBookmarkDataStore.h"
+
+@implementation HMBookmarkDataStore
+
++ (void)load
+{
+       static dispatch_once_t onceToken;
+       dispatch_once(&onceToken, ^{
+               [self defaultManager];
+       });
+}
+- (NSString *)modelName
+{
+       return @"Bookmark";
+}
+- (NSString *)storeFileName
+{
+       return @"Bookmark.storedata";
+}
+- (NSString *)storeType
+{
+       return NSSQLiteStoreType;
+}
+- (NSDictionary *)storeOptions
+{
+       NSDictionary *options = @{
+                                                         NSSQLitePragmasOption : @{@"journal_mode" : @"MEMORY"},
+                                                         NSMigratePersistentStoresAutomaticallyOption : @YES,
+                                                         NSInferMappingModelAutomaticallyOption : @YES
+                                                         };
+       return options;
+}
+- (BOOL)deleteAndRetry
+{
+       return NO;
+}
+
+@end
index d0be610..f72d498 100644 (file)
@@ -8,43 +8,50 @@
 
 #import "HMBookmarkItem.h"
 
-static NSString *HMBMIdentifireKey = @"HMBMIdentifireKey";
-static NSString *HMBMNameKey = @"HMBMNameKey";
-static NSString *HMBMURLStringKey = @"HMBMURLStringKey";
-static NSString *HMBMWindowContentSizeKey = @"HMBMWindowContentSizeKey";
-static NSString *HMBMContentVisibleRectKey = @"HMBMContentVisibleRectKey";
-static NSString *HMBMCanResizeKey = @"HMBMCanResizeKey";
-static NSString *HMBMCanScrollKey = @"HMBMCanScrollKey";
-static NSString *HMBMScrollDelay = @"HMBMScrollDelay";
+@interface HMBookmarkItem ()
+@property (strong, nonatomic) NSString *windowContentSizeString;
+@property (strong, nonatomic) NSString *contentVisibleRectString;
+@property (strong, nonatomic) NSNumber *scrollDelayValue;
+@end
 
 @implementation HMBookmarkItem
 
-- (void)encodeWithCoder:(NSCoder *)aCoder
+@dynamic identifier;
+@dynamic name;
+@dynamic urlString;
+@dynamic canScroll;
+@dynamic canResize;
+@dynamic windowContentSizeString;
+@dynamic contentVisibleRectString;
+@dynamic order;
+@dynamic scrollDelayValue;
+
+
+- (void)setWindowContentSize:(NSSize)windowContentSize
+{
+       self.windowContentSizeString = NSStringFromSize(windowContentSize);
+}
+- (NSSize)windowContentSize
+{
+       return NSSizeFromString(self.windowContentSizeString);
+}
+- (void)setContentVisibleRect:(NSRect)contentVisibleRect
+{
+       self.contentVisibleRectString = NSStringFromRect(contentVisibleRect);
+}
+- (NSRect)contentVisibleRect
 {
-       [aCoder encodeObject:self.identifier forKey:HMBMIdentifireKey];
-       [aCoder encodeObject:self.name forKey:HMBMNameKey];
-       [aCoder encodeObject:self.urlString forKey:HMBMURLStringKey];
-       [aCoder encodeSize:self.windowContentSize forKey:HMBMWindowContentSizeKey];
-       [aCoder encodeRect:self.contentVisibleRect forKey:HMBMContentVisibleRectKey];
-       [aCoder encodeBool:self.canResize forKey:HMBMCanResizeKey];
-       [aCoder encodeBool:self.canScroll forKey:HMBMCanScrollKey];
-       [aCoder encodeDouble:self.scrollDelay forKey:HMBMScrollDelay];
+       return NSRectFromString(self.contentVisibleRectString);
 }
-- (id)initWithCoder:(NSCoder *)aDecoder
+- (void)setScrollDelay:(NSTimeInterval)scrollDelay
 {
-       self = [super init];
-       if(self) {
-               _identifier = [aDecoder decodeObjectForKey:HMBMIdentifireKey];
-               _name = [aDecoder decodeObjectForKey:HMBMNameKey];
-               _urlString = [aDecoder decodeObjectForKey:HMBMURLStringKey];
-               _windowContentSize = [aDecoder decodeSizeForKey:HMBMWindowContentSizeKey];
-               _contentVisibleRect = [aDecoder decodeRectForKey:HMBMContentVisibleRectKey];
-               _canResize = [aDecoder decodeBoolForKey:HMBMCanResizeKey];
-               _canScroll = [aDecoder decodeBoolForKey:HMBMCanScrollKey];
-               _scrollDelay = [aDecoder decodeDoubleForKey:HMBMScrollDelay];
-       }
-       return self;
+       self.scrollDelayValue = [NSNumber numberWithDouble:scrollDelay];
 }
+- (NSTimeInterval)scrollDelay
+{
+       return self.scrollDelayValue.doubleValue;
+}
+
 
 - (id)description
 {
@@ -56,7 +63,8 @@ static NSString *HMBMScrollDelay = @"HMBMScrollDelay";
                        @"contentVisibleRect -> %@,\n"
                        @"canResize -> %@,\n"
                        @"canScroll -> %@,\n"
-                       @"scrollDelay -> %lf}",
+                       @"scrollDelay -> %lf,\n"
+                       @"order -> %@}",
                        self.identifier,
                        self.name,
                        self.urlString,
@@ -64,7 +72,8 @@ static NSString *HMBMScrollDelay = @"HMBMScrollDelay";
                        NSStringFromRect(self.contentVisibleRect),
                        self.canResize ? @"YES" : @"NO",
                        self.canScroll ? @"YES" : @"NO",
-                       self.scrollDelay];
+                       self.scrollDelay,
+                       self.order];
 }
 
 @end
index f702b66..b8b6617 100644 (file)
 
 + (instancetype)sharedManager;
 
-@property (readonly) NSUInteger count;
-- (HMBookmarkItem *)bookmarkAtIndex:(NSUInteger)index;
-
-- (void)addBookmark:(HMBookmarkItem *)item;
-- (void)insertBookmark:(HMBookmarkItem *)item atIndex:(NSUInteger)index;
-- (void)removeBookmark:(HMBookmarkItem *)item;
-- (void)removeBookmarkAtIndex:(NSUInteger)index;
-- (void)replaceBookmarkAtIndex:(NSUInteger)index withBookmark:(HMBookmarkItem *)item;
+@property (readonly) NSManagedObjectContext *manageObjectContext;
+- (HMBookmarkItem *)createNewBookmark;
 
 @end
 
 @interface NSObject (HMBookmarkMenu)
 - (IBAction)selectBookmark:(id)sender;
-@end
\ No newline at end of file
+@end
index 15e3823..8797045 100644 (file)
@@ -8,6 +8,8 @@
 
 #import "HMBookmarkManager.h"
 
+#import "HMBookmarkDataStore.h"
+
 
 const NSUInteger kBookmarkMenuItemTag = 5000;
 
@@ -17,7 +19,10 @@ static NSMenu *bookmarkMenu = nil;
 
 
 @interface HMBookmarkManager () <NSMenuDelegate>
-@property (strong, nonatomic) NSMutableArray *realBookmarks;
+
+@property (strong, nonatomic) NSArrayController *bookmarksController;
+
+@property (readonly) HMBookmarkDataStore *editorStore;
 @end
 
 @implementation HMBookmarkManager
@@ -43,11 +48,25 @@ static NSMenu *bookmarkMenu = nil;
 {
        self = [super init];
        if(self) {
-               _realBookmarks = [NSMutableArray new];
+               _bookmarksController = [[NSArrayController alloc] initWithContent:nil];
+               _bookmarksController.managedObjectContext = self.manageObjectContext;
+               _bookmarksController.entityName = @"Bookmark";
+               
+               NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"order"
+                                                                                                                                  ascending:NO];
+               _bookmarksController.sortDescriptors = @[sortDesc];
+               
+               
+               [NSTimer scheduledTimerWithTimeInterval:0.1
+                                                                                target:self
+                                                                          selector:@selector(storeData:)
+                                                                          userInfo:nil
+                                                                               repeats:YES];
        }
        return self;
 }
 
+
 - (void)applicationDidFinishLaunching:(NSNotification *)notification
 {
        NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
@@ -69,9 +88,10 @@ static NSMenu *bookmarkMenu = nil;
                [bookmarkMenu removeItemAtIndex:i];
        }
        
-       NSInteger bookmarkNum = self.count;
+       NSArray *bookmarksArray = self.bookmarks;
+       NSInteger bookmarkNum = bookmarksArray.count;
        for(NSInteger i = 0; i < bookmarkNum; i++) {
-               HMBookmarkItem *item = self.bookmarks[i];
+               HMBookmarkItem *item = bookmarksArray[i];
                NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:item.name
                                                                                                                 action:@selector(selectBookmark:)
                                                                                                  keyEquivalent:@""];
@@ -80,43 +100,44 @@ static NSMenu *bookmarkMenu = nil;
        }
 }
 
-- (NSArray *)bookmarks
-{
-       return [self.realBookmarks copy];
-}
-
-- (NSUInteger)count
-{
-       return self.realBookmarks.count;
-}
-- (HMBookmarkItem *)bookmarkAtIndex:(NSUInteger)index
+- (NSManagedObjectContext *)manageObjectContext
 {
-       return [self.realBookmarks objectAtIndex:index];
+       return [[HMBookmarkDataStore defaultManager] managedObjectContext];
 }
-
-- (void)addBookmark:(HMBookmarkItem *)item
+- (HMBookmarkDataStore *)editorStore
 {
-       [self.realBookmarks addObject:item];
+       static HMBookmarkDataStore *_store = nil;
+       if(_store) return _store;
        
-       if(!item.identifier) {
-               item.identifier = [NSString stringWithFormat:@"hogehogheo%@", [NSDate date]];
-       }
+       _store = [HMBookmarkDataStore oneTimeEditor];
+       return _store;
 }
-- (void)insertBookmark:(HMBookmarkItem *)item atIndex:(NSUInteger)index
+- (void)storeData:(id)timer
 {
-       [self.realBookmarks insertObject:item atIndex:index];
-}
-- (void)removeBookmark:(HMBookmarkItem *)item
-{
-       [self.realBookmarks removeObject:item];
+       HMBookmarkDataStore *store = self.editorStore;
+       NSManagedObjectContext *context = store.managedObjectContext;
+       if(context.hasChanges) {
+               [store saveAction:nil];
+       }
 }
-- (void)removeBookmarkAtIndex:(NSUInteger)index
+- (HMBookmarkItem *)createNewBookmark
 {
-       [self.realBookmarks removeObjectAtIndex:index];
+       NSNumber *maxOrder = [self.bookmarksController valueForKeyPath:@"arrangedObjects.@max.order"];
+       
+       HMBookmarkItem *object = [NSEntityDescription insertNewObjectForEntityForName:@"Bookmark"
+                                                                                                                  inManagedObjectContext:self.editorStore.managedObjectContext];
+       object.identifier = [NSString stringWithFormat:@"HMBM%@", [NSDate date]];
+       object.order = @(maxOrder.integerValue + 1);
+
+       return object;
 }
-- (void)replaceBookmarkAtIndex:(NSUInteger)index withBookmark:(HMBookmarkItem *)item
+
+- (NSArray *)bookmarks
 {
-       [self.realBookmarks replaceObjectAtIndex:index withObject:item];
+       [self.bookmarksController fetch:nil];
+       NSArray *array = self.bookmarksController.arrangedObjects;
+       return array;
 }
 
+
 @end
index 15c2998..9405bc0 100644 (file)
@@ -15,6 +15,8 @@
 
 @property (nonatomic, weak) IBOutlet WebView *webView;
 
+@property (readonly) NSManagedObjectContext *managedObjectContext;
+
 @property (nonatomic) NSString *urlString;
 @property (nonatomic) NSSize windowContentSize;
 @property (readonly) NSRect contentVisibleRect;
index dd2d18e..402660b 100644 (file)
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
 }
 
+- (NSManagedObjectContext *)managedObjectContext
+{
+       return [[HMBookmarkManager sharedManager] manageObjectContext];
+}
+
 static BOOL sameState(BOOL a, BOOL b) {
        if(a && b) {
                return YES;
@@ -195,7 +200,7 @@ static BOOL sameState(BOOL a, BOOL b) {
 - (IBAction)addBookmark:(id)sender
 {
        HMBookmarkManager *bookmarkManager = [HMBookmarkManager sharedManager];
-       HMBookmarkItem *bookmark = [HMBookmarkItem new];
+       HMBookmarkItem *bookmark = [bookmarkManager createNewBookmark];
        bookmark.name = self.window.title;
        bookmark.urlString = self.webView.mainFrameURL;
        bookmark.windowContentSize = self.windowContentSize;
@@ -203,8 +208,6 @@ static BOOL sameState(BOOL a, BOOL b) {
        bookmark.canResize = self.canResize;
        bookmark.canScroll = self.canScroll;
        bookmark.scrollDelay = 0.5;
-       
-       [bookmarkManager addBookmark:bookmark];
 }
 - (IBAction)editBookmark:(id)sender
 {