CCActivity.m 13 KB

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