session-ios/Signal/src/ViewControllers/SendExternalFileViewController.m

182 lines
6.3 KiB
Mathematica
Raw Normal View History

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "SendExternalFileViewController.h"
#import "Environment.h"
2017-10-18 20:53:31 +02:00
#import "NSString+OWS.h"
#import "Signal-Swift.h"
#import "SignalApp.h"
#import "ThreadUtil.h"
2017-04-26 20:35:49 +02:00
#import "UIColor+OWS.h"
#import "UIFont+OWS.h"
#import <SignalServiceKit/OWSMessageSender.h>
#import <SignalServiceKit/TSThread.h>
2017-04-26 18:25:41 +02:00
NS_ASSUME_NONNULL_BEGIN
@interface SendExternalFileViewController () <SelectThreadViewControllerDelegate>
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@end
2017-04-26 18:25:41 +02:00
#pragma mark -
@implementation SendExternalFileViewController
2017-04-26 20:35:49 +02:00
- (instancetype)init
{
if (self = [super init]) {
self.delegate = self;
}
return self;
}
- (void)loadView
{
[super loadView];
_contactsManager = [Environment getCurrent].contactsManager;
_messageSender = [Environment getCurrent].messageSender;
self.title = NSLocalizedString(@"SEND_EXTERNAL_FILE_VIEW_TITLE", @"Title for the 'send external file' view.");
}
#pragma mark - SelectThreadViewControllerDelegate
- (void)threadWasSelected:(TSThread *)thread
{
OWSAssert(self.attachment);
OWSAssert(thread);
2017-06-08 04:33:29 +02:00
__weak typeof(self) weakSelf = self;
BOOL didShowSNAlert =
[SafetyNumberConfirmationAlert presentAlertIfNecessaryWithRecipientIds:thread.recipientIdentifiers
confirmationText:[SafetyNumberStrings confirmSendButton]
contactsManager:self.contactsManager
completion:^(BOOL didConfirm) {
if (didConfirm) {
2017-06-08 04:33:29 +02:00
[weakSelf threadWasSelected:thread];
}
}];
if (didShowSNAlert) {
return;
}
[ThreadUtil addThreadToProfileWhitelistIfEmptyContactThread:thread];
[ThreadUtil sendMessageWithAttachment:self.attachment inThread:thread messageSender:self.messageSender];
[SignalApp.sharedApp presentConversationForThread:thread];
}
- (BOOL)canSelectBlockedContact
{
return NO;
}
2017-04-26 21:44:21 +02:00
- (nullable UIView *)createHeaderWithSearchBar:(UISearchBar *)searchBar
2017-04-26 19:03:51 +02:00
{
2017-04-26 21:44:21 +02:00
OWSAssert(searchBar)
2017-04-26 20:35:49 +02:00
const CGFloat imageSize
2017-04-26 21:44:21 +02:00
= ScaleFromIPhone5To7Plus(40, 50);
const CGFloat imageLabelSpacing = ScaleFromIPhone5To7Plus(5, 8);
const CGFloat titleVSpacing = ScaleFromIPhone5To7Plus(10, 15);
2017-04-26 21:55:01 +02:00
const CGFloat contentVMargin = 20;
2017-04-26 20:35:49 +02:00
UIView *header = [UIView new];
2017-04-26 21:44:21 +02:00
header.backgroundColor = [UIColor whiteColor];
2017-04-26 20:35:49 +02:00
UIView *titleLabel = [self createTitleLabel];
2017-04-26 21:44:21 +02:00
[titleLabel sizeToFit];
2017-04-26 20:35:49 +02:00
[header addSubview:titleLabel];
[titleLabel autoHCenterInSuperview];
[titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:contentVMargin];
UIView *fileView = [UIView new];
[header addSubview:fileView];
[fileView autoHCenterInSuperview];
[fileView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:titleLabel withOffset:titleVSpacing];
2017-04-26 21:44:21 +02:00
UIImage *image = [UIImage imageNamed:@"file-thin-black-filled-large"];
2017-04-26 20:35:49 +02:00
OWSAssert(image);
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.layer.minificationFilter = kCAFilterTrilinear;
imageView.layer.magnificationFilter = kCAFilterTrilinear;
2017-04-26 21:44:21 +02:00
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shadowRadius = 2.f;
imageView.layer.shadowOpacity = 0.2f;
imageView.layer.shadowOffset = CGSizeMake(0.75f, 0.75f);
2017-04-26 20:35:49 +02:00
[fileView addSubview:imageView];
[imageView autoSetDimension:ALDimensionWidth toSize:imageSize];
[imageView autoSetDimension:ALDimensionHeight toSize:imageSize];
2017-10-02 20:26:03 +02:00
[imageView autoPinLeadingToSuperview];
2017-04-26 20:35:49 +02:00
[imageView autoPinEdgeToSuperviewEdge:ALEdgeTop];
[imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
UIView *fileNameLabel = [self createFileNameLabel];
[fileView addSubview:fileNameLabel];
[fileNameLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:imageView];
[fileNameLabel autoPinLeadingToTrailingOfView:imageView margin:imageLabelSpacing];
2017-10-02 20:26:03 +02:00
[fileNameLabel autoPinTrailingToSuperview];
2017-04-26 20:35:49 +02:00
2017-04-26 21:44:21 +02:00
[header addSubview:searchBar];
[searchBar autoPinWidthToSuperview];
[searchBar autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:fileView withOffset:contentVMargin];
[searchBar autoPinEdgeToSuperviewEdge:ALEdgeBottom];
// UITableViewController.tableHeaderView must have its height set.
header.frame = CGRectMake(0,
0,
0,
(contentVMargin * 2 + titleLabel.frame.size.height + titleVSpacing + imageSize + searchBar.frame.size.height));
2017-04-26 20:35:49 +02:00
return header;
}
- (NSString *)formattedFileName
{
// AppDelegate already verifies that this attachment has a valid filename.
//
// TODO: If we reuse this VC, for example to offer a "forward attachment to other thread",
// feature, this assumption would no longer apply.
OWSAssert(self.attachment);
2017-10-18 20:53:31 +02:00
NSString *filename = [self.attachment.sourceFilename ows_stripped];
2017-04-26 20:35:49 +02:00
OWSAssert(filename.length > 0);
const NSUInteger kMaxFilenameLength = 20;
2017-04-26 20:35:49 +02:00
if (filename.length > kMaxFilenameLength) {
// Truncate the filename if necessary.
//
// TODO: Use l10n-safe truncation.
filename = [[[filename substringToIndex:kMaxFilenameLength / 2] stringByAppendingString:@"…"]
stringByAppendingString:[filename substringFromIndex:filename.length - kMaxFilenameLength / 2]];
}
return filename;
}
- (UIView *)createFileNameLabel
{
UILabel *label = [UILabel new];
label.text = [self formattedFileName];
label.textColor = [UIColor ows_materialBlueColor];
2017-04-26 21:44:21 +02:00
label.font = [UIFont ows_regularFontWithSize:ScaleFromIPhone5To7Plus(16.f, 20.f)];
2017-04-26 20:35:49 +02:00
return label;
}
- (UIView *)createTitleLabel
{
UILabel *label = [UILabel new];
label.text
= NSLocalizedString(@"SEND_EXTERNAL_FILE_HEADER_TITLE", @"Header title for the 'send external file' view.");
label.textColor = [UIColor blackColor];
2017-04-26 21:44:21 +02:00
label.font = [UIFont ows_mediumFontWithSize:ScaleFromIPhone5To7Plus(18.f, 20.f)];
2017-04-26 20:35:49 +02:00
return label;
2017-04-26 19:03:51 +02:00
}
@end
2017-04-26 18:25:41 +02:00
NS_ASSUME_NONNULL_END