NCShares.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasource) name:@"SharesReloadDatasource" object:nil];
  56. // changeTheming
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  58. [self changeTheming];
  59. }
  60. // Apparirà
  61. - (void)viewWillAppear:(BOOL)animated
  62. {
  63. [super viewWillAppear:animated];
  64. [self reloadDatasource];
  65. }
  66. - (void)changeTheming
  67. {
  68. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  69. }
  70. #pragma --------------------------------------------------------------------------------------------
  71. #pragma mark ==== DZNEmptyDataSetSource ====
  72. #pragma --------------------------------------------------------------------------------------------
  73. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  74. {
  75. return NCBrandColor.sharedInstance.backgroundView;
  76. }
  77. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  78. {
  79. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] width:300 height:300 color:NCBrandColor.sharedInstance.graySoft];
  80. }
  81. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  82. {
  83. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  84. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  85. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  86. }
  87. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  88. {
  89. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  90. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  91. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  92. paragraph.alignment = NSTextAlignmentCenter;
  93. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  94. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  95. }
  96. #pragma mark -
  97. #pragma --------------------------------------------------------------------------------------------
  98. #pragma mark ===== Peek & Pop =====
  99. #pragma --------------------------------------------------------------------------------------------
  100. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  101. {
  102. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  103. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  104. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  105. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  106. NCSharesCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  107. if (cell) {
  108. previewingContext.sourceRect = cell.frame;
  109. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  110. viewController.metadata = metadata;
  111. viewController.imageFile = cell.fileImageView.image;
  112. viewController.showOpenIn = false;
  113. viewController.showOpenInternalViewer = false;
  114. viewController.showShare = false;
  115. return viewController;
  116. }
  117. return nil;
  118. }
  119. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  120. {
  121. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  122. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  123. }
  124. #pragma --------------------------------------------------------------------------------------------
  125. #pragma mark ==== unShare <Delegate> ====
  126. #pragma --------------------------------------------------------------------------------------------
  127. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  128. {
  129. [[OCNetworking sharedManager] unshareAccount:appDelegate.activeAccount shareID:tableShare.idRemoteShared completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  130. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  131. [[NCManageDatabase sharedInstance] deleteTableShareWithAccount:account idRemoteShared:tableShare.idRemoteShared];
  132. [self reloadDatasource];
  133. } else if (errorCode != 0) {
  134. [[NCContentPresenter shared] messageNotification:@"_share_" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  135. } else {
  136. NSLog(@"[LOG] It has been changed user during networking process, error.");
  137. }
  138. }];
  139. }
  140. #pragma mark -
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark ===== Swipe Tablet -> menu =====
  143. #pragma --------------------------------------------------------------------------------------------
  144. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. return UITableViewCellEditingStyleDelete;
  147. }
  148. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. return NSLocalizedString(@"_delete_", nil);
  151. }
  152. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  153. {
  154. tableMetadata *metadata;
  155. if (indexPath.row+1 <= _dataSource.count) {
  156. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  157. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  158. }
  159. if (metadata) return YES;
  160. else return NO;
  161. }
  162. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. if (editingStyle == UITableViewCellEditingStyleDelete) {
  165. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  166. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  167. [self removeShares:metadata tableShare:table];
  168. }
  169. }
  170. #pragma --------------------------------------------------------------------------------------------
  171. #pragma mark ==== Table ====
  172. #pragma --------------------------------------------------------------------------------------------
  173. - (void)readFolder:(NSString *)serverUrl
  174. {
  175. [self reloadDatasource];
  176. }
  177. - (void)reloadDatasource
  178. {
  179. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.activeAccount];
  180. [self.tableView reloadData];
  181. }
  182. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. return 60;
  185. }
  186. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  187. {
  188. return 1;
  189. }
  190. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  191. {
  192. return [_dataSource count];
  193. }
  194. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  195. {
  196. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  197. tableMetadata *metadata;
  198. // separator
  199. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  200. // Initialize
  201. cell.statusImageView.image = nil;
  202. cell.offlineImageView.image = nil;
  203. // change color selection
  204. UIView *selectionColor = [[UIView alloc] init];
  205. selectionColor.backgroundColor = NCBrandColor.sharedInstance.select;
  206. cell.selectedBackgroundView = selectionColor;
  207. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  208. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView;
  209. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  210. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  211. if (metadata) {
  212. if (metadata.directory) {
  213. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  214. } else {
  215. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  216. if (cell.fileImageView.image == nil) {
  217. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  218. [[NCNetworkingMain sharedInstance] downloadThumbnailWith:metadata view:tableView indexPath:indexPath];
  219. }
  220. }
  221. } else {
  222. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  223. [[OCNetworking sharedManager] readFileWithAccount:appDelegate.activeAccount serverUrl:table.serverUrl fileName:table.fileName completion:^(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode) {
  224. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  225. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  226. [self reloadDatasource];
  227. }
  228. }];
  229. }
  230. cell.labelTitle.text = table.fileName;
  231. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  232. cell.labelInfoFile.text = @"/";
  233. else
  234. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  235. return cell;
  236. }
  237. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. // deselect row
  240. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  241. tableMetadata *metadata;
  242. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  243. if (table.serverUrl) {
  244. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  245. if (metadata) {
  246. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  247. }
  248. }
  249. }
  250. @end