Skip to content

Commit

Permalink
Refactor root view controllers and make refresh/stop work consistent …
Browse files Browse the repository at this point in the history
…during browsing.
  • Loading branch information
alloy committed May 24, 2011
1 parent 676ff0e commit ad5d867
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 335 deletions.
9 changes: 2 additions & 7 deletions Classes/Controllers/CalendarViewController.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#import <UIKit/UIKit.h>
#import "AsyncImageTableViewController.h"
#import "Episode.h"
#import "ImageRootController.h"

@interface CalendarViewController : AsyncImageTableViewController <UISearchDisplayDelegate> {
@interface CalendarViewController : ImageRootController <UISearchDisplayDelegate> {
NSArray *broadcastDates;
NSMutableArray *filteredListContent;
UISearchBar *searchBar;
Expand All @@ -14,8 +13,4 @@
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) UISearchDisplayController *searchController;

- (void)showRefreshDataButton;
- (void)showStopRefreshDataButton;
- (void)cancelRefreshData;

@end
69 changes: 8 additions & 61 deletions Classes/Controllers/CalendarViewController.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "CalendarViewController.h"

#import "Episode.h"
#import "EpisodeTableViewCell.h"
#import "EpisodeDetailsViewController.h"

Expand All @@ -22,7 +24,9 @@ @implementation CalendarViewController
- (void)viewDidLoad {
[super viewDidLoad];

[self showRefreshDataButton];
self.feedSelector = @"calendar:";
self.cachedFeedProperty = @"broadcastDates";

self.navigationItem.title = @"Calendar";
self.tableView.rowHeight = ROW_HEIGHT;

Expand All @@ -34,55 +38,11 @@ - (void)viewDidLoad {
self.searchController.searchResultsTableView.rowHeight = ROW_HEIGHT;
}


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// TODO This should be done in one place (superclass)
NSString *username = [Trakt sharedInstance].apiUser;
self.navigationItem.rightBarButtonItem.title = username == nil ? @"Sign in" : username;

if (self.broadcastDates == nil && [Trakt sharedInstance].broadcastDates != nil) {
NSLog(@"Loading calendar data from Trakt instance which has already loaded it");
self.broadcastDates = [Trakt sharedInstance].broadcastDates;
[self reloadTableViewData];
}
}


- (void)refreshData {
NSLog(@"Refresh calendar data!");
[self showStopRefreshDataButton];
[[Trakt sharedInstance] retrieveTopLevelControllerdataStartingWith:@"calendar:" block:^(NSArray *dates) {
[self showRefreshDataButton];
self.broadcastDates = dates;
[self reloadTableViewData];
}];
- (void)reloadTableViewData:(NSArray *)data {
self.broadcastDates = data;
[super reloadTableViewData:data];
}

// TODO This should be done in one place (superclass)
- (void)showRefreshDataButton {
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshData)];
self.navigationItem.leftBarButtonItem = refreshButton;
[refreshButton release];
}
- (void)showStopRefreshDataButton {
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:@selector(cancelRefreshData)];
self.navigationItem.leftBarButtonItem = stopButton;
[stopButton release];
}


- (void)cancelRefreshData {
[self showRefreshDataButton];
[HTTPDownload cancelDownloadsInProgress];
}


- (void)loadImageForCell:(UITableViewCell *)cell {
EpisodeTableViewCell *episodeCell = (EpisodeTableViewCell *)cell;
[episodeCell.episode.show ensurePosterIsLoaded:^{
Expand Down Expand Up @@ -214,19 +174,6 @@ - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldRe
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
self.broadcastDates = nil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#import <UIKit/UIKit.h>
#import "RootViewController.h"

@interface AsyncImageTableViewController : UITableViewController {
@interface ImageRootController : RootViewController {
BOOL dragging;
}

- (void)reloadTableViewData;
- (void)loadImagesForVisibleCells;
- (void)loadImageForCell:(UITableViewCell *)cell;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
#import "AsyncImageTableViewController.h"
#import "ImageRootController.h"
#import "HTTPDownload.h"

@implementation AsyncImageTableViewController
#define ROW_HEIGHT 66.0

@implementation ImageRootController

- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = ROW_HEIGHT;
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
dragging = NO;
[self loadImagesForVisibleCells];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[HTTPDownload cancelDownloadsInProgress];
}


- (void)reloadTableViewData {
[self.tableView reloadData];
// The subclass should override this to actually set the data before calling super.
- (void)reloadTableViewData:(NSArray *)data {
[super reloadTableViewData:data]; // first reload!
[self loadImagesForVisibleCells];
}


- (void)loadImageForCell:(UITableViewCell *)cell {
NSLog(@"[!] The [AsyncImageTableViewController loadImageForCell:] method should be overriden by the subclass!");
}


- (void)loadImagesForVisibleCells {
NSArray *cells = [self.tableView visibleCells];
for (int i = 0; i < [cells count]; i++) {
[self loadImageForCell:[cells objectAtIndex:i]];
}
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
dragging = YES;
}
Expand All @@ -57,5 +55,4 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
}


@end
18 changes: 4 additions & 14 deletions Classes/Controllers/LibraryViewController.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
#import <UIKit/UIKit.h>
#import "ShowsViewController.h"
#import "ShowDetailsViewController.h"
#import "RootViewController.h"

#import "Trakt.h"
#import "Show.h"

@interface LibraryViewController : UITableViewController <UISearchDisplayDelegate> {
NSMutableArray *shows;
@interface LibraryViewController : RootViewController <UISearchDisplayDelegate> {
NSMutableArray *library;
NSMutableArray *filteredShows;
NSMutableArray *indexTitles;
UISearchBar *searchBar;
UISearchDisplayController *searchController;
}

@property (nonatomic, retain) NSMutableArray *shows;
@property (nonatomic, retain) NSMutableArray *library;
@property (nonatomic, retain) NSMutableArray *filteredShows;
@property (nonatomic, retain) NSMutableArray *indexTitles;
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) UISearchDisplayController *searchController;

- (void)loadData:(NSArray *)loadedShows;

- (void)showRefreshDataButton;
- (void)showStopRefreshDataButton;
- (void)cancelRefreshData;

@end
94 changes: 19 additions & 75 deletions Classes/Controllers/LibraryViewController.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#import "LibraryViewController.h"

#import "Show.h"
#import "ShowDetailsViewController.h"

#import "Trakt.h"
#import "HTTPDownload.h"
#import "Show.h"

@implementation LibraryViewController


@synthesize shows, filteredShows, indexTitles;
@synthesize library, filteredShows, indexTitles;
@synthesize searchBar, searchController;


- (void)viewDidLoad {
[super viewDidLoad];

[self showRefreshDataButton];
self.feedSelector = @"library:";
self.cachedFeedProperty = @"library";

self.navigationItem.title = @"Library";

self.filteredShows = [NSMutableArray new];
Expand All @@ -23,59 +26,12 @@ - (void)viewDidLoad {
self.searchController.searchResultsDelegate = self;
}


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// TODO This should be done in one place (superclass)
NSString *username = [Trakt sharedInstance].apiUser;
self.navigationItem.rightBarButtonItem.title = username == nil ? @"Sign in" : username;

if (self.shows == nil && [Trakt sharedInstance].library != nil) {
NSLog(@"Loading library data from Trakt instance which has already loaded it");
[self loadData:[Trakt sharedInstance].library];
}
}


- (void)refreshData {
NSLog(@"Refresh library data!");
[self showStopRefreshDataButton];
[[Trakt sharedInstance] retrieveTopLevelControllerdataStartingWith:@"library:" block:^(NSArray *loadedShows) {
[self showRefreshDataButton];
[self loadData:loadedShows];
}];
}

// TODO This should be done in one place (superclass)
- (void)showRefreshDataButton {
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshData)];
self.navigationItem.leftBarButtonItem = refreshButton;
[refreshButton release];
}
- (void)showStopRefreshDataButton {
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:@selector(cancelRefreshData)];
self.navigationItem.leftBarButtonItem = stopButton;
[stopButton release];
}


- (void)cancelRefreshData {
[self showRefreshDataButton];
[HTTPDownload cancelDownloadsInProgress];
}


- (void)loadData:(NSArray *)loadedShows {
- (void)reloadTableViewData:(NSArray *)data {
NSMutableArray *groupedShows = [NSMutableArray array];
NSMutableArray *titles = [NSMutableArray array];

NSRange skipPrefixRange = NSMakeRange(4, 1);
for (Show *show in loadedShows) {
for (Show *show in data) {
NSString *letter;
if ([show.title hasPrefix:@"The "]) {
letter = [show.title substringWithRange:skipPrefixRange];
Expand All @@ -102,11 +58,11 @@ - (void)loadData:(NSArray *)loadedShows {
}
}

self.shows = groupedShows;
self.library = groupedShows;
self.indexTitles = titles;
[self.tableView reloadData];
}

[super reloadTableViewData:data];
}

#pragma mark -
#pragma mark Table view data source
Expand Down Expand Up @@ -150,7 +106,7 @@ - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSS

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView) {
return [self.shows count];
return [self.library count];
} else {
return 1;
}
Expand All @@ -159,7 +115,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return [[self.shows objectAtIndex:section] count];
return [[self.library objectAtIndex:section] count];
} else {
return [self.filteredShows count];
}
Expand All @@ -176,7 +132,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

Show *show;
if (tableView == self.tableView) {
show = [[self.shows objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
show = [[self.library objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
} else {
show = [self.filteredShows objectAtIndex:indexPath.row];
}
Expand All @@ -194,7 +150,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Show *show;
if (tableView == self.tableView) {
show = [[self.shows objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
show = [[self.library objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
} else {
show = [self.filteredShows objectAtIndex:indexPath.row];
}
Expand All @@ -210,7 +166,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchText {
[filteredShows removeAllObjects];
for (NSArray *section in self.shows) {
for (NSArray *section in self.library) {
for (Show *show in section) {
NSRange range = [show.title rangeOfString:searchText
options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch];
Expand All @@ -226,21 +182,9 @@ - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldRe
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
self.shows = nil;
self.library = nil;
self.filteredShows = nil;
[super dealloc];
}
Expand Down
Loading

0 comments on commit ad5d867

Please sign in to comment.