CCControlCenterActivity.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. NSPredicate *predicate;
  61. if ([CCUtility getActivityVerboseHigh])
  62. predicate = [NSPredicate predicateWithFormat:@"((account == %@) || (account == ''))", app.activeAccount];
  63. else
  64. predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (verbose == %lu)", app.activeAccount, k_activityVerboseDefault];
  65. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate: predicate];
  66. [self reloadCollection];
  67. }
  68. }
  69. - (void)reloadCollection
  70. {
  71. if ([[app.controlCenter getActivePage] isEqualToString:k_pageControlCenterActivity]) {
  72. if ([_sectionDataSource count] == 0) {
  73. app.controlCenter.labelMessageNoRecord.text = NSLocalizedString(@"_no_activity_",nil);
  74. app.controlCenter.labelMessageNoRecord.hidden = NO;
  75. } else {
  76. app.controlCenter.labelMessageNoRecord.hidden = YES;
  77. }
  78. }
  79. [self.collectionView reloadData];
  80. }
  81. #pragma --------------------------------------------------------------------------------------------
  82. #pragma mark - ==== Table ====
  83. #pragma --------------------------------------------------------------------------------------------
  84. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  85. {
  86. return [_sectionDataSource count];
  87. }
  88. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  89. {
  90. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  91. if ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload]) {
  92. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]])
  93. return 1;
  94. else
  95. return 0;
  96. }
  97. return 0;
  98. }
  99. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  100. {
  101. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  102. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  103. label.numberOfLines = 0;
  104. label.lineBreakMode = NSLineBreakByWordWrapping;
  105. [label sizeToFit];
  106. // Action
  107. [label setFont:fontSizeAction];
  108. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  109. int heightAction = [self getLabelHeight:label];
  110. // Note
  111. [label setFont:fontSizeNote];
  112. if ([CCUtility getActivityVerboseHigh] && activity.idActivity == 0) label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  113. else label.text = activity.note;
  114. int heightNote = [self getLabelHeight:label];
  115. int heightView = 40 + heightAction + heightNote;
  116. return CGSizeMake(collectionView.frame.size.width, heightView);
  117. }
  118. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  119. {
  120. if (kind == UICollectionElementKindSectionHeader) {
  121. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  122. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  123. UILabel *dateLabel = (UILabel *)[headerView viewWithTag:100];
  124. UILabel *actionLabel = (UILabel *)[headerView viewWithTag:101];
  125. UILabel *noteLabel = (UILabel *)[headerView viewWithTag:102];
  126. UIImageView *typeImage = (UIImageView *) [headerView viewWithTag:103];
  127. [dateLabel setFont:fontSizeData];
  128. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  129. if ([CCUtility getActivityVerboseHigh]) {
  130. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  131. } else {
  132. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  133. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  134. }
  135. [actionLabel setFont:fontSizeAction];
  136. [actionLabel sizeToFit];
  137. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  138. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  139. actionLabel.textColor = COLOR_BRAND;
  140. if (activity.idActivity == 0)
  141. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  142. else
  143. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  144. }
  145. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  146. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  147. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  148. }
  149. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  150. actionLabel.textColor = [UIColor redColor];
  151. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  152. }
  153. [noteLabel setFont:fontSizeNote];
  154. [noteLabel sizeToFit];
  155. noteLabel.textColor = COLOR_TEXT_ANTHRACITE;
  156. noteLabel.numberOfLines = 0;
  157. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  158. if ([CCUtility getActivityVerboseHigh] && activity.idActivity == 0) noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  159. else noteLabel.text = activity.note;
  160. return headerView;
  161. }
  162. if (kind == UICollectionElementKindSectionFooter) {
  163. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  164. return footerView;
  165. }
  166. return nil;
  167. }
  168. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  171. cell.backgroundColor = [UIColor clearColor];
  172. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  173. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  174. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]];
  175. return cell;
  176. }
  177. #pragma --------------------------------------------------------------------------------------------
  178. #pragma mark - ==== Utility ====
  179. #pragma --------------------------------------------------------------------------------------------
  180. - (CGFloat)getLabelHeight:(UILabel*)label
  181. {
  182. CGSize constraint = CGSizeMake(self.collectionView.frame.size.width, CGFLOAT_MAX);
  183. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  184. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  185. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  186. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  187. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  188. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  189. return size.height;
  190. }
  191. @end