CCControlCenterActivity.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // CCControlCenterActivity.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/03/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "CCControlCenterActivity.h"
  9. #import "AppDelegate.h"
  10. #import "CCSection.h"
  11. #define fontSizeData [UIFont boldSystemFontOfSize:15]
  12. #define fontSizeAction [UIFont systemFontOfSize:14]
  13. #define fontSizeNote [UIFont systemFontOfSize:14]
  14. #define daysOfActivity 7
  15. @interface CCControlCenterActivity ()
  16. {
  17. BOOL _verbose;
  18. // Datasource
  19. NSArray *_sectionDataSource;
  20. }
  21. @end
  22. @implementation CCControlCenterActivity
  23. #pragma --------------------------------------------------------------------------------------------
  24. #pragma mark ===== Init =====
  25. #pragma --------------------------------------------------------------------------------------------
  26. - (id)initWithCoder:(NSCoder *)aDecoder
  27. {
  28. if (self = [super initWithCoder:aDecoder]) {
  29. app.controlCenterActivity = self;
  30. }
  31. return self;
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _verbose = [CCUtility getActivityVerboseHigh];
  36. _sectionDataSource = [NSArray new];
  37. [self reloadDatasource];
  38. }
  39. // Apparirà
  40. - (void)viewWillAppear:(BOOL)animated
  41. {
  42. [super viewWillAppear:animated];
  43. _verbose = [CCUtility getActivityVerboseHigh];
  44. app.controlCenter.labelMessageNoRecord.hidden = YES;
  45. }
  46. // E' arrivato
  47. - (void)viewDidAppear:(BOOL)animated
  48. {
  49. [super viewDidAppear:animated];
  50. [self reloadDatasource];
  51. }
  52. - (void)didReceiveMemoryWarning {
  53. [super didReceiveMemoryWarning];
  54. }
  55. #pragma --------------------------------------------------------------------------------------------
  56. #pragma mark - ==== Datasource ====
  57. #pragma --------------------------------------------------------------------------------------------
  58. - (void)reloadDatasource
  59. {
  60. // test
  61. if (app.activeAccount.length == 0)
  62. return;
  63. if (app.controlCenter.isOpen) {
  64. NSPredicate *predicate;
  65. NSDate *sixDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-daysOfActivity toDate:[NSDate date] options:0];
  66. if (_verbose)
  67. predicate = [NSPredicate predicateWithFormat:@"((account == %@) || (account == '')) AND (date > %@)", app.activeAccount, sixDaysAgo];
  68. else
  69. predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (verbose == %lu) AND (date > %@)", app.activeAccount, k_activityVerboseDefault, sixDaysAgo];
  70. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate: predicate];
  71. [self reloadCollection];
  72. }
  73. }
  74. - (void)reloadCollection
  75. {
  76. NSDate *dateActivity;
  77. if ([_sectionDataSource count] == 0) {
  78. app.controlCenter.labelMessageNoRecord.text = NSLocalizedString(@"_no_activity_",nil);
  79. app.controlCenter.labelMessageNoRecord.hidden = NO;
  80. } else {
  81. app.controlCenter.labelMessageNoRecord.hidden = YES;
  82. dateActivity = ((TableActivity *)[_sectionDataSource objectAtIndex:0]).date;
  83. }
  84. if ([dateActivity compare:_storeDateFirstActivity] == NSOrderedDescending || _storeDateFirstActivity == nil || dateActivity == nil) {
  85. _storeDateFirstActivity = dateActivity;
  86. [self.collectionView reloadData];
  87. }
  88. }
  89. #pragma --------------------------------------------------------------------------------------------
  90. #pragma mark - ==== Table ====
  91. #pragma --------------------------------------------------------------------------------------------
  92. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  93. {
  94. return [_sectionDataSource count];
  95. }
  96. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  97. {
  98. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  99. if ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload]) {
  100. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]])
  101. return 1;
  102. else
  103. return 0;
  104. }
  105. return 0;
  106. }
  107. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  108. {
  109. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  110. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  111. label.numberOfLines = 0;
  112. label.lineBreakMode = NSLineBreakByWordWrapping;
  113. [label sizeToFit];
  114. // Action
  115. [label setFont:fontSizeAction];
  116. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  117. int heightAction = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  118. // Note
  119. [label setFont:fontSizeNote];
  120. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  121. label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  122. else
  123. label.text = activity.note;
  124. int heightNote = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  125. int heightView = 40 + heightAction + heightNote + 17;
  126. return CGSizeMake(collectionView.frame.size.width, heightView);
  127. }
  128. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  129. {
  130. UICollectionReusableView *reusableview;
  131. if (kind == UICollectionElementKindSectionHeader) {
  132. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  133. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  134. UILabel *dateLabel = (UILabel *)[reusableview viewWithTag:100];
  135. UILabel *actionLabel = (UILabel *)[reusableview viewWithTag:101];
  136. UILabel *noteLabel = (UILabel *)[reusableview viewWithTag:102];
  137. UIImageView *typeImage = (UIImageView *) [reusableview viewWithTag:103];
  138. [dateLabel setFont:fontSizeData];
  139. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  140. if (_verbose) {
  141. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  142. } else {
  143. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  144. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  145. }
  146. [actionLabel setFont:fontSizeAction];
  147. [actionLabel sizeToFit];
  148. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  149. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  150. actionLabel.textColor = COLOR_BRAND;
  151. if (activity.idActivity == 0)
  152. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  153. else
  154. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  155. }
  156. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  157. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  158. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  159. }
  160. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  161. actionLabel.textColor = [UIColor redColor];
  162. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  163. }
  164. [noteLabel setFont:fontSizeNote];
  165. [noteLabel sizeToFit];
  166. noteLabel.textColor = COLOR_TEXT_ANTHRACITE;
  167. noteLabel.numberOfLines = 0;
  168. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  169. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  170. noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  171. else
  172. noteLabel.text = activity.note;
  173. }
  174. if (kind == UICollectionElementKindSectionFooter) {
  175. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  176. }
  177. return reusableview;
  178. }
  179. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  180. {
  181. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  182. cell.backgroundColor = [UIColor clearColor];
  183. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  184. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  185. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]];
  186. return cell;
  187. }
  188. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  189. {
  190. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  191. CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activity.account, activity.fileID] context:nil];
  192. if (metadata) {
  193. if (!self.splitViewController.isCollapsed && app.activeMain.detailViewController.isViewLoaded && app.activeMain.detailViewController.view.window)
  194. [app.activeMain.navigationController popToRootViewControllerAnimated:NO];
  195. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  196. [app.activeMain performSegueWithIdentifier:@"segueDetail" sender:metadata];
  197. [app.controlCenter closeControlCenter];
  198. });
  199. } else {
  200. [app messageNotification:@"_info_" description:@"_activity_file_not_present_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  201. }
  202. }
  203. #pragma --------------------------------------------------------------------------------------------
  204. #pragma mark - ==== Utility ====
  205. #pragma --------------------------------------------------------------------------------------------
  206. + (CGFloat)getLabelHeight:(UILabel*)label width:(int)width
  207. {
  208. CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
  209. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  210. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  211. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  212. NSStringDrawingContext *context = [NSStringDrawingContext new];
  213. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  214. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  215. return size.height;
  216. }
  217. @end