Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Improve robustness of photo viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
joehewitt committed Feb 10, 2009
1 parent 5a1bad1 commit 8ecc4b4
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 117 deletions.
12 changes: 6 additions & 6 deletions samples/T3Catalog/Classes/ImageTest2Controller.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (void)viewDidLoad {
} }


- (void)viewDidDisappear:(BOOL)animated { - (void)viewDidDisappear:(BOOL)animated {
[T3URLCache sharedCache].paused = NO; [T3URLRequestQueue mainQueue].suspended = NO;
} }


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Expand Down Expand Up @@ -59,26 +59,26 @@ - (void)dealloc {
// UIScrollViewDelegate // UIScrollViewDelegate


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[T3URLCache sharedCache].paused = YES; [T3URLRequestQueue mainQueue].suspended = YES;
} }


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) { if (!decelerate) {
[T3URLCache sharedCache].paused = NO; [T3URLRequestQueue mainQueue].suspended = NO;
} }
} }


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[T3URLCache sharedCache].paused = NO; [T3URLRequestQueue mainQueue].suspended = NO;
} }


- (BOOL)scrollViewWillScrollToTop:(UIScrollView *)scrollView { - (BOOL)scrollViewWillScrollToTop:(UIScrollView *)scrollView {
[T3URLCache sharedCache].paused = YES; [T3URLRequestQueue mainQueue].suspended = YES;
return YES; return YES;
} }


- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView { - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
[T3URLCache sharedCache].paused = NO; [T3URLRequestQueue mainQueue].suspended = NO;
} }


@end @end
Expand Down
2 changes: 1 addition & 1 deletion samples/T3Catalog/Classes/MockPhotoSource.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef enum {


@interface MockPhotoSource : NSObject <T3PhotoSource> { @interface MockPhotoSource : NSObject <T3PhotoSource> {
MockPhotoSourceType _type; MockPhotoSourceType _type;
id<T3PhotoSourceDelegate> _delegate; T3URLRequest* _request;
NSString* _title; NSString* _title;
NSMutableArray* _photos; NSMutableArray* _photos;
NSArray* _tempPhotos; NSArray* _tempPhotos;
Expand Down
22 changes: 9 additions & 13 deletions samples/T3Catalog/Classes/MockPhotoSource.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (void)fakeLoadReady {
_isInvalid = T3Valid; _isInvalid = T3Valid;


if (_type & MockPhotoSourceLoadError) { if (_type & MockPhotoSourceLoadError) {
[_delegate photoSource:self loadDidFailWithError:nil]; [_request.delegate request:_request didFailWithError:nil];
} else { } else {
NSMutableArray* newPhotos = [NSMutableArray array]; NSMutableArray* newPhotos = [NSMutableArray array];


Expand All @@ -68,11 +68,11 @@ - (void)fakeLoadReady {
} }
} }


[_delegate photoSourceLoaded:self]; [_request.delegate request:_request loadedData:nil media:nil];
} }


[_delegate release]; [_request release];
_delegate = nil; _request = nil;
} }


/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -126,15 +126,11 @@ - (BOOL)loading {
} }
} }


- (NSUInteger)indexOfPhoto:(id<T3Photo>)photo { - (void)loadPhotos:(T3URLRequest*)request fromIndex:(NSUInteger)fromIndex
return [_photos indexOfObject:photo]; toIndex:(NSUInteger)toIndex {
} if (request.cachePolicy & T3URLRequestCachePolicyNetwork) {

_request = [request retain];
- (void)loadPhotosFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex [_request.delegate requestLoading:_request];
cachePolicy:(T3URLRequestCachePolicy)cachePolicy delegate:(id<T3PhotoSourceDelegate>)delegate {
if (cachePolicy & T3URLRequestCachePolicyNetwork) {
_delegate = [delegate retain];
[_delegate photoSourceLoading:self fromIndex:fromIndex toIndex:toIndex];


_fakeLoadTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self _fakeLoadTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(fakeLoadReady) userInfo:nil repeats:NO]; selector:@selector(fakeLoadReady) userInfo:nil repeats:NO];
Expand Down
8 changes: 5 additions & 3 deletions src/T3ImageView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ - (void)setImage:(UIImage*)image {
// T3URLRequestDelegate // T3URLRequestDelegate


- (void)requestLoading:(T3URLRequest*)request { - (void)requestLoading:(T3URLRequest*)request {
_request = [request retain];

if ([_delegate respondsToSelector:@selector(imageViewLoading:)]) { if ([_delegate respondsToSelector:@selector(imageViewLoading:)]) {
[_delegate imageViewLoading:self]; [_delegate imageViewLoading:self];
} }
Expand Down Expand Up @@ -117,10 +119,10 @@ - (void)reload {
if (_request) if (_request)
return; return;


_request = [[T3URLRequest alloc] initWithURL:_url delegate:self]; T3URLRequest* request = [[[T3URLRequest alloc] initWithURL:_url delegate:self] autorelease];
_request.shouldConvertToMedia = YES; request.shouldConvertToMedia = YES;


if (_url && ![_request send]) { if (_url && ![request send]) {
// Put the default image in place while waiting for the request to load // Put the default image in place while waiting for the request to load
if (_defaultImage && self.image != _defaultImage) { if (_defaultImage && self.image != _defaultImage) {
self.image = _defaultImage; self.image = _defaultImage;
Expand Down
10 changes: 6 additions & 4 deletions src/T3PhotoView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ - (void)imageViewLoading:(T3ImageView*)imageView {
} }


- (void)imageView:(T3ImageView*)imageView loaded:(UIImage*)image { - (void)imageView:(T3ImageView*)imageView loaded:(UIImage*)image {
[self showProgress:-1]; if (!_photo.photoSource.loading) {
[self showStatus:nil]; [self showProgress:-1];

[self showStatus:nil];
}

if (!_photo.size.width) { if (!_photo.size.width) {
_photo.size = image.size; _photo.size = image.size;
} }
Expand All @@ -120,7 +122,7 @@ - (void)setPhoto:(id<T3Photo>)photo {


self.url = nil; self.url = nil;


if (!_photo) { if (!_photo || _photo.photoSource.loading) {
[self showProgress:0]; [self showProgress:0];
} else { } else {
[self showStatus:nil]; [self showStatus:nil];
Expand Down
58 changes: 37 additions & 21 deletions src/T3PhotoViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (T3PhotoView*)centerPhotoView {
} }


- (void)updateTitle { - (void)updateTitle {
if (!_photoSource.numberOfPhotos) { if (!_photoSource.numberOfPhotos || _photoSource.numberOfPhotos == T3_INFINITE_PHOTO_INDEX) {
self.title = _photoSource.title; self.title = _photoSource.title;
} else { } else {
self.title = [NSString stringWithFormat: self.title = [NSString stringWithFormat:
Expand Down Expand Up @@ -100,7 +100,7 @@ - (void)loadImages {
} }


- (void)moveToPhotoAtIndex:(NSInteger)photoIndex withDelay:(BOOL)withDelay { - (void)moveToPhotoAtIndex:(NSInteger)photoIndex withDelay:(BOOL)withDelay {
_centerPhotoIndex = photoIndex; _centerPhotoIndex = photoIndex == T3_NULL_PHOTO_INDEX ? 0 : photoIndex;
[_centerPhoto release]; [_centerPhoto release];
_centerPhoto = [[_photoSource photoAtIndex:_centerPhotoIndex] retain]; _centerPhoto = [[_photoSource photoAtIndex:_centerPhotoIndex] retain];
_delayLoad = withDelay; _delayLoad = withDelay;
Expand All @@ -113,10 +113,11 @@ - (void)showPhoto:(id<T3Photo>)photo inView:(T3PhotoView*)photoView {
} }
} }


- (void)loadPhotos { - (void)loadPhotosFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
if (!_photoSource.loading) { if (!_photoSource.loading) {
[_photoSource loadPhotosFromIndex:_photoSource.maxPhotoIndex+1 toIndex:-1 T3URLRequest* request = [T3URLRequest request];
cachePolicy:T3URLRequestCachePolicyAny delegate:self]; request.delegate = self;
[_photoSource loadPhotos:request fromIndex:fromIndex toIndex:toIndex];
} }
} }


Expand All @@ -129,6 +130,14 @@ - (void)refreshVisiblePhotoViews {
} }
} }


- (void)resetVisiblePhotoViews {
NSDictionary* photoViews = _scrollView.visiblePages;
for (NSNumber* key in photoViews.keyEnumerator) {
T3PhotoView* photoView = [photoViews objectForKey:key];
[photoView showProgress:-1];
}
}

- (BOOL)isShowingChrome { - (BOOL)isShowingChrome {
UINavigationBar* bar = self.navigationController.navigationBar; UINavigationBar* bar = self.navigationController.navigationBar;
return bar ? bar.alpha != 0 : 1; return bar ? bar.alpha != 0 : 1;
Expand Down Expand Up @@ -271,7 +280,10 @@ - (void)updateContent {
if (_photoSource.loading) { if (_photoSource.loading) {
self.contentState = T3ContentActivity; self.contentState = T3ContentActivity;
} else if (!_centerPhoto) { } else if (!_centerPhoto) {
[self loadPhotos]; [self loadPhotosFromIndex:_photoSource.isInvalid ? 0 : _photoSource.maxPhotoIndex+1
toIndex:T3_INFINITE_PHOTO_INDEX];
} else if (_photoSource.numberOfPhotos == T3_INFINITE_PHOTO_INDEX) {
[self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX];
} else { } else {
if (_photoSource.numberOfPhotos) { if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady; self.contentState = T3ContentReady;
Expand All @@ -281,16 +293,14 @@ - (void)updateContent {
} }
} }


- (void)refreshContent { //- (void)refreshContent {
if (_photoSource.isInvalid && !_photoSource.loading) { // if (_photoSource.isInvalid && !_photoSource.loading) {
[_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax // [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX];
cachePolicy:T3URLRequestCachePolicyNetwork delegate:self]; // }
} //}
}


- (void)reloadContent { - (void)reloadContent {
[_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX];
cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
} }


- (void)updateView { - (void)updateView {
Expand Down Expand Up @@ -349,16 +359,15 @@ - (NSString*)subtitleForError:(NSError*)error {
} }


/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// T3PhotoSourceDelegate // T3URLRequestDelegate


- (void)photoSourceLoading:(id<T3PhotoSource>)photoSource fromIndex:(NSUInteger)fromIndex - (void)requestLoading:(T3URLRequest*)request {
toIndex:(NSUInteger)toIndex {
self.contentState |= T3ContentActivity; self.contentState |= T3ContentActivity;
} }


- (void)photoSourceLoaded:(id<T3PhotoSource>)photoSource { - (void)request:(T3URLRequest*)request loadedData:(NSData*)data media:(id)media {
if (_centerPhotoIndex >= photoSource.numberOfPhotos) { if (_centerPhotoIndex >= _photoSource.numberOfPhotos) {
[self moveToPhotoAtIndex:photoSource.numberOfPhotos - 1 withDelay:NO]; [self moveToPhotoAtIndex:_photoSource.numberOfPhotos - 1 withDelay:NO];
[_scrollView reloadData]; [_scrollView reloadData];
} else { } else {
[self refreshVisiblePhotoViews]; [self refreshVisiblePhotoViews];
Expand All @@ -371,12 +380,19 @@ - (void)photoSourceLoaded:(id<T3PhotoSource>)photoSource {
} }
} }


- (void)photoSource:(id<T3PhotoSource>)photoSource loadDidFailWithError:(NSError*)error { - (void)request:(T3URLRequest*)request didFailWithError:(NSError*)error {
[self resetVisiblePhotoViews];

self.contentState &= ~T3ContentActivity; self.contentState &= ~T3ContentActivity;
self.contentState |= T3ContentError; self.contentState |= T3ContentError;
self.contentError = error; self.contentError = error;
} }


- (void)requestCancelled:(T3URLRequest*)request {
self.contentState &= ~T3ContentActivity;
self.contentState |= T3ContentError;
}

/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// T3ScrollViewDelegate // T3ScrollViewDelegate


Expand Down
31 changes: 20 additions & 11 deletions src/T3ThumbsViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ - (void)showObject:(id<T3Object>)object inView:(NSString*)viewType withState:(NS
self.photoSource = (id<T3PhotoSource>)object; self.photoSource = (id<T3PhotoSource>)object;
} }


- (void)loadPhotosFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
fromCache:(BOOL)fromCache {
T3URLRequest* request = [T3URLRequest request];
request.delegate = self;
request.cachePolicy = fromCache ? T3URLRequestCachePolicyAny : T3URLRequestCachePolicyNetwork;
[_photoSource loadPhotos:request fromIndex:fromIndex toIndex:toIndex];
}

- (void)updateContent { - (void)updateContent {
if (_photoSource.loading) { if (_photoSource.loading) {
self.contentState = T3ContentActivity; self.contentState = T3ContentActivity;
} else if (_photoSource.isInvalid) { } else if (_photoSource.isInvalid) {
[_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:YES];
cachePolicy:T3URLRequestCachePolicyMemory|T3URLRequestCachePolicyDisk delegate:self];
} else if (_photoSource.numberOfPhotos) { } else if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady; self.contentState = T3ContentReady;
} else { } else {
Expand All @@ -143,14 +150,12 @@ - (void)updateContent {


- (void)refreshContent { - (void)refreshContent {
if (_photoSource.isInvalid && !_photoSource.loading) { if (_photoSource.isInvalid && !_photoSource.loading) {
[_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:NO];
cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
} }
} }


- (void)reloadContent { - (void)reloadContent {
[_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:NO];
cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
} }


- (void)updateView { - (void)updateView {
Expand Down Expand Up @@ -217,27 +222,31 @@ - (void)tableView:(UITableView*)tableView didSelectPhoto:(id<T3Photo>)photo {
} }


/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// T3PhotoSourceDelegate // T3URLRequestDelegate


- (void)photoSourceLoading:(id<T3PhotoSource>)photoSource fromIndex:(NSUInteger)fromIndex - (void)requestLoading:(T3URLRequest*)request {
toIndex:(NSUInteger)toIndex {
self.contentState |= T3ContentActivity; self.contentState |= T3ContentActivity;
} }


- (void)photoSourceLoaded:(id<T3PhotoSource>)photoSource { - (void)request:(T3URLRequest*)request loadedData:(NSData*)data media:(id)media {
if (_photoSource.numberOfPhotos) { if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady; self.contentState = T3ContentReady;
} else { } else {
self.contentState = T3ContentNone; self.contentState = T3ContentNone;
} }
} }


- (void)photoSource:(id<T3PhotoSource>)photoSource loadDidFailWithError:(NSError*)error { - (void)request:(T3URLRequest*)request didFailWithError:(NSError*)error {
self.contentState &= ~T3ContentActivity; self.contentState &= ~T3ContentActivity;
self.contentState |= T3ContentError; self.contentState |= T3ContentError;
self.contentError = error; self.contentError = error;
} }


- (void)requestCancelled:(T3URLRequest*)request {
self.contentState &= ~T3ContentActivity;
self.contentState |= T3ContentError;
}

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


- (void)setPhotoSource:(id<T3PhotoSource>)photoSource { - (void)setPhotoSource:(id<T3PhotoSource>)photoSource {
Expand Down
Loading

0 comments on commit 8ecc4b4

Please sign in to comment.