CCControlCenterActivity.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. @interface CCControlCenterActivity ()
  12. {
  13. // Datasource
  14. NSArray *_sectionDataSource;
  15. NSDate *_oldDate;
  16. }
  17. @end
  18. @implementation CCControlCenterActivity
  19. #pragma --------------------------------------------------------------------------------------------
  20. #pragma mark ===== Init =====
  21. #pragma --------------------------------------------------------------------------------------------
  22. - (id)initWithCoder:(NSCoder *)aDecoder
  23. {
  24. if (self = [super initWithCoder:aDecoder]) {
  25. app.controlCenterActivity = self;
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. _sectionDataSource = [NSArray new];
  32. _oldDate = [NSDate date];
  33. // empty Data Source
  34. /*
  35. _tableView.delegate = self;
  36. _tableView.dataSource = self;
  37. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  38. _tableView.backgroundColor = [UIColor clearColor];
  39. */
  40. }
  41. // Apparirà
  42. - (void)viewWillAppear:(BOOL)animated
  43. {
  44. [super viewWillAppear:animated];
  45. [self reloadDatasource];
  46. }
  47. // E' arrivato
  48. - (void)viewDidAppear:(BOOL)animated
  49. {
  50. [super viewDidAppear:animated];
  51. }
  52. - (void)didReceiveMemoryWarning {
  53. [super didReceiveMemoryWarning];
  54. }
  55. #pragma --------------------------------------------------------------------------------------------
  56. #pragma mark - ==== Datasource ====
  57. #pragma --------------------------------------------------------------------------------------------
  58. - (void)reloadDatasource
  59. {
  60. // test
  61. if (app.activeAccount.length == 0)
  62. return;
  63. if (app.controlCenter.isOpen) {
  64. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", app.activeAccount]];
  65. //_sectionDataSource = [CCSectionActivity creataDataSourseSectionActivity:records activeAccount:app.activeAccount];
  66. if ([_sectionDataSource count] == 0) {
  67. app.controlCenter.noRecord.text = NSLocalizedString(@"_no_activity_",nil);
  68. app.controlCenter.noRecord.hidden = NO;
  69. } else {
  70. app.controlCenter.noRecord.hidden = YES;
  71. }
  72. }
  73. [self.collectionView reloadData];
  74. [app updateApplicationIconBadgeNumber];
  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. //return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  86. return 10;
  87. }
  88. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  89. {
  90. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  91. UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width , CGFLOAT_MAX)];
  92. subjectLabel.numberOfLines = 0;
  93. subjectLabel.lineBreakMode = NSLineBreakByWordWrapping;
  94. [subjectLabel setFont:[UIFont fontWithName:@"System" size:12]];
  95. subjectLabel.text = activity.subject;
  96. [subjectLabel sizeToFit];
  97. return CGSizeMake(collectionView.frame.size.width, subjectLabel.frame.size.height+22+20);
  98. }
  99. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  100. {
  101. if (kind == UICollectionElementKindSectionHeader) {
  102. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  103. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  104. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  105. NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
  106. UILabel *dataLabel = (UILabel *)[headerView viewWithTag:100];
  107. UILabel *subjectLabel = (UILabel *)[headerView viewWithTag:101];
  108. [dataLabel setFont:[UIFont fontWithName:@"System" size:12]];
  109. dataLabel.textColor = COLOR_TEXT_ANTHRACITE;
  110. dataLabel.text = [CCUtility getTitleSectionDate:date];
  111. [subjectLabel setFont:[UIFont fontWithName:@"System" size:12]];
  112. subjectLabel.textColor = COLOR_TEXT_ANTHRACITE;
  113. subjectLabel.text = activity.subject;
  114. CGFloat x = [self getLabelHeight:subjectLabel];
  115. headerView.frame = CGRectMake(headerView.frame.origin.x, headerView.frame.origin.y, headerView.frame.size.width, 20 + dataLabel.frame.size.height + x);
  116. headerView.backgroundColor = [UIColor redColor];
  117. return headerView;
  118. }
  119. return nil;
  120. }
  121. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  124. cell.backgroundColor = [UIColor clearColor];
  125. //NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  126. //TableActivity *activity = [metadatasForKey objectAtIndex:indexPath.row];
  127. return cell;
  128. }
  129. #pragma --------------------------------------------------------------------------------------------
  130. #pragma mark - ==== Utility ====
  131. #pragma --------------------------------------------------------------------------------------------
  132. - (CGFloat)getLabelHeight:(UILabel*)label
  133. {
  134. CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
  135. CGSize size;
  136. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  137. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:context].size;
  138. size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  139. return size.height;
  140. }
  141. @end