element-ios/Vector/Views/RoomInputToolbar/RoomInputToolbarView.m

512 lines
21 KiB
Mathematica
Raw Normal View History

2015-08-18 08:04:30 +00:00
/*
Copyright 2015 OpenMarket Ltd
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 "RoomInputToolbarView.h"
//#import <MediaPlayer/MediaPlayer.h>
//#import <MobileCoreServices/MobileCoreServices.h>
//
//#import <AssetsLibrary/ALAsset.h>
//#import <AssetsLibrary/ALAssetRepresentation.h>
#define MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE 1024
#define MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE 768
#define MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE 512
@interface RoomInputToolbarView()
{
MediaPickerViewController *mediaPicker;
}
@end
@implementation RoomInputToolbarView
+ (UINib *)nib
{
return [UINib nibWithNibName:NSStringFromClass([RoomInputToolbarView class])
bundle:[NSBundle bundleForClass:[RoomInputToolbarView class]]];
}
+ (instancetype)roomInputToolbarView
{
if ([[self class] nib])
{
return [[[self class] nib] instantiateWithOwner:nil options:nil].firstObject;
}
else
{
return [[self alloc] init];
}
}
- (void)awakeFromNib
{
[super awakeFromNib];
self.leftInputToolbarButton.backgroundColor = [UIColor clearColor];
[self.leftInputToolbarButton setImage:[UIImage imageNamed:@"attach_media"] forState:UIControlStateNormal];
[self.leftInputToolbarButton setImage:[UIImage imageNamed:@"attach_media"] forState:UIControlStateHighlighted];
self.rightInputToolbarButton.backgroundColor = [UIColor clearColor];
[self.rightInputToolbarButton setImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
[self.rightInputToolbarButton setImage:[UIImage imageNamed:@"send"] forState:UIControlStateHighlighted];
[self.rightInputToolbarButton setTitle:nil forState:UIControlStateNormal];
[self.rightInputToolbarButton setTitle:nil forState:UIControlStateHighlighted];
self.rightInputToolbarButton.enabled = YES;
}
- (void)setTextMessage:(NSString *)textMessage
{
[super setTextMessage:textMessage];
// Force right button to be enabled (even if the text input is empty)
self.rightInputToolbarButton.enabled = YES;
}
#pragma mark - HPGrowingTextView delegate
- (void)growingTextViewDidChange:(HPGrowingTextView *)sender
{
[super growingTextViewDidChange:sender];
// Force right button to be enabled (even if the text input is empty)
self.rightInputToolbarButton.enabled = YES;
}
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView
{
NSString *message = self.textMessage;
// Reset message
self.textMessage = nil;
// Send button has been pressed
if (message.length && [self.delegate respondsToSelector:@selector(roomInputToolbarView:sendTextMessage:)])
{
[self.delegate roomInputToolbarView:self sendTextMessage:message];
}
return YES;
}
#pragma mark - Override MXKRoomInputToolbarView
- (IBAction)onTouchUpInside:(UIButton*)button
{
if (button == self.leftInputToolbarButton)
{
// Check whether media attachment is supported
if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:presentViewController:)])
{
mediaPicker = [MediaPickerViewController mediaPickerViewController];
mediaPicker.delegate = self;
UINavigationController *navigationController = [UINavigationController new];
[navigationController pushViewController:mediaPicker animated:NO];
[self.delegate roomInputToolbarView:self presentViewController:mediaPicker];
}
else
{
NSLog(@"[RoomInputToolbarView] Attach media is not supported");
}
}
else if (button == self.rightInputToolbarButton)
{
// Check whether media attachment is supported
if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:presentViewController:)])
{
mediaPicker = [MediaPickerViewController mediaPickerViewController];
mediaPicker.delegate = self;
UINavigationController *navigationController = [UINavigationController new];
[navigationController pushViewController:mediaPicker animated:NO];
[self.delegate roomInputToolbarView:self presentViewController:mediaPicker];
}
else
{
NSLog(@"[RoomInputToolbarView] Attach media is not supported");
}
}
// else if (button == self.rightInputToolbarButton)
// {
//
// NSString *message = self.textMessage;
//
// // Reset message
// self.textMessage = nil;
//
// // Send button has been pressed
// if (message.length && [self.delegate respondsToSelector:@selector(roomInputToolbarView:sendTextMessage:)])
// {
// [self.delegate roomInputToolbarView:self sendTextMessage:message];
// }
// }
}
- (void)destroy
{
[super destroy];
}
//#pragma mark - UIImagePickerControllerDelegate
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
// NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
// {
//
// /*
// NSData *dataOfGif = [NSData dataWithContentsOfFile: [info objectForKey:UIImagePickerControllerReferenceURL]];
//
// NSLog(@"%d", dataOfGif.length);
//
// ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// [library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset)
// {
//
// NSLog(@"%@", asset.defaultRepresentation.metadata);
//
//
// NSLog(@"%@", asset.defaultRepresentation.url);
//
// NSData *dataOfGif = [NSData dataWithContentsOfURL: asset.defaultRepresentation.url];
//
// NSLog(@"%d", dataOfGif.length);
// ;
//
// } failureBlock:^(NSError *error)
// {
//
// }];
//
// */
//
// if (![self.delegate respondsToSelector:@selector(roomInputToolbarView:sendImage:)])
// {
// NSLog(@"[RoomInputToolbarView] Attach image is not supported");
// }
// else
// {
// UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// if (selectedImage)
// {
// // media picker does not offer a preview
// // so add a preview to let the user validates his selection
// if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
// {
// __weak typeof(self) weakSelf = self;
//
// imageValidationView = [[MXKImageView alloc] initWithFrame:CGRectZero];
// imageValidationView.stretchable = YES;
//
// // the user validates the image
// [imageValidationView setRightButtonTitle:[NSBundle mxk_localizedStringForKey:@"ok"] handler:^(MXKImageView* imageView, NSString* buttonTitle)
// {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
//
// // Dismiss the image view
// [strongSelf dismissImageValidationView];
//
// // prompt user about image compression
// [strongSelf promptCompressionForSelectedImage:info];
// }];
//
// // the user wants to use an other image
// [imageValidationView setLeftButtonTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] handler:^(MXKImageView* imageView, NSString* buttonTitle)
// {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
//
// // dismiss the image view
// [strongSelf dismissImageValidationView];
//
// // Open again media gallery
// strongSelf->mediaPicker = [[UIImagePickerController alloc] init];
// strongSelf->mediaPicker.delegate = strongSelf;
// strongSelf->mediaPicker.sourceType = picker.sourceType;
// strongSelf->mediaPicker.allowsEditing = NO;
// strongSelf->mediaPicker.mediaTypes = picker.mediaTypes;
// [strongSelf.delegate roomInputToolbarView:strongSelf presentMediaPicker:strongSelf->mediaPicker];
// }];
//
// imageValidationView.image = selectedImage;
// [imageValidationView showFullScreen];
// }
// else
// {
// // Save the original image in user's photos library and suggest compression before sending image
// [MXKMediaManager saveImageToPhotosLibrary:selectedImage success:nil failure:nil];
// [self promptCompressionForSelectedImage:info];
// }
// }
// }
// }
// else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
// {
// NSURL* selectedVideo = [info objectForKey:UIImagePickerControllerMediaURL];
//
// // Check the selected video, and ignore multiple calls (observed when user pressed several time Choose button)
// if (selectedVideo && !tmpVideoPlayer)
// {
// if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
// {
// [MXKMediaManager saveMediaToPhotosLibrary:selectedVideo isImage:NO success:nil failure:nil];
// }
//
// // Create video thumbnail
// tmpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:selectedVideo];
// if (tmpVideoPlayer)
// {
// [tmpVideoPlayer setShouldAutoplay:NO];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(moviePlayerThumbnailImageRequestDidFinishNotification:)
// name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
// object:nil];
// [tmpVideoPlayer requestThumbnailImagesAtTimes:@[@1.0f] timeOption:MPMovieTimeOptionNearestKeyFrame];
// // We will finalize video attachment when thumbnail will be available (see movie player callback)
// return;
// }
// }
// }
//
// [self dismissMediaPicker];
//}
//
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
// [self dismissMediaPicker];
//}
//
//- (void)dismissImageValidationView
//{
// if (imageValidationView)
// {
// [imageValidationView dismissSelection];
// [imageValidationView removeFromSuperview];
// imageValidationView = nil;
// }
//}
//
//- (void)promptCompressionForSelectedImage:(NSDictionary*)selectedImageInfo
//{
// if (currentAlert)
// {
// [currentAlert dismiss:NO];
// currentAlert = nil;
// }
//
// UIImage *selectedImage = [selectedImageInfo objectForKey:UIImagePickerControllerOriginalImage];
// CGSize originalSize = selectedImage.size;
// NSLog(@"Selected image size : %f %f", originalSize.width, originalSize.height);
//
// [self getSelectedImageFileData:selectedImageInfo success:^(NSData *selectedImageFileData) {
//
// long long smallFilesize = 0;
// long long mediumFilesize = 0;
// long long largeFilesize = 0;
//
// // succeed to get the file size (provided by the photo library)
// long long originalFileSize = selectedImageFileData.length;
// NSLog(@"- use the photo library file size: %tu", originalFileSize);
//
// CGFloat maxSize = MAX(originalSize.width, originalSize.height);
// if (maxSize >= MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE)
// {
// CGFloat factor = MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE / maxSize;
// smallFilesize = factor * factor * originalFileSize;
// }
// else
// {
// NSLog(@"- too small to fit in %d", MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE);
// }
//
// if (maxSize >= MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE)
// {
// CGFloat factor = MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE / maxSize;
// mediumFilesize = factor * factor * originalFileSize;
// }
// else
// {
// NSLog(@"- too small to fit in %d", MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE);
// }
//
// if (maxSize >= MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE)
// {
// CGFloat factor = MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE / maxSize;
// largeFilesize = factor * factor * originalFileSize;
// }
// else
// {
// NSLog(@"- too small to fit in %d", MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE);
// }
//
// if (smallFilesize || mediumFilesize || largeFilesize)
// {
// currentAlert = [[MXKAlert alloc] initWithTitle:[NSBundle mxk_localizedStringForKey:@"attachment_size_prompt"] message:nil style:MXKAlertStyleActionSheet];
// __weak typeof(self) weakSelf = self;
//
// if (smallFilesize)
// {
// NSString *title = [NSString stringWithFormat:[NSBundle mxk_localizedStringForKey:@"attachment_small"], [MXKTools fileSizeToString: (int)smallFilesize]];
// [currentAlert addActionWithTitle:title style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
// strongSelf->currentAlert = nil;
//
// // Send the small image
// UIImage *smallImage = [MXKTools resize:selectedImage toFitInSize:CGSizeMake(MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE, MXKROOM_INPUT_TOOLBAR_VIEW_SMALL_IMAGE_SIZE)];
// [strongSelf.delegate roomInputToolbarView:weakSelf sendImage:smallImage];
// }];
// }
//
// if (mediumFilesize)
// {
// NSString *title = [NSString stringWithFormat:[NSBundle mxk_localizedStringForKey:@"attachment_medium"], [MXKTools fileSizeToString: (int)mediumFilesize]];
// [currentAlert addActionWithTitle:title style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
// strongSelf->currentAlert = nil;
//
// // Send the medium image
// UIImage *mediumImage = [MXKTools resize:selectedImage toFitInSize:CGSizeMake(MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE, MXKROOM_INPUT_TOOLBAR_VIEW_MEDIUM_IMAGE_SIZE)];
// [strongSelf.delegate roomInputToolbarView:weakSelf sendImage:mediumImage];
// }];
// }
//
// if (largeFilesize)
// {
// NSString *title = [NSString stringWithFormat:[NSBundle mxk_localizedStringForKey:@"attachment_large"], [MXKTools fileSizeToString: (int)largeFilesize]];
// [currentAlert addActionWithTitle:title style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
// strongSelf->currentAlert = nil;
//
// // Send the large image
// UIImage *largeImage = [MXKTools resize:selectedImage toFitInSize:CGSizeMake(MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE, MXKROOM_INPUT_TOOLBAR_VIEW_LARGE_IMAGE_SIZE)];
// [strongSelf.delegate roomInputToolbarView:weakSelf sendImage:largeImage];
// }];
// }
//
// NSString *title = [NSString stringWithFormat:[NSBundle mxk_localizedStringForKey:@"attachment_original"], [MXKTools fileSizeToString: (int)originalFileSize]];
// [currentAlert addActionWithTitle:title style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
// strongSelf->currentAlert = nil;
//
// // Send the original image
// [strongSelf.delegate roomInputToolbarView:weakSelf sendImage:selectedImage];
// }];
//
// currentAlert.cancelButtonIndex = [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
// __strong __typeof(weakSelf)strongSelf = weakSelf;
// strongSelf->currentAlert = nil;
// }];
//
// currentAlert.sourceView = self;
//
// [self.delegate roomInputToolbarView:self presentMXKAlert:currentAlert];
// }
// else
// {
// // Send the original image
// [self.delegate roomInputToolbarView:self sendImage:selectedImage];
// }
// } failure:^(NSError *error) {
//
// // Send the original image
// [self.delegate roomInputToolbarView:self sendImage:selectedImage];
// }];
//}
//- (void)getSelectedImageFileData:(NSDictionary*)selectedImageInfo success:(void (^)(NSData *selectedImageFileData))success failure:(void (^)(NSError *error))failure
//{
// ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
// [assetLibrary assetForURL:[selectedImageInfo valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
//
// NSData *selectedImageFileData;
//
// // asset may be nil if the image is not saved in photos library
// if (asset)
// {
// ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
//
// // Check whether the user select an image with a cropping
// if ([[assetRepresentation metadata] objectForKey:@"AdjustmentXMP"])
// {
// // In case of crop we have to consider the original image
// selectedImageFileData = UIImageJPEGRepresentation([selectedImageInfo objectForKey:UIImagePickerControllerOriginalImage], 0.9);
// }
// else
// {
// // cannot use assetRepresentation size to get the image size
// // it gives wrong result with panorama picture
// unsigned long imageDataSize = (unsigned long)[assetRepresentation size];
// uint8_t* imageDataBytes = malloc(imageDataSize);
// [assetRepresentation getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
//
// selectedImageFileData = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES];
// }
// }
// else
// {
// selectedImageFileData = UIImageJPEGRepresentation([selectedImageInfo objectForKey:UIImagePickerControllerOriginalImage], 0.9);
// }
//
// if (success)
// {
// success (selectedImageFileData);
// }
// } failureBlock:^(NSError *err) {
//
// if (failure)
// {
// failure (err);
// }
// }];
//}
#pragma mark - Media Picker handling
//- (void)dismissMediaPicker
//{
// mediaPicker.delegate = nil;
//
// if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:dismissMediaPicker:)])
// {
// [self.delegate roomInputToolbarView:self dismissMediaPicker:mediaPicker];
// }
//}
//
//- (void)moviePlayerThumbnailImageRequestDidFinishNotification:(NSNotification *)notification
//{
// // Finalize video attachment
// UIImage* videoThumbnail = [[notification userInfo] objectForKey:MPMoviePlayerThumbnailImageKey];
// NSURL* selectedVideo = [tmpVideoPlayer contentURL];
// [tmpVideoPlayer stop];
// tmpVideoPlayer = nil;
//
// if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:sendVideo:withThumbnail:)])
// {
// [self.delegate roomInputToolbarView:self sendVideo:selectedVideo withThumbnail:videoThumbnail];
// }
// else
// {
// NSLog(@"[RoomInputToolbarView] Attach video is not supported");
// }
//
// [self dismissMediaPicker];
//}
@end