CCQuickActions.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // CCQuickActions.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/06/16.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCQuickActions.h"
  24. #import "CTAssetCheckmark.h"
  25. #import "CCHud.h"
  26. #import "AppDelegate.h"
  27. #import "CCMain.h"
  28. #ifdef CUSTOM_BUILD
  29. #import "CustomSwift.h"
  30. #else
  31. #import "Nextcloud-Swift.h"
  32. #endif
  33. @interface CCQuickActions ()
  34. {
  35. BOOL _cryptated;
  36. long _numTaskUploadInProgress;
  37. CTAssetsPickerController *_picker;
  38. CCMove *_move;
  39. CCMain *_mainVC;
  40. NSMutableArray *_assets;
  41. }
  42. @end
  43. @implementation CCQuickActions
  44. + (instancetype)quickActionsManager
  45. {
  46. static dispatch_once_t once;
  47. static CCQuickActions *__quickActionsManager;
  48. dispatch_once(&once, ^{
  49. __quickActionsManager = [[CCQuickActions alloc] init];
  50. });
  51. return __quickActionsManager;
  52. }
  53. - (instancetype)init
  54. {
  55. if (self = [super init]) {
  56. _cryptated = NO;
  57. _numTaskUploadInProgress = 0;
  58. }
  59. return self;
  60. }
  61. - (void)startQuickActionsEncrypted:(BOOL)cryptated viewController:(UITableViewController *)viewController
  62. {
  63. _numTaskUploadInProgress = [[CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (session CONTAINS 'upload') AND ((sessionTaskIdentifier >= 0) OR (sessionTaskIdentifierPlist >= 0))", app.activeAccount] context:nil] count];
  64. _cryptated = cryptated;
  65. _mainVC = (CCMain *)viewController;
  66. [self openAssetsPickerController];
  67. }
  68. - (void)closeAll
  69. {
  70. if (_picker)
  71. [_picker dismissViewControllerAnimated:NO completion:nil];
  72. if (_move)
  73. [_move dismissViewControllerAnimated:NO completion:nil];
  74. _cryptated = NO;
  75. _numTaskUploadInProgress = 0;
  76. _picker = nil;
  77. _move = nil;
  78. _assets = nil;
  79. }
  80. #pragma --------------------------------------------------------------------------------------------
  81. #pragma mark ===== Assets Picker =====
  82. #pragma --------------------------------------------------------------------------------------------
  83. - (void)openAssetsPickerController
  84. {
  85. CTAssetCheckmark *checkmark = [CTAssetCheckmark appearance];
  86. checkmark.tintColor = [NCColorBrand sharedInstance].brand;
  87. [checkmark setMargin:0.0 forVerticalEdge:NSLayoutAttributeRight horizontalEdge:NSLayoutAttributeTop];
  88. UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[CTAssetsPickerController class], nil];
  89. [app aspectNavigationControllerBar:navBar encrypted:NO online:YES hidden:NO];
  90. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. // init picker
  93. _picker = [[CTAssetsPickerController alloc] init];
  94. // set delegate
  95. _picker.delegate = self;
  96. // to present picker as a form sheet in iPad
  97. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  98. _picker.modalPresentationStyle = UIModalPresentationFormSheet;
  99. // present picker
  100. [_mainVC presentViewController:_picker animated:YES completion:nil];
  101. });
  102. }];
  103. }
  104. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
  105. {
  106. __block float imageSize;
  107. PHImageRequestOptions *option = [PHImageRequestOptions new];
  108. option.synchronous = YES;
  109. // self Asset
  110. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  111. imageSize = imageData.length;
  112. }];
  113. // Add selected Asset
  114. for (PHAsset *asset in picker.selectedAssets) {
  115. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  116. imageSize = imageData.length + imageSize;
  117. }];
  118. }
  119. if (imageSize > k_MaxDimensionUpload || (picker.selectedAssets.count >= (k_pickerControllerMax - _numTaskUploadInProgress))) {
  120. [app messageNotification:@"_info_" description:@"_limited_dimension_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  121. return NO;
  122. }
  123. return YES;
  124. }
  125. - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
  126. {
  127. [picker dismissViewControllerAnimated:YES completion:^{
  128. _assets = [[NSMutableArray alloc] initWithArray:assets];
  129. if ([assets count] > 0)
  130. [self moveOpenWindow:nil];
  131. }];
  132. }
  133. - (void)assetsPickerControllerDidCancel:(CTAssetsPickerController *)picker
  134. {
  135. [picker dismissViewControllerAnimated:YES completion:nil];
  136. }
  137. #pragma --------------------------------------------------------------------------------------------
  138. #pragma mark ===== Move =====
  139. #pragma --------------------------------------------------------------------------------------------
  140. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title selectedMetadatas:(NSArray *)selectedMetadatas
  141. {
  142. [_mainVC uploadFileAsset:_assets serverUrl:serverUrlTo cryptated:_cryptated useSubFolder:NO session:k_upload_session];
  143. }
  144. - (void)moveOpenWindow:(NSArray *)indexPaths
  145. {
  146. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  147. _move = (CCMove *)navigationController.topViewController;
  148. if (_cryptated)
  149. _move.move.title = NSLocalizedString(@"_upload_encrypted_file_", nil);
  150. else
  151. _move.move.title = NSLocalizedString(@"_upload_file_", nil);
  152. _move.delegate = self;
  153. _move.selectedMetadatas = nil;
  154. _move.tintColor = [NCColorBrand sharedInstance].navigationBarText;
  155. _move.barTintColor = [NCColorBrand sharedInstance].navigationBar;
  156. _move.tintColorTitle = [NCColorBrand sharedInstance].navigationBarText;
  157. _move.networkingOperationQueue = app.netQueue;
  158. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  159. [_mainVC presentViewController:navigationController animated:YES completion:nil];
  160. }
  161. @end