CCControlCenterActivity.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. NSDate *dateActivity;
  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. dateActivity = ((TableActivity *)[_sectionDataSource objectAtIndex:0]).date;
  78. }
  79. if ([dateActivity compare:_storeDateFirstActivity] == NSOrderedDescending || _storeDateFirstActivity == nil) {
  80. _storeDateFirstActivity = dateActivity;
  81. [self.collectionView reloadData];
  82. }
  83. }
  84. #pragma --------------------------------------------------------------------------------------------
  85. #pragma mark - ==== Table ====
  86. #pragma --------------------------------------------------------------------------------------------
  87. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  88. {
  89. return [_sectionDataSource count];
  90. }
  91. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  92. {
  93. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  94. if ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload]) {
  95. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]])
  96. return 1;
  97. else
  98. return 0;
  99. }
  100. return 0;
  101. }
  102. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  103. {
  104. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  105. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  106. label.numberOfLines = 0;
  107. label.lineBreakMode = NSLineBreakByWordWrapping;
  108. [label sizeToFit];
  109. // Action
  110. [label setFont:fontSizeAction];
  111. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  112. int heightAction = [self getLabelHeight:label];
  113. // Note
  114. [label setFont:fontSizeNote];
  115. if ([CCUtility getActivityVerboseHigh] && activity.idActivity == 0) label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  116. else label.text = activity.note;
  117. int heightNote = [self getLabelHeight:label];
  118. int heightView = 40 + heightAction + heightNote;
  119. return CGSizeMake(collectionView.frame.size.width, heightView);
  120. }
  121. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  122. {
  123. if (kind == UICollectionElementKindSectionHeader) {
  124. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  125. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  126. UILabel *dateLabel = (UILabel *)[headerView viewWithTag:100];
  127. UILabel *actionLabel = (UILabel *)[headerView viewWithTag:101];
  128. UILabel *noteLabel = (UILabel *)[headerView viewWithTag:102];
  129. UIImageView *typeImage = (UIImageView *) [headerView viewWithTag:103];
  130. [dateLabel setFont:fontSizeData];
  131. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  132. if ([CCUtility getActivityVerboseHigh]) {
  133. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  134. } else {
  135. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  136. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  137. }
  138. [actionLabel setFont:fontSizeAction];
  139. [actionLabel sizeToFit];
  140. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  141. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  142. actionLabel.textColor = COLOR_BRAND;
  143. if (activity.idActivity == 0)
  144. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  145. else
  146. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  147. }
  148. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  149. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  150. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  151. }
  152. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  153. actionLabel.textColor = [UIColor redColor];
  154. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  155. }
  156. [noteLabel setFont:fontSizeNote];
  157. [noteLabel sizeToFit];
  158. noteLabel.textColor = COLOR_TEXT_ANTHRACITE;
  159. noteLabel.numberOfLines = 0;
  160. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  161. if ([CCUtility getActivityVerboseHigh] && activity.idActivity == 0) noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  162. else noteLabel.text = activity.note;
  163. return headerView;
  164. }
  165. if (kind == UICollectionElementKindSectionFooter) {
  166. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  167. return footerView;
  168. }
  169. return nil;
  170. }
  171. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  172. {
  173. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  174. cell.backgroundColor = [UIColor clearColor];
  175. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  176. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  177. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]];
  178. return cell;
  179. }
  180. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  181. {
  182. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  183. CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activity.account, activity.fileID] context:nil];
  184. if (metadata) {
  185. if (!self.splitViewController.isCollapsed && app.activeMain.detailViewController.isViewLoaded && app.activeMain.detailViewController.view.window)
  186. [app.activeMain.navigationController popToRootViewControllerAnimated:NO];
  187. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  188. [app.activeMain performSegueWithIdentifier:@"segueDetail" sender:metadata];
  189. });
  190. } else {
  191. [app messageNotification:@"_info_" description:@"_activity_file_not_present_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  192. }
  193. }
  194. #pragma --------------------------------------------------------------------------------------------
  195. #pragma mark - ==== Utility ====
  196. #pragma --------------------------------------------------------------------------------------------
  197. - (CGFloat)getLabelHeight:(UILabel*)label
  198. {
  199. CGSize constraint = CGSizeMake(self.collectionView.frame.size.width, CGFLOAT_MAX);
  200. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  201. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  202. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  203. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  204. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  205. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  206. return size.height;
  207. }
  208. @end