CCActivity.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // CCActivity.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/04/17.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCActivity.h"
  24. #import "AppDelegate.h"
  25. #import "CCSection.h"
  26. #define fontSizeData [UIFont boldSystemFontOfSize:15]
  27. #define fontSizeAction [UIFont systemFontOfSize:14]
  28. #define fontSizeNote [UIFont systemFontOfSize:14]
  29. #define daysOfActivity 7
  30. @interface CCActivity ()
  31. {
  32. BOOL _verbose;
  33. // Datasource
  34. NSArray *_sectionDataSource;
  35. }
  36. @end
  37. @implementation CCActivity
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. app.activeActivity = self;
  45. }
  46. return self;
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. _verbose = [CCUtility getActivityVerboseHigh];
  51. _sectionDataSource = [NSArray new];
  52. self.title = NSLocalizedString(@"_activity_", nil);
  53. [self reloadDatasource];
  54. }
  55. // Apparirà
  56. - (void)viewWillAppear:(BOOL)animated
  57. {
  58. [super viewWillAppear:animated];
  59. _verbose = [CCUtility getActivityVerboseHigh];
  60. // Color
  61. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  62. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  63. }
  64. // E' arrivato
  65. - (void)viewDidAppear:(BOOL)animated
  66. {
  67. [super viewDidAppear:animated];
  68. [self reloadDatasource];
  69. }
  70. - (void)didReceiveMemoryWarning {
  71. [super didReceiveMemoryWarning];
  72. }
  73. #pragma --------------------------------------------------------------------------------------------
  74. #pragma mark - ==== Datasource ====
  75. #pragma --------------------------------------------------------------------------------------------
  76. - (void)reloadDatasource
  77. {
  78. // test
  79. if (app.activeAccount.length == 0)
  80. return;
  81. NSPredicate *predicate;
  82. NSDate *sixDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-daysOfActivity toDate:[NSDate date] options:0];
  83. if (_verbose)
  84. predicate = [NSPredicate predicateWithFormat:@"((account == %@) || (account == '')) AND (date > %@)", app.activeAccount, sixDaysAgo];
  85. else
  86. predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (verbose == %lu) AND (date > %@)", app.activeAccount, k_activityVerboseDefault, sixDaysAgo];
  87. _sectionDataSource = [CCCoreData getAllTableActivityWithPredicate: predicate];
  88. [self reloadCollection];
  89. }
  90. - (void)reloadCollection
  91. {
  92. NSDate *dateActivity;
  93. if ([_sectionDataSource count] == 0) {
  94. } else {
  95. dateActivity = ((TableActivity *)[_sectionDataSource objectAtIndex:0]).date;
  96. }
  97. if ([dateActivity compare:_storeDateFirstActivity] == NSOrderedDescending || _storeDateFirstActivity == nil || dateActivity == nil) {
  98. _storeDateFirstActivity = dateActivity;
  99. [self.collectionView reloadData];
  100. }
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark - ==== Table ====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  106. {
  107. return [_sectionDataSource count];
  108. }
  109. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  110. {
  111. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  112. if ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload]) {
  113. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]])
  114. return 1;
  115. else
  116. return 0;
  117. }
  118. return 0;
  119. }
  120. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  121. {
  122. TableActivity *activity = [_sectionDataSource objectAtIndex:section];
  123. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  124. label.numberOfLines = 0;
  125. label.lineBreakMode = NSLineBreakByWordWrapping;
  126. [label sizeToFit];
  127. // Action
  128. [label setFont:fontSizeAction];
  129. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  130. int heightAction = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  131. // Note
  132. [label setFont:fontSizeNote];
  133. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  134. label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  135. else
  136. label.text = activity.note;
  137. int heightNote = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  138. int heightView = 40 + heightAction + heightNote + 17;
  139. return CGSizeMake(collectionView.frame.size.width, heightView);
  140. }
  141. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  142. {
  143. UICollectionReusableView *reusableview;
  144. if (kind == UICollectionElementKindSectionHeader) {
  145. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  146. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  147. UILabel *dateLabel = (UILabel *)[reusableview viewWithTag:100];
  148. UILabel *actionLabel = (UILabel *)[reusableview viewWithTag:101];
  149. UILabel *noteLabel = (UILabel *)[reusableview viewWithTag:102];
  150. UIImageView *typeImage = (UIImageView *) [reusableview viewWithTag:103];
  151. [dateLabel setFont:fontSizeData];
  152. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  153. if (_verbose) {
  154. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  155. } else {
  156. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  157. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  158. }
  159. [actionLabel setFont:fontSizeAction];
  160. [actionLabel sizeToFit];
  161. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  162. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  163. actionLabel.textColor = COLOR_BRAND;
  164. if (activity.idActivity == 0)
  165. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  166. else
  167. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  168. }
  169. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  170. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  171. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  172. }
  173. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  174. actionLabel.textColor = [UIColor redColor];
  175. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  176. }
  177. [noteLabel setFont:fontSizeNote];
  178. [noteLabel sizeToFit];
  179. noteLabel.textColor = COLOR_TEXT_ANTHRACITE;
  180. noteLabel.numberOfLines = 0;
  181. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  182. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  183. noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  184. else
  185. noteLabel.text = activity.note;
  186. }
  187. if (kind == UICollectionElementKindSectionFooter) {
  188. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  189. }
  190. return reusableview;
  191. }
  192. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  195. //cell.backgroundColor = [UIColor clearColor];
  196. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  197. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  198. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.fileID]];
  199. return cell;
  200. }
  201. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  202. {
  203. TableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  204. CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activity.account, activity.fileID] context:nil];
  205. if (metadata) {
  206. if (!self.splitViewController.isCollapsed && app.activeMain.detailViewController.isViewLoaded && app.activeMain.detailViewController.view.window)
  207. [app.activeMain.navigationController popToRootViewControllerAnimated:NO];
  208. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  209. [app.activeMain performSegueWithIdentifier:@"segueDetail" sender:metadata];
  210. });
  211. } else {
  212. [app messageNotification:@"_info_" description:@"_activity_file_not_present_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  213. }
  214. }
  215. #pragma --------------------------------------------------------------------------------------------
  216. #pragma mark - ==== Utility ====
  217. #pragma --------------------------------------------------------------------------------------------
  218. + (CGFloat)getLabelHeight:(UILabel*)label width:(int)width
  219. {
  220. CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
  221. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  222. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  223. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  224. NSStringDrawingContext *context = [NSStringDrawingContext new];
  225. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  226. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  227. return size.height;
  228. }
  229. @end