Skip to content
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

Mesfin's MarsWater #11

Open
wants to merge 9 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
384 changes: 384 additions & 0 deletions MarsWater/MarsWater.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// AppDelegate.h
// SavingForever
// MarsWater
//
// Created by Michael Kavouras on 10/4/15.
// Copyright © 2015 Michael Kavouras. All rights reserved.
// Created by Mesfin Bekele Mekonnen on 10/4/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import <UIKit/UIKit.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// AppDelegate.m
// SavingForever
// MarsWater
//
// Created by Michael Kavouras on 10/4/15.
// Copyright © 2015 Michael Kavouras. All rights reserved.
// Created by Mesfin Bekele Mekonnen on 10/4/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import "AppDelegate.h"
Expand Down Expand Up @@ -51,7 +51,7 @@ - (void)applicationWillTerminate:(UIApplication *)application {
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.mikekavouras.SavingForever" in the application's documents directory.
// The directory the application uses to store the Core Data store file. This code uses a directory named "nyc.c4q.mesbekmek.MarsWater" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Expand All @@ -60,7 +60,7 @@ - (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SavingForever" withExtension:@"momd"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MarsWater" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
Expand All @@ -74,7 +74,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SavingForever.sqlite"];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MarsWater.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
Expand Down
307 changes: 307 additions & 0 deletions MarsWater/MarsWater/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

File renamed without changes.
40 changes: 40 additions & 0 deletions MarsWater/MarsWater/List+CoreDataProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// List+CoreDataProperties.h
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/7/15.
// Copyright © 2015 Mesfin. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//

#import "List.h"

NS_ASSUME_NONNULL_BEGIN

@interface List (CoreDataProperties)

@property (nullable, nonatomic, retain) id color;
@property (nullable, nonatomic, retain) NSDate *createdAt;
@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) NSOrderedSet<Task *> *tasks;

@end

@interface List (CoreDataGeneratedAccessors)

- (void)insertObject:(Task *)value inTasksAtIndex:(NSUInteger)idx;
- (void)removeObjectFromTasksAtIndex:(NSUInteger)idx;
- (void)insertTasks:(NSArray<Task *> *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeTasksAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInTasksAtIndex:(NSUInteger)idx withObject:(Task *)value;
- (void)replaceTasksAtIndexes:(NSIndexSet *)indexes withTasks:(NSArray<Task *> *)values;
- (void)addTasksObject:(Task *)value;
- (void)removeTasksObject:(Task *)value;
- (void)addTasks:(NSOrderedSet<Task *> *)values;
- (void)removeTasks:(NSOrderedSet<Task *> *)values;

@end

NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions MarsWater/MarsWater/List+CoreDataProperties.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// List+CoreDataProperties.m
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/7/15.
// Copyright © 2015 Mesfin. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//

#import "List+CoreDataProperties.h"

@implementation List (CoreDataProperties)

@dynamic color;
@dynamic createdAt;
@dynamic title;
@dynamic tasks;

@end
23 changes: 23 additions & 0 deletions MarsWater/MarsWater/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// List.h
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/7/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Task;

NS_ASSUME_NONNULL_BEGIN

@interface List : NSManagedObject

-(NSString *)subtitleText;
@end

NS_ASSUME_NONNULL_END

#import "List+CoreDataProperties.h"
21 changes: 21 additions & 0 deletions MarsWater/MarsWater/List.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// List.m
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/7/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import "List.h"
#import "Task.h"

@implementation List

-(NSString *)subtitleText{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateStyle = NSDateFormatterShortStyle;

return [dateFormatter stringFromDate:self.createdAt];
}
@end
13 changes: 13 additions & 0 deletions MarsWater/MarsWater/ListCreationTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ListCreationTableViewController.h
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/4/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ListCreationTableViewController : UITableViewController

@end
137 changes: 137 additions & 0 deletions MarsWater/MarsWater/ListCreationTableViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// ListCreationTableViewController.m
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/4/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import "ListCreationTableViewController.h"
#import "List.h"
#import "Task.h"
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "NSString+Length.h"

@interface ListCreationTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *titleTextField;
@property (weak, nonatomic) IBOutlet UITextField *taskTextField;

@property (nonatomic) UIColor *listColor;

@end

@implementation ListCreationTableViewController

- (void)viewDidLoad {

[super viewDidLoad];
[self setupNavigationBar];
}

- (void)setupNavigationBar {

self.navigationItem.title = @"Create new list";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0.0/255 green:118.0/255 blue:255.0/255 alpha:1.0];
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

}

- (void)cancel {

[self dismissViewControllerAnimated:YES completion:nil];
}

-(NSArray *)commaSeparatedTasksFromString:(NSString *)taskString {

NSArray *commaSeparatedTasks = [taskString componentsSeparatedByString:@","];
return commaSeparatedTasks;
}

- (void)save {

AppDelegate *delegate = [UIApplication sharedApplication].delegate;

NSString *title = self.titleTextField.text;
NSString *taskString = self.taskTextField.text;

if([title isValid]) {
List *list = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:delegate.managedObjectContext];
if(self.listColor == nil) {
//assigns a default color to a list if user decides not assign one.
[list setValue:[UIColor colorWithRed:0.0/255 green:118.0/255 blue:255.0/255 alpha:1.0] forKey:@"color"];
}
else {
[list setValue:self.listColor forKey:@"color"];
}
[list setValue:title forKey:@"title"];
[list setValue:[NSDate date] forKey:@"createdAt"];

if(taskString && taskString.length) {
//Checks to see if user has comma separated tasks or just one task
if([taskString rangeOfString:@","].location != NSNotFound) {
NSArray *commaSeparatedTasks = [self commaSeparatedTasksFromString:taskString];
NSMutableOrderedSet *mutableSet = [list mutableOrderedSetValueForKey:@"tasks"];
for(NSString *taskString in commaSeparatedTasks){
Task *task = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:delegate.managedObjectContext];
[task setValue:taskString forKey:@"taskDescription"];
[task setValue:[NSDate date] forKey:@"createdAt"];
[mutableSet addObject:task];
[list setValue:mutableSet forKey:@"tasks"];
NSLog(@"These are your taskStrings: %@",task.taskDescription);
}
//there's only one task
}
else {
Task *task = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:delegate.managedObjectContext];
NSMutableOrderedSet *mutableSet = [list mutableOrderedSetValueForKey:@"tasks"];
task.taskDescription = taskString;
task.createdAt = [NSDate date];
[mutableSet addObject:task];
[list setValue:mutableSet forKey:@"tasks"];
}
}
NSError *error = nil;
if([list.title isValid]) {
if([delegate.managedObjectContext save:&error]){
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
if(error) {
NSLog(@"Unable to save list");
NSLog(@"%@ %@",error, error.localizedDescription);
}
[self alertWithMessage:@"Your to-do list could not be saved" andTitle:@"Warning/Error"];
}
}
//there was no title
}
else {
[self alertWithMessage:@"You need to put a title before you save" andTitle:@"Hold up"];
}
}

#pragma mark - Alert Methods

- (void)alertWithMessage:(NSString *)message andTitle:(NSString *)title {

UIAlertController *controller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

[controller addAction:okAction];

[self presentViewController:controller animated:YES completion:nil];
}

- (IBAction)colorButtonTapped:(UIButton *)sender
{
self.listColor = sender.backgroundColor;
}


@end
13 changes: 13 additions & 0 deletions MarsWater/MarsWater/ListsTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ListsTableViewController.h
// MarsWater
//
// Created by Mesfin Bekele Mekonnen on 10/4/15.
// Copyright © 2015 Mesfin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ListsTableViewController : UITableViewController

@end
Loading