Changeset 13132


Ignore:
Timestamp:
Jan 7, 2012, 3:46:42 PM (10 years ago)
Author:
livings124
Message:

#4696 Step one of getting the main table to animate for everything besides the already-added sorting. This also contains improvements to the sorting code by sorting in-place.

Location:
trunk/macosx
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.h

    r13131 r13132  
    191191
    192192- (void) sortTorrents: (BOOL) includeQueueOrder;
    193 - (void) sortTorrentsIgnoreSelectedReloadTable: (BOOL) reload includeQueueOrder: (BOOL) includeQueueOrder;
    194 - (void) rearrangeTorrentArray: (NSMutableArray *) rearrangeArray to: (NSArray *) endingArray forParent: parent beganTableUpdate: (BOOL *) beganTableUpdate;
     193- (void) sortTorrentsIgnoreSelectedCallUpdates: (BOOL) callUpdates includeQueueOrder: (BOOL) includeQueueOrder;
     194- (void) rearrangeTorrentTableArray: (NSMutableArray *) rearrangeArray forParent: (id) parent withSortDescriptors: (NSArray *) descriptors beganTableUpdate: (BOOL *) beganTableUpdate;
    195195- (void) setSort: (id) sender;
    196196- (void) setSortByGroup: (id) sender;
  • trunk/macosx/Controller.m

    r13131 r13132  
    13141314- (void) confirmRemoveTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData
    13151315{
    1316     NSMutableArray * selectedValues = [NSMutableArray arrayWithArray: [fTableView selectedValues]];
    1317     [selectedValues removeObjectsInArray: torrents];
    1318    
    1319     //don't want any of these starting then stopping
     1316    NSMutableArray * selectedValues = nil;
     1317    if ([NSApp isOnLionOrBetter])
     1318    {
     1319        selectedValues = [NSMutableArray arrayWithArray: [fTableView selectedValues]];
     1320        [selectedValues removeObjectsInArray: torrents];
     1321    }
     1322   
     1323    //miscellaneous
    13201324    for (Torrent * torrent in torrents)
     1325    {
     1326        //don't want any of these starting then stopping
    13211327        if ([torrent waitingToStart])
    13221328            [torrent stopTransfer];
    1323    
    1324     [fTorrents removeObjectsInArray: torrents];
    1325    
    1326     //if not removed from displayed torrents, fullUpdateUI might cause a crash
    1327     if ([fDisplayedTorrents count] > 0)
    1328     {
    1329         if ([[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
    1330         {
    1331             for (TorrentGroup * group in fDisplayedTorrents)
    1332                 [[group torrents] removeObjectsInArray: torrents];
    1333         }
    1334         else
    1335             [fDisplayedTorrents removeObjectsInArray: torrents];
    1336     }
    1337    
    1338     for (Torrent * torrent in torrents)
    1339     {
     1329       
    13401330        //let's expand all groups that have removed items - they either don't exist anymore, are already expanded, or are collapsed (rpc)
    13411331        [fTableView removeCollapsedGroup: [torrent groupValue]];
     
    13431333        //we can't assume the window is active - RPC removal, for example
    13441334        [fBadger removeTorrent: torrent];
    1345        
    1346         [torrent closeRemoveTorrent: deleteData];
    1347     }
     1335    }
     1336   
     1337    [fTorrents removeObjectsInArray: torrents];
     1338   
     1339    //set up helpers to remove from the table
     1340    __block BOOL beganUpdate = NO;
     1341   
     1342    void (^doTableRemoval)(NSMutableArray *, id) = ^(NSMutableArray * displayedTorrents, id parent) {
     1343        NSIndexSet * indexes = [displayedTorrents indexesOfObjectsWithOptions: NSEnumerationConcurrent passingTest: ^(id obj, NSUInteger idx, BOOL * stop) {
     1344            return [torrents containsObject: obj];
     1345        }];
     1346       
     1347        if ([indexes count] > 0)
     1348        {
     1349            if ([NSApp isOnLionOrBetter])
     1350            {
     1351                if (!beganUpdate)
     1352                {
     1353                    //we can't closeRemoveTorrent: until it's no longer in the GUI at all
     1354                    [[NSAnimationContext currentContext] setCompletionHandler: ^{
     1355                        for (Torrent * torrent in torrents)
     1356                            [torrent closeRemoveTorrent: deleteData];
     1357                    }];
     1358                   
     1359                    [NSAnimationContext beginGrouping];
     1360                    [fTableView beginUpdates];
     1361                    beganUpdate = YES;
     1362                }
     1363               
     1364                [fTableView removeItemsAtIndexes: indexes inParent: parent withAnimation: NSTableViewAnimationSlideLeft];
     1365            }
     1366            [displayedTorrents removeObjectsAtIndexes: indexes];
     1367        }
     1368    };
     1369   
     1370    //if not removed from the displayed torrents here, fullUpdateUI might cause a crash
     1371    if ([fDisplayedTorrents count] > 0)
     1372    {
     1373        if ([[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
     1374        {
     1375            for (TorrentGroup * group in fDisplayedTorrents)
     1376                doTableRemoval([group torrents], group);
     1377        }
     1378        else
     1379            doTableRemoval(fDisplayedTorrents, nil);
     1380       
     1381        if (beganUpdate)
     1382        {
     1383            [fTableView endUpdates];
     1384            [NSAnimationContext endGrouping];
     1385        }
     1386    }
     1387   
     1388    if (!beganUpdate)
     1389    {
     1390        //do here if we're not doing it at the end of the animation
     1391        for (Torrent * torrent in torrents)
     1392            [torrent closeRemoveTorrent: deleteData];
     1393       
     1394        if (selectedValues)
     1395            [fTableView selectValues: selectedValues];
     1396    }
     1397   
     1398    [self fullUpdateUI];
    13481399   
    13491400    #warning why do we need them retained?
    1350     [torrents release];
    1351    
    1352     [fTableView selectValues: selectedValues];
    1353    
    1354     [self fullUpdateUI];
     1401    [torrents autorelease];
    13551402}
    13561403
     
    18391886        selectedValues = [fTableView selectedValues];
    18401887   
    1841     [self sortTorrentsIgnoreSelectedReloadTable: YES includeQueueOrder: includeQueueOrder]; //actually sort
     1888    //actually sort
     1889    [self sortTorrentsIgnoreSelectedCallUpdates: YES includeQueueOrder: includeQueueOrder];
    18421890   
    18431891    if (!onLion)
     
    18511899
    18521900#warning rename
    1853 - (void) sortTorrentsIgnoreSelectedReloadTable: (BOOL) reload includeQueueOrder: (BOOL) includeQueueOrder
     1901- (void) sortTorrentsIgnoreSelectedCallUpdates: (BOOL) callUpdates includeQueueOrder: (BOOL) includeQueueOrder
    18541902{
    18551903    //don't do anything else if we don't have to
     
    19241972    }
    19251973   
    1926     BOOL beganTableUpdate = NO;
     1974    BOOL beganTableUpdate = !callUpdates || ![NSApp isOnLionOrBetter];
    19271975   
    19281976    //actually sort
     
    19301978    {
    19311979        for (TorrentGroup * group in fDisplayedTorrents)
    1932         {
    1933             if ([[group torrents] count] > 1)
    1934             {
    1935                 if (reload)
    1936                 {
    1937                     NSArray * sorted = [[group torrents] sortedArrayUsingDescriptors: descriptors];
    1938                     [self rearrangeTorrentArray: [group torrents] to: sorted forParent: group beganTableUpdate: &beganTableUpdate];
    1939                 }
    1940                 else
    1941                     [[group torrents] sortUsingDescriptors: descriptors];
    1942             }
    1943         }
     1980            [self rearrangeTorrentTableArray: [group torrents] forParent: group withSortDescriptors: descriptors beganTableUpdate: &beganTableUpdate];
    19441981    }
    19451982    else
    1946     {
    1947         if (reload)
    1948         {
    1949             NSArray * sorted = [fDisplayedTorrents sortedArrayUsingDescriptors: descriptors];
    1950             [self rearrangeTorrentArray: fDisplayedTorrents to: sorted forParent: nil beganTableUpdate: &beganTableUpdate];
    1951         }
    1952         else
    1953             [fDisplayedTorrents sortUsingDescriptors: descriptors];
    1954     }
    1955    
    1956     if (beganTableUpdate)
     1983        [self rearrangeTorrentTableArray: fDisplayedTorrents forParent: nil withSortDescriptors: descriptors beganTableUpdate: &beganTableUpdate];
     1984   
     1985    if (beganTableUpdate && callUpdates)
    19571986    {
    19581987        if ([NSApp isOnLionOrBetter])
     
    19651994}
    19661995
    1967 - (void) rearrangeTorrentArray: (NSMutableArray *) rearrangeArray to: (NSArray *) endingArray forParent: parent beganTableUpdate: (BOOL *) beganTableUpdate
    1968 {
    1969     NSAssert2([rearrangeArray count] == [endingArray count], @"Torrent arrays aren't equal size: %d and %d", [rearrangeArray count], [endingArray count]);
    1970    
    1971     for (NSUInteger currentIndex = 0; currentIndex < [rearrangeArray count]; ++currentIndex)
    1972     {
    1973         Torrent * torrent = [endingArray objectAtIndex: currentIndex];
    1974         const NSUInteger previousIndex = [rearrangeArray indexOfObject: torrent inRange: NSMakeRange(currentIndex, [rearrangeArray count]-currentIndex)];
    1975        
    1976         if (previousIndex != currentIndex)
    1977         {
    1978             NSAssert3(previousIndex != NSNotFound, @"Expected torrent %@ not found! %@ %@", torrent, rearrangeArray, endingArray);
     1996#warning redo so that we search a copy once again (best explained by changing sorting from ascending to descending)
     1997- (void) rearrangeTorrentTableArray: (NSMutableArray *) rearrangeArray forParent: parent withSortDescriptors: (NSArray *) descriptors beganTableUpdate: (BOOL *) beganTableUpdate
     1998{
     1999    for (NSUInteger currentIndex = 1; currentIndex < [rearrangeArray count]; ++currentIndex)
     2000    {
     2001        //manually do the sorting in-place
     2002        const NSUInteger insertIndex = [rearrangeArray indexOfObject: [rearrangeArray objectAtIndex: currentIndex] inSortedRange: NSMakeRange(0, currentIndex) options: (NSBinarySearchingInsertionIndex | NSBinarySearchingLastEqual) usingComparator: ^(id obj1, id obj2) {
     2003            for (NSSortDescriptor * descriptor in descriptors)
     2004            {
     2005                const NSComparisonResult result = [descriptor compareObject: obj1 toObject: obj2];
     2006                if (result != NSOrderedSame)
     2007                    return result;
     2008            }
    19792009           
    1980             if (beganTableUpdate && !*beganTableUpdate)
     2010            return NSOrderedSame;
     2011        }];
     2012       
     2013        if (insertIndex != currentIndex)
     2014        {
     2015            if (!*beganTableUpdate)
    19812016            {
    19822017                *beganTableUpdate = YES;
     
    19852020            }
    19862021           
    1987             [rearrangeArray moveObjectAtIndex: previousIndex toIndex: currentIndex];
     2022            [rearrangeArray moveObjectAtIndex: currentIndex toIndex: insertIndex];
    19882023            if ([NSApp isOnLionOrBetter])
    1989                 [fTableView moveItemAtIndex: previousIndex inParent: parent toIndex: currentIndex inParent: parent];
    1990         }
    1991     }
    1992    
    1993     NSAssert2([rearrangeArray isEqualToArray: endingArray], @"Torrent rearranging didn't work! %@ %@", rearrangeArray, endingArray);
    1994 }
    1995 
     2024                [fTableView moveItemAtIndex: currentIndex inParent: parent toIndex: insertIndex inParent: parent];
     2025        }
     2026    }
     2027   
     2028    NSAssert2([rearrangeArray isEqualToArray: [rearrangeArray sortedArrayUsingDescriptors: descriptors]], @"Torrent rearranging didn't work! %@ %@", rearrangeArray, [rearrangeArray sortedArrayUsingDescriptors: descriptors]);
     2029}
     2030
     2031#warning don't animate on launch
    19962032- (void) applyFilter
    19972033{
     2034    #warning re-add
    19982035    //get all the torrents in the table
    1999     NSMutableArray * previousTorrents;
     2036    /*NSMutableArray * previousTorrents;
    20002037    if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
    20012038    {
     
    20062043    }
    20072044    else
    2008         previousTorrents = fDisplayedTorrents;
    2009    
    2010     NSArray * selectedValues = [fTableView selectedValues];
     2045        previousTorrents = fDisplayedTorrents;*/
     2046   
     2047    const BOOL onLion = [NSApp isOnLionOrBetter];
     2048   
     2049    NSArray * selectedValues = nil;
     2050    if (!onLion)
     2051        selectedValues = [fTableView selectedValues];
    20112052   
    20122053    NSUInteger active = 0, downloading = 0, seeding = 0, paused = 0;
     
    21192160   
    21202161    //clear display cache for not-shown torrents
    2121     [previousTorrents removeObjectsInArray: allTorrents];
     2162    /*[previousTorrents removeObjectsInArray: allTorrents];
    21222163    for (Torrent * torrent in previousTorrents)
    2123         [torrent setPreviousFinishedPieces: nil];
     2164        [torrent setPreviousFinishedPieces: nil];*/
     2165   
     2166    BOOL beganUpdates = NO;
    21242167   
    21252168    //place torrents into groups
    2126     const BOOL groupRows = [fDefaults boolForKey: @"SortByGroup"];
    2127     if (groupRows)
    2128     {
    2129         NSMutableArray * oldTorrentGroups = [NSMutableArray array];
    2130         if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
    2131             [oldTorrentGroups addObjectsFromArray: fDisplayedTorrents];
    2132        
    2133         [fDisplayedTorrents removeAllObjects];
    2134        
     2169    //if either the previous or current lists are blank, set its value to the other
     2170    const BOOL groupRows = [allTorrents count] > 0 ? [fDefaults boolForKey: @"SortByGroup"] : ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]]);
     2171    const BOOL wasGroupRows = [fDisplayedTorrents count] > 0 ? [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]] : groupRows;
     2172    if (!groupRows && !wasGroupRows)
     2173    {
     2174        const NSRange existingTorrentRange = NSMakeRange(0, [fDisplayedTorrents count]);
     2175        NSMutableIndexSet * addIndexes = [NSMutableIndexSet indexSet],
     2176                        * remainingPreviousIndexes = [NSMutableIndexSet indexSetWithIndexesInRange: existingTorrentRange];
     2177       
     2178        for (NSUInteger previousIndex = 0; previousIndex < [allTorrents count]; ++previousIndex)
     2179        {
     2180            Torrent * torrent = [allTorrents objectAtIndex: previousIndex];
     2181            const NSUInteger currentIndex = [fDisplayedTorrents indexOfObjectAtIndexes: remainingPreviousIndexes options: NSEnumerationConcurrent passingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
     2182                return (BOOL)(obj == torrent);
     2183            }];
     2184            if (currentIndex == NSNotFound)
     2185                [addIndexes addIndex: previousIndex];
     2186            else
     2187                [remainingPreviousIndexes removeIndex: currentIndex];
     2188        }
     2189       
     2190        if ([addIndexes count] > 0 || [remainingPreviousIndexes count] > 0)
     2191        {
     2192            beganUpdates = YES;
     2193            if (onLion)
     2194                [fTableView beginUpdates];
     2195           
     2196            //remove torrents we didn't find
     2197            if ([remainingPreviousIndexes count] > 0)
     2198            {
     2199                [fDisplayedTorrents removeObjectsAtIndexes: remainingPreviousIndexes];
     2200                if (onLion)
     2201                    [fTableView removeItemsAtIndexes: remainingPreviousIndexes inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2202            }
     2203           
     2204            //add new torrents
     2205            if ([addIndexes count] > 0)
     2206            {
     2207                [fDisplayedTorrents addObjectsFromArray: [allTorrents objectsAtIndexes: addIndexes]];
     2208                if (onLion)
     2209                    [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange([fDisplayedTorrents count] - [addIndexes count], [addIndexes count])] inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2210            }
     2211        }
     2212    }
     2213    else if (!groupRows && wasGroupRows)
     2214    {
     2215        //since we're not doing this the right way (boo buggy animation), we need to remember selected group
     2216        NSArray * selectedValues = [fTableView selectedValues];
     2217       
     2218        beganUpdates = YES;
     2219        if (onLion)
     2220            [fTableView beginUpdates];
     2221       
     2222#if 1
     2223        if (onLion)
     2224            [fTableView removeItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedTorrents count])] inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2225             
     2226        [fDisplayedTorrents setArray: allTorrents];
     2227       
     2228        if (onLion)
     2229            [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedTorrents count])] inParent: nil withAnimation: NSTableViewAnimationEffectFade];
     2230       
     2231        [fTableView selectValues: selectedValues];
     2232#else
     2233        #warning hard hat zone
     2234       
     2235        #warning use and don't modify all torrents?
     2236        NSMutableIndexSet * addIndexes = [NSMutableIndexSet indexSet];
     2237       
     2238        const NSUInteger groupCount = [fDisplayedTorrents count];
     2239        for (NSUInteger groupIndex = 0; groupIndex < groupCount; ++groupIndex)
     2240        {
     2241            TorrentGroup * group = [fDisplayedTorrents objectAtIndex: groupIndex];
     2242           
     2243            for (NSInteger indexInGroup = [[group torrents] count]-1; indexInGroup >= 0; --indexInGroup)
     2244            {
     2245                Torrent * torrent = [[group torrents] objectAtIndex: indexInGroup];
     2246               
     2247                #warning maybe keep some sort of index set? idk - don't modify allTorrents?
     2248                const NSUInteger indexInAll = [allTorrents indexOfObject: torrent];
     2249                if (indexInAll != NSNotFound)
     2250                {
     2251                    [allTorrents removeObjectAtIndex: indexInAll];
     2252                    [[group torrents] removeObjectAtIndex: indexInGroup];
     2253                    [fDisplayedTorrents addObject: torrent];
     2254                    if (onLion)
     2255                        [fTableView moveItemAtIndex: indexInGroup inParent: group toIndex: [fDisplayedTorrents count]-1 inParent: nil];
     2256                }
     2257            }
     2258        }
     2259       
     2260        NSIndexSet * groupIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, groupCount)];
     2261        [fDisplayedTorrents removeObjectsAtIndexes: groupIndexes];
     2262        if (onLion)
     2263            [fTableView removeItemsAtIndexes: groupIndexes inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2264       
     2265        if ([allTorrents count] > 0)
     2266        {
     2267            [fDisplayedTorrents addObjectsFromArray: allTorrents];
     2268            if (onLion)
     2269                [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange([fDisplayedTorrents count] - [allTorrents count], [allTorrents count])] inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2270        }
     2271#endif
     2272    }
     2273    else if (groupRows && !wasGroupRows)
     2274    {
     2275        //since we're not doing this the right way (boo buggy animation), we need to remember selected group
     2276        selectedValues = [fTableView selectedValues];
     2277       
     2278        beganUpdates = YES;
     2279        if (onLion)
     2280            [fTableView beginUpdates];
     2281       
     2282        //a map for quickly finding groups
     2283        NSMutableDictionary * groupsByIndex = [NSMutableDictionary dictionaryWithCapacity: [[GroupsController groups] numberOfGroups]];
     2284        for (Torrent * torrent in allTorrents)
     2285        {
     2286            const NSInteger groupValue = [torrent groupValue];
     2287            TorrentGroup * group = [groupsByIndex objectForKey: [NSNumber numberWithInteger: groupValue]];
     2288            if (!group)
     2289            {
     2290                group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
     2291                [groupsByIndex setObject: group forKey: [NSNumber numberWithInteger: groupValue]];
     2292            }
     2293           
     2294            [[group torrents] addObject: torrent];
     2295        }
     2296       
     2297        #warning duplicate from above
     2298        if (onLion)
     2299            [fTableView removeItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedTorrents count])] inParent: nil withAnimation: NSTableViewAnimationSlideDown];
     2300       
     2301        [fDisplayedTorrents setArray: [groupsByIndex allValues]];
     2302       
     2303        //we need the groups to be sorted, and we can do it without moving items in the table, too!
    21352304        NSSortDescriptor * groupDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"groupOrderValue" ascending: YES];
    2136         [allTorrents sortUsingDescriptors: [NSArray arrayWithObject: groupDescriptor]];
    2137        
    2138         TorrentGroup * group = nil;
    2139         NSInteger lastGroupValue = -2, currentOldGroupIndex = 0;
    2140         for (Torrent * torrent in allTorrents)
    2141         {
    2142             const NSInteger groupValue = [torrent groupValue];
    2143             if (groupValue != lastGroupValue)
     2305        [fDisplayedTorrents sortUsingDescriptors: [NSArray arrayWithObject: groupDescriptor]];
     2306       
     2307        if (onLion)
     2308            [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedTorrents count])] inParent: nil withAnimation: NSTableViewAnimationEffectFade];
     2309    }
     2310    else
     2311    {
     2312        NSAssert(groupRows && wasGroupRows, @"Should have had group rows and should remain with group rows");
     2313       
     2314        #warning not remembering selected when changing to a new group
     2315       
     2316        #warning don't always do?
     2317        beganUpdates = YES;
     2318        if (onLion)
     2319            [fTableView beginUpdates];
     2320       
     2321        NSMutableIndexSet * unusedAllTorrentsIndexes = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [allTorrents count])];
     2322       
     2323        NSMutableDictionary * groupsByIndex = [NSMutableDictionary dictionaryWithCapacity: [fDisplayedTorrents count]];
     2324       
     2325        #warning necessary? make more efficient?
     2326        for (TorrentGroup * group in fDisplayedTorrents)
     2327            [groupsByIndex setObject: group forKey: [NSNumber numberWithInteger: [group groupIndex]]];
     2328       
     2329        const NSUInteger originalGroupCount = [fDisplayedTorrents count];
     2330        for (NSUInteger index = 0; index < originalGroupCount; ++index)
     2331        {
     2332            TorrentGroup * group = [fDisplayedTorrents objectAtIndex: index];
     2333           
     2334            NSMutableIndexSet * removeIndexes = [NSMutableIndexSet indexSet];
     2335           
     2336            //needs to be a signed integer
     2337            for (NSInteger indexInGroup = 0; indexInGroup < [[group torrents] count]; ++indexInGroup)
    21442338            {
    2145                 lastGroupValue = groupValue;
    2146                
    2147                 group = nil;
    2148                
    2149                 //try to see if the group already exists
    2150                 for (; currentOldGroupIndex < [oldTorrentGroups count]; ++currentOldGroupIndex)
     2339                #warning indexOfObject:inSortedRange:options:usingComparator:?
     2340                Torrent * torrent = [[group torrents] objectAtIndex: indexInGroup];
     2341                const NSUInteger allIndex = [allTorrents indexOfObjectAtIndexes: unusedAllTorrentsIndexes options: NSEnumerationConcurrent passingTest: ^(id obj, NSUInteger idx, BOOL * stop) {
     2342                    return (BOOL)(obj == torrent);
     2343                }];
     2344                if (allIndex == NSNotFound)
     2345                    [removeIndexes addIndex: indexInGroup];
     2346                else
    21512347                {
    2152                     TorrentGroup * currentGroup = [oldTorrentGroups objectAtIndex: currentOldGroupIndex];
    2153                     const NSInteger currentGroupValue = [currentGroup groupIndex];
    2154                     if (currentGroupValue == groupValue)
     2348                    BOOL markTorrentAsUsed = YES;
     2349                   
     2350                    const NSInteger groupValue = [torrent groupValue];
     2351                    if (groupValue != [group groupIndex])
    21552352                    {
    2156                         group = currentGroup;
    2157                         [[currentGroup torrents] removeAllObjects];
     2353                        TorrentGroup * newGroup = [groupsByIndex objectForKey: [NSNumber numberWithInteger: groupValue]];
     2354                        if (!newGroup)
     2355                        {
     2356                            newGroup = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
     2357                            [groupsByIndex setObject: newGroup forKey: [NSNumber numberWithInteger: groupValue]];
     2358                            [fDisplayedTorrents addObject: newGroup];
     2359                           
     2360                            if (onLion)
     2361                                [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndex: [fDisplayedTorrents count]-1] inParent: nil withAnimation: NSTableViewAnimationSlideLeft];
     2362                        }
     2363                        else //if we haven't processed the other group yet, we have to make sure we don't flag it for removal the next time
     2364                        {
     2365                            //ugggh, but shouldn't happen too often
     2366                            if ([fDisplayedTorrents indexOfObject: newGroup inRange: NSMakeRange(index+1, originalGroupCount-(index+1))] != NSNotFound)
     2367                                markTorrentAsUsed = NO;
     2368                        }
    21582369                       
    2159                         ++currentOldGroupIndex;
     2370                        [[group torrents] removeObjectAtIndex: indexInGroup];
     2371                        [[newGroup torrents] addObject: torrent];
     2372                       
     2373                        if (onLion)
     2374                            [fTableView moveItemAtIndex: indexInGroup inParent: group toIndex: [[newGroup torrents] count]-1 inParent: newGroup];
     2375                       
     2376                        --indexInGroup;
    21602377                    }
    21612378                   
    2162                     if (currentGroupValue >= groupValue)
    2163                         break;
     2379                    if (markTorrentAsUsed)
     2380                        [unusedAllTorrentsIndexes removeIndex: allIndex];
    21642381                }
    2165                
    2166                 if (!group)
    2167                     group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
    2168                 [fDisplayedTorrents addObject: group];
    21692382            }
    21702383           
    2171             NSAssert(group != nil, @"No group object to add torrents to");
     2384            if ([removeIndexes count] > 0)
     2385            {
     2386                [[group torrents] removeObjectsAtIndexes: removeIndexes];
     2387                if (onLion)
     2388                    [fTableView removeItemsAtIndexes: removeIndexes inParent: group withAnimation: NSTableViewAnimationSlideDown];
     2389            }
     2390        }
     2391       
     2392        //add remaining new torrents
     2393        for (Torrent * torrent in [allTorrents objectsAtIndexes: unusedAllTorrentsIndexes])
     2394        {
     2395            const NSInteger groupValue = [torrent groupValue];
     2396            TorrentGroup * group = [groupsByIndex objectForKey: [NSNumber numberWithInteger: groupValue]];
     2397            if (!group)
     2398            {
     2399                group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
     2400                [groupsByIndex setObject: group forKey: [NSNumber numberWithInteger: groupValue]];
     2401                [fDisplayedTorrents addObject: group];
     2402               
     2403                if (onLion)
     2404                    [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndex: [fDisplayedTorrents count]-1] inParent: nil withAnimation: NSTableViewAnimationSlideLeft];
     2405            }
     2406           
    21722407            [[group torrents] addObject: torrent];
    2173         }
     2408            if (onLion)
     2409                [fTableView insertItemsAtIndexes: [NSIndexSet indexSetWithIndex: [[group torrents] count]-1] inParent: group withAnimation: NSTableViewAnimationSlideDown];
     2410        }
     2411       
     2412        //remove empty groups
     2413        NSMutableIndexSet * removeIndexes = [NSMutableIndexSet indexSet];
     2414        for (NSUInteger index = 0; index < originalGroupCount; ++index)
     2415        {
     2416            TorrentGroup * group = [fDisplayedTorrents objectAtIndex: index];
     2417            if ([[group torrents] count] == 0)
     2418                [removeIndexes addIndex: index];
     2419        }
     2420       
     2421        if ([removeIndexes count] > 0)
     2422        {
     2423            [fDisplayedTorrents removeObjectsAtIndexes: removeIndexes];
     2424            if (onLion)
     2425                [fTableView removeItemsAtIndexes: removeIndexes inParent: nil withAnimation: NSTableViewAnimationSlideLeft];
     2426        }
     2427       
     2428        //now that all groups are there, sort them - don't insert on the fly in case groups were reordered in prefs
     2429        NSSortDescriptor * groupDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"groupOrderValue" ascending: YES];
     2430        [self rearrangeTorrentTableArray: fDisplayedTorrents forParent: nil withSortDescriptors: [NSArray arrayWithObject: groupDescriptor] beganTableUpdate: &beganUpdates];
     2431    }
     2432   
     2433    //sort the torrents (won't sort the groups, though)
     2434    [self sortTorrentsIgnoreSelectedCallUpdates: !beganUpdates includeQueueOrder: YES];
     2435   
     2436    if (onLion)
     2437    {
     2438        if (beganUpdates)
     2439            [fTableView endUpdates];
     2440        [fTableView setNeedsDisplay: YES];
    21742441    }
    21752442    else
    2176         [fDisplayedTorrents setArray: allTorrents];
    2177    
    2178     //actually sort
    2179     [self sortTorrentsIgnoreSelectedReloadTable: NO includeQueueOrder: NO];
    2180     [fTableView reloadData];
     2443        [fTableView reloadData];
    21812444   
    21822445    //reset expanded/collapsed rows
     
    21922455    }
    21932456   
    2194     [fTableView selectValues: selectedValues];
     2457    if (selectedValues)
     2458        [fTableView selectValues: selectedValues];
     2459   
    21952460    [self resetInfo]; //if group is already selected, but the torrents in it change
    21962461   
     
    26252890}
    26262891
    2627 - (BOOL) outlineView: (NSOutlineView *) outlineView acceptDrop: (id < NSDraggingInfo >) info item: (id) item
    2628     childIndex: (NSInteger) newRow
     2892#warning don't accept drop on overall group (without groups, or maybe with?)
     2893- (BOOL) outlineView: (NSOutlineView *) outlineView acceptDrop: (id < NSDraggingInfo >) info item: (id) item childIndex: (NSInteger) newRow
    26292894{
    26302895    NSPasteboard * pasteboard = [info draggingPasteboard];
     
    26322897    {
    26332898        //remember selected rows
    2634         NSArray * selectedValues = [fTableView selectedValues];
     2899        NSArray * selectedValues = nil;
     2900        if (![NSApp isOnLionOrBetter])
     2901            selectedValues = [fTableView selectedValues];
    26352902   
    26362903        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: TORRENT_TABLE_VIEW_DATA_TYPE]];
     
    26452912        {
    26462913            //change groups
    2647             NSInteger groupValue = [item groupIndex];
    2648             for (Torrent * torrent in movingTorrents)
    2649             {
    2650                 //have to reset objects here to avoid weird crash
    2651                 [[[fTableView parentForItem: torrent] torrents] removeObject: torrent];
    2652                 [[item torrents] addObject: torrent];
    2653                
    2654                 [torrent setGroupValue: groupValue];
    2655             }
    2656             //part 2 of avoiding weird crash
    2657             [fTableView reloadItem: nil reloadChildren: YES];
     2914            const NSInteger groupValue = [item groupIndex];
     2915            [movingTorrents enumerateObjectsWithOptions: NSEnumerationConcurrent usingBlock: ^(id obj, NSUInteger idx, BOOL *stop)  {
     2916                [(Torrent *)obj setGroupValue: groupValue];
     2917            }];
    26582918        }
    26592919       
     
    26802940            NSUInteger insertIndex = topTorrent ? [fTorrents indexOfObject: topTorrent] + 1 : 0;
    26812941            NSIndexSet * insertIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(insertIndex, [movingTorrents count])];
    2682             for (Torrent * torrent in movingTorrents)
    2683                 [torrent setQueuePosition: insertIndex++];
    26842942            [fTorrents insertObjects: movingTorrents atIndexes: insertIndexes];
     2943           
     2944            //we need to make sure the queue order is updated in the Torrent object before we sort - safest to just reset all queue positions
     2945            NSUInteger i = 0;
     2946            for (Torrent * torrent in fTorrents)
     2947            {
     2948                [torrent setQueuePosition: i++];
     2949                [torrent update];
     2950            }
    26852951        }
    26862952       
    26872953        [self applyFilter];
    2688         [fTableView selectValues: selectedValues];
     2954        if (selectedValues)
     2955            [fTableView selectValues: selectedValues];
    26892956    }
    26902957   
  • trunk/macosx/TorrentGroup.h

    r11617 r13132  
    3434
    3535- (NSInteger) groupIndex;
     36- (NSInteger) groupOrderValue;
    3637- (NSMutableArray *) torrents;
    3738
  • trunk/macosx/TorrentGroup.m

    r11617 r13132  
    2424
    2525#import "TorrentGroup.h"
     26#import "GroupsController.h"
    2627#import "Torrent.h"
    2728
     
    4748}
    4849
     50- (NSString *) description
     51{
     52    return [NSString stringWithFormat: @"Torrent Group %d: %@", fGroup, fTorrents];
     53}
     54
    4955- (NSInteger) groupIndex
    5056{
    5157    return fGroup;
     58}
     59
     60- (NSInteger) groupOrderValue
     61{
     62    return [[GroupsController groups] rowValueForIndex: fGroup];
    5263}
    5364
Note: See TracChangeset for help on using the changeset viewer.