CCQuickActions.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. NSArray *metadatas = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND session CONTAINS 'upload' AND (sessionTaskIdentifier >= 0 OR sessionTaskIdentifierPlist >= 0)", app.activeAccount] sorted:nil ascending:NO];
  60. _numTaskUploadInProgress = [metadatas count];
  61. _cryptated = cryptated;
  62. _mainVC = (CCMain *)viewController;
  63. [self openAssetsPickerController];
  64. }
  65. - (void)closeAll
  66. {
  67. if (_picker)
  68. [_picker dismissViewControllerAnimated:NO completion:nil];
  69. if (_move)
  70. [_move dismissViewControllerAnimated:NO completion:nil];
  71. _cryptated = NO;
  72. _numTaskUploadInProgress = 0;
  73. _picker = nil;
  74. _move = nil;
  75. _assets = nil;
  76. }
  77. #pragma --------------------------------------------------------------------------------------------
  78. #pragma mark ===== Assets Picker =====
  79. #pragma --------------------------------------------------------------------------------------------
  80. - (void)openAssetsPickerController
  81. {
  82. CTAssetCheckmark *checkmark = [CTAssetCheckmark appearance];
  83. checkmark.tintColor = [NCBrandColor sharedInstance].brand;
  84. [checkmark setMargin:0.0 forVerticalEdge:NSLayoutAttributeRight horizontalEdge:NSLayoutAttributeTop];
  85. UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[CTAssetsPickerController class], nil];
  86. [app aspectNavigationControllerBar:navBar encrypted:NO online:YES hidden:NO];
  87. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. // init picker
  90. _picker = [[CTAssetsPickerController alloc] init];
  91. // set delegate
  92. _picker.delegate = self;
  93. // to present picker as a form sheet in iPad
  94. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  95. _picker.modalPresentationStyle = UIModalPresentationFormSheet;
  96. // present picker
  97. [_mainVC presentViewController:_picker animated:YES completion:nil];
  98. });
  99. }];
  100. }
  101. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
  102. {
  103. __block float imageSize;
  104. PHImageRequestOptions *option = [PHImageRequestOptions new];
  105. option.synchronous = YES;
  106. // self Asset
  107. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  108. imageSize = imageData.length;
  109. }];
  110. // Add selected Asset
  111. for (PHAsset *asset in picker.selectedAssets) {
  112. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  113. imageSize = imageData.length + imageSize;
  114. }];
  115. }
  116. if (picker.selectedAssets.count >= (k_pickerControllerMax - _numTaskUploadInProgress)) {
  117. [app messageNotification:@"_info_" description:@"_limited_dimension_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  118. return NO;
  119. }
  120. return YES;
  121. }
  122. - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
  123. {
  124. [picker dismissViewControllerAnimated:YES completion:^{
  125. _assets = [[NSMutableArray alloc] initWithArray:assets];
  126. if ([assets count] > 0)
  127. [self moveOpenWindow:nil];
  128. }];
  129. }
  130. - (void)assetsPickerControllerDidCancel:(CTAssetsPickerController *)picker
  131. {
  132. [picker dismissViewControllerAnimated:YES completion:nil];
  133. }
  134. #pragma --------------------------------------------------------------------------------------------
  135. #pragma mark ===== Move =====
  136. #pragma --------------------------------------------------------------------------------------------
  137. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title
  138. {
  139. [_mainVC uploadFileAsset:_assets serverUrl:serverUrlTo cryptated:_cryptated useSubFolder:NO session:k_upload_session];
  140. }
  141. - (void)moveOpenWindow:(NSArray *)indexPaths
  142. {
  143. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  144. _move = (CCMove *)navigationController.topViewController;
  145. if (_cryptated)
  146. _move.move.title = NSLocalizedString(@"_upload_encrypted_file_", nil);
  147. else
  148. _move.move.title = NSLocalizedString(@"_upload_file_", nil);
  149. _move.delegate = self;
  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