1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-04 02:17:51 +01:00
Telegram/Telegraph/TGWebSearchImageItemView.m

150 lines
4.4 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGWebSearchImageItemView.h"
2016-02-25 01:03:51 +01:00
#import "TGMediaEditingContext.h"
2015-10-01 18:19:52 +02:00
#import "TGWebSearchImageItem.h"
2016-02-25 01:03:51 +01:00
#import "PGPhotoEditorValues.h"
2015-10-01 18:19:52 +02:00
#import "TGStringUtils.h"
2016-02-25 01:03:51 +01:00
#import "TGCheckButtonView.h"
2015-10-01 18:19:52 +02:00
#import "TGImageView.h"
@interface TGWebSearchImageItemView ()
{
2016-02-25 01:03:51 +01:00
TGCheckButtonView *_checkButton;
SMetaDisposable *_imageDisposable;
SMetaDisposable *_itemSelectedDisposable;
2015-10-01 18:19:52 +02:00
}
@end
@implementation TGWebSearchImageItemView
2016-02-25 01:03:51 +01:00
- (void)dealloc
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
[_imageDisposable dispose];
[_itemSelectedDisposable dispose];
2015-10-01 18:19:52 +02:00
}
- (void)setItem:(TGWebSearchImageItem *)item synchronously:(bool)synchronously
{
[super setItem:item synchronously:synchronously];
2016-02-25 01:03:51 +01:00
if (item.selectionContext != nil)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if (_checkButton == nil)
{
_checkButton = [[TGCheckButtonView alloc] initWithStyle:TGCheckButtonStyleMedia];
[_checkButton addTarget:self action:@selector(checkButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_checkButton];
}
if (_itemSelectedDisposable == nil)
_itemSelectedDisposable = [[SMetaDisposable alloc] init];
__weak TGWebSearchImageItemView *weakSelf = self;
[_checkButton setSelected:[item.selectionContext isItemSelected:item.selectableMediaItem] animated:false];
[_itemSelectedDisposable setDisposable:[[item.selectionContext itemInformativeSelectedSignal:item.selectableMediaItem] startWithNext:^(TGMediaSelectionChange *next)
{
__strong TGWebSearchImageItemView *strongSelf = weakSelf;
if (strongSelf == nil)
return;
if (next.sender != strongSelf->_checkButton)
[strongSelf->_checkButton setSelected:next.selected animated:next.animated];
}]];
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
__weak TGWebSearchImageItemView *weakSelf = self;
void (^setOriginalImage)(void) = ^
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
__strong TGWebSearchImageItemView *strongSelf = weakSelf;
if (strongSelf == nil)
return;
[strongSelf.imageView loadUri:[[NSString alloc] initWithFormat:@"web-search-thumbnail://?url=%@&width=90&height=90", [TGStringUtils stringByEscapingForURL:item.previewUrl]] withOptions:nil];
2015-10-01 18:19:52 +02:00
};
if ([item conformsToProtocol:@protocol(TGModernMediaListEditableItem)])
{
2016-02-25 01:03:51 +01:00
id<TGMediaEditableItem> editableItem = item.editableMediaItem;
if (_imageDisposable == nil)
_imageDisposable = [[SMetaDisposable alloc] init];
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
[_imageDisposable setDisposable:[[item.editingContext thumbnailImageSignalForItem:editableItem] startWithNext:^(id next)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
__strong TGWebSearchImageItemView *strongSelf = weakSelf;
if (strongSelf == nil)
return;
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
if ([next isKindOfClass:[UIImage class]])
[strongSelf.imageView loadUri:@"embedded-image://" withOptions:@{ TGImageViewOptionEmbeddedImage: next }];
2015-10-01 18:19:52 +02:00
else
2016-02-25 01:03:51 +01:00
setOriginalImage();
}]];
2015-10-01 18:19:52 +02:00
}
else
{
2016-02-25 01:03:51 +01:00
setOriginalImage();
2015-10-01 18:19:52 +02:00
}
}
2016-02-25 01:03:51 +01:00
- (void)layoutSubviews
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
[super layoutSubviews];
_checkButton.frame = (CGRect){{self.frame.size.width - _checkButton.frame.size.width - 2.0f, 2.0f}, _checkButton.frame.size};
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
- (void)checkButtonPressed
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
TGWebSearchImageItem *item = (TGWebSearchImageItem *)self.item;
[_checkButton setSelected:!_checkButton.selected animated:true];
[item.selectionContext setItem:item.selectableMediaItem selected:_checkButton.selected animated:false sender:_checkButton];
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
- (void)setHidden:(bool)hidden animated:(bool)animated
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if (hidden == self.imageView.hidden)
2015-10-01 18:19:52 +02:00
return;
2016-02-25 01:03:51 +01:00
self.imageView.hidden = hidden;
2015-10-01 18:19:52 +02:00
if (animated)
{
2016-02-25 01:03:51 +01:00
if (!hidden)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
for (UIView *view in self.subviews)
{
if (view != self.imageView)
view.alpha = 0.0f;
}
}
[UIView animateWithDuration:0.2 animations:^
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if (!hidden)
{
for (UIView *view in self.subviews)
{
if (view != self.imageView)
view.alpha = 1.0f;
}
}
2015-10-01 18:19:52 +02:00
}];
}
else
{
2016-02-25 01:03:51 +01:00
for (UIView *view in self.subviews)
{
if (view != self.imageView)
view.alpha = hidden ? 0.0f : 1.0f;
}
2015-10-01 18:19:52 +02:00
}
}
@end