CCSection.m 10 KB

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