CCControlCenterActivity.m 6.4 KB

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