CCControlCenterActivity.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. @interface CCControlCenterActivity ()
  15. {
  16. // Datasource
  17. NSArray *_sectionDataSource;
  18. }
  19. @end
  20. @implementation CCControlCenterActivity
  21. #pragma --------------------------------------------------------------------------------------------
  22. #pragma mark ===== Init =====
  23. #pragma --------------------------------------------------------------------------------------------
  24. - (id)initWithCoder:(NSCoder *)aDecoder
  25. {
  26. if (self = [super initWithCoder:aDecoder]) {
  27. app.controlCenterActivity = self;
  28. }
  29. return self;
  30. }
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. _sectionDataSource = [NSArray new];
  34. [self reloadDatasource];
  35. }
  36. // Apparirà
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. [super viewWillAppear:animated];
  40. app.controlCenter.labelMessageNoRecord.hidden = YES;
  41. }
  42. // E' arrivato
  43. - (void)viewDidAppear:(BOOL)animated
  44. {
  45. [super viewDidAppear:animated];
  46. [self reloadDatasource];
  47. }
  48. - (void)didReceiveMemoryWarning {
  49. [super didReceiveMemoryWarning];
  50. }
  51. #pragma --------------------------------------------------------------------------------------------
  52. #pragma mark - ==== Datasource ====
  53. #pragma --------------------------------------------------------------------------------------------
  54. - (void)reloadDatasource
  55. {
  56. // test
  57. if (app.activeAccount.length == 0)
  58. return;
  59. if (app.controlCenter.isOpen) {
  60. //_sectionDataSource = [CCCoreData getAllTableActivityWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (idActivity != 0)", app.activeAccount]];
  61. if ([CCUtility getActivityVerboseHigh])
  62. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", app.activeAccount]];
  63. else
  64. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (verbose == %lu)", app.activeAccount, k_activityVerboseDefault]];
  65. if ([[app.controlCenter getActivePage] isEqualToString:k_pageControlCenterActivity]) {
  66. if ([_sectionDataSource count] == 0) {
  67. app.controlCenter.labelMessageNoRecord.text = NSLocalizedString(@"_no_activity_",nil);
  68. app.controlCenter.labelMessageNoRecord.hidden = NO;
  69. } else {
  70. app.controlCenter.labelMessageNoRecord.hidden = YES;
  71. }
  72. }
  73. }
  74. [self.collectionView reloadData];
  75. }
  76. #pragma --------------------------------------------------------------------------------------------
  77. #pragma mark - ==== Table ====
  78. #pragma --------------------------------------------------------------------------------------------
  79. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  80. {
  81. return [_sectionDataSource count];
  82. }
  83. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  84. {
  85. /*
  86. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  87. if ([activity.file length] > 0)
  88. return 1;
  89. else
  90. return 0;
  91. */
  92. return 0;
  93. }
  94. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  95. {
  96. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  97. UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 100, CGFLOAT_MAX)];
  98. noteLabel.numberOfLines = 0;
  99. [noteLabel setFont:fontSizeNote];
  100. [noteLabel sizeToFit];
  101. noteLabel.text = activity.note;
  102. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  103. int heighNoteLabel = [self getLabelHeight:noteLabel];
  104. int heightView = 90 + heighNoteLabel + (heighNoteLabel/5);
  105. return CGSizeMake(collectionView.frame.size.width, heightView);
  106. }
  107. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  108. {
  109. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  110. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  111. UILabel *dateLabel = (UILabel *)[headerView viewWithTag:100];
  112. UILabel *actionLabel = (UILabel *)[headerView viewWithTag:101];
  113. UILabel *noteLabel = (UILabel *)[headerView viewWithTag:102];
  114. UIImageView *typeImage = (UIImageView *) [headerView viewWithTag:103];
  115. [dateLabel setFont:fontSizeData];
  116. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  117. if ([CCUtility getActivityVerboseHigh]) {
  118. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  119. } else {
  120. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  121. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  122. }
  123. [actionLabel setFont:fontSizeAction];
  124. [actionLabel sizeToFit];
  125. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  126. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  127. actionLabel.textColor = COLOR_BRAND;
  128. if ([activity.verbose integerValue] == k_activityVerboseHigh)
  129. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  130. else
  131. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  132. }
  133. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  134. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  135. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  136. }
  137. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  138. actionLabel.textColor = [UIColor redColor];
  139. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  140. }
  141. [noteLabel setFont:fontSizeNote];
  142. [noteLabel sizeToFit];
  143. noteLabel.textColor = COLOR_TEXT_ANTHRACITE;
  144. noteLabel.numberOfLines = 0;
  145. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  146. noteLabel.text = activity.note;
  147. //headerView.backgroundColor = [UIColor blueColor];
  148. return headerView;
  149. }
  150. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  153. cell.backgroundColor = [UIColor clearColor];
  154. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  155. NSString *dir = [activity.file stringByDeletingLastPathComponent];
  156. NSString *fileName = [activity.file lastPathComponent];
  157. if ([dir length] > 0 && [fileName length] > 0) {
  158. }
  159. return cell;
  160. }
  161. #pragma --------------------------------------------------------------------------------------------
  162. #pragma mark - ==== Utility ====
  163. #pragma --------------------------------------------------------------------------------------------
  164. - (CGFloat)getLabelHeight:(UILabel*)label
  165. {
  166. CGSize constraint = CGSizeMake(self.collectionView.frame.size.width, CGFLOAT_MAX);
  167. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  168. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  169. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  170. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  171. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  172. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  173. return size.height;
  174. }
  175. @end