1 | // Copyright 2018-present 650 Industries. All rights reserved.
|
2 |
|
3 | #import <EXSharing/EXSharingModule.h>
|
4 | #import <UMCore/UMUtilitiesInterface.h>
|
5 | #import <UMFileSystemInterface/UMFileSystemInterface.h>
|
6 | #import <UMFileSystemInterface/UMFilePermissionModuleInterface.h>
|
7 |
|
8 | @interface EXSharingModule ()
|
9 |
|
10 | @property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
|
11 | @property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
|
12 |
|
13 | @property (nonatomic, strong) UMPromiseResolveBlock pendingResolver;
|
14 |
|
15 | @end
|
16 |
|
17 | @implementation EXSharingModule
|
18 |
|
19 | UM_EXPORT_MODULE(ExpoSharing);
|
20 |
|
21 | - (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
|
22 | {
|
23 | _moduleRegistry = moduleRegistry;
|
24 | }
|
25 |
|
26 | UM_EXPORT_METHOD_AS(shareAsync,
|
27 | fileUrl:(NSString *)fileUrl
|
28 | params:(NSDictionary *)params
|
29 | resolve:(UMPromiseResolveBlock)resolve
|
30 | reject:(UMPromiseRejectBlock)reject)
|
31 | {
|
32 | if (_documentInteractionController) {
|
33 | NSString *errorMessage = @"Another item is being shared. Await the `shareAsync` request and then share the item again.";
|
34 | reject(@"E_SHARING_MUL", errorMessage, UMErrorWithMessage(errorMessage));
|
35 | return;
|
36 | }
|
37 |
|
38 | NSURL *url = [NSURL URLWithString:fileUrl];
|
39 |
|
40 | id<UMFilePermissionModuleInterface> filePermissionsModule = [_moduleRegistry getModuleImplementingProtocol:@protocol(UMFilePermissionModuleInterface)];
|
41 | if (filePermissionsModule && !([filePermissionsModule getPathPermissions:url.path] & UMFileSystemPermissionRead)) {
|
42 | NSString *errorMessage = @"You don't have access to provided file.";
|
43 | reject(@"E_SHARING_PERM", errorMessage, UMErrorWithMessage(errorMessage));
|
44 | return;
|
45 | }
|
46 |
|
47 | _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
|
48 | _documentInteractionController.delegate = self;
|
49 | _documentInteractionController.UTI = params[@"UTI"];
|
50 |
|
51 | UIViewController *viewController = [[_moduleRegistry getModuleImplementingProtocol:@protocol(UMUtilitiesInterface)] currentViewController];
|
52 |
|
53 | UM_WEAKIFY(self);
|
54 | dispatch_async(dispatch_get_main_queue(), ^{
|
55 | UM_ENSURE_STRONGIFY(self);
|
56 | UIView *rootView = [viewController view];
|
57 | if ([self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:rootView animated:YES]) {
|
58 | self.pendingResolver = resolve;
|
59 | } else {
|
60 | reject(@"ERR_SHARING_UNSUPPORTED_TYPE", @"Could not share file since there were no apps registered for its type", nil);
|
61 | self.documentInteractionController = nil;
|
62 | }
|
63 | });
|
64 | }
|
65 |
|
66 | - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
|
67 | {
|
68 | _pendingResolver(@{});
|
69 | _pendingResolver = nil;
|
70 |
|
71 | _documentInteractionController = nil;
|
72 | }
|
73 |
|
74 | @end
|