2017-04-28 18:18:42 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2017-07-31 23:12:09 +02:00
|
|
|
#import "AvatarViewHelper.h"
|
2017-04-28 18:18:42 +02:00
|
|
|
#import "OWSContactsManager.h"
|
2017-08-24 23:23:17 +02:00
|
|
|
#import "OWSNavigationController.h"
|
2017-08-21 22:58:26 +02:00
|
|
|
#import "Signal-Swift.h"
|
2017-04-28 18:18:42 +02:00
|
|
|
#import "UIUtil.h"
|
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
|
|
|
#import <SignalServiceKit/PhoneNumber.h>
|
|
|
|
#import <SignalServiceKit/TSGroupModel.h>
|
|
|
|
#import <SignalServiceKit/TSGroupThread.h>
|
|
|
|
#import <SignalServiceKit/TSThread.h>
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-07-31 23:12:09 +02:00
|
|
|
@interface AvatarViewHelper () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
|
2017-04-28 18:18:42 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-07-31 23:12:09 +02:00
|
|
|
@implementation AvatarViewHelper
|
2017-04-28 18:18:42 +02:00
|
|
|
|
2017-07-31 23:12:09 +02:00
|
|
|
#pragma mark - Avatar Avatar
|
2017-04-28 18:18:42 +02:00
|
|
|
|
2017-07-31 23:12:09 +02:00
|
|
|
- (void)showChangeAvatarUI
|
2017-04-28 18:18:42 +02:00
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(self.delegate);
|
|
|
|
|
|
|
|
UIAlertController *actionSheetController =
|
2017-08-01 17:31:00 +02:00
|
|
|
[UIAlertController alertControllerWithTitle:self.delegate.avatarActionSheetTitle
|
2017-04-28 18:18:42 +02:00
|
|
|
message:nil
|
|
|
|
preferredStyle:UIAlertControllerStyleActionSheet];
|
2017-08-21 22:58:26 +02:00
|
|
|
[actionSheetController addAction:[OWSAlerts cancelAction]];
|
2017-04-28 18:18:42 +02:00
|
|
|
|
|
|
|
UIAlertAction *takePictureAction = [UIAlertAction
|
|
|
|
actionWithTitle:NSLocalizedString(@"MEDIA_FROM_CAMERA_BUTTON", @"media picker option to take photo or video")
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[self takePicture];
|
|
|
|
}];
|
|
|
|
[actionSheetController addAction:takePictureAction];
|
|
|
|
|
|
|
|
UIAlertAction *choosePictureAction = [UIAlertAction
|
|
|
|
actionWithTitle:NSLocalizedString(@"MEDIA_FROM_LIBRARY_BUTTON", @"media picker option to choose from library")
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[self chooseFromLibrary];
|
|
|
|
}];
|
|
|
|
[actionSheetController addAction:choosePictureAction];
|
|
|
|
|
2017-08-01 17:58:51 +02:00
|
|
|
if (self.delegate.hasClearAvatarAction) {
|
|
|
|
UIAlertAction *clearAction = [UIAlertAction actionWithTitle:self.delegate.clearAvatarActionLabel
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[self.delegate clearAvatar];
|
|
|
|
}];
|
|
|
|
[actionSheetController addAction:clearAction];
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:18:42 +02:00
|
|
|
[self.delegate.fromViewController presentViewController:actionSheetController animated:true completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)takePicture
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(self.delegate);
|
|
|
|
|
|
|
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
|
|
|
picker.delegate = self;
|
|
|
|
picker.allowsEditing = NO;
|
|
|
|
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
|
|
|
|
|
|
|
|
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
|
|
|
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
|
|
|
|
[self.delegate.fromViewController presentViewController:picker
|
|
|
|
animated:YES
|
|
|
|
completion:[UIUtil modalCompletionBlock]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)chooseFromLibrary
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(self.delegate);
|
|
|
|
|
|
|
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
|
|
|
picker.delegate = self;
|
2017-09-19 16:46:37 +02:00
|
|
|
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
|
2017-04-28 18:18:42 +02:00
|
|
|
|
2017-09-19 16:46:37 +02:00
|
|
|
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
|
2017-04-28 18:18:42 +02:00
|
|
|
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
|
|
|
|
[self.delegate.fromViewController presentViewController:picker
|
|
|
|
animated:YES
|
|
|
|
completion:[UIUtil modalCompletionBlock]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dismissing UIImagePickerController
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(self.delegate);
|
|
|
|
|
|
|
|
[self.delegate.fromViewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch data from UIImagePickerController
|
|
|
|
*/
|
|
|
|
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(self.delegate);
|
|
|
|
|
|
|
|
UIImage *rawAvatar = [info objectForKey:UIImagePickerControllerOriginalImage];
|
|
|
|
|
2017-08-24 23:23:17 +02:00
|
|
|
[self.delegate.fromViewController
|
|
|
|
dismissViewControllerAnimated:YES
|
|
|
|
completion:^{
|
|
|
|
if (rawAvatar) {
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
CropScaleImageViewController *vc = [[CropScaleImageViewController alloc]
|
|
|
|
initWithSrcImage:rawAvatar
|
|
|
|
successCompletion:^(UIImage *_Nonnull dstImage) {
|
|
|
|
[self.delegate avatarDidChange:dstImage];
|
|
|
|
}];
|
|
|
|
[self.delegate.fromViewController
|
2017-09-01 15:30:16 +02:00
|
|
|
presentViewController:vc
|
2017-08-24 23:23:17 +02:00
|
|
|
animated:YES
|
|
|
|
completion:[UIUtil modalCompletionBlock]];
|
|
|
|
}
|
|
|
|
}];
|
2017-04-28 18:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|