2014-10-29 21:58:58 +01:00
|
|
|
//
|
|
|
|
// NewGroupViewController.m
|
|
|
|
// Signal
|
|
|
|
//
|
|
|
|
// Created by Dylan Bourgeois on 13/11/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NewGroupViewController.h"
|
|
|
|
#import "SignalsViewController.h"
|
2014-11-21 14:38:37 +01:00
|
|
|
#import "Contact.h"
|
|
|
|
#import "ContactsManager.h"
|
|
|
|
#import "Environment.h"
|
2014-12-24 02:25:10 +01:00
|
|
|
#import "FunctionalUtil.h"
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
#import "Contact.h"
|
|
|
|
#import "GroupModel.h"
|
2014-12-17 06:44:36 +01:00
|
|
|
#import "SecurityUtils.h"
|
|
|
|
#import "SignalKeyingStorage.h"
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2014-12-31 19:31:27 +01:00
|
|
|
#import "UIImage+normalizeImage.h"
|
|
|
|
|
2014-11-24 21:51:43 +01:00
|
|
|
#import "UIUtil.h"
|
2014-10-29 21:58:58 +01:00
|
|
|
#import "DJWActionSheet.h"
|
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import <CoreMedia/CoreMedia.h>
|
|
|
|
|
2014-12-24 02:25:10 +01:00
|
|
|
static NSString* const kUnwindToMessagesViewSegue = @"UnwindToMessagesViewSegue";
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@interface NewGroupViewController () {
|
|
|
|
NSArray* contacts;
|
|
|
|
}
|
2014-12-17 06:44:36 +01:00
|
|
|
@property TSGroupThread* thread;
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
@implementation NewGroupViewController
|
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
- (void)configWithThread:(TSGroupThread *)gThread{
|
|
|
|
_thread = gThread;
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
[super viewDidLoad];
|
2014-12-24 02:25:10 +01:00
|
|
|
contacts = [Environment getCurrent].contactsManager.textSecureContacts;
|
|
|
|
|
|
|
|
contacts = [contacts filter:^int(Contact* contact) {
|
|
|
|
for(PhoneNumber* number in [contact parsedPhoneNumbers]) {
|
|
|
|
if([[number toE164] isEqualToString:[SignalKeyingStorage.localNumber toE164]]) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self initializeDelegates];
|
|
|
|
[self initializeTableView];
|
|
|
|
[self initializeKeyboardHandlers];
|
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
if(_thread==nil) {
|
|
|
|
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Create" style:UIBarButtonItemStylePlain target:self action:@selector(createGroup)];
|
|
|
|
self.navigationItem.title = @"New Group";
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Update" style:UIBarButtonItemStylePlain target:self action:@selector(updateGroup)];
|
|
|
|
self.navigationItem.title = _thread.groupModel.groupName;
|
|
|
|
self.nameGroupTextField.text = _thread.groupModel.groupName;
|
2014-12-24 02:25:10 +01:00
|
|
|
[self setupGroupImageButton:_thread.groupModel.groupImage];
|
|
|
|
// Select the contacts already selected:
|
|
|
|
for (NSInteger r = 0; r < [_tableView numberOfRowsInSection:0]-1; r++) {
|
|
|
|
// TODOGROUP this will not scale well
|
|
|
|
NSMutableSet *usersInGroup = [NSMutableSet setWithArray:_thread.groupModel.groupMemberIds];
|
|
|
|
NSMutableArray *contactPhoneNumbers = [[NSMutableArray alloc] init];
|
|
|
|
for(PhoneNumber* number in [[contacts objectAtIndex:(NSUInteger)r] parsedPhoneNumbers]) {
|
|
|
|
[contactPhoneNumbers addObject:[number toE164]];
|
|
|
|
}
|
|
|
|
[usersInGroup intersectSet:[NSSet setWithArray:contactPhoneNumbers]];
|
|
|
|
if([usersInGroup count]>0) {
|
|
|
|
[_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:(r+1) inSection:0]
|
|
|
|
animated:NO
|
|
|
|
scrollPosition:UITableViewScrollPositionNone];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning {
|
|
|
|
[super didReceiveMemoryWarning];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Initializers
|
|
|
|
|
|
|
|
-(void)initializeDelegates
|
|
|
|
{
|
|
|
|
self.nameGroupTextField.delegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)initializeTableView
|
|
|
|
{
|
|
|
|
self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Keyboard notifications
|
|
|
|
|
|
|
|
- (void)initializeKeyboardHandlers{
|
|
|
|
UITapGestureRecognizer *outsideTabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboardFromAppropriateSubView)];
|
|
|
|
[self.tapToDismissView addGestureRecognizer:outsideTabRecognizer];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dismissKeyboardFromAppropriateSubView {
|
|
|
|
[self.nameGroupTextField resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
-(void)createGroup {
|
2014-12-17 06:44:36 +01:00
|
|
|
GroupModel* model = [self makeGroup];
|
|
|
|
[Environment groupModel:model];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(void)updateGroup {
|
2014-12-24 02:25:10 +01:00
|
|
|
NSMutableArray* mut = [[NSMutableArray alloc]init];
|
|
|
|
for (NSIndexPath* idx in _tableView.indexPathsForSelectedRows) {
|
|
|
|
[mut addObjectsFromArray:[[contacts objectAtIndex:(NSUInteger)idx.row-1] textSecureIdentifiers]];
|
|
|
|
}
|
|
|
|
[mut addObject:[SignalKeyingStorage.localNumber toE164]]; // Also add the originator
|
|
|
|
_groupModel = [[GroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_groupImageButton.imageView.image groupId:_thread.groupModel.groupId];
|
|
|
|
|
|
|
|
[self performSegueWithIdentifier:kUnwindToMessagesViewSegue sender:self];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
-(GroupModel*)makeGroup {
|
|
|
|
NSString* title = _nameGroupTextField.text;
|
|
|
|
UIImage* img = _groupImageButton.imageView.image;
|
|
|
|
NSMutableArray* mut = [[NSMutableArray alloc]init];
|
|
|
|
|
|
|
|
for (NSIndexPath* idx in _tableView.indexPathsForSelectedRows) {
|
2014-12-17 06:44:36 +01:00
|
|
|
[mut addObjectsFromArray:[[contacts objectAtIndex:(NSUInteger)idx.row-1] textSecureIdentifiers]];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2014-12-17 06:44:36 +01:00
|
|
|
// Also add the originator
|
|
|
|
[mut addObject:[SignalKeyingStorage.localNumber toE164]];
|
|
|
|
NSData* groupId = [SecurityUtils generateRandomBytes:16];
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
return [[GroupModel alloc] initWithTitle:title memberIds:mut image:img groupId:groupId];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
-(IBAction)addGroupPhoto:(id)sender
|
|
|
|
{
|
2014-12-24 02:25:10 +01:00
|
|
|
[self.nameGroupTextField resignFirstResponder];
|
2014-10-29 21:58:58 +01:00
|
|
|
[DJWActionSheet showInView:self.parentViewController.view withTitle:nil cancelButtonTitle:@"Cancel"
|
|
|
|
destructiveButtonTitle:nil otherButtonTitles:@[@"Take a Picture",@"Choose from Library"]
|
|
|
|
tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) {
|
|
|
|
|
|
|
|
if (tappedButtonIndex == actionSheet.cancelButtonIndex) {
|
|
|
|
NSLog(@"User Cancelled");
|
|
|
|
} else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) {
|
|
|
|
NSLog(@"Destructive button tapped");
|
|
|
|
}else {
|
|
|
|
switch (tappedButtonIndex) {
|
|
|
|
case 0:
|
|
|
|
[self takePicture];
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
[self chooseFromLibrary];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Group Image
|
|
|
|
|
|
|
|
-(void)takePicture
|
|
|
|
{
|
|
|
|
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 presentViewController:picker animated:YES completion:NULL];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)chooseFromLibrary
|
|
|
|
{
|
|
|
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
|
|
|
picker.delegate = self;
|
|
|
|
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
|
|
|
|
|
|
|
|
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
|
|
|
|
{
|
|
|
|
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
|
|
|
|
[self presentViewController:picker animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dismissing UIImagePickerController
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch data from UIImagePickerController
|
|
|
|
*/
|
|
|
|
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
|
|
|
{
|
|
|
|
UIImage *picture_camera = [info objectForKey:UIImagePickerControllerOriginalImage];
|
|
|
|
|
|
|
|
if (picture_camera) {
|
2014-12-31 19:31:27 +01:00
|
|
|
[self setupGroupImageButton:[picture_camera resizedImageToFitInSize:CGSizeMake(100.0,100.0) scaleIfSmaller:NO]];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
2014-12-24 02:25:10 +01:00
|
|
|
-(void)setupGroupImageButton:(UIImage*)image {
|
|
|
|
_groupImageButton.imageView.image = image;
|
|
|
|
_groupImageButton.imageView.layer.cornerRadius = 4.0f;
|
|
|
|
_groupImageButton.imageView.clipsToBounds = YES;
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
#pragma mark - Table view data source
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
return (NSInteger)[contacts count]+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell"];
|
|
|
|
|
|
|
|
if (cell == nil) {
|
|
|
|
|
|
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: indexPath.row == 0 ? @"HeaderCell" : @"GroupSearchCell"];
|
|
|
|
}
|
|
|
|
if (indexPath.row > 0) {
|
2014-12-17 06:44:36 +01:00
|
|
|
NSUInteger row = (NSUInteger)indexPath.row;
|
|
|
|
Contact* contact = contacts[row-1];
|
|
|
|
|
|
|
|
cell.textLabel.attributedText = [self attributedStringForContact:contact inCell:cell];
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
cell.textLabel.text = @"Add People:";
|
|
|
|
cell.textLabel.textColor = [UIColor lightGrayColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Table View delegate
|
|
|
|
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Text Field Delegate
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
|
|
|
|
[self.nameGroupTextField resignFirstResponder];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
#pragma mark - Cell Utility
|
|
|
|
|
|
|
|
- (NSAttributedString *)attributedStringForContact:(Contact *)contact inCell:(UITableViewCell*)cell {
|
|
|
|
NSMutableAttributedString *fullNameAttributedString = [[NSMutableAttributedString alloc] initWithString:contact.fullName];
|
|
|
|
|
|
|
|
UIFont *firstNameFont;
|
|
|
|
UIFont *lastNameFont;
|
|
|
|
|
|
|
|
if (ABPersonGetSortOrdering() == kABPersonCompositeNameFormatFirstNameFirst) {
|
2014-11-24 21:51:43 +01:00
|
|
|
firstNameFont = [UIFont ows_lightFontWithSize:cell.textLabel.font.pointSize];
|
2014-11-21 14:38:37 +01:00
|
|
|
lastNameFont = [UIFont systemFontOfSize:cell.textLabel.font.pointSize];
|
|
|
|
} else{
|
2014-11-24 21:51:43 +01:00
|
|
|
firstNameFont = [UIFont ows_lightFontWithSize:cell.textLabel.font.pointSize];
|
2014-11-21 14:38:37 +01:00
|
|
|
lastNameFont = [UIFont systemFontOfSize:cell.textLabel.font.pointSize];
|
|
|
|
}
|
|
|
|
[fullNameAttributedString addAttribute:NSFontAttributeName value:firstNameFont range:NSMakeRange(0, contact.firstName.length)];
|
|
|
|
[fullNameAttributedString addAttribute:NSFontAttributeName value:lastNameFont range:NSMakeRange(contact.firstName.length + 1, contact.lastName.length)];
|
|
|
|
|
|
|
|
[fullNameAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, contact.fullName.length)];
|
|
|
|
return fullNameAttributedString;
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
@end
|