Skip to content

Commit

Permalink
Closes #5 - Added load more
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksan committed Nov 1, 2011
1 parent db23850 commit a08da4b
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,62 @@
<Bucket
type = "1"
version = "1.0">
<FileBreakpoints>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
isPathRelative = "1"
filePath = "OpenPhoto/PhotoSource.m"
timestampString = "341803626.428545"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "69"
endingLineNumber = "69"
landmarkName = "-load:more:"
landmarkType = "5">
</FileBreakpoint>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
isPathRelative = "1"
filePath = "OpenPhoto/PhotoSource.m"
timestampString = "341807041.680858"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "133"
endingLineNumber = "133"
landmarkName = "-receivedResponse:"
landmarkType = "5">
</FileBreakpoint>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
isPathRelative = "1"
filePath = "OpenPhoto/PhotoSource.m"
timestampString = "341807041.680858"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "179"
endingLineNumber = "179"
landmarkName = "-receivedResponse:"
landmarkType = "5">
</FileBreakpoint>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
isPathRelative = "1"
filePath = "OpenPhoto/PhotoSource.m"
timestampString = "341807041.680858"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "170"
endingLineNumber = "170"
landmarkName = "-receivedResponse:"
landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>
4 changes: 2 additions & 2 deletions OpenPhoto/GalleryViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (id)init{

self.photoSource = [[[PhotoSource alloc]
initWithTitle:@"Gallery"
photos:nil size:0] autorelease];
photos:nil size:0 tag:nil] autorelease];
}
return self;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ -(void) receivedResponse:(NSDictionary *)response{

self.photoSource = [[[PhotoSource alloc]
initWithTitle:@"Gallery"
photos:photos size:totalRows] autorelease];
photos:photos size:totalRows tag:self.tagName] autorelease];

[photos release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Expand Down
14 changes: 12 additions & 2 deletions OpenPhoto/PhotoSource.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#import <Three20/Three20.h>
#import "Three20Core/NSArrayAdditions.h"
#import "WebService.h"

///////////////////////////////////////////////////////////////////////////////////////////////////

@interface PhotoSource : TTURLRequestModel <TTPhotoSource> {
NSString* _title;
NSMutableArray* _photos;
NSMutableArray* photos;
int numberOfPhotos;
int currentPage;
NSString* tagName;
WebService* service;
}
- (id)initWithTitle:(NSString*)title photos:(NSArray*)photos size:(int) size;

@property (nonatomic, copy) NSString* tagName;
@property (nonatomic, retain) WebService *service;
@property (nonatomic, retain) NSMutableArray* photos;
@property (nonatomic) int currentPage;

- (id)initWithTitle:(NSString*)title photos:(NSArray*)listPhotos size:(int) size tag:(NSString*) tag;

@end

Expand Down
167 changes: 152 additions & 15 deletions OpenPhoto/PhotoSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@
@implementation PhotoSource

@synthesize title = _title;
@synthesize tagName = _tagName;
@synthesize numberOfPhotos = _numberOfPhotos;
@synthesize currentPage = _currentPage;
@synthesize service;
@synthesize photos;

int number = 0;
int actualMaxPhotoIndex = 0;
BOOL isLoading = NO;

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)initWithTitle:(NSString*)title photos:(NSArray*)photos size:(int) size{
- (id)initWithTitle:(NSString*)title photos:(NSArray*)listPhotos size:(int) size tag:(NSString*) tag{
if (self = [super init]) {
_title = [title copy];
_photos = [photos mutableCopy];
self.photos = [listPhotos mutableCopy];
_numberOfPhotos = size;
_tagName = tag;

for (int i = 0; i < _photos.count; ++i) {
id<TTPhoto> photo = [_photos objectAtIndex:i];
// the first page
_currentPage = 1;

// create service and the delegate
self.service = [[WebService alloc]init];
[service setDelegate:self];

for (int i = 0; i < self.photos.count; ++i) {
id<TTPhoto> photo = [self.photos objectAtIndex:i];
if ((NSNull*)photo != [NSNull null]) {
photo.photoSource = self;
photo.index = i;
Expand All @@ -28,31 +41,155 @@ - (id)initWithTitle:(NSString*)title photos:(NSArray*)photos size:(int) size{
}

- (id)init {
return [self initWithTitle:nil photos:nil size:0];
return [self initWithTitle:nil photos:nil size:0 tag:nil];
}

- (void)dealloc {
TT_RELEASE_SAFELY(_photos);
TT_RELEASE_SAFELY(_title);
[self.service release];
[self.photos release];
[super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTModel
- (BOOL)isLoaded {
return !!_photos;
return !!self.photos;
}

- (BOOL) isLoading{
return isLoading;
}

- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
if (cachePolicy & TTURLRequestCachePolicyNetwork) {
[_delegates perform:@selector(modelDidStartLoad:) withObject:self];
number = number+25;
NSLog(@"loading");
[_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
_currentPage++;
actualMaxPhotoIndex = actualMaxPhotoIndex+25;

if (self.photos != nil && _title != nil && _currentPage > 1){
isLoading = YES;
[_delegates perform:@selector(modelDidStartLoad:) withObject:self];

NSArray *keys;
NSArray *objects;
NSNumber* number=[NSNumber numberWithInt:_currentPage];

if (_tagName != nil){
keys = [NSArray arrayWithObjects:@"tag", @"page",nil];
objects= [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", _tagName], number, nil];
}else{
keys = [NSArray arrayWithObjects:@"page",nil];
objects= [NSArray arrayWithObjects:number, nil];
}
NSDictionary *values = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

// to send the request we add a thread.
[NSThread detachNewThreadSelector:@selector(loadNewPhotosOnDetachTread:)
toTarget:self
withObject:values];
}
}
}
-(void) loadNewPhotosOnDetachTread:(NSDictionary*) values
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if ([values objectForKey:@"tag"] == nil){
[service loadGallery:25 onPage:[[values objectForKey:@"page"] intValue] ];
}else{
[service loadGallery:25 withTag:[values objectForKey:@"tag"] onPage:[[values objectForKey:@"page"] intValue] ];
}

[pool release];
}


- (void)cancel {
isLoading = NO;
}

// delegate to add more pictures
-(void) receivedResponse:(NSDictionary *)response{
// check if message is valid
if (![WebService isMessageValid:response]){
NSString* message = [WebService getResponseMessage:response];
NSLog(@"Invalid response = %@",message);

// show alert to user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Response Error" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];

return;
}

NSArray *responsePhotos = [response objectForKey:@"result"] ;
NSMutableArray *localPhotos = [[NSMutableArray alloc] init];
int photoId=self.photos.count;

bool isFirst = YES;

// result can be null
if ([responsePhotos class] != [NSNull class]) {

// Loop through each entry in the dictionary and create an array of MockPhoto
for (NSDictionary *photo in responsePhotos){
// index photo
photoId++;

// Get title/description of the image
NSString *localTitle = [photo objectForKey:@"title"];

#ifdef DEVELOPMENT_ENABLED
NSString *description = [photo objectForKey:@"description"];
NSString *photoURLString = [NSString stringWithFormat:@"http://%@%@", [photo objectForKey:@"host"], [photo objectForKey:@"path200x200"]];

NSLog(@"Photo url [%@] with tile [%@] and description [%@]", photoURLString, (localTitle.length > 0 ? localTitle : @"Untitled"),(description.length > 0 ? description : @"Untitled"));
#endif

float width = [[photo objectForKey:@"width"] floatValue];
float height = [[photo objectForKey:@"height"] floatValue];

// calculate the real size of the image. It will keep the aspect ratio.
float realWidth = 0;
float realHeight = 0;

if(width/height >= 1) {
// portrait or square
realWidth = 640;
realHeight = height/width*640;
} else {
// landscape
realHeight = 960;
realWidth = width/height*960;
}
Photo* obj = [[[Photo alloc]
initWithURL:[NSString stringWithFormat:@"%@", [photo objectForKey:@"path640x960"]]
smallURL:[NSString stringWithFormat:@"%@",[photo objectForKey:@"path200x200"]]
size:CGSizeMake(realWidth, realHeight) caption:localTitle] autorelease];
obj.index=photoId;
obj.photoSource = self;
if (isFirst == YES){
isFirst = NO;
}else{
[localPhotos addObject:obj];
}
}
}
[self.photos addObjectsFromArray:localPhotos];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

#ifdef TEST_FLIGHT_ENABLED
[TestFlight passCheckpoint:@"Gallery Loaded"];
#endif

// Finishes
isLoading = NO;
[localPhotos release];
[_delegates perform:@selector(modelDidFinishLoad:) withObject:self];

}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTPhotoSource

Expand All @@ -61,12 +198,12 @@ - (NSInteger)numberOfPhotos {
}

- (NSInteger)maxPhotoIndex {
return number;
return actualMaxPhotoIndex-1;
}

- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex {
if (photoIndex < _photos.count) {
id photo = [_photos objectAtIndex:photoIndex];
if (photoIndex < self.photos.count) {
id photo = [self.photos objectAtIndex:photoIndex];
if (photo == [NSNull null]) {
return nil;
} else {
Expand Down

0 comments on commit a08da4b

Please sign in to comment.