CCSection.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // CCSection.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/02/16.
  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 "CCSection.h"
  24. #import "CCMetadata.h"
  25. #import "CCCoreData.h"
  26. #import "CCExifGeo.h"
  27. #import "NCBridgeSwift.h"
  28. @implementation CCSectionDataSourceMetadata
  29. - (id)init {
  30. self = [super init];
  31. _allRecordsDataSource = [[NSMutableDictionary alloc] init];
  32. _allFileID = [[NSMutableArray alloc] init];
  33. _sections = [[NSMutableArray alloc] init];
  34. _sectionArrayRow = [[NSMutableDictionary alloc] init];
  35. _fileIDIndexPath = [[NSMutableDictionary alloc] init];
  36. _image = 0;
  37. _video = 0;
  38. _directories = 0;
  39. _files = 0;
  40. _totalSize = 0;
  41. return self;
  42. }
  43. @end
  44. @implementation CCSectionMetadata
  45. //
  46. // orderByField : nil, date, typeFile
  47. //
  48. + (CCSectionDataSourceMetadata *)creataDataSourseSectionMetadata:(NSArray *)records listProgressMetadata:(NSMutableDictionary *)listProgressMetadata groupByField:(NSString *)groupByField replaceDateToExifDate:(BOOL)replaceDateToExifDate activeAccount:(NSString *)activeAccount
  49. {
  50. id dataSection;
  51. long counterSessionDownload = 0;
  52. long counterSessionUpload = 0;
  53. NSMutableArray *copyRecords = [[NSMutableArray alloc] init];
  54. NSMutableDictionary *dictionaryFileIDMetadataForIndexPath = [[NSMutableDictionary alloc] init];
  55. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionDataSourceMetadata new];
  56. /*
  57. Initialize datasource
  58. */
  59. NSInteger numDirectory = 0;
  60. BOOL directoryOnTop = [CCUtility getDirectoryOnTop];
  61. for (id record in records) {
  62. CCMetadata *metadata;
  63. // verify type of class
  64. if ([record isKindOfClass:[TableMetadata class]])
  65. metadata = [CCCoreData insertEntityInMetadata:record];
  66. else
  67. metadata = (CCMetadata *)record;
  68. // if exists replace date with exif date
  69. if (replaceDateToExifDate) {
  70. TableLocalFile *localFile = [CCCoreData getLocalFileWithFileID:metadata.fileID activeAccount:activeAccount];
  71. if (localFile.exifDate)
  72. metadata.date = localFile.exifDate;
  73. }
  74. if ([listProgressMetadata objectForKey:metadata.fileID] && [groupByField isEqualToString:@"session"]) {
  75. [copyRecords insertObject:metadata atIndex:0];
  76. } else {
  77. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_directory] && directoryOnTop) {
  78. [copyRecords insertObject:metadata atIndex:numDirectory++];
  79. } else {
  80. [copyRecords addObject:metadata];
  81. }
  82. }
  83. }
  84. /*
  85. sectionArrayRow
  86. */
  87. for (CCMetadata *metadata in copyRecords) {
  88. // how many download underway (only for groupSession)
  89. if ([metadata.session containsString:@"download"] && [groupByField isEqualToString:@"session"]) {
  90. counterSessionDownload++;
  91. if (counterSessionDownload > k_maxConcurrentOperationDownloadUpload)
  92. continue;
  93. }
  94. // how many upload underway (only for groupSession)
  95. if ([metadata.session containsString:@"upload"] && [groupByField isEqualToString:@"session"]) {
  96. counterSessionUpload++;
  97. if (counterSessionUpload > k_maxConcurrentOperationDownloadUpload)
  98. continue;
  99. }
  100. if ([metadata.session length] > 0 && [groupByField isEqualToString:@"session"]) {
  101. if ([metadata.session containsString:@"wwan"]) dataSection = [@"." stringByAppendingString:metadata.session];
  102. else dataSection = metadata.session;
  103. }
  104. else if ([groupByField isEqualToString:@"none"]) dataSection = @"_none_";
  105. else if ([groupByField isEqualToString:@"date"]) dataSection = [CCUtility datetimeWithOutTime:metadata.date];
  106. else if ([groupByField isEqualToString:@"alphabetic"]) dataSection = [[metadata.fileNamePrint substringToIndex:1] uppercaseString];
  107. else if ([groupByField isEqualToString:@"typefile"]) dataSection = metadata.typeFile;
  108. if (!dataSection) dataSection = @"_none_";
  109. NSMutableArray *metadatas = [sectionDataSource.sectionArrayRow objectForKey:dataSection];
  110. if (metadatas) {
  111. // ROW ++
  112. [metadatas addObject:metadata.fileID];
  113. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  114. } else {
  115. // SECTION ++
  116. metadatas = [[NSMutableArray alloc] initWithObjects:metadata.fileID, nil];
  117. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  118. }
  119. if (metadata && [metadata.fileID length] > 0)
  120. [dictionaryFileIDMetadataForIndexPath setObject:metadata forKey:metadata.fileID];
  121. }
  122. /*
  123. Sections order
  124. */
  125. BOOL ascending;
  126. if (replaceDateToExifDate || [groupByField isEqualToString:@"date"]) ascending = NO;
  127. else ascending = YES;
  128. NSArray *sortSections = [[sectionDataSource.sectionArrayRow allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  129. if ([groupByField isEqualToString:@"session"]) {
  130. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"download"]) return NSOrderedAscending;
  131. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"download"]) return NSOrderedDescending;
  132. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"upload"]) return NSOrderedAscending;
  133. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"upload"]) return NSOrderedDescending;
  134. }
  135. // Directory at Top
  136. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString: k_metadataTypeFile_directory]) return NSOrderedAscending;
  137. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString: k_metadataTypeFile_directory]) return NSOrderedDescending;
  138. if (ascending) return [obj1 compare:obj2];
  139. else return [obj2 compare:obj1];
  140. }];
  141. /*
  142. create allFileID, allRecordsDataSource, fileIDIndexPath, section
  143. */
  144. NSInteger indexSection = 0;
  145. NSInteger indexRow = 0;
  146. for (id section in sortSections) {
  147. [sectionDataSource.sections addObject:section];
  148. NSArray *rows = [sectionDataSource.sectionArrayRow objectForKey:section];
  149. for (NSString *fileID in rows) {
  150. CCMetadata *metadata = [dictionaryFileIDMetadataForIndexPath objectForKey:fileID];
  151. if (metadata.fileID) {
  152. [sectionDataSource.allFileID addObject:metadata.fileID];
  153. [sectionDataSource.allRecordsDataSource setObject:metadata forKey:metadata.fileID];
  154. [sectionDataSource.fileIDIndexPath setObject:[NSIndexPath indexPathForRow:indexRow inSection:indexSection] forKey:metadata.fileID];
  155. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  156. sectionDataSource.image++;
  157. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  158. sectionDataSource.video++;
  159. if (metadata.directory)
  160. sectionDataSource.directories++;
  161. else {
  162. sectionDataSource.files++;
  163. sectionDataSource.totalSize = sectionDataSource.totalSize + metadata.size;
  164. }
  165. indexRow++;
  166. }
  167. }
  168. indexSection++;
  169. indexRow = 0;
  170. }
  171. /*
  172. end
  173. */
  174. return sectionDataSource;
  175. }
  176. + (void)removeAllObjectsSectionDataSource:(CCSectionDataSourceMetadata *)sectionDataSource
  177. {
  178. [sectionDataSource.allRecordsDataSource removeAllObjects];
  179. [sectionDataSource.allFileID removeAllObjects];
  180. [sectionDataSource.sections removeAllObjects];
  181. [sectionDataSource.sectionArrayRow removeAllObjects];
  182. [sectionDataSource.fileIDIndexPath removeAllObjects];
  183. sectionDataSource.image = 0;
  184. sectionDataSource.video = 0;
  185. sectionDataSource.directories = 0;
  186. sectionDataSource.files = 0;
  187. sectionDataSource.totalSize = 0;
  188. }
  189. @end
  190. @implementation CCSectionDataSourceActivity
  191. - (id)init {
  192. self = [super init];
  193. _sections = [NSMutableArray new];
  194. _sectionArrayRow = [NSMutableDictionary new];
  195. return self;
  196. }
  197. @end
  198. @implementation CCSectionActivity
  199. + (CCSectionDataSourceActivity *)creataDataSourseSectionActivity:(NSArray *)records activeAccount:(NSString *)activeAccount
  200. {
  201. CCSectionDataSourceActivity *sectionDataSource = [CCSectionDataSourceActivity new];
  202. NSDate *oldDate = [NSDate date];
  203. for (tableActivity *record in records) {
  204. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:record.date];
  205. NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
  206. if ([oldDate compare:date] != NSOrderedSame) {
  207. [sectionDataSource.sections addObject:date];
  208. oldDate = date;
  209. }
  210. NSMutableArray *activities = [sectionDataSource.sectionArrayRow objectForKey:date];
  211. if (activities) {
  212. // ROW ++
  213. [activities addObject:record];
  214. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  215. } else {
  216. // SECTION ++
  217. activities = [[NSMutableArray alloc] initWithObjects:record, nil];
  218. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  219. }
  220. }
  221. return sectionDataSource;
  222. }
  223. @end