Permalink
Browse files

Improve robustness of photo viewer

  • Loading branch information...
1 parent 5a1bad1 commit 8ecc4b4c143147ebe5c20ed44af31673af221d6e @joehewitt joehewitt committed Feb 10, 2009
@@ -26,7 +26,7 @@ - (void)viewDidLoad {
}
- (void)viewDidDisappear:(BOOL)animated {
- [T3URLCache sharedCache].paused = NO;
+ [T3URLRequestQueue mainQueue].suspended = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
@@ -59,26 +59,26 @@ - (void)dealloc {
// UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- [T3URLCache sharedCache].paused = YES;
+ [T3URLRequestQueue mainQueue].suspended = YES;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
- [T3URLCache sharedCache].paused = NO;
+ [T3URLRequestQueue mainQueue].suspended = NO;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- [T3URLCache sharedCache].paused = NO;
+ [T3URLRequestQueue mainQueue].suspended = NO;
}
- (BOOL)scrollViewWillScrollToTop:(UIScrollView *)scrollView {
- [T3URLCache sharedCache].paused = YES;
+ [T3URLRequestQueue mainQueue].suspended = YES;
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
- [T3URLCache sharedCache].paused = NO;
+ [T3URLRequestQueue mainQueue].suspended = NO;
}
@end
@@ -13,7 +13,7 @@ typedef enum {
@interface MockPhotoSource : NSObject <T3PhotoSource> {
MockPhotoSourceType _type;
- id<T3PhotoSourceDelegate> _delegate;
+ T3URLRequest* _request;
NSString* _title;
NSMutableArray* _photos;
NSArray* _tempPhotos;
@@ -42,7 +42,7 @@ - (void)fakeLoadReady {
_isInvalid = T3Valid;
if (_type & MockPhotoSourceLoadError) {
- [_delegate photoSource:self loadDidFailWithError:nil];
+ [_request.delegate request:_request didFailWithError:nil];
} else {
NSMutableArray* newPhotos = [NSMutableArray array];
@@ -68,11 +68,11 @@ - (void)fakeLoadReady {
}
}
- [_delegate photoSourceLoaded:self];
+ [_request.delegate request:_request loadedData:nil media:nil];
}
- [_delegate release];
- _delegate = nil;
+ [_request release];
+ _request = nil;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -126,15 +126,11 @@ - (BOOL)loading {
}
}
-- (NSUInteger)indexOfPhoto:(id<T3Photo>)photo {
- return [_photos indexOfObject:photo];
-}
-
-- (void)loadPhotosFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
- cachePolicy:(T3URLRequestCachePolicy)cachePolicy delegate:(id<T3PhotoSourceDelegate>)delegate {
- if (cachePolicy & T3URLRequestCachePolicyNetwork) {
- _delegate = [delegate retain];
- [_delegate photoSourceLoading:self fromIndex:fromIndex toIndex:toIndex];
+- (void)loadPhotos:(T3URLRequest*)request fromIndex:(NSUInteger)fromIndex
+ toIndex:(NSUInteger)toIndex {
+ if (request.cachePolicy & T3URLRequestCachePolicyNetwork) {
+ _request = [request retain];
+ [_request.delegate requestLoading:_request];
_fakeLoadTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(fakeLoadReady) userInfo:nil repeats:NO];
View
@@ -54,6 +54,8 @@ - (void)setImage:(UIImage*)image {
// T3URLRequestDelegate
- (void)requestLoading:(T3URLRequest*)request {
+ _request = [request retain];
+
if ([_delegate respondsToSelector:@selector(imageViewLoading:)]) {
[_delegate imageViewLoading:self];
}
@@ -117,10 +119,10 @@ - (void)reload {
if (_request)
return;
- _request = [[T3URLRequest alloc] initWithURL:_url delegate:self];
- _request.shouldConvertToMedia = YES;
+ T3URLRequest* request = [[[T3URLRequest alloc] initWithURL:_url delegate:self] autorelease];
+ request.shouldConvertToMedia = YES;
- if (_url && ![_request send]) {
+ if (_url && ![request send]) {
// Put the default image in place while waiting for the request to load
if (_defaultImage && self.image != _defaultImage) {
self.image = _defaultImage;
View
@@ -95,9 +95,11 @@ - (void)imageViewLoading:(T3ImageView*)imageView {
}
- (void)imageView:(T3ImageView*)imageView loaded:(UIImage*)image {
- [self showProgress:-1];
- [self showStatus:nil];
-
+ if (!_photo.photoSource.loading) {
+ [self showProgress:-1];
+ [self showStatus:nil];
+ }
+
if (!_photo.size.width) {
_photo.size = image.size;
}
@@ -120,7 +122,7 @@ - (void)setPhoto:(id<T3Photo>)photo {
self.url = nil;
- if (!_photo) {
+ if (!_photo || _photo.photoSource.loading) {
[self showProgress:0];
} else {
[self showStatus:nil];
@@ -56,7 +56,7 @@ - (T3PhotoView*)centerPhotoView {
}
- (void)updateTitle {
- if (!_photoSource.numberOfPhotos) {
+ if (!_photoSource.numberOfPhotos || _photoSource.numberOfPhotos == T3_INFINITE_PHOTO_INDEX) {
self.title = _photoSource.title;
} else {
self.title = [NSString stringWithFormat:
@@ -100,7 +100,7 @@ - (void)loadImages {
}
- (void)moveToPhotoAtIndex:(NSInteger)photoIndex withDelay:(BOOL)withDelay {
- _centerPhotoIndex = photoIndex;
+ _centerPhotoIndex = photoIndex == T3_NULL_PHOTO_INDEX ? 0 : photoIndex;
[_centerPhoto release];
_centerPhoto = [[_photoSource photoAtIndex:_centerPhotoIndex] retain];
_delayLoad = withDelay;
@@ -113,10 +113,11 @@ - (void)showPhoto:(id<T3Photo>)photo inView:(T3PhotoView*)photoView {
}
}
-- (void)loadPhotos {
+- (void)loadPhotosFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
if (!_photoSource.loading) {
- [_photoSource loadPhotosFromIndex:_photoSource.maxPhotoIndex+1 toIndex:-1
- cachePolicy:T3URLRequestCachePolicyAny delegate:self];
+ T3URLRequest* request = [T3URLRequest request];
+ request.delegate = self;
+ [_photoSource loadPhotos:request fromIndex:fromIndex toIndex:toIndex];
}
}
@@ -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 {
UINavigationBar* bar = self.navigationController.navigationBar;
return bar ? bar.alpha != 0 : 1;
@@ -271,7 +280,10 @@ - (void)updateContent {
if (_photoSource.loading) {
self.contentState = T3ContentActivity;
} 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 {
if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady;
@@ -281,16 +293,14 @@ - (void)updateContent {
}
}
-- (void)refreshContent {
- if (_photoSource.isInvalid && !_photoSource.loading) {
- [_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax
- cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
- }
-}
+//- (void)refreshContent {
+// if (_photoSource.isInvalid && !_photoSource.loading) {
+// [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX];
+// }
+//}
- (void)reloadContent {
- [_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax
- cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
+ [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX];
}
- (void)updateView {
@@ -349,16 +359,15 @@ - (NSString*)subtitleForError:(NSError*)error {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-// T3PhotoSourceDelegate
+// T3URLRequestDelegate
-- (void)photoSourceLoading:(id<T3PhotoSource>)photoSource fromIndex:(NSUInteger)fromIndex
- toIndex:(NSUInteger)toIndex {
+- (void)requestLoading:(T3URLRequest*)request {
self.contentState |= T3ContentActivity;
}
-- (void)photoSourceLoaded:(id<T3PhotoSource>)photoSource {
- if (_centerPhotoIndex >= photoSource.numberOfPhotos) {
- [self moveToPhotoAtIndex:photoSource.numberOfPhotos - 1 withDelay:NO];
+- (void)request:(T3URLRequest*)request loadedData:(NSData*)data media:(id)media {
+ if (_centerPhotoIndex >= _photoSource.numberOfPhotos) {
+ [self moveToPhotoAtIndex:_photoSource.numberOfPhotos - 1 withDelay:NO];
[_scrollView reloadData];
} else {
[self refreshVisiblePhotoViews];
@@ -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 |= T3ContentError;
self.contentError = error;
}
+- (void)requestCancelled:(T3URLRequest*)request {
+ self.contentState &= ~T3ContentActivity;
+ self.contentState |= T3ContentError;
+}
+
///////////////////////////////////////////////////////////////////////////////////////////////////
// T3ScrollViewDelegate
@@ -128,12 +128,19 @@ - (void)showObject:(id<T3Object>)object inView:(NSString*)viewType withState:(NS
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 {
if (_photoSource.loading) {
self.contentState = T3ContentActivity;
} else if (_photoSource.isInvalid) {
- [_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax
- cachePolicy:T3URLRequestCachePolicyMemory|T3URLRequestCachePolicyDisk delegate:self];
+ [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:YES];
} else if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady;
} else {
@@ -143,14 +150,12 @@ - (void)updateContent {
- (void)refreshContent {
if (_photoSource.isInvalid && !_photoSource.loading) {
- [_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax
- cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
+ [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:NO];
}
}
- (void)reloadContent {
- [_photoSource loadPhotosFromIndex:0 toIndex:NSUIntegerMax
- cachePolicy:T3URLRequestCachePolicyNetwork delegate:self];
+ [self loadPhotosFromIndex:0 toIndex:T3_INFINITE_PHOTO_INDEX fromCache:NO];
}
- (void)updateView {
@@ -217,27 +222,31 @@ - (void)tableView:(UITableView*)tableView didSelectPhoto:(id<T3Photo>)photo {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-// T3PhotoSourceDelegate
+// T3URLRequestDelegate
-- (void)photoSourceLoading:(id<T3PhotoSource>)photoSource fromIndex:(NSUInteger)fromIndex
- toIndex:(NSUInteger)toIndex {
+- (void)requestLoading:(T3URLRequest*)request {
self.contentState |= T3ContentActivity;
}
-- (void)photoSourceLoaded:(id<T3PhotoSource>)photoSource {
+- (void)request:(T3URLRequest*)request loadedData:(NSData*)data media:(id)media {
if (_photoSource.numberOfPhotos) {
self.contentState = T3ContentReady;
} else {
self.contentState = T3ContentNone;
}
}
-- (void)photoSource:(id<T3PhotoSource>)photoSource loadDidFailWithError:(NSError*)error {
+- (void)request:(T3URLRequest*)request didFailWithError:(NSError*)error {
self.contentState &= ~T3ContentActivity;
self.contentState |= T3ContentError;
self.contentError = error;
}
+- (void)requestCancelled:(T3URLRequest*)request {
+ self.contentState &= ~T3ContentActivity;
+ self.contentState |= T3ContentError;
+}
+
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setPhotoSource:(id<T3PhotoSource>)photoSource {
Oops, something went wrong.

0 comments on commit 8ecc4b4

Please sign in to comment.