CCSection.m 11 KB

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