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

238 lines
7.8 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
#import "TGMediaFoldersController.h"
#import "TGMediaPickerAsset.h"
#import "TGMediaPickerAssetsGroup.h"
#import "TGMediaFoldersLayout.h"
#import "TGMediaFoldersCell.h"
#import "TGModernMediaPickerController.h"
2015-10-01 18:19:52 +02:00
#import "TGAppDelegate.h"
2014-07-10 16:11:09 +02:00
@interface TGMediaFoldersController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
{
2015-10-01 18:19:52 +02:00
TGMediaPickerAssetsLibrary *_assetsLibrary;
NSArray *_items;
NSArray *_loadedItems;
NSString *_currentGroupId;
2014-07-10 16:11:09 +02:00
UICollectionView *_collectionView;
CGFloat _collectionViewWidth;
TGMediaFoldersLayout *_collectionLayout;
2015-10-01 18:19:52 +02:00
TGModernMediaPickerControllerIntent _intent;
2014-07-10 16:11:09 +02:00
dispatch_semaphore_t _waitSemaphore;
bool _usedSemaphore;
}
@end
@implementation TGMediaFoldersController
- (instancetype)init
2015-10-01 18:19:52 +02:00
{
return [self initWithIntent:TGModernMediaPickerControllerDefaultIntent];
}
- (instancetype)initWithIntent:(TGModernMediaPickerControllerIntent)intent
2014-07-10 16:11:09 +02:00
{
self = [super init];
if (self != nil)
{
2015-10-01 18:19:52 +02:00
_intent = intent;
2014-07-10 16:11:09 +02:00
self.title = TGLocalized(@"SearchImages.Title");
2015-10-01 18:19:52 +02:00
[self setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:TGLocalized(@"Common.Cancel") style:UIBarButtonItemStylePlain target:self action:@selector(cancelPressed)]];
2014-07-10 16:11:09 +02:00
_waitSemaphore = dispatch_semaphore_create(0);
2015-10-01 18:19:52 +02:00
__weak TGMediaFoldersController *weakSelf = self;
_assetsLibrary = [[TGMediaPickerAssetsLibrary alloc] initForAssetType:[TGModernMediaPickerController assetTypeForIntent:intent]];
_assetsLibrary.libraryChanged = ^
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
__strong TGMediaFoldersController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
if (strongSelf->_items)
[strongSelf reloadData];
};
2014-07-10 16:11:09 +02:00
}
return self;
}
- (void)cancelPressed
{
2015-10-01 18:19:52 +02:00
if (self.dismiss != nil)
self.dismiss();
2014-07-10 16:11:09 +02:00
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
2015-10-01 18:19:52 +02:00
CGSize frameSize = TGAppDelegateInstance.rootController.view.bounds.size;
2014-07-10 16:11:09 +02:00
_collectionLayout = [[TGMediaFoldersLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frameSize.width, frameSize.height) collectionViewLayout:_collectionLayout];
_collectionView.alwaysBounceVertical = true;
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView registerClass:[TGMediaFoldersCell class] forCellWithReuseIdentifier:@"TGMediaFoldersCell"];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[self.view addSubview:_collectionView];
[self controllerInsetUpdated:UIEdgeInsetsZero];
}
- (void)viewWillAppear:(BOOL)animated
{
if (_waitSemaphore != nil && !_usedSemaphore)
{
_usedSemaphore = true;
2015-10-01 18:19:52 +02:00
[self reloadData];
2014-07-10 16:11:09 +02:00
2015-10-01 18:19:52 +02:00
dispatch_semaphore_wait(_waitSemaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)));
2014-07-10 16:11:09 +02:00
@synchronized (self)
{
2015-10-01 18:19:52 +02:00
if (_loadedItems != nil)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
_items = _loadedItems;
_loadedItems = nil;
2014-07-10 16:11:09 +02:00
}
}
}
[super viewWillAppear:animated];
2015-10-01 18:19:52 +02:00
_currentGroupId = nil;
CGSize frameSize = TGAppDelegateInstance.rootController.view.bounds.size;
2014-07-10 16:11:09 +02:00
CGRect collectionViewFrame = CGRectMake(0.0f, 0.0f, frameSize.width, frameSize.height);
_collectionViewWidth = collectionViewFrame.size.width;
_collectionView.frame = collectionViewFrame;
[_collectionLayout invalidateLayout];
if ([_collectionView indexPathsForSelectedItems].count != 0)
[_collectionView deselectItemAtIndexPath:[_collectionView indexPathsForSelectedItems][0] animated:animated];
}
2015-10-01 18:19:52 +02:00
- (void)layoutControllerForSize:(CGSize)size duration:(NSTimeInterval)__unused duration {
[super layoutControllerForSize:size duration:duration];
2014-07-10 16:11:09 +02:00
2015-10-01 18:19:52 +02:00
CGRect tableFrame = CGRectMake(0, 0, size.width, size.height);
2014-07-10 16:11:09 +02:00
_collectionViewWidth = tableFrame.size.width;
_collectionView.frame = tableFrame;
[_collectionLayout invalidateLayout];
}
2015-10-01 18:19:52 +02:00
- (void)_replaceAssetGroupList:(NSArray *)items
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
_items = items;
2014-07-10 16:11:09 +02:00
[_collectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)__unused collectionView layout:(UICollectionViewLayout *)__unused collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)__unused indexPath
{
return CGSizeMake(_collectionViewWidth, 86.0f);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)__unused collectionView layout:(UICollectionViewLayout *)__unused collectionViewLayout insetForSectionAtIndex:(NSInteger)__unused section
{
return UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)__unused collectionView layout:(UICollectionViewLayout *)__unused collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)__unused section
{
return 0.0f;
}
- (CGFloat)collectionView:(UICollectionView *)__unused collectionView layout:(UICollectionViewLayout *)__unused collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)__unused section
{
return 0.0f;
}
- (NSInteger)collectionView:(UICollectionView *)__unused collectionView numberOfItemsInSection:(NSInteger)__unused section
{
2015-10-01 18:19:52 +02:00
return _items.count;
2014-07-10 16:11:09 +02:00
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TGMediaFoldersCell *cell = (TGMediaFoldersCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"TGMediaFoldersCell" forIndexPath:indexPath];
2015-10-01 18:19:52 +02:00
[cell setAssetsGroup:_items[indexPath.item]];
2014-07-10 16:11:09 +02:00
return cell;
}
2015-10-01 18:19:52 +02:00
- (void)reloadData
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
[_assetsLibrary fetchAssetsGroupsWithCompletionBlock:^(NSArray *groups, TGMediaPickerAuthorizationStatus status, __unused NSError *error)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
if (status == TGMediaPickerAuthorizationStatusAuthorized)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
@synchronized(self)
{
_loadedItems = groups;
}
TGDispatchOnMainThread(^
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
[self _replaceAssetGroupList:groups];
2014-07-10 16:11:09 +02:00
2015-10-01 18:19:52 +02:00
if (_currentGroupId != nil)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
bool currentGroupStillExists = false;
for (TGMediaPickerAssetsGroup *group in groups)
2014-07-10 16:11:09 +02:00
{
2015-10-01 18:19:52 +02:00
if ([group.persistentId isEqualToString:_currentGroupId])
{
currentGroupStillExists = true;
break;
}
2014-07-10 16:11:09 +02:00
}
2015-10-01 18:19:52 +02:00
if (!currentGroupStillExists)
{
_currentGroupId = nil;
[self.navigationController popToRootViewControllerAnimated:true];
}
2014-07-10 16:11:09 +02:00
}
2015-10-01 18:19:52 +02:00
});
}
if (_waitSemaphore != nil)
dispatch_semaphore_signal(_waitSemaphore);
2014-07-10 16:11:09 +02:00
}];
}
- (void)collectionView:(UICollectionView *)__unused collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
2015-10-01 18:19:52 +02:00
TGMediaPickerAssetsGroup *assetsGroup = _items[indexPath.row];
_currentGroupId = assetsGroup.persistentId;
TGModernMediaPickerController *controller = [[TGModernMediaPickerController alloc] initWithAssetsGroup:assetsGroup intent:_intent];
controller.photosPicked = self.photosPicked;
controller.videoPicked = self.videoPicked;
controller.liveUploadEnabled = self.liveUpload;
controller.serverAssetCacheEnabled = self.enableServerAssetCache;
controller.avatarCreated = self.avatarCreated;
controller.dismiss = self.dismiss;
controller.userListSignal = self.userListSignal;
controller.hashtagListSignal = self.hashtagListSignal;
controller.disallowCaptions = self.disallowCaptions;
[self.navigationController pushViewController:controller animated:true];
2014-07-10 16:11:09 +02:00
}
@end