diff --git a/matrixConsole/View/CustomImageView.h b/matrixConsole/View/CustomImageView.h index 9095727d5..303436354 100644 --- a/matrixConsole/View/CustomImageView.h +++ b/matrixConsole/View/CustomImageView.h @@ -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 diff --git a/matrixConsole/View/CustomImageView.m b/matrixConsole/View/CustomImageView.m index c231e8137..9bf9967fc 100644 --- a/matrixConsole/View/CustomImageView.m +++ b/matrixConsole/View/CustomImageView.m @@ -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 \ No newline at end of file diff --git a/matrixConsole/ViewController/RoomViewController.m b/matrixConsole/ViewController/RoomViewController.m index 2f3049477..23b19a72d 100644 --- a/matrixConsole/ViewController/RoomViewController.m +++ b/matrixConsole/ViewController/RoomViewController.m @@ -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) {