Rename sortTorrentsAndIncludeQueueOrder and nonEmptyComponentsSeparatedByCharactersInSet (#3051)

Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
Antoine Cœur 2022-05-15 03:00:55 +08:00 committed by GitHub
parent 82bede40d1
commit 60254710b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 74 deletions

View File

@ -122,7 +122,7 @@ typedef NS_ENUM(unsigned int, addType) { //
- (void)applyFilter;
- (void)sortTorrents:(BOOL)includeQueueOrder;
- (void)sortTorrentsAndIncludeQueueOrder:(BOOL)includeQueueOrder;
- (void)sortTorrentsCallUpdates:(BOOL)callUpdates includeQueueOrder:(BOOL)includeQueueOrder;
- (void)rearrangeTorrentTableArray:(NSMutableArray*)rearrangeArray
forParent:(id)parent

View File

@ -2219,7 +2219,7 @@ static void removeKeRangerRansomware()
{
if (self.fWindow.visible)
{
[self sortTorrents:NO];
[self sortTorrentsAndIncludeQueueOrder:NO];
[self.fStatusBar updateWithDownload:dlRate upload:ulRate];
@ -2303,13 +2303,42 @@ static void removeKeRangerRansomware()
else if (notification.activationType == NSUserNotificationActivationTypeContentsClicked)
{
Torrent* torrent = [self torrentForHash:notification.userInfo[@"Hash"]];
if (torrent)
if (!torrent)
{
//select in the table - first see if it's already shown
NSInteger row = [self.fTableView rowForItem:torrent];
return;
}
//select in the table - first see if it's already shown
NSInteger row = [self.fTableView rowForItem:torrent];
if (row == -1)
{
//if it's not shown, see if it's in a collapsed row
if ([self.fDefaults boolForKey:@"SortByGroup"])
{
__block TorrentGroup* parent = nil;
[self.fDisplayedTorrents enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(TorrentGroup* group, NSUInteger idx, BOOL* stop) {
if ([group.torrents containsObject:torrent])
{
parent = group;
*stop = YES;
}
}];
if (parent)
{
[[self.fTableView animator] expandItem:parent];
row = [self.fTableView rowForItem:torrent];
}
}
if (row == -1)
{
//if it's not shown, see if it's in a collapsed row
//not found - must be filtering
NSAssert([self.fDefaults boolForKey:@"FilterBar"], @"expected the filter to be enabled");
[self.fFilterBar reset:YES];
row = [self.fTableView rowForItem:torrent];
//if it's not shown, it has to be in a collapsed row...again
if ([self.fDefaults boolForKey:@"SortByGroup"])
{
__block TorrentGroup* parent = nil;
@ -2327,41 +2356,13 @@ static void removeKeRangerRansomware()
row = [self.fTableView rowForItem:torrent];
}
}
if (row == -1)
{
//not found - must be filtering
NSAssert([self.fDefaults boolForKey:@"FilterBar"], @"expected the filter to be enabled");
[self.fFilterBar reset:YES];
row = [self.fTableView rowForItem:torrent];
//if it's not shown, it has to be in a collapsed row...again
if ([self.fDefaults boolForKey:@"SortByGroup"])
{
__block TorrentGroup* parent = nil;
[self.fDisplayedTorrents enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(TorrentGroup* group, NSUInteger idx, BOOL* stop) {
if ([group.torrents containsObject:torrent])
{
parent = group;
*stop = YES;
}
}];
if (parent)
{
[[self.fTableView animator] expandItem:parent];
row = [self.fTableView rowForItem:torrent];
}
}
}
}
NSAssert1(row != -1, @"expected a row to be found for torrent %@", torrent);
[self showMainWindow:nil];
[self.fTableView selectAndScrollToRow:row];
}
NSAssert1(row != -1, @"expected a row to be found for torrent %@", torrent);
[self showMainWindow:nil];
[self.fTableView selectAndScrollToRow:row];
}
}
@ -2542,7 +2543,7 @@ static void removeKeRangerRansomware()
[self.fDefaults setObject:sortType forKey:@"Sort"];
[self sortTorrents:YES];
[self sortTorrentsAndIncludeQueueOrder:YES];
}
- (void)setSortByGroup:(id)sender
@ -2559,11 +2560,11 @@ static void removeKeRangerRansomware()
if (setReverse != [self.fDefaults boolForKey:@"SortReverse"])
{
[self.fDefaults setBool:setReverse forKey:@"SortReverse"];
[self sortTorrents:NO];
[self sortTorrentsAndIncludeQueueOrder:NO];
}
}
- (void)sortTorrents:(BOOL)includeQueueOrder
- (void)sortTorrentsAndIncludeQueueOrder:(BOOL)includeQueueOrder
{
//actually sort
[self sortTorrentsCallUpdates:YES includeQueueOrder:includeQueueOrder];
@ -5460,7 +5461,7 @@ static void removeKeRangerRansomware()
NSArray* descriptors = @[ descriptor ];
[self.fTorrents sortUsingDescriptors:descriptors];
[self sortTorrents:YES];
[self sortTorrentsAndIncludeQueueOrder:YES];
}
@end

View File

@ -80,7 +80,7 @@ typedef NS_ENUM(unsigned int, filePriorityMenuTag) { //
- (void)setFilterText:(NSString*)text
{
NSArray* components = [text betterComponentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
NSArray* components = [text nonEmptyComponentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (!components || components.count == 0)
{
text = nil;
@ -106,8 +106,8 @@ typedef NS_ENUM(unsigned int, filePriorityMenuTag) { //
__block BOOL filter = NO;
if (components)
{
[components enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
if ([item.name rangeOfString:(NSString*)obj options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location == NSNotFound)
[components enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSString* obj, NSUInteger idx, BOOL* stop) {
if ([item.name rangeOfString:obj options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location == NSNotFound)
{
filter = YES;
*stop = YES;

View File

@ -322,7 +322,7 @@
- (NSArray<NSString*>*)searchStrings
{
return [self.fSearchField.stringValue betterComponentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
return [self.fSearchField.stringValue nonEmptyComponentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
}
- (void)setCountAll:(NSUInteger)all

View File

@ -24,6 +24,6 @@
- (NSComparisonResult)compareNumeric:(NSString*)string;
// like componentsSeparatedByCharactersInSet:, but excludes blank values
- (NSArray*)betterComponentsSeparatedByCharactersInSet:(NSCharacterSet*)separators;
- (NSArray<NSString*>*)nonEmptyComponentsSeparatedByCharactersInSet:(NSCharacterSet*)separators;
@end

View File

@ -34,13 +34,15 @@
return [NSString localizedStringWithFormat:@"%lu", value];
}
#warning should we take long long instead?
// Maximum supported localization is 9.22 EB, which is the maximum supported filesystem size by macOS, 8 EiB.
// https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/APFS_Guide/VolumeFormatComparison/VolumeFormatComparison.html
+ (NSString*)stringForFileSize:(uint64_t)size
{
return [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
}
#warning should we take long long instead?
// Maximum supported localization is 9.22 EB, which is the maximum supported filesystem size by macOS, 8 EiB.
// https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/APFS_Guide/VolumeFormatComparison/VolumeFormatComparison.html
+ (NSString*)stringForFilePartialSize:(uint64_t)partialSize fullSize:(uint64_t)fullSize
{
NSByteCountFormatter* fileSizeFormatter = [[NSByteCountFormatter alloc] init];
@ -128,34 +130,16 @@
return [self compare:string options:comparisonOptions range:NSMakeRange(0, self.length) locale:NSLocale.currentLocale];
}
- (NSArray<NSString*>*)betterComponentsSeparatedByCharactersInSet:(NSCharacterSet*)separators
- (NSArray<NSString*>*)nonEmptyComponentsSeparatedByCharactersInSet:(NSCharacterSet*)separators
{
NSMutableArray* components = [NSMutableArray array];
NSCharacterSet* includedCharSet = separators.invertedSet;
NSUInteger index = 0;
NSUInteger const fullLength = self.length;
do
NSMutableArray<NSString*>* components = [NSMutableArray array];
for (NSString* evaluatedObject in [self componentsSeparatedByCharactersInSet:separators])
{
NSUInteger const start = [self rangeOfCharacterFromSet:includedCharSet options:0 range:NSMakeRange(index, fullLength - index)]
.location;
if (start == NSNotFound)
if (evaluatedObject.length > 0)
{
break;
[components addObject:evaluatedObject];
}
NSRange const endRange = [self rangeOfCharacterFromSet:separators options:0 range:NSMakeRange(start, fullLength - start)];
if (endRange.location == NSNotFound)
{
[components addObject:[self substringFromIndex:start]];
break;
}
[components addObject:[self substringWithRange:NSMakeRange(start, endRange.location - start)]];
index = NSMaxRange(endRange);
} while (YES);
}
return components;
}