CCQuickActions.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // CCQuickActions.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/06/16.
  6. // Copyright (c) 2017 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. #import "NCBridgeSwift.h"
  29. @interface CCQuickActions ()
  30. {
  31. BOOL _cryptated;
  32. long _numTaskUploadInProgress;
  33. CTAssetsPickerController *_picker;
  34. CCMove *_move;
  35. CCMain *_mainVC;
  36. NSMutableArray *_assets;
  37. }
  38. @end
  39. @implementation CCQuickActions
  40. + (instancetype)quickActionsManager
  41. {
  42. static dispatch_once_t once;
  43. static CCQuickActions *__quickActionsManager;
  44. dispatch_once(&once, ^{
  45. __quickActionsManager = [[CCQuickActions alloc] init];
  46. });
  47. return __quickActionsManager;
  48. }
  49. - (instancetype)init
  50. {
  51. if (self = [super init]) {
  52. _cryptated = NO;
  53. _numTaskUploadInProgress = 0;
  54. }
  55. return self;
  56. }
  57. - (void)startQuickActionsEncrypted:(BOOL)cryptated viewController:(UITableViewController *)viewController
  58. {
  59. _numTaskUploadInProgress = [[CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (session CONTAINS 'upload') AND ((sessionTaskIdentifier >= 0) OR (sessionTaskIdentifierPlist >= 0))", app.activeAccount] context:nil] count];
  60. _cryptated = cryptated;
  61. _mainVC = (CCMain *)viewController;
  62. [self openAssetsPickerController];
  63. }
  64. - (void)closeAll
  65. {
  66. if (_picker)
  67. [_picker dismissViewControllerAnimated:NO completion:nil];
  68. if (_move)
  69. [_move dismissViewControllerAnimated:NO completion:nil];
  70. _cryptated = NO;
  71. _numTaskUploadInProgress = 0;
  72. _picker = nil;
  73. _move = nil;
  74. _assets = nil;
  75. }
  76. #pragma --------------------------------------------------------------------------------------------
  77. #pragma mark ===== Assets Picker =====
  78. #pragma --------------------------------------------------------------------------------------------
  79. - (void)openAssetsPickerController
  80. {
  81. CTAssetCheckmark *checkmark = [CTAssetCheckmark appearance];
  82. checkmark.tintColor = [NCBrandColor sharedInstance].brand;
  83. [checkmark setMargin:0.0 forVerticalEdge:NSLayoutAttributeRight horizontalEdge:NSLayoutAttributeTop];
  84. UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[CTAssetsPickerController class], nil];
  85. [app aspectNavigationControllerBar:navBar encrypted:NO online:YES hidden:NO];
  86. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. // init picker
  89. _picker = [[CTAssetsPickerController alloc] init];
  90. // set delegate
  91. _picker.delegate = self;
  92. // to present picker as a form sheet in iPad
  93. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  94. _picker.modalPresentationStyle = UIModalPresentationFormSheet;
  95. // present picker
  96. [_mainVC presentViewController:_picker animated:YES completion:nil];
  97. });
  98. }];
  99. }
  100. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
  101. {
  102. __block float imageSize;
  103. PHImageRequestOptions *option = [PHImageRequestOptions new];
  104. option.synchronous = YES;
  105. // self Asset
  106. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  107. imageSize = imageData.length;
  108. }];
  109. // Add selected Asset
  110. for (PHAsset *asset in picker.selectedAssets) {
  111. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  112. imageSize = imageData.length + imageSize;
  113. }];
  114. }
  115. if (imageSize > k_MaxDimensionUpload || (picker.selectedAssets.count >= (k_pickerControllerMax - _numTaskUploadInProgress))) {
  116. [app messageNotification:@"_info_" description:@"_limited_dimension_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  117. return NO;
  118. }
  119. return YES;
  120. }
  121. - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
  122. {
  123. [picker dismissViewControllerAnimated:YES completion:^{
  124. _assets = [[NSMutableArray alloc] initWithArray:assets];
  125. if ([assets count] > 0)
  126. [self moveOpenWindow:nil];
  127. }];
  128. }
  129. - (void)assetsPickerControllerDidCancel:(CTAssetsPickerController *)picker
  130. {
  131. [picker dismissViewControllerAnimated:YES completion:nil];
  132. }
  133. #pragma --------------------------------------------------------------------------------------------
  134. #pragma mark ===== Move =====
  135. #pragma --------------------------------------------------------------------------------------------
  136. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title selectedMetadatas:(NSArray *)selectedMetadatas
  137. {
  138. [_mainVC uploadFileAsset:_assets serverUrl:serverUrlTo cryptated:_cryptated useSubFolder:NO session:k_upload_session];
  139. }
  140. - (void)moveOpenWindow:(NSArray *)indexPaths
  141. {
  142. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  143. _move = (CCMove *)navigationController.topViewController;
  144. if (_cryptated)
  145. _move.move.title = NSLocalizedString(@"_upload_encrypted_file_", nil);
  146. else
  147. _move.move.title = NSLocalizedString(@"_upload_file_", nil);
  148. _move.delegate = self;
  149. _move.selectedMetadatas = nil;
  150. _move.tintColor = [NCBrandColor sharedInstance].navigationBarText;
  151. _move.barTintColor = [NCBrandColor sharedInstance].brand;
  152. _move.tintColorTitle = [NCBrandColor sharedInstance].navigationBarText;
  153. _move.networkingOperationQueue = app.netQueue;
  154. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  155. [_mainVC presentViewController:navigationController animated:YES completion:nil];
  156. }
  157. @end