session-ios/SignalMessaging/attachments/AttachmentSharing.m

121 lines
3.6 KiB
Mathematica
Raw Normal View History

2017-02-16 23:59:40 +01:00
//
2018-01-18 19:39:52 +01:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-02-16 23:59:40 +01:00
//
#import "AttachmentSharing.h"
#import "UIUtil.h"
#import <SignalUtilitiesKit/AppContext.h>
#import <SignalUtilitiesKit/TSAttachmentStream.h>
2017-02-16 23:59:40 +01:00
2018-03-02 20:29:53 +01:00
NS_ASSUME_NONNULL_BEGIN
2017-02-16 23:59:40 +01:00
@implementation AttachmentSharing
+ (void)showShareUIForAttachments:(NSArray<TSAttachmentStream *> *)attachmentStreams
completion:(nullable AttachmentSharingCompletion)completion
{
OWSAssertDebug(attachmentStreams.count > 0);
NSMutableArray<NSURL *> *urls = [NSMutableArray new];
for (TSAttachmentStream *attachmentStream in attachmentStreams) {
[urls addObject:attachmentStream.originalMediaURL];
}
[AttachmentSharing showShareUIForActivityItems:urls completion:completion];
}
+ (void)showShareUIForAttachment:(TSAttachmentStream *)stream
{
OWSAssertDebug(stream);
2017-02-16 23:59:40 +01:00
2018-09-04 16:25:42 +02:00
[self showShareUIForURL:stream.originalMediaURL];
2017-02-16 23:59:40 +01:00
}
+ (void)showShareUIForURL:(NSURL *)url
2018-03-02 20:29:53 +01:00
{
[self showShareUIForURL:url completion:nil];
}
+ (void)showShareUIForURL:(NSURL *)url completion:(nullable AttachmentSharingCompletion)completion
{
OWSAssertDebug(url);
2018-11-12 16:44:39 +01:00
2017-10-10 22:13:54 +02:00
[AttachmentSharing showShareUIForActivityItems:@[
2018-11-12 16:44:39 +01:00
url,
]
completion:completion];
}
+ (void)showShareUIForURLs:(NSArray<NSURL *> *)urls completion:(nullable AttachmentSharingCompletion)completion
{
OWSAssertDebug(urls.count > 0);
[AttachmentSharing showShareUIForActivityItems:urls
2018-03-02 20:29:53 +01:00
completion:completion];
2017-10-10 22:13:54 +02:00
}
2017-10-12 19:48:09 +02:00
+ (void)showShareUIForText:(NSString *)text
2018-03-02 20:29:53 +01:00
{
[self showShareUIForText:text completion:nil];
}
+ (void)showShareUIForText:(NSString *)text completion:(nullable AttachmentSharingCompletion)completion
2017-10-12 19:48:09 +02:00
{
OWSAssertDebug(text);
2017-10-12 19:48:09 +02:00
[AttachmentSharing showShareUIForActivityItems:@[
text,
2018-03-02 20:29:53 +01:00
]
completion:completion];
2017-10-12 19:48:09 +02:00
}
2018-01-18 19:39:52 +01:00
#ifdef DEBUG
+ (void)showShareUIForUIImage:(UIImage *)image
{
OWSAssertDebug(image);
2018-01-18 19:39:52 +01:00
[AttachmentSharing showShareUIForActivityItems:@[
image,
2018-03-02 20:29:53 +01:00
]
completion:nil];
2018-01-18 19:39:52 +01:00
}
#endif
2018-03-02 20:29:53 +01:00
+ (void)showShareUIForActivityItems:(NSArray *)activityItems completion:(nullable AttachmentSharingCompletion)completion
2017-10-10 22:13:54 +02:00
{
OWSAssertDebug(activityItems);
2017-10-10 22:13:54 +02:00
DispatchMainThreadSafe(^{
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[]];
[activityViewController setCompletionWithItemsHandler:^(UIActivityType __nullable activityType,
BOOL completed,
NSArray *__nullable returnedItems,
NSError *__nullable activityError) {
2017-10-10 22:13:54 +02:00
if (activityError) {
OWSLogInfo(@"Failed to share with activityError: %@", activityError);
2017-10-10 22:13:54 +02:00
} else if (completed) {
OWSLogInfo(@"Did share with activityType: %@", activityType);
2017-10-10 22:13:54 +02:00
}
2018-03-02 20:29:53 +01:00
if (completion) {
DispatchMainThreadSafe(completion);
}
2017-10-10 22:13:54 +02:00
}];
2017-12-04 22:09:26 +01:00
UIViewController *fromViewController = CurrentAppContext().frontmostViewController;
2017-10-10 22:13:54 +02:00
while (fromViewController.presentedViewController) {
fromViewController = fromViewController.presentedViewController;
2017-02-16 23:59:40 +01:00
}
OWSAssertDebug(fromViewController);
2018-06-30 00:17:49 +02:00
[fromViewController presentViewController:activityViewController animated:YES completion:nil];
2017-10-10 22:13:54 +02:00
});
}
2017-02-16 23:59:40 +01:00
@end
2018-03-02 20:29:53 +01:00
NS_ASSUME_NONNULL_END