CCQuickActions.m 6.7 KB

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