espagram/Espagram/EspagramWordsInLessonViewController.m
2012-12-20 18:09:11 +01:00

171 lines
5.9 KiB
Objective-C

//
// EspagramWordsInLessonViewController.m
// Espagram
//
// Created by Abel Fokkinga on 11/14/12.
// Copyright (c) 2012 Abel Fokkinga. All rights reserved.
//
#import "EspagramWordsInLessonViewController.h"
#import "Conjugator.h"
#import "Verb+Create.h"
@interface EspagramWordsInLessonViewController ()
@end
@implementation EspagramWordsInLessonViewController
@synthesize lesson = _lesson;
- (IBAction)AddButtonPressed:(id)sender {
[self performSegueWithIdentifier:@"Add A Word To Lesson" sender:self];
}
- (void) addWord:(NSString *)word withMeaning:(NSString *)meaning{
if ( word ) {
[Verb addVerb:word andMeaning:meaning toLesson:self.lesson];
}
[[self parentViewController] dismissViewControllerAnimated:TRUE completion:^{
NSLog(@"Word added");
}];
}
- (void) cancelAddingWord{
[[self parentViewController] dismissViewControllerAnimated:TRUE completion:^{ NSLog(@"Adding a word cancelled");
}];
}
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Verb"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"verb"
ascending:YES
selector:@selector(localizedCompare:)]];
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"lesson.title = %@", self.lesson.title], [NSPredicate predicateWithFormat:@"lesson.tense = %@", self.lesson.tense], [NSPredicate predicateWithFormat:@"lesson.conjugator = %@", self.lesson.conjugator],nil]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.lesson.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
// Use for copying verbs from other tense
// if ( [self.lesson.verbs count] == 0){
// Lesson * copyLesson = [self getLessonWithName:self.lesson.title andTense:@"PRESENT_PERFECT" withConjugator:self.lesson.conjugator inContext:[self.lesson managedObjectContext]];
// if ( copyLesson && [copyLesson.verbs count] > 0){
// for (Verb * v in copyLesson.verbs) {
// [Verb addVerb:v.verb andMeaning:v.meaning toLesson:self.lesson];
// }
// [[self.lesson managedObjectContext] save:nil];
// }
// }
}
- (void)setLesson:(Lesson *)lesson
{
_lesson = lesson;
self.title = lesson.title;
[self setupFetchedResultsController];
}
- (Lesson *) getLessonWithName:(NSString *)title andTense:(NSString *)tense withConjugator:(NSString *)conjugator inContext:(NSManagedObjectContext *)context
{
Lesson * les = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Lesson"];
// Where clause
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"title = %@", title],[NSPredicate predicateWithFormat:@"tense = %@", tense], [NSPredicate predicateWithFormat:@"conjugator = %@", conjugator],nil]];
NSError *error = nil;
NSArray *lessons = [context executeFetchRequest:request error:&error];
if ( lessons && lessons.count > 0 ){
return [lessons lastObject];
}
return les;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Word Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Verb * verb = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = verb.verb;
cell.detailTextLabel.text = verb.meaning;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ( [segue.identifier isEqualToString:@"Add A Word To Lesson"]) {
[segue.destinationViewController setDataSource:self];
}
if ( [segue.identifier isEqualToString:@"Conjugate"]) {
Verb * selectedVerb = [self.fetchedResultsController objectAtIndexPath:sender];
[segue.destinationViewController setVerb:selectedVerb];
}
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
Verb * selectedVerb = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.lesson removeVerbsObject:selectedVerb];
}
// else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
// }
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"Conjugate" sender:indexPath];
}
@end