- (void)viewDidLoad
{
   [super viewDidLoad];

   self.navigationItem.leftBarButtonItem = self.editButtonItem;

   UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
      initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                           target:self
                           action:@selector(addPerson)];
   self.navigationItem.rightBarButtonItem = addButton;
   [addButton release];
}

- (void)addPerson
{
   PersonDetailViewController *detailController = [[PersonDetailViewController alloc]
      initWithManagedObjectContext:managedObjectContext];

   UINavigationController *controller = [[UINavigationController alloc]
      initWithRootViewController:detailController];

   detailController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
      initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                           target:detailController
                           action:@selector(saveAndDismiss)] autorelease];

   [self presentModalViewController:controller animated:YES];

   [controller release];
   [detailController release];
}

- (void)tableView:(UITableView *)tableView
   commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (editingStyle == UITableViewCellEditingStyleDelete) {
      [managedObjectContext deleteObject:[resultsController 
         objectAtIndexPath:indexPath]];
      NSError *error = nil;
      if (![managedObjectContext save:&error]) {
         NSLog(@"Wystąpił błąd %@, %@", error, [error userInfo]);
         abort();
      }
   }
}

- (void)controllerWillChangeContent: (NSFetchedResultsController *)controller
{
   [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller 
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
   UITableView *tableView = self.tableView;

   switch(type) {
      case NSFetchedResultsChangeInsert:
         [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
         break;
      case NSFetchedResultsChangeDelete:
         [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
         break;
      case NSFetchedResultsChangeUpdate:
         [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
         break;
      case NSFetchedResultsChangeMove:
         [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
         [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
         break;
   }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
   [self.tableView endUpdates];
}
