CCActivity.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // CCActivity.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/04/17.
  6. // Copyright (c) 2017 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. #import "NCBridgeSwift.h"
  27. #define fontSizeData [UIFont boldSystemFontOfSize:15]
  28. #define fontSizeAction [UIFont systemFontOfSize:14]
  29. #define fontSizeNote [UIFont systemFontOfSize:14]
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  46. }
  47. return self;
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. self.collectionView.emptyDataSetSource = self;
  52. self.collectionView.emptyDataSetDelegate = self;
  53. self.collectionView.delegate = self;
  54. _verbose = [CCUtility getActivityVerboseHigh];
  55. _sectionDataSource = [NSArray new];
  56. if ([NCBrandOptions sharedInstance].use_recent_activity_title == true)
  57. self.title = NSLocalizedString(@"_recent_activity_", nil);
  58. else
  59. self.title = NSLocalizedString(@"_activity_", nil);
  60. [self reloadDatasource];
  61. }
  62. // Apparirà
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [super viewWillAppear:animated];
  66. _verbose = [CCUtility getActivityVerboseHigh];
  67. // Color
  68. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  69. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  70. // Plus Button
  71. [app plusButtonVisibile:true];
  72. }
  73. // E' arrivato
  74. - (void)viewDidAppear:(BOOL)animated
  75. {
  76. [super viewDidAppear:animated];
  77. [self reloadDatasource];
  78. }
  79. - (void)changeTheming
  80. {
  81. if (self.isViewLoaded && self.view.window)
  82. [app changeTheming:self];
  83. }
  84. - (void)didReceiveMemoryWarning {
  85. [super didReceiveMemoryWarning];
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ==== DZNEmptyDataSetSource ====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  91. {
  92. if([_sectionDataSource count] > 0)
  93. return NO;
  94. else
  95. return YES;
  96. }
  97. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  98. {
  99. return [UIColor whiteColor];
  100. }
  101. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  102. {
  103. return [UIImage imageNamed:@"activityNoRecord"];
  104. }
  105. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  106. {
  107. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_activity_", nil)];
  108. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  109. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark - ==== Datasource ====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)reloadDatasource
  115. {
  116. // test
  117. if (app.activeAccount.length == 0)
  118. return;
  119. NSPredicate *predicate;
  120. NSDate *sixDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-k_daysOfActivity toDate:[NSDate date] options:0];
  121. if (_verbose)
  122. predicate = [NSPredicate predicateWithFormat:@"((account == %@) || (account == '')) AND (date > %@)", app.activeAccount, sixDaysAgo];
  123. else
  124. predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (verbose == %lu) AND (date > %@)", app.activeAccount, k_activityVerboseDefault, sixDaysAgo];
  125. _sectionDataSource = [[NCManageDatabase sharedInstance] getActivityWithPredicate:predicate];
  126. [self reloadCollection];
  127. }
  128. - (void)reloadCollection
  129. {
  130. NSDate *dateActivity;
  131. if ([_sectionDataSource count] > 0)
  132. dateActivity = ((tableActivity *)[_sectionDataSource objectAtIndex:0]).date;
  133. if ([dateActivity compare:_storeDateFirstActivity] == NSOrderedDescending || _storeDateFirstActivity == nil || dateActivity == nil) {
  134. _storeDateFirstActivity = dateActivity;
  135. [self.collectionView reloadData];
  136. }
  137. }
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark - ==== Table ====
  140. #pragma --------------------------------------------------------------------------------------------
  141. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  142. {
  143. return [_sectionDataSource count];
  144. }
  145. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  146. {
  147. tableActivity *activity = [_sectionDataSource objectAtIndex:section];
  148. if ([activity.action isEqual: k_activityDebugActionDownload] || [activity.action isEqual: k_activityDebugActionUpload]) {
  149. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.etag]])
  150. return 1;
  151. else
  152. return 0;
  153. }
  154. return 0;
  155. }
  156. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  157. {
  158. tableActivity *activity = [_sectionDataSource objectAtIndex:section];
  159. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width - 40, CGFLOAT_MAX)];
  160. label.numberOfLines = 0;
  161. label.lineBreakMode = NSLineBreakByWordWrapping;
  162. [label sizeToFit];
  163. // Action
  164. [label setFont:fontSizeAction];
  165. label.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  166. int heightAction = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  167. // Note
  168. [label setFont:fontSizeNote];
  169. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  170. label.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  171. else
  172. label.text = activity.note;
  173. int heightNote = [[self class] getLabelHeight:label width:self.collectionView.frame.size.width];
  174. int heightView = 40 + heightAction + heightNote + 17;
  175. return CGSizeMake(collectionView.frame.size.width, heightView);
  176. }
  177. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  178. {
  179. UICollectionReusableView *reusableview;
  180. if (kind == UICollectionElementKindSectionHeader) {
  181. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  182. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  183. UILabel *dateLabel = (UILabel *)[reusableview viewWithTag:100];
  184. UILabel *actionLabel = (UILabel *)[reusableview viewWithTag:101];
  185. UILabel *noteLabel = (UILabel *)[reusableview viewWithTag:102];
  186. UIImageView *typeImage = (UIImageView *) [reusableview viewWithTag:103];
  187. [dateLabel setFont:fontSizeData];
  188. dateLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
  189. if (_verbose) {
  190. dateLabel.text = [NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  191. } else {
  192. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:activity.date];
  193. dateLabel.text = [CCUtility getTitleSectionDate:[[NSCalendar currentCalendar] dateFromComponents:comps]];
  194. }
  195. [actionLabel setFont:fontSizeAction];
  196. [actionLabel sizeToFit];
  197. actionLabel.text = [NSString stringWithFormat:@"%@ %@", activity.action, activity.file];
  198. if ([activity.type isEqualToString:k_activityTypeInfo]) {
  199. actionLabel.textColor = [NCBrandColor sharedInstance].brand;
  200. if (activity.idActivity == 0)
  201. typeImage.image = [UIImage imageNamed:@"activityTypeInfo"];
  202. else
  203. typeImage.image = [UIImage imageNamed:@"activityTypeInfoServer"];
  204. }
  205. if ([activity.type isEqualToString:k_activityTypeSuccess]) {
  206. actionLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:187.0/255.0 blue:57.0/255.0 alpha:1.0];;
  207. typeImage.image = [UIImage imageNamed:@"activityTypeSuccess"];
  208. }
  209. if ([activity.type isEqualToString:k_activityTypeFailure]) {
  210. actionLabel.textColor = [UIColor redColor];
  211. typeImage.image = [UIImage imageNamed:@"activityTypeFailure"];
  212. }
  213. [noteLabel setFont:fontSizeNote];
  214. [noteLabel sizeToFit];
  215. noteLabel.textColor = [UIColor blackColor];
  216. noteLabel.numberOfLines = 0;
  217. noteLabel.lineBreakMode = NSLineBreakByWordWrapping;
  218. if (_verbose && activity.idActivity == 0 && [activity.selector length] > 0)
  219. noteLabel.text = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  220. else
  221. noteLabel.text = activity.note;
  222. }
  223. if (kind == UICollectionElementKindSectionFooter) {
  224. reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  225. }
  226. return reusableview;
  227. }
  228. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  229. {
  230. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  231. //cell.backgroundColor = [UIColor clearColor];
  232. UIImageView *imageView = (UIImageView *)[cell viewWithTag:104];
  233. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  234. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, activity.etag]];
  235. return cell;
  236. }
  237. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. tableActivity *activity = [_sectionDataSource objectAtIndex:indexPath.section];
  240. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"etag = %@", activity.etag]];
  241. if (metadata) {
  242. if (!self.splitViewController.isCollapsed && app.activeMain.detailViewController.isViewLoaded && app.activeMain.detailViewController.view.window)
  243. [app.activeMain.navigationController popToRootViewControllerAnimated:NO];
  244. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  245. [app.activeMain performSegueWithIdentifier:@"segueDetail" sender:metadata];
  246. });
  247. } else {
  248. [app messageNotification:@"_info_" description:@"_activity_file_not_present_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  249. }
  250. }
  251. #pragma --------------------------------------------------------------------------------------------
  252. #pragma mark - ==== Utility ====
  253. #pragma --------------------------------------------------------------------------------------------
  254. + (CGFloat)getLabelHeight:(UILabel*)label width:(int)width
  255. {
  256. CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
  257. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  258. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  259. NSDictionary *attributes = @{NSFontAttributeName : label.font, NSParagraphStyleAttributeName: paragraph};
  260. NSStringDrawingContext *context = [NSStringDrawingContext new];
  261. CGSize boundingBox = [label.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context].size;
  262. CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  263. return size.height;
  264. }
  265. @end