CCSection.m 13 KB

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