Skip to content

MRC PR #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions Sprint 12/Module 4/MRC/MRC.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppDelegate.h
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

24 changes: 24 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AppDelegate.m
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

return YES;
}

@end
19 changes: 19 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContact+NSJSONSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// BHContact+NSJSONSerializer.h
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import "BHContact.h"

NS_ASSUME_NONNULL_BEGIN

@interface BHContact (NSJSONSerializer)

-(NSDictionary *)makeDictionaryFromContact: (BHContact *)contact;

@end

NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContact+NSJSONSerializer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// BHContact+NSJSONSerializer.m
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import "BHContact+NSJSONSerializer.h"

@implementation BHContact (NSJSONSerializer)

-(NSDictionary *)makeDictionaryFromContact: (BHContact *)contact{

NSDictionary *bottomLevelDictionary = [NSDictionary dictionaryWithObjectsAndKeys: contact.name, @"name", contact.email, @"email", contact.phoneNumber, @"phoneNumber", contact.uuid, @"uuid", nil];

return bottomLevelDictionary;

}

@end
27 changes: 27 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContact.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// BHContact.h
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface BHContact : NSObject

- (instancetype)initWithName: (NSString *)name
email:(NSString *)email
phoneNumber:(NSString *)phoneNumber
uuid:(NSString *)uuid;

@property (retain, nonatomic) NSString *name;
@property (retain, nonatomic) NSString *email;
@property (retain, nonatomic) NSString *phoneNumber;
@property (retain, nonatomic) NSString *uuid;

@end

NS_ASSUME_NONNULL_END
53 changes: 53 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContact.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// BHContact.m
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import "BHContact.h"

@implementation BHContact


- (instancetype)init{
self = [super init];
if (self) {
_uuid = [[NSUUID UUID] UUIDString];
_email = @"";
_name = @"";
_phoneNumber = @"";
}
return self;
}

- (instancetype)initWithName: (NSString *)name
email:(NSString *)email
phoneNumber:(NSString *)phoneNumber
uuid:(NSString *)uuid{

self = [super init];
if (self){
_name = name;
_email = email;
_phoneNumber = phoneNumber;
_uuid = [[NSUUID UUID] UUIDString];
}
[_name retain];
[_email retain];
[_phoneNumber retain];
[_uuid retain];
return self;
}

- (void)dealloc {
[_name release];
[_email release];
[_phoneNumber release];
[_uuid release];
[super dealloc];
}


@end
33 changes: 33 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContactsController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// BHContactsController.h
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BHContact.h"

NS_ASSUME_NONNULL_BEGIN

@interface BHContactsController : NSObject

+ (id)shared;

@property (nonatomic, retain) NSMutableArray<BHContact *> *contacts;

@property (nonatomic, retain) NSFileManager *fileManager;

// create
-(void)createContact: (BHContact *)contact;

// update
-(void)updateContact: (BHContact *)updatedContact;

// delete
-(void)deleteContact: (BHContact *)contactToDelete;

@end

NS_ASSUME_NONNULL_END
185 changes: 185 additions & 0 deletions Sprint 12/Module 4/MRC/MRC/BHContactsController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//
// BHContactsController.m
// MRC
//
// Created by Benjamin Hakes on 3/6/19.
// Copyright © 2019 Benjamin Hakes. All rights reserved.
//

#import "BHContactsController.h"
#import "BHContact.h"

@interface BHContactsController()

@end

static BHContactsController *shared = nil;

@implementation BHContactsController

@synthesize contacts;

#pragma mark Singleton Methods
+ (id)shared {
@synchronized(self) {
if(shared == nil){
shared = [[super alloc] init];
}
}
return shared;
}

- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (oneway void)release {
// never release
}
- (id)autorelease {
return self;
}

- (void)dealloc {
// Should never be called, but just here for clarity really.
[contacts release];
[_fileManager release];
[super dealloc];
}

-(instancetype)init{
self = [super init];
if (self) {

// file manager setup
_fileManager = [[NSFileManager alloc] init];
[_fileManager autorelease];
_fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
[_fileManager contentsOfDirectoryAtURL:documentsDirectoryURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];

if (![self getContactsFromDisk]) {
contacts = [[NSMutableArray<BHContact *> alloc] init];
[contacts retain];
} else {
contacts = [self getContactsFromDisk];
[contacts retain];
}

}
return self;
}

// create
-(void)createContact: (BHContact *)contact{

[contacts addObject:contact];
[self saveContactsToDisk];
}

// update
-(void)updateContact: (BHContact *)updatedContact{
NSUInteger j = [contacts count];

for (int q = 0; q < j; q++)
{
BHContact *thisObject = [contacts objectAtIndex:q];
BOOL shouldUpdateThisObject = ([updatedContact uuid] == [thisObject uuid]);
if (shouldUpdateThisObject){

[contacts removeObjectAtIndex:q];
[contacts insertObject:updatedContact atIndex:q];
break;
}
}

[self saveContactsToDisk];
}

// delete
-(void)deleteContact: (BHContact *)songToDelete{
[contacts removeObject:songToDelete];
[self saveContactsToDisk];
}

// save to local storage
-(void)saveContactsToDisk {

// get the document directory URL
NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];

// append our file name
NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:@"Contacts.data" isDirectory:NO];

NSUInteger j = [contacts count];

NSMutableArray *topLevelArray = [[NSMutableArray alloc] init];

for (int q = 0; q < j; q++)
{

BHContact *thisObject = [contacts objectAtIndex:q];

NSDictionary *newDictionary = [thisObject makeDictionaryFromContact:thisObject];

[topLevelArray addObject:newDictionary];
}

NSData *data = [NSJSONSerialization dataWithJSONObject:topLevelArray options:NSJSONWritingPrettyPrinted error:nil];
[topLevelArray release];

if (![data writeToURL:url atomically:YES]) {
NSLog(@"Failed to writeToURL:'%@'", url);
}


}

-(NSMutableArray*)getContactsFromDisk {

// get the document directory URL
NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];

// append our file name
NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:@"Contacts.data" isDirectory:NO];

NSData* data = [NSData dataWithContentsOfURL:url];
NSMutableArray *objects;

@try {
objects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
} @catch (NSException* error) {
NSLog(@"Error Decoding Data: %@", error);
NSMutableArray *newArray = [[NSMutableArray<BHContact *> alloc] init];
[newArray autorelease];
return newArray;
}

NSUInteger j = [objects count];

NSMutableArray *newArray = [[NSMutableArray<BHContact *> alloc] init];
[newArray autorelease];


for (int q = 0; q < j; q++)
{

NSDictionary *thisObject = [objects objectAtIndex:q];

BHContact *newContact = [[BHContact alloc] initWithName:[thisObject objectForKey:@"name"] email:[thisObject objectForKey:@"email"] phoneNumber:[thisObject objectForKey:@"phoneNumber"] uuid:[thisObject objectForKey:@"uuid"]];
[newArray addObject:newContact];
[newContact release];
// [newContact dealloc];
}

return newArray;

}


@end
Loading