1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 09:27:55 +01:00
Telegram/Telegraph/TGGroupAvatarGalleryModel.m

112 lines
4.2 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGGroupAvatarGalleryModel.h"
#import "TGGroupAvatarGalleryItem.h"
#import "ActionStage.h"
#import "TGDatabase.h"
#import "TGImageMediaAttachment.h"
#import "TGGenericPeerMediaGalleryDefaultHeaderView.h"
#import "TGGenericPeerMediaGalleryActionsAccessoryView.h"
#import "TGActionSheet.h"
#import "TGProgressWindow.h"
#import "TGAccessChecker.h"
2016-02-25 01:03:51 +01:00
#import "TGMediaAssetsLibrary.h"
2015-10-01 18:19:52 +02:00
@implementation TGGroupAvatarGalleryModel
- (instancetype)initWithMessageId:(int32_t)messageId legacyThumbnailUrl:(NSString *)legacyThumbnailUrl legacyUrl:(NSString *)legacyUrl imageSize:(CGSize)imageSize
{
self = [super init];
if (self != nil)
{
TGGroupAvatarGalleryItem *item = [[TGGroupAvatarGalleryItem alloc] initWithMessageId:messageId legacyThumbnailUrl:legacyThumbnailUrl legacyUrl:legacyUrl imageSize:imageSize];
[self _replaceItems:@[item] focusingOnItem:item];
}
return self;
}
- (UIView<TGModernGalleryDefaultFooterAccessoryView> *)createDefaultLeftAccessoryView
{
TGGenericPeerMediaGalleryActionsAccessoryView *accessoryView = [[TGGenericPeerMediaGalleryActionsAccessoryView alloc] init];
__weak TGGroupAvatarGalleryModel *weakSelf = self;
accessoryView.action = ^(id<TGModernGalleryItem> item)
{
if ([item isKindOfClass:[TGGroupAvatarGalleryItem class]])
{
__strong TGGroupAvatarGalleryModel *strongSelf = weakSelf;
if (strongSelf != nil)
{
UIView *actionSheetView = nil;
if (strongSelf.actionSheetView)
actionSheetView = strongSelf.actionSheetView();
if (actionSheetView != nil)
{
NSMutableArray *actions = [[NSMutableArray alloc] init];
if ([strongSelf _isDataAvailableForSavingItemToCameraRoll:item])
{
[actions addObject:[[TGActionSheetAction alloc] initWithTitle:TGLocalized(@"Preview.SaveToCameraRoll") action:@"save" type:TGActionSheetActionTypeGeneric]];
}
[actions addObject:[[TGActionSheetAction alloc] initWithTitle:TGLocalized(@"Common.Cancel") action:@"cancel" type:TGActionSheetActionTypeCancel]];
[[[TGActionSheet alloc] initWithTitle:nil actions:actions actionBlock:^(__unused id target, NSString *action)
{
__strong TGGroupAvatarGalleryModel *strongSelf = weakSelf;
if ([action isEqualToString:@"save"])
[strongSelf _commitSaveItemToCameraRoll:item];
} target:strongSelf] showInView:actionSheetView];
}
}
}
};
return accessoryView;
}
- (bool)_isDataAvailableForSavingItemToCameraRoll:(id<TGModernGalleryItem>)item
{
if ([item isKindOfClass:[TGGroupAvatarGalleryItem class]])
{
TGGroupAvatarGalleryItem *avatarItem = (TGGroupAvatarGalleryItem *)item;
return [[NSFileManager defaultManager] fileExistsAtPath:[avatarItem filePath]];
}
return false;
}
- (void)_commitSaveItemToCameraRoll:(id<TGModernGalleryItem>)item
{
if ([item isKindOfClass:[TGGroupAvatarGalleryItem class]])
{
TGGroupAvatarGalleryItem *avatarItem = (TGGroupAvatarGalleryItem *)item;
NSData *data = [[NSData alloc] initWithContentsOfFile:[avatarItem filePath]];
[self _saveImageDataToCameraRoll:data];
}
}
- (void)_saveImageDataToCameraRoll:(NSData *)data
{
if (data == nil)
return;
if (![TGAccessChecker checkPhotoAuthorizationStatusForIntent:TGPhotoAccessIntentSave alertDismissCompletion:nil])
return;
TGProgressWindow *progressWindow = [[TGProgressWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[progressWindow show:true];
2016-02-25 01:03:51 +01:00
[[[[TGMediaAssetsLibrary sharedLibrary] saveAssetWithImageData:data] deliverOn:[SQueue mainQueue]] startWithNext:nil error:^(__unused id error)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
[TGAccessChecker checkPhotoAuthorizationStatusForIntent:TGPhotoAccessIntentSave alertDismissCompletion:nil];
[progressWindow dismiss:true];
} completed:^
{
[progressWindow dismissWithSuccess];
2015-10-01 18:19:52 +02:00
}];
}
@end