Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Dropbox.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <React/RCTBridgeModule.h>
  17. #import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
  18. #import "Dropbox.h"
  19. RCTPromiseResolveBlock currentResolve = nil;
  20. RCTPromiseRejectBlock currentReject = nil;
  21. @implementation Dropbox
  22. + (NSString *)getAppKey{
  23. NSArray *urlTypes
  24. = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  25. for (NSDictionary<NSString *, NSArray *> *urlType in urlTypes) {
  26. NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"];
  27. if (urlSchemes) {
  28. for (NSString *urlScheme in urlSchemes) {
  29. if (urlScheme && [urlScheme hasPrefix:@"db-"]) {
  30. return [urlScheme substringFromIndex:3];
  31. }
  32. }
  33. }
  34. }
  35. return nil;
  36. }
  37. RCT_EXPORT_MODULE();
  38. + (BOOL)requiresMainQueueSetup {
  39. return NO;
  40. }
  41. - (NSDictionary *)constantsToExport {
  42. BOOL enabled = [Dropbox getAppKey] != nil;
  43. return @{
  44. @"ENABLED": [NSNumber numberWithBool:enabled]
  45. };
  46. };
  47. RCT_EXPORT_METHOD(authorize:(RCTPromiseResolveBlock)resolve
  48. reject:(__unused RCTPromiseRejectBlock)reject) {
  49. currentResolve = resolve;
  50. currentReject = reject;
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
  53. controller:[[self class] topMostController]
  54. openURL:^(NSURL *url) {
  55. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  56. }];
  57. });
  58. }
  59. RCT_EXPORT_METHOD(getDisplayName: (NSString *)token
  60. resolve: (RCTPromiseResolveBlock)resolve
  61. reject:(RCTPromiseRejectBlock)reject) {
  62. DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
  63. [[client.usersRoutes getCurrentAccount] setResponseBlock:^(DBUSERSFullAccount *result, DBNilObject *routeError, DBRequestError *networkError) {
  64. if (result) {
  65. resolve(result.name.displayName);
  66. } else {
  67. NSString *msg = @"Failed!";
  68. if (networkError) {
  69. msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
  70. }
  71. reject(@"getDisplayName", @"Failed", nil);
  72. }
  73. }];
  74. }
  75. RCT_EXPORT_METHOD(getSpaceUsage: (NSString *)token
  76. resolve: (RCTPromiseResolveBlock)resolve
  77. reject:(RCTPromiseRejectBlock)reject) {
  78. DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
  79. [[client.usersRoutes getSpaceUsage] setResponseBlock:^(DBUSERSSpaceUsage *result, DBNilObject *routeError, DBRequestError *networkError) {
  80. if (result) {
  81. DBUSERSSpaceAllocation *allocation = result.allocation;
  82. NSNumber *allocated = 0;
  83. NSNumber *used = 0;
  84. if ([allocation isIndividual]) {
  85. allocated = allocation.individual.allocated;
  86. used = result.used;
  87. } else if ([allocation isTeam]) {
  88. allocated = allocation.team.allocated;
  89. used = allocation.team.used;
  90. }
  91. id objects[] = { used, allocated };
  92. id keys[] = { @"used", @"allocated" };
  93. NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
  94. forKeys:keys
  95. count:2];
  96. resolve(dictionary);
  97. } else {
  98. NSString *msg = @"Failed!";
  99. if (networkError) {
  100. msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
  101. }
  102. reject(@"getSpaceUsage", msg, nil);
  103. }
  104. }];
  105. }
  106. + (BOOL)application:(UIApplication *)app
  107. openURL:(NSURL *)url
  108. options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  109. if (currentReject == nil || currentResolve == nil) {
  110. return NO;
  111. }
  112. DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url];
  113. if (authResult) {
  114. if ([authResult isSuccess]) {
  115. currentResolve(authResult.accessToken.accessToken);
  116. } else {
  117. NSString *msg;
  118. if ([authResult isError]) {
  119. msg = [NSString stringWithFormat:@"%@, error type: %zd",[authResult errorDescription], [authResult errorType]];
  120. } else {
  121. msg = @"OAuth canceled!";
  122. }
  123. currentReject(@"authorize", msg, nil);
  124. }
  125. currentResolve = nil;
  126. currentReject = nil;
  127. return YES;
  128. }
  129. return NO;
  130. }
  131. + (UIViewController *)topMostController {
  132. UIViewController *topController
  133. = [UIApplication sharedApplication].keyWindow.rootViewController;
  134. while (topController.presentedViewController) {
  135. topController = topController.presentedViewController;
  136. }
  137. return topController;
  138. }
  139. + (void)setAppKey {
  140. NSString *appKey = [self getAppKey];
  141. if (appKey) {
  142. [DBClientsManager setupWithAppKey:appKey];
  143. }
  144. }
  145. @end