Skip to content

Commit

Permalink
Tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
dhennessy committed Jan 16, 2016
1 parent 912d919 commit ccc8023
Show file tree
Hide file tree
Showing 234 changed files with 11,684 additions and 0 deletions.
496 changes: 496 additions & 0 deletions Examples/List/List.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Examples/List/List/AppDelegate.h
@@ -0,0 +1,15 @@
//
// AppDelegate.h
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>


@end

26 changes: 26 additions & 0 deletions Examples/List/List/AppDelegate.m
@@ -0,0 +1,26 @@
//
// AppDelegate.m
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import "AppDelegate.h"
#import "Database.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[Database sharedInstance];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}

@end
759 changes: 759 additions & 0 deletions Examples/List/List/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions Examples/List/List/Database.h
@@ -0,0 +1,32 @@
//
// Database.h
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "YapDatabase.h"
#import "YapDatabaseView.h"

extern NSString *const UIDatabaseConnectionWillUpdateNotification;
extern NSString *const UIDatabaseConnectionDidUpdateNotification;
extern NSString *const kNotificationsKey;

@interface Database : NSObject <NSFilePresenter> {
NSFileCoordinator *_fileCoordinator;
}

@property (nonatomic, readonly) NSURL *databaseURL;
@property (nonatomic, readonly) YapDatabase *database;
@property (nonatomic, readonly) YapDatabaseConnection *uiConnection;
@property (nonatomic, readonly) YapDatabaseConnection *bgConnection;

+ (Database *)sharedInstance;
+ (NSString *)newUuid;

- (void)reconnect;
- (void)invalidateCaches;

@end
113 changes: 113 additions & 0 deletions Examples/List/List/Database.m
@@ -0,0 +1,113 @@
//
// Database.m
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import "Database.h"

NSString *const UIDatabaseConnectionWillUpdateNotification = @"UIDatabaseConnectionWillUpdateNotification";
NSString *const UIDatabaseConnectionDidUpdateNotification = @"UIDatabaseConnectionDidUpdateNotification";
NSString *const kNotificationsKey = @"notifications";

static NSOperationQueue *_presentedItemOperationQueue;

@implementation Database

+ (NSString *)newUuid {
return [[NSUUID UUID] UUIDString];
}

+ (Database *)sharedInstance {
static Database *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_presentedItemOperationQueue = [[NSOperationQueue alloc] init];
_sharedInstance = [[Database alloc] init];
});

return _sharedInstance;
}

- (id)init {
if (self = [super init]) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *url = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSURL *folder = [url URLByAppendingPathComponent:bundleID];
[fileManager createDirectoryAtURL:folder withIntermediateDirectories:YES attributes:nil error:NULL];
_databaseURL = [folder URLByAppendingPathComponent:@"List.sqlite"];

[NSFileCoordinator addFilePresenter:self];
_fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
[self openDatabase];
}
return self;
}

#pragma mark - NSFilePresenter

- (NSURL *)presentedItemURL {
return _databaseURL;
}

- (NSOperationQueue *)presentedItemOperationQueue {
return _presentedItemOperationQueue;
}

- (void)invalidateCaches {
[_bgConnection flushMemoryWithFlags:YapDatabaseConnectionFlushMemoryFlags_Caches];
[_uiConnection endLongLivedReadTransaction];
[_uiConnection flushMemoryWithFlags:YapDatabaseConnectionFlushMemoryFlags_Caches];
[_uiConnection beginLongLivedReadTransaction];
}

#pragma mark - Implementation

- (void)openDatabase {
NSError *error = nil;
[_fileCoordinator coordinateWritingItemAtURL:_databaseURL options:0 error:&error byAccessor:^(NSURL *newURL) {
NSLog(@"DB: %@", newURL.path);
_database = [[YapDatabase alloc] initWithPath:newURL.path];
if (!_database) {
NSLog(@"Fatal: unable to create/open database");
}
_bgConnection = [_database newConnection];
_bgConnection.objectCacheLimit = 400;
_bgConnection.metadataCacheEnabled = NO;

_uiConnection = [_database newConnection];
_uiConnection.objectCacheLimit = 400;
_uiConnection.metadataCacheEnabled = NO;
#if DEBUG
_uiConnection.permittedTransactions = YDB_SyncReadTransaction | YDB_MainThreadOnly;
#endif
[_uiConnection enableExceptionsForImplicitlyEndingLongLivedReadTransaction];
[_uiConnection beginLongLivedReadTransaction];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yapDatabaseModified:)
name:YapDatabaseModifiedNotification
object:_database];
}];
}

- (void)reconnect {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_bgConnection = nil;
_uiConnection = nil;
_database = nil;

[self openDatabase];
}

- (void)yapDatabaseModified:(NSNotification *)ignored {
[[NSNotificationCenter defaultCenter] postNotificationName:UIDatabaseConnectionWillUpdateNotification object:self];
NSArray *notifications = [_uiConnection beginLongLivedReadTransaction];
NSDictionary *userInfo = @{ kNotificationsKey : notifications };
[[NSNotificationCenter defaultCenter] postNotificationName:UIDatabaseConnectionDidUpdateNotification object:self userInfo:userInfo];
}


@end
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
34 changes: 34 additions & 0 deletions Examples/List/List/Info.plist
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.peerassembly.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015 Peer Assembly. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
19 changes: 19 additions & 0 deletions Examples/List/List/ViewController.h
@@ -0,0 +1,19 @@
//
// ViewController.h
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController

@property (weak) IBOutlet NSButton *breakfastButton;
@property (weak) IBOutlet NSButton *lunchButton;
@property (weak) IBOutlet NSButton *dinnerButton;
@property (weak) IBOutlet NSTextField *snapshotTextField;

@end

60 changes: 60 additions & 0 deletions Examples/List/List/ViewController.m
@@ -0,0 +1,60 @@
//
// ViewController.m
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import "ViewController.h"
#import "Database.h"

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self configureUI];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(databaseConnectionDidUpdate:)
name:UIDatabaseConnectionDidUpdateNotification
object:nil];
}

- (void)databaseConnectionDidUpdate:(NSNotification *)notification {
NSLog(@"databaseConnectionDidUpdate");
[self configureUI];
}

- (void)configureUI {
[[Database sharedInstance].uiConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[_breakfastButton setState:[[transaction objectForKey:@"breakfast" inCollection:@"meals"] integerValue]];
[_lunchButton setState:[[transaction objectForKey:@"lunch" inCollection:@"meals"] integerValue]];
[_dinnerButton setState:[[transaction objectForKey:@"dinner" inCollection:@"meals"] integerValue]];
}];
_snapshotTextField.stringValue = [NSString stringWithFormat:@"%llu", [[Database sharedInstance].uiConnection snapshot]];
}

- (IBAction)mealTapped:(NSButton *)sender {
NSString *meal = nil;
if (sender == _breakfastButton) {
meal = @"breakfast";
} else if (sender == _lunchButton) {
meal = @"lunch";
} else {
meal = @"dinner";
}
[[Database sharedInstance].bgConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:[NSNumber numberWithInteger:sender.state] forKey:meal inCollection:@"meals"];
}];

}

- (IBAction)refreshTapped:(id)sender {
[[Database sharedInstance] invalidateCaches];
// [[Database sharedInstance] reconnect];
[self configureUI];
}

@end
13 changes: 13 additions & 0 deletions Examples/List/List/main.m
@@ -0,0 +1,13 @@
//
// main.m
// List
//
// Created by Denis Hennessy on 06/04/2015.
// Copyright (c) 2015 Peer Assembly. All rights reserved.
//

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
return NSApplicationMain(argc, argv);
}

0 comments on commit ccc8023

Please sign in to comment.