2014-11-24 09:38:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "CustomImageView.h"
|
|
|
|
#import "MediaManager.h"
|
2014-12-22 10:52:29 +00:00
|
|
|
#import "AppDelegate.h"
|
2015-01-06 17:12:45 +00:00
|
|
|
#import "PieChartView.h"
|
2014-11-24 09:38:23 +00:00
|
|
|
|
|
|
|
@interface CustomImageView () {
|
2015-01-08 15:01:25 +00:00
|
|
|
NSString *imageURL;
|
|
|
|
UIImage *currentImage;
|
2014-12-22 10:52:29 +00:00
|
|
|
|
2015-01-06 17:12:45 +00:00
|
|
|
// the loading view is composed with the spinner and a pie chart
|
|
|
|
// the spinner is display until progress > 0
|
2015-01-08 15:01:25 +00:00
|
|
|
UIView *loadingView;
|
2015-01-06 17:12:45 +00:00
|
|
|
UIActivityIndicatorView *waitingDownloadSpinner;
|
|
|
|
PieChartView *pieChartView;
|
2015-01-08 15:01:25 +00:00
|
|
|
UILabel *progressInfoLabel;
|
2015-01-06 17:12:45 +00:00
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
// validation buttons
|
2015-01-08 15:01:25 +00:00
|
|
|
UIButton *leftButton;
|
|
|
|
UIButton *rightButton;
|
2014-12-22 10:52:29 +00:00
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
NSString *leftButtonTitle;
|
|
|
|
NSString *rightButtonTitle;
|
2014-12-22 10:52:29 +00:00
|
|
|
|
|
|
|
blockCustomImageView_onClick leftHandler;
|
|
|
|
blockCustomImageView_onClick rightHandler;
|
|
|
|
|
|
|
|
UIView* bottomBarView;
|
2015-01-06 08:41:44 +00:00
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
// Subviews
|
|
|
|
UIScrollView *scrollView;
|
|
|
|
UIImageView *imageView;
|
2015-01-06 08:41:44 +00:00
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
BOOL useFullScreen;
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CustomImageView
|
2015-01-19 13:33:57 +00:00
|
|
|
@synthesize stretchable, mediaFolder;
|
2014-11-24 09:38:23 +00:00
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
#define CUSTOM_IMAGE_VIEW_BUTTON_WIDTH 100
|
|
|
|
|
|
|
|
- (id)initWithFrame:(CGRect)frame {
|
|
|
|
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
leftButtonTitle = nil;
|
|
|
|
leftHandler = nil;
|
|
|
|
rightButtonTitle = nil;
|
|
|
|
rightHandler = nil;
|
|
|
|
|
|
|
|
self.backgroundColor = [UIColor blackColor];
|
|
|
|
self.contentMode = UIViewContentModeScaleAspectFit;
|
|
|
|
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
- (void)dealloc {
|
2015-01-08 15:01:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-06 17:12:45 +00:00
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
[self stopActivityIndicator];
|
|
|
|
|
2015-01-06 17:12:45 +00:00
|
|
|
if (loadingView) {
|
|
|
|
[loadingView removeFromSuperview];
|
|
|
|
loadingView = nil;
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
if (bottomBarView) {
|
|
|
|
[bottomBarView removeFromSuperview];
|
|
|
|
bottomBarView = nil;
|
|
|
|
}
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startActivityIndicator {
|
2015-01-06 17:12:45 +00:00
|
|
|
// create the views if they don't exist
|
|
|
|
if (!waitingDownloadSpinner) {
|
|
|
|
waitingDownloadSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2015-01-06 17:12:45 +00:00
|
|
|
CGRect frame = waitingDownloadSpinner.frame;
|
2015-01-06 10:08:29 +00:00
|
|
|
frame.size.width += 30;
|
|
|
|
frame.size.height += 30;
|
2015-01-06 17:12:45 +00:00
|
|
|
waitingDownloadSpinner.bounds = frame;
|
|
|
|
[waitingDownloadSpinner.layer setCornerRadius:5];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loadingView) {
|
|
|
|
loadingView = [[UIView alloc] init];
|
|
|
|
loadingView.frame = waitingDownloadSpinner.bounds;
|
2015-01-07 14:49:29 +00:00
|
|
|
waitingDownloadSpinner.frame = waitingDownloadSpinner.bounds;
|
2015-01-06 17:12:45 +00:00
|
|
|
[loadingView addSubview:waitingDownloadSpinner];
|
|
|
|
loadingView.backgroundColor = [UIColor clearColor];
|
|
|
|
[self addSubview:loadingView];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pieChartView) {
|
|
|
|
pieChartView = [[PieChartView alloc] init];
|
|
|
|
pieChartView.frame = loadingView.bounds;
|
|
|
|
pieChartView.progress = 0;
|
2015-01-07 09:45:35 +00:00
|
|
|
pieChartView.progressColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.25];
|
2015-01-06 17:12:45 +00:00
|
|
|
pieChartView.unprogressColor = [UIColor clearColor];
|
|
|
|
|
|
|
|
[loadingView addSubview:pieChartView];
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2015-01-07 14:49:29 +00:00
|
|
|
// display the download statistics
|
|
|
|
if (useFullScreen && !progressInfoLabel) {
|
|
|
|
progressInfoLabel = [[UILabel alloc] init];
|
|
|
|
progressInfoLabel.backgroundColor = [UIColor whiteColor];
|
|
|
|
progressInfoLabel.textColor = [UIColor blackColor];
|
|
|
|
progressInfoLabel.font = [UIFont systemFontOfSize:8];
|
|
|
|
progressInfoLabel.alpha = 0.25;
|
|
|
|
progressInfoLabel.text = @"";
|
|
|
|
progressInfoLabel.numberOfLines = 0;
|
|
|
|
[progressInfoLabel sizeToFit];
|
|
|
|
[self addSubview:progressInfoLabel];
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:12:45 +00:00
|
|
|
// initvalue
|
|
|
|
loadingView.hidden = NO;
|
|
|
|
pieChartView.progress = 0;
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
// Adjust color
|
|
|
|
if ([self.backgroundColor isEqual:[UIColor blackColor]]) {
|
2015-01-06 17:12:45 +00:00
|
|
|
waitingDownloadSpinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
|
2015-01-06 10:08:29 +00:00
|
|
|
// a preview image could be displayed
|
|
|
|
// ensure that the white spinner is visible
|
|
|
|
// it could be drawn on a white area
|
2015-01-06 17:12:45 +00:00
|
|
|
waitingDownloadSpinner.backgroundColor = [UIColor darkGrayColor];
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
} else {
|
2015-01-06 17:12:45 +00:00
|
|
|
waitingDownloadSpinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
2015-01-06 17:12:45 +00:00
|
|
|
|
|
|
|
// ensure that the spinner is drawn at the top
|
|
|
|
[loadingView.superview bringSubviewToFront:loadingView];
|
2015-01-06 10:08:29 +00:00
|
|
|
|
|
|
|
// Adjust position
|
|
|
|
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
|
2015-01-06 17:12:45 +00:00
|
|
|
loadingView.center = center;
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
// Start
|
2015-01-06 17:12:45 +00:00
|
|
|
[waitingDownloadSpinner startAnimating];
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopActivityIndicator {
|
2015-01-06 17:12:45 +00:00
|
|
|
if (waitingDownloadSpinner && waitingDownloadSpinner.isAnimating) {
|
|
|
|
[waitingDownloadSpinner stopAnimating];
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
2015-01-06 17:12:45 +00:00
|
|
|
|
|
|
|
pieChartView.progress = 0;
|
|
|
|
loadingView.hidden = YES;
|
2015-01-07 14:49:29 +00:00
|
|
|
|
|
|
|
if (progressInfoLabel) {
|
|
|
|
[progressInfoLabel removeFromSuperview];
|
|
|
|
progressInfoLabel = nil;
|
|
|
|
}
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
#pragma mark - setters/getters
|
2014-11-28 16:02:46 +00:00
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
- (void)setImage:(UIImage *)anImage {
|
2015-01-08 15:01:25 +00:00
|
|
|
currentImage = anImage;
|
2015-01-06 08:41:44 +00:00
|
|
|
imageView.image = anImage;
|
|
|
|
[self initScrollZoomFactors];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImage*)image {
|
2015-01-08 15:01:25 +00:00
|
|
|
return currentImage;
|
2015-01-06 08:41:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
- (void)setFullScreen:(BOOL)fullScreen {
|
|
|
|
useFullScreen = fullScreen;
|
|
|
|
|
|
|
|
[self initLayout];
|
|
|
|
|
|
|
|
if (useFullScreen) {
|
|
|
|
[self removeFromSuperview];
|
|
|
|
[UIApplication sharedApplication].statusBarHidden = YES;
|
|
|
|
|
|
|
|
self.frame = [AppDelegate theDelegate].window.rootViewController.view.bounds;
|
|
|
|
[[AppDelegate theDelegate].window.rootViewController.view addSubview:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)fullScreen {
|
|
|
|
return useFullScreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
2014-12-22 10:52:29 +00:00
|
|
|
- (IBAction)onButtonToggle:(id)sender
|
|
|
|
{
|
|
|
|
if (sender == leftButton) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
leftHandler(self, leftButtonTitle);
|
|
|
|
});
|
|
|
|
} else if (sender == rightButton) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
rightHandler(self, rightButtonTitle);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a generic button to the bottom view
|
|
|
|
// return the added UIButton
|
|
|
|
- (UIButton*) addbuttonWithTitle:(NSString*)title {
|
|
|
|
UIButton* button = [[UIButton alloc] init];
|
|
|
|
[button setTitle:title forState:UIControlStateNormal];
|
|
|
|
[button setTitle:title forState:UIControlStateHighlighted];
|
2015-01-06 14:44:34 +00:00
|
|
|
|
|
|
|
if (useFullScreen) {
|
|
|
|
// use the same text color as the tabbar
|
|
|
|
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
|
|
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// use the same text color as the tabbar
|
|
|
|
[button setTitleColor:[AppDelegate theDelegate].masterTabBarController.tabBar.tintColor forState:UIControlStateNormal];
|
|
|
|
[button setTitleColor:[AppDelegate theDelegate].masterTabBarController.tabBar.tintColor forState:UIControlStateHighlighted];
|
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
|
|
|
|
// keep the bottomView background color
|
|
|
|
button.backgroundColor = [UIColor clearColor];
|
|
|
|
|
|
|
|
[button addTarget:self action:@selector(onButtonToggle:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[bottomBarView addSubview:button];
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
- (void)initScrollZoomFactors {
|
|
|
|
// check if the image can be zoomed
|
2015-01-06 14:44:34 +00:00
|
|
|
if (self.image && self.stretchable && imageView.frame.size.width && imageView.frame.size.height) {
|
2015-01-06 08:41:44 +00:00
|
|
|
// ensure that the content size is properly initialized
|
|
|
|
scrollView.contentSize = scrollView.frame.size;
|
|
|
|
|
|
|
|
// compute the appliable zoom factor
|
|
|
|
// assume that the user does not expect to zoom more than 100%
|
|
|
|
CGSize imageSize = self.image.size;
|
|
|
|
|
|
|
|
CGFloat scaleX = imageSize.width / imageView.frame.size.width;
|
|
|
|
CGFloat scaleY = imageSize.height / imageView.frame.size.height;
|
|
|
|
|
|
|
|
if (scaleX < scaleY)
|
|
|
|
{
|
|
|
|
scaleX = scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scaleX < 1.0)
|
|
|
|
{
|
|
|
|
scaleX = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollView.zoomScale = 1.0;
|
|
|
|
scrollView.minimumZoomScale = 1.0;
|
|
|
|
scrollView.maximumZoomScale = scaleX;
|
|
|
|
|
|
|
|
// update the image frame to ensure that it fits to the scrollview frame
|
|
|
|
imageView.frame = scrollView.bounds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
- (void)removeFromSuperview {
|
|
|
|
[super removeFromSuperview];
|
2014-12-22 10:52:29 +00:00
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-06 17:12:45 +00:00
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
if (useFullScreen) {
|
|
|
|
[UIApplication sharedApplication].statusBarHidden = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
- (void)initLayout {
|
2015-01-06 08:41:44 +00:00
|
|
|
// create the subviews if they don't exist
|
|
|
|
if (!scrollView) {
|
|
|
|
scrollView = [[UIScrollView alloc] init];
|
|
|
|
scrollView.delegate = self;
|
|
|
|
scrollView.backgroundColor = [UIColor clearColor];
|
|
|
|
[self addSubview:scrollView];
|
|
|
|
|
|
|
|
imageView = [[UIImageView alloc] init];
|
|
|
|
imageView.backgroundColor = [UIColor clearColor];
|
2015-01-07 11:18:20 +00:00
|
|
|
imageView.contentMode = self.contentMode;
|
2015-01-06 08:41:44 +00:00
|
|
|
[scrollView addSubview:imageView];
|
|
|
|
}
|
2015-01-06 14:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews {
|
|
|
|
|
|
|
|
// call upper layer
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
[self initLayout];
|
2015-01-06 08:41:44 +00:00
|
|
|
|
|
|
|
// the image has been updated
|
|
|
|
if (imageView.image != self.image) {
|
|
|
|
imageView.image = self.image;
|
|
|
|
}
|
2015-01-06 14:44:34 +00:00
|
|
|
|
|
|
|
CGRect tabBarFrame = [AppDelegate theDelegate].masterTabBarController.tabBar.frame;
|
2015-01-06 08:41:44 +00:00
|
|
|
|
|
|
|
// update the scrollview frame
|
2015-01-06 14:44:34 +00:00
|
|
|
CGRect oneSelfFrame = self.frame;
|
2015-01-06 08:41:44 +00:00
|
|
|
CGRect scrollViewFrame = CGRectIntegral(scrollView.frame);
|
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
if (leftButtonTitle || rightButtonTitle) {
|
|
|
|
oneSelfFrame.size.height -= tabBarFrame.size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
oneSelfFrame = CGRectIntegral(oneSelfFrame);
|
2015-01-06 08:41:44 +00:00
|
|
|
oneSelfFrame.origin = scrollViewFrame.origin = CGPointZero;
|
|
|
|
|
|
|
|
// use integral rect to avoid rounded value issue (float precision)
|
2015-01-06 14:44:34 +00:00
|
|
|
if (!CGRectEqualToRect(oneSelfFrame, scrollViewFrame)) {
|
2015-01-06 08:41:44 +00:00
|
|
|
scrollView.frame = oneSelfFrame;
|
|
|
|
imageView.frame = oneSelfFrame;
|
|
|
|
|
|
|
|
[self initScrollZoomFactors];
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
// check if the dedicated buttons are already added
|
|
|
|
if (leftButtonTitle || rightButtonTitle) {
|
|
|
|
|
|
|
|
if (!bottomBarView) {
|
|
|
|
bottomBarView = [[UIView alloc] init];
|
|
|
|
|
|
|
|
if (leftButtonTitle) {
|
|
|
|
leftButton = [self addbuttonWithTitle:leftButtonTitle];
|
|
|
|
}
|
|
|
|
|
|
|
|
rightButton = [[UIButton alloc] init];
|
|
|
|
|
|
|
|
if (rightButtonTitle) {
|
|
|
|
rightButton = [self addbuttonWithTitle:rightButtonTitle];
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:44:34 +00:00
|
|
|
// in fullscreen, display both buttons above the view
|
|
|
|
if (useFullScreen) {
|
|
|
|
bottomBarView.backgroundColor = [UIColor blackColor];
|
|
|
|
[self addSubview:bottomBarView];
|
|
|
|
}
|
|
|
|
// display them above the tabbar
|
|
|
|
else {
|
|
|
|
// default tabbar background color
|
|
|
|
CGFloat base = 248.0 / 255.0f;
|
|
|
|
|
|
|
|
bottomBarView.backgroundColor = [UIColor colorWithRed:base green:base blue:base alpha:1.0];
|
|
|
|
[[AppDelegate theDelegate].masterTabBarController.tabBar addSubview:bottomBarView];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useFullScreen) {
|
|
|
|
tabBarFrame.origin.y = self.frame.size.height - tabBarFrame.size.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tabBarFrame.origin.y = 0;
|
2014-12-22 10:52:29 +00:00
|
|
|
}
|
|
|
|
bottomBarView.frame = tabBarFrame;
|
|
|
|
|
|
|
|
if (leftButton) {
|
|
|
|
leftButton.frame = CGRectMake(0, 0, CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, bottomBarView.frame.size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rightButton) {
|
|
|
|
rightButton.frame = CGRectMake(bottomBarView.frame.size.width - CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, 0, CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, bottomBarView.frame.size.height);
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2015-01-06 17:12:45 +00:00
|
|
|
if (!loadingView.hidden) {
|
2015-01-06 10:08:29 +00:00
|
|
|
// ensure that the spinner is drawn at the top
|
2015-01-06 17:12:45 +00:00
|
|
|
[loadingView.superview bringSubviewToFront:loadingView];
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2015-01-07 14:49:29 +00:00
|
|
|
// Adjust positions
|
2015-01-06 10:08:29 +00:00
|
|
|
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
|
2015-01-06 17:12:45 +00:00
|
|
|
loadingView.center = center;
|
2015-01-07 14:49:29 +00:00
|
|
|
|
|
|
|
CGRect progressInfoLabelFrame = progressInfoLabel.frame;
|
|
|
|
progressInfoLabelFrame.origin.x = center.x - (progressInfoLabelFrame.size.width / 2);
|
|
|
|
progressInfoLabelFrame.origin.y = 10 + loadingView.frame.origin.y + loadingView.frame.size.height;
|
|
|
|
progressInfoLabel.frame = progressInfoLabelFrame;
|
2015-01-06 10:08:29 +00:00
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
- (void)setHideActivityIndicator:(BOOL)hideActivityIndicator {
|
|
|
|
_hideActivityIndicator = hideActivityIndicator;
|
|
|
|
if (hideActivityIndicator) {
|
|
|
|
[self stopActivityIndicator];
|
2015-01-19 13:33:57 +00:00
|
|
|
} else if ([MediaManager existingDownloaderForURL:imageURL inFolder:mediaFolder]) {
|
2014-11-28 16:02:46 +00:00
|
|
|
// Loading is in progress, start activity indicator
|
|
|
|
[self startActivityIndicator];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 10:08:29 +00:00
|
|
|
- (void)setImageURL:(NSString *)anImageURL withPreviewImage:(UIImage*)previewImage {
|
2015-01-08 15:01:25 +00:00
|
|
|
// Remove any pending observers
|
2015-01-07 16:26:37 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-08 15:01:25 +00:00
|
|
|
imageURL = anImageURL;
|
2014-11-24 09:38:23 +00:00
|
|
|
|
2015-01-08 17:20:34 +00:00
|
|
|
if (!imageURL) {
|
|
|
|
// Set preview by default
|
|
|
|
self.image = previewImage;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
// Check whether the image download is in progress
|
2015-01-19 13:33:57 +00:00
|
|
|
MediaLoader* loader = [MediaManager existingDownloaderForURL:imageURL inFolder:mediaFolder];
|
2015-01-08 15:01:25 +00:00
|
|
|
if (loader) {
|
|
|
|
// Set preview until the image is loaded
|
|
|
|
self.image = previewImage;
|
|
|
|
// update the progress UI with the current info
|
2015-01-12 17:07:39 +00:00
|
|
|
[self updateProgressUI:loader.statisticsDict];
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
// Add observers
|
2015-01-06 17:12:45 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadProgress:) name:kMediaDownloadProgressNotification object:nil];
|
2015-01-08 15:01:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadEnd:) name:kMediaDownloadDidFinishNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadEnd:) name:kMediaDownloadDidFailNotification object:nil];
|
|
|
|
} else {
|
|
|
|
// Retrieve the image from cache
|
2015-01-19 13:33:57 +00:00
|
|
|
UIImage* image = [MediaManager loadCachePictureForURL:imageURL inFolder:mediaFolder];
|
2015-01-08 15:01:25 +00:00
|
|
|
if (image) {
|
|
|
|
self.image = image;
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
} else {
|
|
|
|
// Set preview until the image is loaded
|
|
|
|
self.image = previewImage;
|
|
|
|
// Trigger image downloading
|
|
|
|
if (!_hideActivityIndicator) {
|
|
|
|
[self startActivityIndicator];
|
|
|
|
}
|
|
|
|
// Add observers
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadProgress:) name:kMediaDownloadProgressNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadEnd:) name:kMediaDownloadDidFinishNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaDownloadEnd:) name:kMediaDownloadDidFailNotification object:nil];
|
2015-01-19 13:33:57 +00:00
|
|
|
[MediaManager downloadMediaFromURL:imageURL withType:@"image/jpeg" inFolder:mediaFolder];
|
2015-01-07 16:26:37 +00:00
|
|
|
}
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 16:26:37 +00:00
|
|
|
- (void)onMediaDownloadEnd:(NSNotification *)notif {
|
2015-01-06 17:12:45 +00:00
|
|
|
// sanity check
|
|
|
|
if ([notif.object isKindOfClass:[NSString class]]) {
|
|
|
|
NSString* url = notif.object;
|
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
if ([url isEqualToString:imageURL]) {
|
2015-01-07 16:26:37 +00:00
|
|
|
[self stopActivityIndicator];
|
|
|
|
// update the image
|
2015-01-19 13:33:57 +00:00
|
|
|
UIImage* image = [MediaManager loadCachePictureForURL:imageURL inFolder:mediaFolder];
|
2015-01-07 16:26:37 +00:00
|
|
|
if (image) {
|
|
|
|
self.image = image;
|
2015-01-07 14:49:29 +00:00
|
|
|
}
|
2015-01-07 16:26:37 +00:00
|
|
|
// remove the observers
|
2015-01-08 15:01:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-07 16:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateProgressUI:(NSDictionary*)downloadStatsDict {
|
2015-01-13 16:11:15 +00:00
|
|
|
|
|
|
|
// Sanity check: updateProgressUI may be called while there is no stats available
|
|
|
|
// This happens when the download failed at the very beginning.
|
|
|
|
if (nil == downloadStatsDict) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-08 20:23:49 +00:00
|
|
|
NSNumber* progressNumber = [downloadStatsDict valueForKey:kMediaLoaderProgressRateKey];
|
2015-01-07 16:26:37 +00:00
|
|
|
|
|
|
|
if (progressNumber) {
|
|
|
|
pieChartView.progress = progressNumber.floatValue;
|
|
|
|
waitingDownloadSpinner.hidden = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressInfoLabel) {
|
2015-01-08 20:23:49 +00:00
|
|
|
NSString* downloadRate = [downloadStatsDict valueForKey:kMediaLoaderProgressDownloadRateKey];
|
|
|
|
NSString* remaingTime = [downloadStatsDict valueForKey:kMediaLoaderProgressRemaingTimeKey];
|
|
|
|
NSString* progressString = [downloadStatsDict valueForKey:kMediaLoaderProgressStringKey];
|
2015-01-07 16:26:37 +00:00
|
|
|
|
|
|
|
NSMutableString* text = [[NSMutableString alloc] init];
|
|
|
|
|
|
|
|
[text appendString:progressString];
|
|
|
|
|
|
|
|
if (remaingTime) {
|
|
|
|
[text appendFormat:@" (%@)", remaingTime];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadRate) {
|
|
|
|
[text appendFormat:@"\n %@", downloadRate];
|
|
|
|
}
|
|
|
|
|
|
|
|
progressInfoLabel.text = text;
|
|
|
|
|
|
|
|
// on multilines, sizeToFit uses the current width
|
|
|
|
// so reset it
|
|
|
|
progressInfoLabel.frame = CGRectZero;
|
|
|
|
|
|
|
|
[progressInfoLabel sizeToFit];
|
|
|
|
|
|
|
|
//
|
|
|
|
CGRect progressInfoLabelFrame = progressInfoLabel.frame;
|
|
|
|
progressInfoLabelFrame.origin.x = self.center.x - (progressInfoLabelFrame.size.width / 2);
|
|
|
|
progressInfoLabelFrame.origin.y = 10 + loadingView.frame.origin.y + loadingView.frame.size.height;
|
|
|
|
progressInfoLabel.frame = progressInfoLabelFrame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)onMediaDownloadProgress:(NSNotification *)notif {
|
|
|
|
// sanity check
|
|
|
|
if ([notif.object isKindOfClass:[NSString class]]) {
|
|
|
|
NSString* url = notif.object;
|
|
|
|
|
2015-01-08 15:01:25 +00:00
|
|
|
if ([url isEqualToString:imageURL]) {
|
2015-01-07 16:26:37 +00:00
|
|
|
[self updateProgressUI:notif.userInfo];
|
2015-01-06 17:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
#pragma mark - buttons management
|
|
|
|
|
|
|
|
- (void)setLeftButtonTitle: aLeftButtonTitle handler:(blockCustomImageView_onClick)handler {
|
|
|
|
leftButtonTitle = aLeftButtonTitle;
|
|
|
|
leftHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRightButtonTitle:aRightButtonTitle handler:(blockCustomImageView_onClick)handler {
|
|
|
|
rightButtonTitle = aRightButtonTitle;
|
|
|
|
rightHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismissSelection {
|
|
|
|
if (bottomBarView) {
|
|
|
|
[bottomBarView removeFromSuperview];
|
|
|
|
bottomBarView = nil;
|
|
|
|
}
|
2015-01-06 14:44:34 +00:00
|
|
|
|
|
|
|
if (useFullScreen) {
|
|
|
|
[UIApplication sharedApplication].statusBarHidden = NO;
|
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
// require to be able to zoom an image
|
|
|
|
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
|
|
|
|
{
|
2015-01-06 14:44:34 +00:00
|
|
|
return self.stretchable ? imageView : nil;
|
2015-01-06 08:41:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
@end
|