espagram/Espagram/EspagramWordsInLessonViewController.m
2012-11-23 23:06:49 +01:00

141 lines
4.6 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];
}
- (void)setLesson:(Lesson *)lesson
{
_lesson = lesson;
self.title = lesson.title;
[self setupFetchedResultsController];
}
- (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