CCSection.m 9.8 KB

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