CCControlCenterActivity.m 11 KB

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