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

112 lines
4.2 KiB
Objective-C

#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"
#import "TGMediaAssetsLibrary.h"
@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];
[[[[TGMediaAssetsLibrary sharedLibrary] saveAssetWithImageData:data] deliverOn:[SQueue mainQueue]] startWithNext:nil error:^(__unused id error)
{
[TGAccessChecker checkPhotoAuthorizationStatusForIntent:TGPhotoAccessIntentSave alertDismissCompletion:nil];
[progressWindow dismiss:true];
} completed:^
{
[progressWindow dismissWithSuccess];
}];
}
@end