2017-07-24 12:56:31 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Aram Sargsyan
|
|
|
|
|
|
|
|
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 "RoomsListViewController.h"
|
|
|
|
#import "RoomTableViewCell.h"
|
2017-07-30 17:29:38 +00:00
|
|
|
#import "NSBundle+MatrixKit.h"
|
2017-07-24 22:08:41 +00:00
|
|
|
@import MobileCoreServices;
|
2017-07-24 12:56:31 +00:00
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
|
2017-07-24 12:56:31 +00:00
|
|
|
@interface RoomsListViewController () <UITableViewDataSource, UITableViewDelegate>
|
|
|
|
|
2017-07-24 22:08:41 +00:00
|
|
|
@property (nonatomic) NSExtensionContext *shareExtensionContext;
|
2017-07-30 17:29:38 +00:00
|
|
|
@property (copy) void (^failureBlock)();
|
2017-07-24 12:56:31 +00:00
|
|
|
@property (nonatomic) NSArray <MXRoom *> *rooms;
|
|
|
|
@property (nonatomic) UITableView *mainTableView;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-07-24 22:08:41 +00:00
|
|
|
|
2017-07-24 12:56:31 +00:00
|
|
|
@implementation RoomsListViewController
|
|
|
|
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
[self configureTableView];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public
|
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
+ (instancetype)listViewControllerWithContext:(NSExtensionContext *)context failureBlock:(void(^)())failureBlock
|
2017-07-24 22:08:41 +00:00
|
|
|
{
|
|
|
|
RoomsListViewController *listViewController = [[self class] new];
|
|
|
|
listViewController.shareExtensionContext = context;
|
2017-07-30 17:29:38 +00:00
|
|
|
listViewController.failureBlock = failureBlock;
|
2017-07-24 22:08:41 +00:00
|
|
|
return listViewController;
|
|
|
|
}
|
|
|
|
|
2017-07-24 12:56:31 +00:00
|
|
|
- (void)updateWithRooms:(NSArray <MXRoom *>*)rooms
|
|
|
|
{
|
|
|
|
self.rooms = rooms;
|
|
|
|
[self.mainTableView reloadData];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Views
|
|
|
|
|
|
|
|
- (void)configureTableView
|
|
|
|
{
|
|
|
|
self.mainTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
|
|
|
|
self.mainTableView.dataSource = self;
|
|
|
|
self.mainTableView.delegate = self;
|
|
|
|
[self.mainTableView registerNib:[RoomTableViewCell nib] forCellReuseIdentifier:[RoomTableViewCell defaultReuseIdentifier]];
|
|
|
|
|
|
|
|
[self.view addSubview:self.mainTableView];
|
|
|
|
self.mainTableView.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
self.view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.mainTableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
|
|
|
|
widthConstraint.active = YES;
|
|
|
|
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.mainTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
|
|
|
|
heightConstraint.active = YES;
|
|
|
|
NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:self.mainTableView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
|
|
|
|
centerXConstraint.active = YES;
|
|
|
|
NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:self.mainTableView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
|
|
|
|
centerYConstraint.active = YES;
|
|
|
|
}
|
|
|
|
|
2017-07-24 22:08:41 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)sendToRoom:(MXRoom *)room
|
|
|
|
{
|
2017-07-28 17:47:38 +00:00
|
|
|
NSString *UTTypeText = (__bridge NSString *)kUTTypeText;
|
|
|
|
NSString *UTTypeURL = (__bridge NSString *)kUTTypeURL;
|
2017-07-24 22:08:41 +00:00
|
|
|
NSString *UTTypeImage = (__bridge NSString *)kUTTypeImage;
|
|
|
|
NSString *UTTypeVideo = (__bridge NSString *)kUTTypeVideo;
|
|
|
|
|
|
|
|
for (NSExtensionItem *item in self.shareExtensionContext.inputItems)
|
|
|
|
{
|
|
|
|
for (NSItemProvider *itemProvider in item.attachments)
|
|
|
|
{
|
2017-07-28 17:47:38 +00:00
|
|
|
if ([itemProvider hasItemConformingToTypeIdentifier:UTTypeText])
|
|
|
|
{
|
|
|
|
[itemProvider loadItemForTypeIdentifier:UTTypeText options:nil completionHandler:^(NSString *text, NSError * _Null_unspecified error) {
|
2017-07-30 17:29:38 +00:00
|
|
|
if (!text)
|
|
|
|
{
|
|
|
|
[self showFailureAlert];
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 17:47:38 +00:00
|
|
|
[room sendTextMessage:text success:^(NSString *eventId) {
|
|
|
|
[self.shareExtensionContext completeRequestReturningItems:@[item] completionHandler:nil];
|
|
|
|
} failure:^(NSError *error) {
|
2017-07-30 17:29:38 +00:00
|
|
|
NSLog(@"[RoomsListViewController] sendTextMessage failed.");
|
|
|
|
[self showFailureAlert];
|
2017-07-28 17:47:38 +00:00
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else if ([itemProvider hasItemConformingToTypeIdentifier:UTTypeURL])
|
|
|
|
{
|
|
|
|
[itemProvider loadItemForTypeIdentifier:UTTypeURL options:nil completionHandler:^(NSURL *url, NSError * _Null_unspecified error) {
|
2017-07-30 17:29:38 +00:00
|
|
|
if (!url)
|
|
|
|
{
|
|
|
|
[self showFailureAlert];
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 17:47:38 +00:00
|
|
|
[room sendTextMessage:url.absoluteString success:^(NSString *eventId) {
|
|
|
|
[self.shareExtensionContext completeRequestReturningItems:@[item] completionHandler:nil];
|
|
|
|
} failure:^(NSError *error) {
|
2017-07-30 17:29:38 +00:00
|
|
|
NSLog(@"[RoomsListViewController] sendTextMessage failed.");
|
|
|
|
[self showFailureAlert];
|
2017-07-28 17:47:38 +00:00
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else if ([itemProvider hasItemConformingToTypeIdentifier:UTTypeImage])
|
2017-07-24 22:08:41 +00:00
|
|
|
{
|
|
|
|
[itemProvider loadItemForTypeIdentifier:UTTypeImage options:nil completionHandler:^(NSData *imageData, NSError * _Null_unspecified error)
|
|
|
|
{
|
2017-07-30 17:29:38 +00:00
|
|
|
if (!imageData)
|
|
|
|
{
|
|
|
|
[self showFailureAlert];
|
|
|
|
return;
|
|
|
|
}
|
2017-07-31 15:26:06 +00:00
|
|
|
//Send the image
|
2017-07-24 22:08:41 +00:00
|
|
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
2017-08-03 11:09:36 +00:00
|
|
|
|
|
|
|
NSString *mimeType;
|
|
|
|
if ([itemProvider hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypePNG])
|
|
|
|
{
|
|
|
|
mimeType = @"image/png";
|
|
|
|
}
|
|
|
|
else if ([itemProvider hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypeJPEG])
|
|
|
|
{
|
|
|
|
mimeType = @"image/jpeg";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
image = [[UIImage alloc] initWithData:UIImageJPEGRepresentation(image, 1.0)];
|
|
|
|
mimeType = @"image/jpeg";
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:26:06 +00:00
|
|
|
UIImage *thumbnail = nil;
|
|
|
|
// Thumbnail is useful only in case of encrypted room
|
|
|
|
if (room.state.isEncrypted)
|
|
|
|
{
|
|
|
|
thumbnail = [MXKTools reduceImage:image toFitInSize:CGSizeMake(800, 600)];
|
|
|
|
if (thumbnail == image)
|
|
|
|
{
|
|
|
|
thumbnail = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[room sendImage:imageData withImageSize:image.size mimeType:mimeType andThumbnail:thumbnail localEcho:nil success:^(NSString *eventId)
|
2017-07-24 22:08:41 +00:00
|
|
|
{
|
|
|
|
[self.shareExtensionContext completeRequestReturningItems:@[item] completionHandler:nil];
|
|
|
|
}
|
|
|
|
failure:^(NSError *error)
|
|
|
|
{
|
2017-07-30 17:29:38 +00:00
|
|
|
NSLog(@"[RoomsListViewController] sendImage failed.");
|
|
|
|
[self showFailureAlert];
|
2017-07-24 22:08:41 +00:00
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else if ([itemProvider hasItemConformingToTypeIdentifier:UTTypeVideo])
|
|
|
|
{
|
2017-07-30 17:29:38 +00:00
|
|
|
[itemProvider loadItemForTypeIdentifier:UTTypeVideo options:nil completionHandler:^(NSURL *videoLocalUrl, NSError * _Null_unspecified error)
|
2017-07-24 22:08:41 +00:00
|
|
|
{
|
2017-07-30 17:29:38 +00:00
|
|
|
if (!videoLocalUrl)
|
|
|
|
{
|
|
|
|
[self showFailureAlert];
|
|
|
|
return;
|
|
|
|
}
|
2017-07-31 15:26:06 +00:00
|
|
|
|
|
|
|
// Retrieve the video frame at 1 sec to define the video thumbnail
|
|
|
|
AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:videoLocalUrl options:nil];
|
|
|
|
AVAssetImageGenerator *assetImageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
|
|
|
|
assetImageGenerator.appliesPreferredTrackTransform = YES;
|
|
|
|
CMTime time = CMTimeMake(1, 1);
|
|
|
|
CGImageRef imageRef = [assetImageGenerator copyCGImageAtTime:time actualTime:NULL error:nil];
|
|
|
|
// Finalize video attachment
|
|
|
|
UIImage *videoThumbnail = [[UIImage alloc] initWithCGImage:imageRef];
|
|
|
|
CFRelease(imageRef);
|
|
|
|
|
|
|
|
[room sendVideo:videoLocalUrl withThumbnail:videoThumbnail localEcho:nil success:^(NSString *eventId) {
|
2017-07-30 17:29:38 +00:00
|
|
|
[self.shareExtensionContext completeRequestReturningItems:@[item] completionHandler:nil];
|
2017-07-28 17:47:38 +00:00
|
|
|
} failure:^(NSError *error) {
|
2017-07-30 17:29:38 +00:00
|
|
|
NSLog(@"[RoomsListViewController] sendVideo failed.");
|
|
|
|
[self showFailureAlert];
|
2017-07-28 17:47:38 +00:00
|
|
|
}];
|
|
|
|
|
2017-07-24 22:08:41 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
- (void)showFailureAlert
|
|
|
|
{
|
|
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedStringFromTable(@"room_event_failed_to_send", @"Vector", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
UIAlertAction *okAction = [UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
|
if (self.failureBlock)
|
|
|
|
{
|
|
|
|
self.failureBlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self.shareExtensionContext cancelRequestWithError:[NSError errorWithDomain:@"MXUserFailureErrorDomain" code:500 userInfo:nil]];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[alertController addAction:okAction];
|
|
|
|
[self presentViewController:alertController animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
2017-07-24 12:56:31 +00:00
|
|
|
#pragma mark - UITableViewDataSource
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
|
|
|
return self.rooms.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
RoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[RoomTableViewCell defaultReuseIdentifier]];
|
|
|
|
MXRoom *room = self.rooms[indexPath.row];
|
|
|
|
|
|
|
|
[cell render:room];
|
|
|
|
if (!room.summary.displayname.length && !cell.titleLabel.text.length)
|
|
|
|
{
|
|
|
|
cell.titleLabel.text = NSLocalizedStringFromTable(@"room_displayname_no_title", @"Vector", nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
return [RoomTableViewCell cellHeight];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-07-24 22:08:41 +00:00
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:NSLocalizedStringFromTable(@"send_to", @"Vector", nil), self.rooms[indexPath.row].riotDisplayname] message:nil preferredStyle:UIAlertControllerStyleAlert];
|
2017-07-24 22:08:41 +00:00
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] style:UIAlertActionStyleCancel handler:nil];
|
2017-07-24 22:08:41 +00:00
|
|
|
[alertController addAction:cancelAction];
|
|
|
|
|
2017-07-30 17:29:38 +00:00
|
|
|
UIAlertAction *sendAction = [UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"send"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
2017-07-24 22:08:41 +00:00
|
|
|
[self sendToRoom:self.rooms[indexPath.row]];
|
|
|
|
}];
|
|
|
|
[alertController addAction:sendAction];
|
|
|
|
|
|
|
|
[self presentViewController:alertController animated:YES completion:nil];
|
2017-07-24 12:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|