CCControlCenterActivity.m 9.7 KB

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