NCShares.m 14 KB

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