CCQuickActions.m 6.7 KB

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