CCSection.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // CCSection.m
  3. // Nextcloud iOS
  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 "CCExifGeo.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCSectionDataSourceMetadata
  27. - (id)init {
  28. self = [super init];
  29. _allRecordsDataSource = [[NSMutableDictionary alloc] init];
  30. _allFileID = [[NSMutableArray alloc] init];
  31. _sections = [[NSMutableArray alloc] init];
  32. _sectionArrayRow = [[NSMutableDictionary alloc] init];
  33. _fileIDIndexPath = [[NSMutableDictionary alloc] init];
  34. _image = 0;
  35. _video = 0;
  36. _directories = 0;
  37. _files = 0;
  38. _totalSize = 0;
  39. return self;
  40. }
  41. - (id)copyWithZone: (NSZone *) zone
  42. {
  43. CCSectionDataSourceMetadata *sectionDataSourceMetadata = [[CCSectionDataSourceMetadata allocWithZone: zone] init];
  44. [sectionDataSourceMetadata setAllRecordsDataSource: self.allRecordsDataSource];
  45. [sectionDataSourceMetadata setAllFileID: self.allFileID];
  46. [sectionDataSourceMetadata setSections: self.sections];
  47. [sectionDataSourceMetadata setSectionArrayRow: self.sectionArrayRow];
  48. [sectionDataSourceMetadata setFileIDIndexPath: self.fileIDIndexPath];
  49. [sectionDataSourceMetadata setVideo: self.video];
  50. [sectionDataSourceMetadata setImage: self.image];
  51. [sectionDataSourceMetadata setDirectories: self.directories];
  52. [sectionDataSourceMetadata setFiles: self.files];
  53. [sectionDataSourceMetadata setTotalSize: self.totalSize];
  54. return sectionDataSourceMetadata;
  55. }
  56. @end
  57. @implementation CCSectionMetadata
  58. //
  59. // orderByField : nil, date, typeFile
  60. //
  61. + (CCSectionDataSourceMetadata *)creataDataSourseSectionMetadata:(NSArray *)records listProgressMetadata:(NSMutableDictionary *)listProgressMetadata groupByField:(NSString *)groupByField fileIDHide:(NSArray *)fileIDHide activeAccount:(NSString *)activeAccount
  62. {
  63. id dataSection;
  64. NSMutableArray *copyRecords = [NSMutableArray new];
  65. NSMutableDictionary *dictionaryEtagMetadataForIndexPath = [NSMutableDictionary new];
  66. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionDataSourceMetadata new];
  67. /*
  68. Initialize datasource
  69. */
  70. NSInteger numDirectory = 0;
  71. NSInteger numDirectoryFavorite = 0;
  72. BOOL directoryOnTop = [CCUtility getDirectoryOnTop];
  73. NSMutableArray *metadataFilesFavorite = [NSMutableArray new];
  74. for (tableMetadata* metadata in records) {
  75. // *** LIST : DO NOT INSERT ***
  76. if (metadata.status == k_metadataStatusHide || [fileIDHide containsObject: metadata.fileID]) {
  77. continue;
  78. }
  79. if ([listProgressMetadata objectForKey:metadata.fileID] && [groupByField isEqualToString:@"session"]) {
  80. [copyRecords insertObject:metadata atIndex:0];
  81. } else {
  82. if (metadata.directory && directoryOnTop) {
  83. if (metadata.favorite) {
  84. [copyRecords insertObject:metadata atIndex:numDirectoryFavorite++];
  85. numDirectory++;
  86. } else {
  87. [copyRecords insertObject:metadata atIndex:numDirectory++];
  88. }
  89. } else {
  90. if (metadata.favorite && directoryOnTop) {
  91. [metadataFilesFavorite addObject:metadata];
  92. } else {
  93. [copyRecords addObject:metadata];
  94. }
  95. }
  96. }
  97. }
  98. if (directoryOnTop && metadataFilesFavorite.count > 0)
  99. [copyRecords insertObjects:metadataFilesFavorite atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(numDirectoryFavorite, metadataFilesFavorite.count)]]; // Add Favorite files at end of favorite folders
  100. /*
  101. sectionArrayRow
  102. */
  103. // long counterSessionDownload = 0;
  104. // long counterSessionUpload = 0;
  105. for (tableMetadata *metadata in copyRecords) {
  106. /*
  107. // how many download underway (only for groupSession)
  108. if ([metadata.session containsString:@"download"] && [groupByField isEqualToString:@"session"]) {
  109. counterSessionDownload++;
  110. if (counterSessionDownload > 10)
  111. continue;
  112. }
  113. // how many upload underway (only for groupSession)
  114. if ([metadata.session containsString:@"upload"] && [groupByField isEqualToString:@"session"]) {
  115. counterSessionUpload++;
  116. if (counterSessionUpload > 10)
  117. continue;
  118. }
  119. */
  120. if ([metadata.session length] > 0 && [groupByField isEqualToString:@"session"]) {
  121. if ([metadata.session containsString:@"wwan"]) dataSection = [@"." stringByAppendingString:metadata.session];
  122. else dataSection = metadata.session;
  123. }
  124. else if ([groupByField isEqualToString:@"none"]) dataSection = @"_none_";
  125. else if ([groupByField isEqualToString:@"date"]) dataSection = [CCUtility datetimeWithOutTime:metadata.date];
  126. else if ([groupByField isEqualToString:@"alphabetic"]) dataSection = [[metadata.fileNameView substringToIndex:1] uppercaseString];
  127. else if ([groupByField isEqualToString:@"typefile"]) dataSection = metadata.typeFile;
  128. if (!dataSection) dataSection = @"_none_";
  129. NSMutableArray *metadatas = [sectionDataSource.sectionArrayRow objectForKey:dataSection];
  130. if (metadatas) {
  131. // ROW ++
  132. [metadatas addObject:metadata.fileID];
  133. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  134. } else {
  135. // SECTION ++
  136. metadatas = [[NSMutableArray alloc] initWithObjects:metadata.fileID, nil];
  137. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  138. }
  139. if (metadata && [metadata.fileID length] > 0)
  140. [dictionaryEtagMetadataForIndexPath setObject:metadata forKey:metadata.fileID];
  141. }
  142. /*
  143. Sections order
  144. */
  145. BOOL ascending;
  146. if ([groupByField isEqualToString:@"date"]) ascending = NO;
  147. else ascending = YES;
  148. NSArray *sortSections = [[sectionDataSource.sectionArrayRow allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  149. if ([groupByField isEqualToString:@"session"]) {
  150. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"download"]) return NSOrderedAscending;
  151. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"download"]) return NSOrderedDescending;
  152. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"upload"]) return NSOrderedAscending;
  153. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"upload"]) return NSOrderedDescending;
  154. }
  155. // Directory at Top
  156. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString: k_metadataTypeFile_directory]) return NSOrderedAscending;
  157. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString: k_metadataTypeFile_directory]) return NSOrderedDescending;
  158. if (ascending) return [obj1 compare:obj2];
  159. else return [obj2 compare:obj1];
  160. }];
  161. /*
  162. create allEtag, allRecordsDataSource, fileIDIndexPath, section
  163. */
  164. NSInteger indexSection = 0;
  165. NSInteger indexRow = 0;
  166. for (id section in sortSections) {
  167. [sectionDataSource.sections addObject:section];
  168. NSArray *rows = [sectionDataSource.sectionArrayRow objectForKey:section];
  169. for (NSString *fileID in rows) {
  170. tableMetadata *metadata = [dictionaryEtagMetadataForIndexPath objectForKey:fileID];
  171. if (metadata.fileID) {
  172. [sectionDataSource.allFileID addObject:metadata.fileID];
  173. [sectionDataSource.allRecordsDataSource setObject:metadata forKey:metadata.fileID];
  174. [sectionDataSource.fileIDIndexPath setObject:[NSIndexPath indexPathForRow:indexRow inSection:indexSection] forKey:metadata.fileID];
  175. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  176. sectionDataSource.image++;
  177. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  178. sectionDataSource.video++;
  179. if (metadata.directory)
  180. sectionDataSource.directories++;
  181. else {
  182. sectionDataSource.files++;
  183. sectionDataSource.totalSize = sectionDataSource.totalSize + metadata.size;
  184. }
  185. indexRow++;
  186. }
  187. }
  188. indexSection++;
  189. indexRow = 0;
  190. }
  191. /*
  192. end
  193. */
  194. return sectionDataSource;
  195. }
  196. + (CCSectionDataSourceMetadata *)removeObjectsSectionDataSource:(CCSectionDataSourceMetadata *)sectionDataSource forFileID:(NSString *)fileID
  197. {
  198. return sectionDataSource;
  199. }
  200. + (void)removeAllObjectsSectionDataSource:(CCSectionDataSourceMetadata *)sectionDataSource
  201. {
  202. [sectionDataSource.allRecordsDataSource removeAllObjects];
  203. [sectionDataSource.allFileID removeAllObjects];
  204. [sectionDataSource.sections removeAllObjects];
  205. [sectionDataSource.sectionArrayRow removeAllObjects];
  206. [sectionDataSource.fileIDIndexPath removeAllObjects];
  207. sectionDataSource.image = 0;
  208. sectionDataSource.video = 0;
  209. sectionDataSource.directories = 0;
  210. sectionDataSource.files = 0;
  211. sectionDataSource.totalSize = 0;
  212. }
  213. @end
  214. @implementation CCSectionDataSourceActivity
  215. - (id)init {
  216. self = [super init];
  217. _sections = [NSMutableArray new];
  218. _sectionArrayRow = [NSMutableDictionary new];
  219. return self;
  220. }
  221. @end
  222. @implementation CCSectionActivity
  223. + (CCSectionDataSourceActivity *)creataDataSourseSectionActivity:(NSArray *)records activeAccount:(NSString *)activeAccount
  224. {
  225. CCSectionDataSourceActivity *sectionDataSource = [CCSectionDataSourceActivity new];
  226. NSDate *oldDate = [NSDate date];
  227. for (tableActivity *record in records) {
  228. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:record.date];
  229. NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
  230. if ([oldDate compare:date] != NSOrderedSame) {
  231. [sectionDataSource.sections addObject:date];
  232. oldDate = date;
  233. }
  234. NSMutableArray *activities = [sectionDataSource.sectionArrayRow objectForKey:date];
  235. if (activities) {
  236. // ROW ++
  237. [activities addObject:record];
  238. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  239. } else {
  240. // SECTION ++
  241. activities = [[NSMutableArray alloc] initWithObjects:record, nil];
  242. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  243. }
  244. }
  245. return sectionDataSource;
  246. }
  247. @end