CCControlCenterActivity.m 6.6 KB

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