CCSection.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. _allEtag = [[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 setAllEtag: self.allEtag];
  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 activeAccount:(NSString *)activeAccount
  62. {
  63. id dataSection;
  64. long counterSessionDownload = 0;
  65. long counterSessionUpload = 0;
  66. NSMutableArray *copyRecords = [NSMutableArray new];
  67. NSMutableDictionary *dictionaryEtagMetadataForIndexPath = [NSMutableDictionary new];
  68. NSMutableArray *fileInUpload = [NSMutableArray new];
  69. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionDataSourceMetadata new];
  70. /*
  71. Initialize datasource
  72. */
  73. NSInteger numDirectory = 0;
  74. NSInteger numDirectoryFavorite = 0;
  75. BOOL directoryOnTop = [CCUtility getDirectoryOnTop];
  76. NSMutableArray *metadataFilesFavorite = [NSMutableArray new];
  77. // fileName in Upload [PickerFileProvider]
  78. for (tableMetadata* metadata in records) {
  79. if ([metadata.session containsString:@"upload"]) {
  80. [fileInUpload addObject:metadata.fileName];
  81. }
  82. }
  83. for (tableMetadata* metadata in records) {
  84. // remove duplicate in Upload [PickerFileProvider]
  85. if (fileInUpload.count > 0 && ![metadata.session containsString:@"upload"]) {
  86. if ([fileInUpload containsObject:metadata.fileName])
  87. continue;
  88. }
  89. // *** LIST : DO NOT INSERT ***
  90. if (metadata.status == k_metadataStatusHide || [metadata.session isEqualToString:k_upload_session_extension] ) {
  91. continue;
  92. }
  93. if ([listProgressMetadata objectForKey:metadata.fileID] && [groupByField isEqualToString:@"session"]) {
  94. [copyRecords insertObject:metadata atIndex:0];
  95. } else {
  96. if (metadata.directory && directoryOnTop) {
  97. if (metadata.favorite) {
  98. [copyRecords insertObject:metadata atIndex:numDirectoryFavorite++];
  99. numDirectory++;
  100. } else {
  101. [copyRecords insertObject:metadata atIndex:numDirectory++];
  102. }
  103. } else {
  104. if (metadata.favorite && directoryOnTop) {
  105. [metadataFilesFavorite addObject:metadata];
  106. } else {
  107. [copyRecords addObject:metadata];
  108. }
  109. }
  110. }
  111. }
  112. if (directoryOnTop && metadataFilesFavorite.count > 0)
  113. [copyRecords insertObjects:metadataFilesFavorite atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(numDirectoryFavorite, metadataFilesFavorite.count)]]; // Add Favorite files at end of favorite folders
  114. /*
  115. sectionArrayRow
  116. */
  117. for (tableMetadata *metadata in copyRecords) {
  118. // how many download underway (only for groupSession)
  119. if ([metadata.session containsString:@"download"] && [groupByField isEqualToString:@"session"]) {
  120. counterSessionDownload++;
  121. if (counterSessionDownload > k_maxConcurrentOperationDownloadUpload)
  122. continue;
  123. }
  124. // how many upload underway (only for groupSession)
  125. if ([metadata.session containsString:@"upload"] && [groupByField isEqualToString:@"session"]) {
  126. counterSessionUpload++;
  127. if (counterSessionUpload > k_maxConcurrentOperationDownloadUpload)
  128. continue;
  129. }
  130. if ([metadata.session length] > 0 && [groupByField isEqualToString:@"session"]) {
  131. if ([metadata.session containsString:@"wwan"]) dataSection = [@"." stringByAppendingString:metadata.session];
  132. else dataSection = metadata.session;
  133. }
  134. else if ([groupByField isEqualToString:@"none"]) dataSection = @"_none_";
  135. else if ([groupByField isEqualToString:@"date"]) dataSection = [CCUtility datetimeWithOutTime:metadata.date];
  136. else if ([groupByField isEqualToString:@"alphabetic"]) dataSection = [[metadata.fileNameView substringToIndex:1] uppercaseString];
  137. else if ([groupByField isEqualToString:@"typefile"]) dataSection = metadata.typeFile;
  138. if (!dataSection) dataSection = @"_none_";
  139. NSMutableArray *metadatas = [sectionDataSource.sectionArrayRow objectForKey:dataSection];
  140. if (metadatas) {
  141. // ROW ++
  142. [metadatas addObject:metadata.fileID];
  143. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  144. } else {
  145. // SECTION ++
  146. metadatas = [[NSMutableArray alloc] initWithObjects:metadata.fileID, nil];
  147. [sectionDataSource.sectionArrayRow setObject:metadatas forKey:dataSection];
  148. }
  149. if (metadata && [metadata.fileID length] > 0)
  150. [dictionaryEtagMetadataForIndexPath setObject:metadata forKey:metadata.fileID];
  151. }
  152. /*
  153. Sections order
  154. */
  155. BOOL ascending;
  156. if ([groupByField isEqualToString:@"date"]) ascending = NO;
  157. else ascending = YES;
  158. NSArray *sortSections = [[sectionDataSource.sectionArrayRow allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  159. if ([groupByField isEqualToString:@"session"]) {
  160. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"download"]) return NSOrderedAscending;
  161. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"download"]) return NSOrderedDescending;
  162. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString:@"upload"]) return NSOrderedAscending;
  163. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString:@"upload"]) return NSOrderedDescending;
  164. }
  165. // Directory at Top
  166. if ([obj1 isKindOfClass:[NSString class]] && [obj1 containsString: k_metadataTypeFile_directory]) return NSOrderedAscending;
  167. if ([obj2 isKindOfClass:[NSString class]] && [obj2 containsString: k_metadataTypeFile_directory]) return NSOrderedDescending;
  168. if (ascending) return [obj1 compare:obj2];
  169. else return [obj2 compare:obj1];
  170. }];
  171. /*
  172. create allEtag, allRecordsDataSource, fileIDIndexPath, section
  173. */
  174. NSInteger indexSection = 0;
  175. NSInteger indexRow = 0;
  176. for (id section in sortSections) {
  177. [sectionDataSource.sections addObject:section];
  178. NSArray *rows = [sectionDataSource.sectionArrayRow objectForKey:section];
  179. for (NSString *fileID in rows) {
  180. tableMetadata *metadata = [dictionaryEtagMetadataForIndexPath objectForKey:fileID];
  181. if (metadata.fileID) {
  182. [sectionDataSource.allEtag addObject:metadata.fileID];
  183. [sectionDataSource.allRecordsDataSource setObject:metadata forKey:metadata.fileID];
  184. [sectionDataSource.fileIDIndexPath setObject:[NSIndexPath indexPathForRow:indexRow inSection:indexSection] forKey:metadata.fileID];
  185. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  186. sectionDataSource.image++;
  187. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  188. sectionDataSource.video++;
  189. if (metadata.directory)
  190. sectionDataSource.directories++;
  191. else {
  192. sectionDataSource.files++;
  193. sectionDataSource.totalSize = sectionDataSource.totalSize + metadata.size;
  194. }
  195. indexRow++;
  196. }
  197. }
  198. indexSection++;
  199. indexRow = 0;
  200. }
  201. /*
  202. end
  203. */
  204. return sectionDataSource;
  205. }
  206. + (void)removeAllObjectsSectionDataSource:(CCSectionDataSourceMetadata *)sectionDataSource
  207. {
  208. [sectionDataSource.allRecordsDataSource removeAllObjects];
  209. [sectionDataSource.allEtag removeAllObjects];
  210. [sectionDataSource.sections removeAllObjects];
  211. [sectionDataSource.sectionArrayRow removeAllObjects];
  212. [sectionDataSource.fileIDIndexPath removeAllObjects];
  213. sectionDataSource.image = 0;
  214. sectionDataSource.video = 0;
  215. sectionDataSource.directories = 0;
  216. sectionDataSource.files = 0;
  217. sectionDataSource.totalSize = 0;
  218. }
  219. @end
  220. @implementation CCSectionDataSourceActivity
  221. - (id)init {
  222. self = [super init];
  223. _sections = [NSMutableArray new];
  224. _sectionArrayRow = [NSMutableDictionary new];
  225. return self;
  226. }
  227. @end
  228. @implementation CCSectionActivity
  229. + (CCSectionDataSourceActivity *)creataDataSourseSectionActivity:(NSArray *)records activeAccount:(NSString *)activeAccount
  230. {
  231. CCSectionDataSourceActivity *sectionDataSource = [CCSectionDataSourceActivity new];
  232. NSDate *oldDate = [NSDate date];
  233. for (tableActivity *record in records) {
  234. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:record.date];
  235. NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
  236. if ([oldDate compare:date] != NSOrderedSame) {
  237. [sectionDataSource.sections addObject:date];
  238. oldDate = date;
  239. }
  240. NSMutableArray *activities = [sectionDataSource.sectionArrayRow objectForKey:date];
  241. if (activities) {
  242. // ROW ++
  243. [activities addObject:record];
  244. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  245. } else {
  246. // SECTION ++
  247. activities = [[NSMutableArray alloc] initWithObjects:record, nil];
  248. [sectionDataSource.sectionArrayRow setObject:activities forKey:date];
  249. }
  250. }
  251. return sectionDataSource;
  252. }
  253. @end