1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-03 09:57:46 +01:00
Telegram/Telegraph/TGImageView.m

336 lines
11 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
/*
* This is the source code of Telegram for iOS v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Peter Iakovlev, 2013.
*/
#import "TGImageView.h"
#import "TGImageManager.h"
#import <MTProtoKit/MTTime.h>
2015-10-01 18:19:52 +02:00
#import "UIImage+TG.h"
2014-07-10 16:11:09 +02:00
NSString *TGImageViewOptionKeepCurrentImageAsPlaceholder = @"TGImageViewOptionKeepCurrentImageAsPlaceholder";
NSString *TGImageViewOptionEmbeddedImage = @"TGImageViewOptionEmbeddedImage";
NSString *TGImageViewOptionSynchronous = @"TGImageViewOptionSynchronous";
@interface TGImageView ()
{
id _loadToken;
2015-10-01 18:19:52 +02:00
volatile int _version;
SMetaDisposable *_disposable;
2014-07-10 16:11:09 +02:00
UIImageView *_transitionOverlayView;
}
@end
@implementation TGImageView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
2015-10-01 18:19:52 +02:00
_disposable = [[SMetaDisposable alloc] init];
_legacyAutomaticProgress = true;
2014-07-10 16:11:09 +02:00
}
return self;
}
- (void)dealloc
{
2015-10-01 18:19:52 +02:00
[_disposable dispose];
2014-07-10 16:11:09 +02:00
if (_loadToken != nil)
[[TGImageManager instance] cancelTaskWithId:_loadToken];
}
2015-10-01 18:19:52 +02:00
- (void)setExpectExtendedEdges:(bool)expectExtendedEdges
{
if (_expectExtendedEdges != expectExtendedEdges)
{
_expectExtendedEdges = expectExtendedEdges;
if (_expectExtendedEdges && _extendedInsetsImageView == nil)
{
self.image = nil;
_extendedInsetsImageView = [[UIImageView alloc] init];
[self addSubview:_extendedInsetsImageView];
}
else if (!_expectExtendedEdges && _extendedInsetsImageView != nil)
{
_extendedInsetsImageView.image = nil;
[_extendedInsetsImageView removeFromSuperview];
_extendedInsetsImageView = nil;
}
}
}
2014-07-10 16:11:09 +02:00
- (void)loadUri:(NSString *)uri withOptions:(NSDictionary *)__unused options
{
2015-10-01 18:19:52 +02:00
[_disposable setDisposable:nil];
2014-07-10 16:11:09 +02:00
_version++;
UIImage *image = nil;
2015-10-01 18:19:52 +02:00
bool beganAsyncTask = false;
2014-07-10 16:11:09 +02:00
if (options[TGImageViewOptionEmbeddedImage] != nil)
image = options[TGImageViewOptionEmbeddedImage];
else
2015-10-01 18:19:52 +02:00
{
__autoreleasing id asyncTaskId = nil;
__weak TGImageView *weakSelf = self;
int version = _version;
MTAbsoluteTime loadStartTime = MTAbsoluteSystemTime();
bool legacyAutomaticProgress = _legacyAutomaticProgress;
image = [[TGImageManager instance] loadImageSyncWithUri:uri canWait:[options[TGImageViewOptionSynchronous] boolValue] decode:true acceptPartialData:true asyncTaskId:&asyncTaskId progress:^(float value)
{
if (legacyAutomaticProgress)
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
[strongSelf _updateProgress:value];
});
}
} partialCompletion:^(UIImage *partialImage)
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
[strongSelf _commitImage:partialImage partial:true loadTime:(NSTimeInterval)(MTAbsoluteSystemTime() - loadStartTime)];
else
TGLog(@"[TGImageView _commitImage version mismatch]");
});
} completion:^(UIImage *image)
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
{
if (legacyAutomaticProgress)
[strongSelf _updateProgress:1.0f];
[strongSelf _commitImage:image partial:false loadTime:(NSTimeInterval)(MTAbsoluteSystemTime() - loadStartTime)];
}
else
TGLog(@"[TGImageView _commitImage version mismatch]");
});
}];
if (asyncTaskId != nil)
{
beganAsyncTask = true;
_loadToken = asyncTaskId;
}
}
2014-07-10 16:11:09 +02:00
if (image != nil)
2015-10-01 18:19:52 +02:00
[self _commitImage:image partial:beganAsyncTask loadTime:0.0];
2014-07-10 16:11:09 +02:00
else
{
if (![options[TGImageViewOptionKeepCurrentImageAsPlaceholder] boolValue])
{
UIImage *placeholderImage = [[TGImageManager instance] loadAttributeSyncForUri:uri attribute:@"placeholder"];
if (placeholderImage != nil)
2015-10-01 18:19:52 +02:00
[self _commitImage:placeholderImage partial:beganAsyncTask loadTime:0.0];
else
[self performTransitionToImage:nil partial:true duration:0.0];
2014-07-10 16:11:09 +02:00
}
MTAbsoluteTime loadStartTime = MTAbsoluteSystemTime();
__weak TGImageView *weakSelf = self;
int version = _version;
2015-10-01 18:19:52 +02:00
bool legacyAutomaticProgress = _legacyAutomaticProgress;
_loadToken = [[TGImageManager instance] beginLoadingImageAsyncWithUri:uri decode:true progress:^(float value)
{
if (legacyAutomaticProgress)
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
[strongSelf _updateProgress:value];
});
}
} partialCompletion:^(UIImage *partialImage)
2014-07-10 16:11:09 +02:00
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
2015-10-01 18:19:52 +02:00
[strongSelf _commitImage:partialImage partial:true loadTime:(NSTimeInterval)(MTAbsoluteSystemTime() - loadStartTime)];
else
TGLog(@"[TGImageView _commitImage version mismatch]");
});
} completion:^(UIImage *image)
{
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
{
if (legacyAutomaticProgress)
[strongSelf _updateProgress:1.0f];
[strongSelf _commitImage:image partial:false loadTime:(NSTimeInterval)(MTAbsoluteSystemTime() - loadStartTime)];
}
2014-07-10 16:11:09 +02:00
else
TGLog(@"[TGImageView _commitImage version mismatch]");
});
}];
}
}
2015-10-01 18:19:52 +02:00
- (void)_updateProgress:(float)value
{
[self performProgressUpdate:value];
}
- (void)_commitImage:(UIImage *)image partial:(bool)partial loadTime:(NSTimeInterval)loadTime
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
if (image == self.image)
return;
2014-07-10 16:11:09 +02:00
NSTimeInterval transitionDuration = 0.0;
if (loadTime > DBL_EPSILON)
transitionDuration = 0.16;
2015-10-01 18:19:52 +02:00
[self performTransitionToImage:image partial:partial duration:transitionDuration];
2014-07-10 16:11:09 +02:00
}
- (void)reset
{
_version++;
2015-10-01 18:19:52 +02:00
[_disposable setDisposable:nil];
2014-07-10 16:11:09 +02:00
if (_loadToken != nil)
{
[[TGImageManager instance] cancelTaskWithId:_loadToken];
_loadToken = nil;
}
2015-10-01 18:19:52 +02:00
[self _commitImage:nil partial:false loadTime:0.0];
}
- (UIImage *)currentImage
{
if (_expectExtendedEdges)
return _extendedInsetsImageView.image;
return [super image];
2014-07-10 16:11:09 +02:00
}
2015-10-01 18:19:52 +02:00
- (void)performProgressUpdate:(CGFloat)__unused progress
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
}
- (void)performTransitionToImage:(UIImage *)image partial:(bool)__unused partial duration:(NSTimeInterval)duration
{
if (((_expectExtendedEdges && _extendedInsetsImageView.image != nil) || (!_expectExtendedEdges && self.image != nil)) && duration > DBL_EPSILON)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
self.alpha = 1.0f;
_extendedInsetsImageView.alpha = 1.0f;
2014-07-10 16:11:09 +02:00
if (_transitionOverlayView == nil)
_transitionOverlayView = [[UIImageView alloc] init];
2015-10-01 18:19:52 +02:00
_transitionOverlayView.frame = _extendedInsetsImageView == nil ? self.bounds : _extendedInsetsImageView.frame;
2014-07-10 16:11:09 +02:00
[self insertSubview:_transitionOverlayView atIndex:0];
2015-10-01 18:19:52 +02:00
_transitionOverlayView.image = _extendedInsetsImageView == nil ? self.image : _extendedInsetsImageView.image;
_transitionOverlayView.backgroundColor = self.backgroundColor;
2014-07-10 16:11:09 +02:00
_transitionOverlayView.alpha = 1.0;
[UIView animateWithDuration:duration animations:^
{
_transitionOverlayView.alpha = 0.0;
} completion:^(__unused BOOL finished)
{
_transitionOverlayView.image = nil;
[_transitionOverlayView removeFromSuperview];
}];
2015-10-01 18:19:52 +02:00
if (_extendedInsetsImageView != nil)
{
_extendedInsetsImageView.alpha = 0.0f;
[UIView animateWithDuration:duration / 2.0f animations:^
{
_extendedInsetsImageView.alpha = 1.0f;
}];
}
2014-07-10 16:11:09 +02:00
}
2015-10-01 18:19:52 +02:00
else if (image != nil && duration > DBL_EPSILON)
{
if (_expectExtendedEdges)
_extendedInsetsImageView.alpha = 0.0f;
else
self.alpha = 0.0f;
[UIView animateWithDuration:duration animations:^
{
if (_expectExtendedEdges)
_extendedInsetsImageView.alpha = 1.0f;
else
self.alpha = 1.0f;
} completion:^(__unused BOOL finished)
{
}];
}
else
{
self.alpha = 1.0f;
}
if (!_expectExtendedEdges)
{
self.image = image;
if (_extendedInsetsImageView != nil)
{
[_extendedInsetsImageView removeFromSuperview];
_extendedInsetsImageView = nil;
}
}
else
{
UIEdgeInsets insets = [image extendedEdgeInsets];
_extendedInsetsImageView.image = image;
_extendedInsetsImageView.frame = CGRectMake(-insets.left, -insets.top, self.bounds.size.width + insets.left + insets.right, self.bounds.size.height + insets.top + insets.bottom);
}
}
2014-07-10 16:11:09 +02:00
2015-10-01 18:19:52 +02:00
- (void)setSignal:(SSignal *)signal
{
_version++;
int version = _version;
__weak TGImageView *weakSelf = self;
[_disposable setDisposable:[signal startWithNext:^(id next)
{
bool synchronous = [NSThread isMainThread];
TGDispatchOnMainThread(^
{
__strong TGImageView *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf->_version == version)
{
if ([next isKindOfClass:[UIImage class]])
2016-02-25 01:03:51 +01:00
[strongSelf _commitImage:next partial:[next degraded] && ![next edited] loadTime:synchronous ? 0.0 : 1.0];
2015-10-01 18:19:52 +02:00
else if ([next respondsToSelector:@selector(floatValue)])
[strongSelf _updateProgress:[next floatValue]];
}
});
} error:^(id error)
{
TGLog(@"TGImageView signal error: %@", error);
} completed:^
{
}]];
2014-07-10 16:11:09 +02:00
}
@end