espagram/Espagram/EspagramWordsInLessonViewController.m
2013-02-21 17:08:51 +01:00

234 lines
8.2 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 ()
@property (strong,nonatomic) UIPopoverController *myPopover;
@end
@implementation EspagramWordsInLessonViewController
@synthesize lesson = _lesson;
@synthesize delegate = _delegate;
@synthesize myPopover = _myPopover;
- (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];
}
if ( self.myPopover ) {
// On iPad. Dismiss the pop over
[[self myPopover] dismissPopoverAnimated:TRUE];
self.myPopover = nil;
} else {
// On iPhone
[[self parentViewController] dismissViewControllerAnimated:TRUE completion:^{
}];
}
if ( self.lesson.verbs.count > 0 && self.tabBarController ) {
// Words in lesson, enable tabs
NSArray *tabItems = self.tabBarController.tabBar.items;
for (UIBarItem *tabItem in tabItems)
{
[tabItem setEnabled:true];
}
}
}
- (NSString *) searchWordMeaning:(NSString *)word
{
if ( word ) {
Verb * v= [Verb searchWordMeaning:word usingLessonLanguage:self.lesson];
return v.meaning;
}
return nil;
};
- (void) cancelAddingWord{
if ( self.myPopover ) {
// On iPad, Dismiss the pop over
[[self myPopover] dismissPopoverAnimated:TRUE];
self.myPopover = nil;
} else {
// On iPhone
[[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];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = NSLocalizedString(@"List",@"List of verbs");
}
- (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;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Word Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if ( indexPath.row + 1 > [[[self.fetchedResultsController sections] objectAtIndex:indexPath.section] numberOfObjects]){
cell.textLabel.text = NSLocalizedString(@"Add new...",@"Add a new verb (short description)");
cell.detailTextLabel.text = NSLocalizedString(@"Add a new verb",@"Add a new verb (long description)");
} else {
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 ( self.splitViewController ) {
// On iPad, we want to store the pointer to the popOver
self.myPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
}
}
if ( [segue.identifier isEqualToString:@"Conjugate"]) {
Verb * selectedVerb = [self.fetchedResultsController objectAtIndexPath:sender];
[segue.destinationViewController setVerb:selectedVerb];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected verb %@",[[[self.tableView cellForRowAtIndexPath:indexPath] textLabel] text ]);
if ( [[[[self.tableView cellForRowAtIndexPath:indexPath] textLabel] text] isEqualToString:NSLocalizedString(@"Add new...",@"Add a new verb (short description)")] ) {
[self performSegueWithIdentifier:@"Add A Word To Lesson" sender:self];
} else {
[self performSegueWithIdentifier:@"Conjugate" sender:indexPath];
}
}
// 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
@end