Handle correctly multiple loading wheels when user attachs media to room

This commit is contained in:
giomfo 2014-11-28 17:02:46 +01:00
parent 6422f1bb11
commit c724a72325
3 changed files with 65 additions and 26 deletions

View file

@ -21,7 +21,12 @@
@property (strong, nonatomic) NSString *placeholder;
@property (strong, nonatomic) NSString *imageURL;
// Use this boolean to hide activity indicator during image downloading
@property (nonatomic) BOOL hideActivityIndicator;
// Information about the media represented by this image (image, video...)
@property (strong, nonatomic) NSDictionary *mediaInfo;
@end

View file

@ -25,6 +25,54 @@
@implementation CustomImageView
- (void)dealloc {
if (imageLoader) {
[MediaManager cancel:imageLoader];
imageLoader = nil;
}
if (loadingWheel) {
[loadingWheel removeFromSuperview];
loadingWheel = nil;
}
}
- (void)startActivityIndicator {
// Add activity indicator if none
if (loadingWheel == nil) {
loadingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self addSubview:loadingWheel];
}
// Adjust position
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
loadingWheel.center = center;
// Adjust color
if ([self.backgroundColor isEqual:[UIColor blackColor]]) {
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
} else {
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
// Start
[loadingWheel startAnimating];
}
- (void)stopActivityIndicator {
if (loadingWheel) {
[loadingWheel stopAnimating];
}
}
#pragma mark -
- (void)setHideActivityIndicator:(BOOL)hideActivityIndicator {
_hideActivityIndicator = hideActivityIndicator;
if (hideActivityIndicator) {
[self stopActivityIndicator];
} else if (imageLoader) {
// Loading is in progress, start activity indicator
[self startActivityIndicator];
}
}
- (void)setImageURL:(NSString *)imageURL {
// Cancel media loader in progress (if any)
if (imageLoader) {
@ -42,41 +90,20 @@
}
// Consider provided url to update image view
if (imageURL) {
// Start loading animation
if (loadingWheel == nil) {
loadingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
loadingWheel.center = center;
[self addSubview:loadingWheel];
}
if ([self.backgroundColor isEqual:[UIColor blackColor]]) {
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
} else {
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
[loadingWheel startAnimating];
// Load picture
if (!_hideActivityIndicator) {
[self startActivityIndicator];
}
imageLoader = [MediaManager loadPicture:imageURL
success:^(UIImage *image) {
[loadingWheel stopAnimating];
[self stopActivityIndicator];
self.image = image;
}
failure:^(NSError *error) {
[loadingWheel stopAnimating];
[self stopActivityIndicator];
NSLog(@"Failed to download image (%@): %@", imageURL, error);
}];
}
}
- (void)dealloc {
if (imageLoader) {
[MediaManager cancel:imageLoader];
imageLoader = nil;
}
if (loadingWheel) {
[loadingWheel removeFromSuperview];
loadingWheel = nil;
}
}
@end

View file

@ -918,12 +918,19 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
if (message.messageType != RoomMessageTypeText) {
cell.messageTextView.attributedText = nil; // Note: Text view is used as attachment background view
cell.attachmentView.hidden = NO;
// Update image view frame in order to center loading wheel (if any)
CGRect frame = cell.attachmentView.frame;
frame.size.width = contentSize.width;
frame.size.height = contentSize.height;
cell.attachmentView.frame = frame;
// Fade attachments during upload
if (message.isUploadInProgress) {
cell.attachmentView.alpha = 0.5;
[((OutgoingMessageTableCell*)cell).activityIndicator startAnimating];
cell.attachmentView.hideActivityIndicator = YES;
} else {
cell.attachmentView.alpha = 1;
cell.attachmentView.hideActivityIndicator = NO;
}
NSString *url = message.thumbnailURL;
if (!url && message.messageType == RoomMessageTypeImage) {