mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-01 13:46:57 +02:00
Media Picker: Apply Ribot design
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
Copyright 2015 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 "MediaAlbumContentViewController.h"
|
||||
|
||||
|
||||
@interface MediaAlbumContentViewController ()
|
||||
{
|
||||
/**
|
||||
Observe UIApplicationWillEnterForegroundNotification to refresh bubbles when app leaves the background state.
|
||||
*/
|
||||
id UIApplicationWillEnterForegroundNotificationObserver;
|
||||
|
||||
/**
|
||||
The current list of assets retrieved from collection.
|
||||
*/
|
||||
PHFetchResult *assets;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation MediaAlbumContentViewController
|
||||
|
||||
#pragma mark - Class methods
|
||||
|
||||
+ (UINib *)nib
|
||||
{
|
||||
return [UINib nibWithNibName:NSStringFromClass([MediaAlbumContentViewController class])
|
||||
bundle:[NSBundle bundleForClass:[MediaAlbumContentViewController class]]];
|
||||
}
|
||||
|
||||
+ (instancetype)mediaAlbumContentViewController
|
||||
{
|
||||
return [[[self class] alloc] initWithNibName:NSStringFromClass([MediaAlbumContentViewController class])
|
||||
bundle:[NSBundle bundleForClass:[MediaAlbumContentViewController class]]];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
|
||||
// Register collection view cell class
|
||||
[self.assetsCollectionView registerClass:MXKMediaCollectionViewCell.class forCellWithReuseIdentifier:[MXKMediaCollectionViewCell defaultReuseIdentifier]];
|
||||
|
||||
if (!_mediaTypes)
|
||||
{
|
||||
// Set default media type
|
||||
self.mediaTypes = @[(NSString *)kUTTypeImage];
|
||||
}
|
||||
|
||||
// Observe UIApplicationWillEnterForegroundNotification to refresh captures collection when app leaves the background state.
|
||||
UIApplicationWillEnterForegroundNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
|
||||
|
||||
// Force a full refresh of the displayed collection
|
||||
self.assetsCollection = _assetsCollection;
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning
|
||||
{
|
||||
[super didReceiveMemoryWarning];
|
||||
// Dispose of any resources that can be recreated.
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
|
||||
self.navigationItem.title = _assetsCollection.localizedTitle;
|
||||
|
||||
[self.assetsCollectionView reloadData];
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated
|
||||
{
|
||||
[super viewWillDisappear:animated];
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated
|
||||
{
|
||||
[super viewDidAppear:animated];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)setMediaTypes:(NSArray *)mediaTypes
|
||||
{
|
||||
_mediaTypes = mediaTypes;
|
||||
|
||||
self.assetsCollection = _assetsCollection;
|
||||
}
|
||||
|
||||
- (void)setAssetsCollection:(PHAssetCollection *)assetsCollection
|
||||
{
|
||||
if (assetsCollection)
|
||||
{
|
||||
if (!_mediaTypes)
|
||||
{
|
||||
// Set default media type
|
||||
_mediaTypes = @[(NSString *)kUTTypeImage];
|
||||
}
|
||||
|
||||
// Set up fetch options.
|
||||
PHFetchOptions *options = [[PHFetchOptions alloc] init];
|
||||
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
|
||||
if ([_mediaTypes indexOfObject:(NSString *)kUTTypeImage] != NSNotFound)
|
||||
{
|
||||
if ([_mediaTypes indexOfObject:(NSString *)kUTTypeMovie] != NSNotFound)
|
||||
{
|
||||
options.predicate = [NSPredicate predicateWithFormat:@"(mediaType == %d) || (mediaType == %d)", PHAssetMediaTypeImage, PHAssetMediaTypeVideo];
|
||||
}
|
||||
else
|
||||
{
|
||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d",PHAssetMediaTypeImage];
|
||||
}
|
||||
}
|
||||
else if ([_mediaTypes indexOfObject:(NSString *)kUTTypeMovie] != NSNotFound)
|
||||
{
|
||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d",PHAssetMediaTypeVideo];
|
||||
}
|
||||
|
||||
assets = [PHAsset fetchAssetsInAssetCollection:assetsCollection options:options];
|
||||
|
||||
NSLog(@"[MediaAlbumVC] lists %tu assets", assets.count);
|
||||
}
|
||||
else
|
||||
{
|
||||
assets = nil;
|
||||
}
|
||||
|
||||
_assetsCollection = assetsCollection;
|
||||
|
||||
self.navigationItem.title = _assetsCollection.localizedTitle;
|
||||
|
||||
[self.assetsCollectionView reloadData];
|
||||
}
|
||||
|
||||
#pragma mark - Override MXKViewController
|
||||
|
||||
- (void)destroy
|
||||
{
|
||||
[super destroy];
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDataSource
|
||||
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
||||
{
|
||||
return assets.count;
|
||||
}
|
||||
|
||||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MXKMediaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MXKMediaCollectionViewCell defaultReuseIdentifier] forIndexPath:indexPath];
|
||||
|
||||
// Sanity check: cancel pending asynchronous request (if any)
|
||||
if (cell.tag)
|
||||
{
|
||||
[[PHImageManager defaultManager] cancelImageRequest:(PHImageRequestID)cell.tag];
|
||||
cell.tag = 0;
|
||||
}
|
||||
|
||||
if (indexPath.item < assets.count)
|
||||
{
|
||||
PHAsset *asset = assets[indexPath.item];
|
||||
|
||||
CGFloat collectionViewSquareSize = ((collectionView.frame.size.width - 6) / 4); // Here 6 = 3 * cell margin (= 2).
|
||||
CGSize cellSize = CGSizeMake(collectionViewSquareSize, collectionViewSquareSize);
|
||||
|
||||
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
|
||||
option.synchronous = NO;
|
||||
cell.tag = [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:cellSize contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage *result, NSDictionary *info) {
|
||||
|
||||
cell.mxkImageView.contentMode = UIViewContentModeScaleAspectFill;
|
||||
cell.mxkImageView.image = result;
|
||||
cell.tag = 0;
|
||||
|
||||
}];
|
||||
|
||||
cell.topRightIcon.image = [UIImage imageNamed:@"icon_video"];
|
||||
cell.topRightIcon.hidden = (asset.mediaType == PHAssetMediaTypeImage);
|
||||
|
||||
// Disable user interaction in mxkImageView, in order to let collection handle user selection
|
||||
cell.mxkImageView.userInteractionEnabled = NO;
|
||||
}
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDelegate
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.item < assets.count && self.delegate)
|
||||
{
|
||||
[self.delegate mediaAlbumContentViewController:self didSelectAsset:assets[indexPath.item]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath
|
||||
{
|
||||
// Check whether a asynchronous request is pending
|
||||
if (cell.tag)
|
||||
{
|
||||
[[PHImageManager defaultManager] cancelImageRequest:(PHImageRequestID)cell.tag];
|
||||
cell.tag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDelegateFlowLayout
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.item < assets.count)
|
||||
{
|
||||
CGFloat collectionViewSquareSize = ((collectionView.frame.size.width - 6) / 4); // Here 6 = 3 * cell margin (= 2).
|
||||
CGSize cellSize = CGSizeMake(collectionViewSquareSize, collectionViewSquareSize);
|
||||
|
||||
return cellSize;
|
||||
}
|
||||
return CGSizeZero;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user