CCActivity.m 13 KB

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