CCControlCenterActivity.m 11 KB

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