NCShares.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // NCShares.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/06/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "NCShares.h"
  24. #import "NCSharesCell.h"
  25. #import "AppDelegate.h"
  26. #import "NCBridgeSwift.h"
  27. @interface NCShares ()
  28. {
  29. AppDelegate *appDelegate;
  30. NSArray *_dataSource;
  31. }
  32. @end
  33. @implementation NCShares
  34. #pragma --------------------------------------------------------------------------------------------
  35. #pragma mark ===== Init =====
  36. #pragma --------------------------------------------------------------------------------------------
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  41. // Custom Cell
  42. [self.tableView registerNib:[UINib nibWithNibName:@"NCSharesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  43. // dataSource
  44. _dataSource = [NSMutableArray new];
  45. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  46. self.tableView.emptyDataSetDelegate = self;
  47. self.tableView.emptyDataSetSource = self;
  48. self.tableView.delegate = self;
  49. // Title
  50. self.title = NSLocalizedString(@"_list_shares_", nil);
  51. // Register for 3D Touch Previewing if available
  52. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {
  53. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  54. }
  55. // Notification
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  57. [self changeTheming];
  58. }
  59. // Apparirà
  60. - (void)viewWillAppear:(BOOL)animated
  61. {
  62. [super viewWillAppear:animated];
  63. [self reloadDatasource];
  64. }
  65. - (void)changeTheming
  66. {
  67. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  68. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  69. [self.tableView reloadData];
  70. }
  71. #pragma --------------------------------------------------------------------------------------------
  72. #pragma mark ==== DZNEmptyDataSetSource ====
  73. #pragma --------------------------------------------------------------------------------------------
  74. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  75. {
  76. return NCBrandColor.sharedInstance.backgroundView;
  77. }
  78. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  79. {
  80. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] width:300 height:300 color:[UIColor grayColor]];
  81. }
  82. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  83. {
  84. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  85. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor grayColor]};
  86. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  87. }
  88. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  89. {
  90. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  91. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  92. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  93. paragraph.alignment = NSTextAlignmentCenter;
  94. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  95. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  96. }
  97. #pragma mark -
  98. #pragma --------------------------------------------------------------------------------------------
  99. #pragma mark ===== Peek & Pop =====
  100. #pragma --------------------------------------------------------------------------------------------
  101. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  102. {
  103. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  104. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  105. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  106. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  107. NCSharesCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  108. if (cell) {
  109. previewingContext.sourceRect = cell.frame;
  110. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  111. viewController.metadata = metadata;
  112. viewController.imageFile = cell.fileImageView.image;
  113. viewController.showOpenIn = false;
  114. viewController.showOpenQuickLook = false;
  115. viewController.showShare = false;
  116. return viewController;
  117. }
  118. return nil;
  119. }
  120. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  121. {
  122. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  123. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  124. }
  125. #pragma --------------------------------------------------------------------------------------------
  126. #pragma mark ==== unShare <Delegate> ====
  127. #pragma --------------------------------------------------------------------------------------------
  128. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  129. {
  130. [[NCCommunication shared] deleteShareWithIdShare:tableShare.idShare customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  131. if (errorCode == 0 && [account isEqualToString:appDelegate.account]) {
  132. [[NCManageDatabase sharedInstance] deleteTableShareWithAccount:account idShare:tableShare.idShare];
  133. [self reloadDatasource];
  134. } else if (errorCode != 0) {
  135. [[NCContentPresenter shared] messageNotification:@"_share_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:true];
  136. } else {
  137. NSLog(@"[LOG] It has been changed user during networking process, error.");
  138. }
  139. }];
  140. }
  141. #pragma mark -
  142. #pragma --------------------------------------------------------------------------------------------
  143. #pragma mark ===== Swipe Tablet -> menu =====
  144. #pragma --------------------------------------------------------------------------------------------
  145. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. return UITableViewCellEditingStyleDelete;
  148. }
  149. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. return NSLocalizedString(@"_delete_", nil);
  152. }
  153. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. tableMetadata *metadata;
  156. if (indexPath.row+1 <= _dataSource.count) {
  157. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  158. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.account, table.serverUrl, table.fileName]];
  159. }
  160. if (metadata) return YES;
  161. else return NO;
  162. }
  163. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. if (editingStyle == UITableViewCellEditingStyleDelete) {
  166. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  167. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  168. [self removeShares:metadata tableShare:table];
  169. }
  170. }
  171. #pragma --------------------------------------------------------------------------------------------
  172. #pragma mark ==== Table ====
  173. #pragma --------------------------------------------------------------------------------------------
  174. - (void)readFolder:(NSString *)serverUrl
  175. {
  176. [self reloadDatasource];
  177. }
  178. - (void)reloadDatasource
  179. {
  180. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.account];
  181. [self.tableView reloadData];
  182. }
  183. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  184. {
  185. return 60;
  186. }
  187. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  188. {
  189. return 1;
  190. }
  191. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  192. {
  193. return [_dataSource count];
  194. }
  195. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  198. tableMetadata *metadata;
  199. // separator
  200. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  201. // Initialize
  202. cell.statusImageView.image = nil;
  203. cell.offlineImageView.image = nil;
  204. // change color selection
  205. UIView *selectionColor = [[UIView alloc] init];
  206. selectionColor.backgroundColor = NCBrandColor.sharedInstance.select;
  207. cell.selectedBackgroundView = selectionColor;
  208. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  209. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView;
  210. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  211. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  212. if (metadata) {
  213. if (metadata.directory) {
  214. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  215. } else {
  216. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId etag:metadata.etag]];
  217. if (cell.fileImageView.image == nil) {
  218. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  219. [[NCOperationQueue shared] downloadThumbnailWithMetadata:metadata urlBase:appDelegate.urlBase view:tableView indexPath:indexPath];
  220. }
  221. }
  222. } else {
  223. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  224. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", table.serverUrl, table.fileName];
  225. [[NCNetworking shared] readFileWithServerUrlFileName:serverUrlFileName account:appDelegate.account completion:^(NSString *account, tableMetadata *metadata, NSInteger errorCode, NSString *errorDescription) {
  226. if (errorCode == 0 && [account isEqualToString:appDelegate.account]) {
  227. [[NCManageDatabase sharedInstance] addMetadata:metadata];
  228. [self reloadDatasource];
  229. }
  230. }];
  231. }
  232. cell.labelTitle.text = table.fileName;
  233. if ([table.serverUrl isEqualToString:[[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account]])
  234. cell.labelInfoFile.text = @"/";
  235. else
  236. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account] withString:@""];
  237. return cell;
  238. }
  239. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  240. {
  241. // deselect row
  242. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  243. tableMetadata *metadata;
  244. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  245. if (table.serverUrl) {
  246. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  247. if (metadata) {
  248. [[NCNetworkingNotificationCenter shared] openShareWithViewController:self metadata:metadata indexPage:2];
  249. }
  250. }
  251. }
  252. @end