Files
RestKit/Examples/RKTwitterCoreData/Classes/RKTwitterViewController.m

140 lines
5.1 KiB
Objective-C

//
// RKTwitterViewController.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
@interface RKTwitterViewController () <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *statuses;
@end
@implementation RKTwitterViewController
@synthesize tableView = _tableView;
@synthesize statuses = _statuses;
- (void)loadView
{
[super loadView];
// Setup View and Table View
self.title = @"RestKit Tweets";
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadButtonWasPressed:)] autorelease];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG.png"]] autorelease];
imageView.frame = CGRectOffset(imageView.frame, 0, -64);
[self.view insertSubview:imageView atIndex:0];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
// Load statuses from core data
[self loadObjectsFromDataStore];
}
- (void)dealloc
{
[_tableView release];
[_statuses release];
[super dealloc];
}
- (void)loadObjectsFromDataStore
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RKTStatus"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];
NSError *error;
self.statuses = [[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext executeFetchRequest:fetchRequest error:&error];
}
- (void)loadData
{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"/status/user_timeline/RestKit" delegate:self];
}
- (void)reloadButtonWasPressed:(id)sender
{
// Load the object model via RestKit
[self loadData];
}
#pragma mark RKObjectLoaderDelegate methods
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Loaded statuses: %@", objects);
[self loadObjectsFromDataStore];
[_tableView reloadData];
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
NSLog(@"Hit error: %@", error);
}
#pragma mark UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = [[[_statuses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];
return size.height + 10;
}
#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [_statuses count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDate *lastUpdatedAt = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdatedAt"];
NSString *dateString = [NSDateFormatter localizedStringFromDate:lastUpdatedAt dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle];
if (nil == dateString) {
dateString = @"Never";
}
return [NSString stringWithFormat:@"Last Load: %@", dateString];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = @"Tweet Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.numberOfLines = 0;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
}
RKTStatus *status = [_statuses objectAtIndex:indexPath.row];
cell.textLabel.text = status.text;
return cell;
}
@end