CCActivity.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // CCActivity.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 12/04/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 "CCActivity.h"
  24. #import "AppDelegate.h"
  25. #import "CCSection.h"
  26. #import "NCBridgeSwift.h"
  27. #define fontSizeData [UIFont boldSystemFontOfSize:15]
  28. #define fontSizeAction [UIFont systemFontOfSize:14]
  29. #define fontSizeNote [UIFont systemFontOfSize:14]
  30. @interface CCActivity ()
  31. {
  32. AppDelegate *appDelegate;
  33. BOOL _verbose;
  34. // Datasource
  35. NSArray *_sectionDataSource;
  36. }
  37. @end
  38. @implementation CCActivity
  39. #pragma --------------------------------------------------------------------------------------------
  40. #pragma mark ===== Init =====
  41. #pragma --------------------------------------------------------------------------------------------
  42. - (id)initWithCoder:(NSCoder *)aDecoder
  43. {
  44. if (self = [super initWithCoder:aDecoder]) {
  45. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  46. appDelegate.activeActivity = self;
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. self.collectionView.emptyDataSetSource = self;
  55. self.collectionView.emptyDataSetDelegate = self;
  56. self.collectionView.delegate = self;
  57. _verbose = [CCUtility getActivityVerboseHigh];
  58. _sectionDataSource = [NSArray new];
  59. self.title = NSLocalizedString(@"_activity_", nil);
  60. [self reloadDatasource];
  61. }
  62. // Apparirà
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [super viewWillAppear:animated];
  66. _verbose = [CCUtility getActivityVerboseHigh];
  67. // Color
  68. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  69. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  70. // Plus Button
  71. [appDelegate plusButtonVisibile:true];
  72. }
  73. // E' arrivato
  74. - (void)viewDidAppear:(BOOL)animated
  75. {
  76. [super viewDidAppear:animated];
  77. [self getActivity];
  78. }
  79. - (void)changeTheming
  80. {
  81. if (self.isViewLoaded && self.view.window)
  82. [appDelegate changeTheming:self];
  83. }
  84. - (void)didReceiveMemoryWarning {
  85. [super didReceiveMemoryWarning];
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ==== DZNEmptyDataSetSource ====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  91. {
  92. if([_sectionDataSource count] > 0)
  93. return NO;
  94. else
  95. return YES;
  96. }
  97. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  98. {
  99. return [NCBrandColor sharedInstance].backgroundView;
  100. }
  101. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  102. {
  103. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"activityNoRecord"] multiplier:1 color:[NCBrandColor sharedInstance].graySoft];
  104. }
  105. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  106. {
  107. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_activity_", nil)];
  108. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  109. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark - ==== Datasource ====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)getActivity
  115. {
  116. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  117. [ocNetworking getActivityServer:^(NSArray *listOfActivity) {
  118. [[NCManageDatabase sharedInstance] addActivityServer:listOfActivity account:appDelegate.activeAccount];
  119. [self reloadDatasource];
  120. } failure:^(NSString *message, NSInteger errorCode) {
  121. [self reloadDatasource];
  122. }];
  123. }
  124. #pragma --------------------------------------------------------------------------------------------
  125. #pragma mark - ==== Datasource ====
  126. #pragma --------------------------------------------------------------------------------------------
  127. - (void)reloadDatasource
  128. {
  129. // test
  130. if (appDelegate.activeAccount.length == 0)
  131. return;
  132. NSPredicate *predicate;
  133. NSDate *sixDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-k_daysOfActivity toDate:[NSDate date] options:0];
  134. if (_verbose)
  135. predicate = [NSPredicate predicateWithFormat:@"account == %@ AND date > %@", appDelegate.activeAccount, sixDaysAgo];
  136. else
  137. predicate = [NSPredicate predicateWithFormat:@"account == %@ AND verbose == %lu AND date > %@", appDelegate.activeAccount, k_activityVerboseDefault, sixDaysAgo];
  138. _sectionDataSource = [[NCManageDatabase sharedInstance] getActivityWithPredicate:predicate];
  139. [self reloadCollection];
  140. }
  141. - (void)reloadCollection
  142. {
  143. NSDate *dateActivity;
  144. if ([_sectionDataSource count] > 0)
  145. dateActivity = ((tableActivity *)[_sectionDataSource objectAtIndex:0]).date;
  146. if ([dateActivity compare:_storeDateFirstActivity] == NSOrderedDescending || _storeDateFirstActivity == nil || dateActivity == nil) {
  147. _storeDateFirstActivity = dateActivity;
  148. [self.collectionView reloadData];
  149. }
  150. }
  151. #pragma --------------------------------------------------------------------------------------------
  152. #pragma mark - ==== Table ====
  153. #pragma --------------------------------------------------------------------------------------------
  154. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  155. {
  156. return [_sectionDataSource count];
  157. }
  158. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  159. {
  160. tableActivity *activity = [_sectionDataSource objectAtIndex:section];
  161. if (activity.fileID.length > 0) {
  162. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", activity.fileID]];
  163. if (metadata && ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload])) {
  164. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  165. return 1;
  166. } else {
  167. return 0;
  168. }
  169. }
  170. }
  171. return 0;
  172. }
  173. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  174. {
  175. tableActivity *activity = [_sectionDataSource objectAtIndex:section];
  176. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  177. label.numberOfLines = 0;
  178. label.lineBreakMode = NSLineBreakByWordWrapping;
  179. [label sizeToFit];
  180. // Action
  181. [label setFont:fontSizeAction];
  182. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  183. int heightAction = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  184. // Note
  185. [label setFont:fontSizeNote];
  186. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  187. label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  188. else
  189. label.text = activity.note;
  190. int heightNote = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  191. int heightView = 40 + heightAction + heightNote + 17;
  192. return CGSizeMake(collectionView.frame.size.width, heightView);
  193. }
  194. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  195. {
  196. UICollectionReusableView *reusableview;
  197. if (kind == UICollectionElementKindSectionHeader) {
  198. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  199. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  200. UILabel *dateLabel = (UILabel *)[reusableview viewWithTag:100];
  201. UILabel *actionLabel = (UILabel *)[reusableview viewWithTag:101];
  202. UILabel *noteLabel = (UILabel *)[reusableview viewWithTag:102];
  203. UIImageView *typeImage = (UIImageView *) [reusableview viewWithTag:103];
  204. [dateLabel setFont:fontSizeData];
  205. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  206. if (_verbose) {
  207. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  208. } else {
  209. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  210. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  211. }
  212. [actionLabel setFont:fontSizeAction];
  213. [actionLabel sizeToFit];
  214. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  215. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  216. actionLabel.textColor = [NCBrandColor sharedInstance].nextcloud;
  217. if (activity.idActivity == 0)
  218. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  219. else
  220. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  221. }
  222. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  223. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  224. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  225. }
  226. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  227. actionLabel.textColor = [UIColor redColor];
  228. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  229. }
  230. [noteLabel setFont:fontSizeNote];
  231. [noteLabel sizeToFit];
  232. noteLabel.textColor = [UIColor blackColor];
  233. noteLabel.numberOfLines = 0;
  234. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  235. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  236. noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  237. else
  238. noteLabel.text = activity.note;
  239. }
  240. if (kind == UICollectionElementKindSectionFooter) {
  241. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  242. }
  243. return reusableview;
  244. }
  245. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  246. {
  247. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  248. //cell.backgroundColor = [UIColor clearColor];
  249. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  250. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  251. if (activity.fileID.length > 0) {
  252. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", activity.fileID]];
  253. if (metadata && ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload])) {
  254. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  255. imageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  256. } else {
  257. imageView.image = [UIImage imageNamed:metadata.iconName];
  258. }
  259. }
  260. } else {
  261. imageView.image = [UIImage imageNamed:@"file"];
  262. }
  263. return cell;
  264. }
  265. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  266. {
  267. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  268. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", activity.fileID]];
  269. BOOL existsFile = [CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView];
  270. if (metadata && existsFile) {
  271. if (!self.splitViewController.isCollapsed && appDelegate.activeMain.detailViewController.isViewLoaded && appDelegate.activeMain.detailViewController.view.window)
  272. [appDelegate.activeMain.navigationController popToRootViewControllerAnimated:NO];
  273. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  274. [appDelegate.activeMain performSegueWithIdentifier:@"segueDetail" sender:metadata];
  275. });
  276. } else {
  277. [appDelegate messageNotification:@"_info_" description:@"_activity_file_not_present_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  278. }
  279. }
  280. #pragma --------------------------------------------------------------------------------------------
  281. #pragma mark - ==== Utility ====
  282. #pragma --------------------------------------------------------------------------------------------
  283. + (CGFloat)getLabelHeight:(UILabel*)label width:(int)width
  284. {
  285. CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
  286. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  287. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  288. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  289. NSStringDrawingContext *context = [NSStringDrawingContext new];
  290. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  291. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  292. return size.height;
  293. }
  294. @end