|
| 1 | +// |
| 2 | +// CGFTimelineViewController.m |
| 3 | +// FJImporter |
| 4 | +// |
| 5 | +// Created by Marcus Zarra on 11/3/11. |
| 6 | +// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | + |
| 29 | +#import "CGFTimelineViewController.h" |
| 30 | + |
| 31 | +@interface CGFTimelineViewController() <NSFetchedResultsControllerDelegate> |
| 32 | + |
| 33 | +@property (nonatomic, retain) NSFetchedResultsController *fetchController; |
| 34 | + |
| 35 | +@end |
| 36 | + |
| 37 | +@implementation CGFTimelineViewController |
| 38 | + |
| 39 | +- (void)viewDidLoad |
| 40 | +{ |
| 41 | + [super viewDidLoad]; |
| 42 | + |
| 43 | + [self setTitle:[[self user] valueForKey:@"name"]]; |
| 44 | + |
| 45 | + if ([self fetchController]) return; |
| 46 | + |
| 47 | + NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Tweet"]; |
| 48 | + [request setPredicate:[NSPredicate predicateWithFormat:@"ANY timeline == %@", [self user]]]; |
| 49 | + |
| 50 | + NSSortDescriptor *sort = [[[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO] autorelease]; |
| 51 | + [request setSortDescriptors:[NSArray arrayWithObject:sort]]; |
| 52 | + |
| 53 | + NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:[[self user] valueForKey:@"name"]]; |
| 54 | + [controller setDelegate:self]; |
| 55 | + |
| 56 | + NSError *error = nil; |
| 57 | + ZAssert([controller performFetch:&error], @"Error fetching tweets: %@", error); |
| 58 | + |
| 59 | + [self setFetchController:controller]; |
| 60 | + MCRelease(controller); |
| 61 | +} |
| 62 | + |
| 63 | +#pragma mark - Table view data source |
| 64 | + |
| 65 | +- (void)configureCell:(id)cell atIndexPath:(NSIndexPath*)indexPath |
| 66 | +{ |
| 67 | + NSManagedObject *tweet = [[self fetchController] objectAtIndexPath:indexPath]; |
| 68 | + |
| 69 | + [[cell textLabel] setText:[tweet valueForKey:@"text"]]; |
| 70 | + [[cell detailTextLabel] setText:[tweet valueForKeyPath:@"author.name"]]; |
| 71 | +} |
| 72 | + |
| 73 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 74 | +{ |
| 75 | + return [[[[self fetchController] sections] objectAtIndex:section] numberOfObjects]; |
| 76 | +} |
| 77 | + |
| 78 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 79 | +{ |
| 80 | + id cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; |
| 81 | + |
| 82 | + if (!cell) { |
| 83 | + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease]; |
| 84 | + [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
| 85 | + } |
| 86 | + |
| 87 | + [self configureCell:cell atIndexPath:indexPath]; |
| 88 | + |
| 89 | + return cell; |
| 90 | +} |
| 91 | + |
| 92 | +- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller |
| 93 | +{ |
| 94 | + [[self tableView] beginUpdates]; |
| 95 | +} |
| 96 | + |
| 97 | +- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath |
| 98 | +{ |
| 99 | + switch(type) { |
| 100 | + case NSFetchedResultsChangeInsert: |
| 101 | + [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 102 | + break; |
| 103 | + case NSFetchedResultsChangeDelete: |
| 104 | + [[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 105 | + break; |
| 106 | + case NSFetchedResultsChangeUpdate: |
| 107 | + [self configureCell:[[self tableView] cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; |
| 108 | + break; |
| 109 | + case NSFetchedResultsChangeMove: |
| 110 | + [[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 111 | + [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 112 | + break; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller |
| 117 | +{ |
| 118 | + [[self tableView] endUpdates]; |
| 119 | +} |
| 120 | + |
| 121 | +@synthesize user; |
| 122 | +@synthesize managedObjectContext; |
| 123 | +@synthesize fetchController; |
| 124 | + |
| 125 | +@end |
0 commit comments