mirror of
https://github.com/Instagram/IGListKit
synced 2026-04-21 21:47:34 +00:00
Summary: Pull Request resolved: https://github.com/Instagram/IGListKit/pull/1377 This splits the diffing-only components from `IGListKit` out into a separate library, `IGListDiffKit`, allowing them to be used independently of the rest of `IGListKit`. We've found that the diffing components of the library are useful independently of the rest of the `UICollectionView` infrastructure, and separating the libraries allows apps to take advantage of those components without paying the full binary size cost of `IGListKit`. The diffing components are available as a subspec in Cocoapods, and as an independent framework in Carthage and direct Xcode project import (i.e. Git submodules). This is a breaking change, necessitating a major version bump, although it should have only minor impact for projects using the umbrella header `IGListKit.h`, as that header continues to import the diffing utilities. In particular, for a project using Cocoapods and importing `IGListKit.h`, there should be no code changes required. Reviewed By: calimarkus Differential Revision: D18114249 fbshipit-source-id: 363b5a427e32800bbc6e82f897230963b4167245
93 lines
3.5 KiB
Objective-C
93 lines
3.5 KiB
Objective-C
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "IGListIndexSetResult.h"
|
|
#import "IGListIndexSetResultInternal.h"
|
|
|
|
@implementation IGListIndexSetResult {
|
|
NSMapTable<id<NSObject>, NSNumber *> *_oldIndexMap;
|
|
NSMapTable<id<NSObject>, NSNumber *> *_newIndexMap;
|
|
}
|
|
|
|
- (instancetype)initWithInserts:(NSIndexSet *)inserts
|
|
deletes:(NSIndexSet *)deletes
|
|
updates:(NSIndexSet *)updates
|
|
moves:(NSArray<IGListMoveIndex *> *)moves
|
|
oldIndexMap:(NSMapTable<id<NSObject>, NSNumber *> *)oldIndexMap
|
|
newIndexMap:(NSMapTable<id<NSObject>, NSNumber *> *)newIndexMap {
|
|
if (self = [super init]) {
|
|
_inserts = inserts;
|
|
_deletes = deletes;
|
|
_updates = updates;
|
|
_moves = moves;
|
|
_oldIndexMap = oldIndexMap;
|
|
_newIndexMap = newIndexMap;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (BOOL)hasChanges {
|
|
return self.changeCount > 0;
|
|
}
|
|
|
|
- (NSInteger)changeCount {
|
|
return self.inserts.count + self.deletes.count + self.updates.count + self.moves.count;
|
|
}
|
|
|
|
- (IGListIndexSetResult *)resultForBatchUpdates {
|
|
NSMutableIndexSet *deletes = [self.deletes mutableCopy];
|
|
NSMutableIndexSet *inserts = [self.inserts mutableCopy];
|
|
NSMutableIndexSet *filteredUpdates = [self.updates mutableCopy];
|
|
|
|
NSArray<IGListMoveIndex *> *moves = self.moves;
|
|
NSMutableArray<IGListMoveIndex *> *filteredMoves = [moves mutableCopy];
|
|
|
|
// convert all update+move to delete+insert
|
|
const NSInteger moveCount = moves.count;
|
|
for (NSInteger i = moveCount - 1; i >= 0; i--) {
|
|
IGListMoveIndex *move = moves[i];
|
|
if ([filteredUpdates containsIndex:move.from]) {
|
|
[filteredMoves removeObjectAtIndex:i];
|
|
[filteredUpdates removeIndex:move.from];
|
|
[deletes addIndex:move.from];
|
|
[inserts addIndex:move.to];
|
|
}
|
|
}
|
|
|
|
// iterate all new identifiers. if its index is updated, delete from the old index and insert the new index
|
|
for (id<NSObject> key in [_oldIndexMap keyEnumerator]) {
|
|
const NSInteger index = [[_oldIndexMap objectForKey:key] integerValue];
|
|
if ([filteredUpdates containsIndex:index]) {
|
|
[deletes addIndex:index];
|
|
[inserts addIndex:[[_newIndexMap objectForKey:key] integerValue]];
|
|
}
|
|
}
|
|
|
|
return [[IGListIndexSetResult alloc] initWithInserts:inserts
|
|
deletes:deletes
|
|
updates:[NSIndexSet new]
|
|
moves:filteredMoves
|
|
oldIndexMap:_oldIndexMap
|
|
newIndexMap:_newIndexMap];
|
|
}
|
|
|
|
- (NSInteger)oldIndexForIdentifier:(id<NSObject>)identifier {
|
|
NSNumber *index = [_oldIndexMap objectForKey:identifier];
|
|
return index == nil ? NSNotFound : [index integerValue];
|
|
}
|
|
|
|
- (NSInteger)newIndexForIdentifier:(id<NSObject>)identifier {
|
|
NSNumber *index = [_newIndexMap objectForKey:identifier];
|
|
return index == nil ? NSNotFound : [index integerValue];
|
|
}
|
|
|
|
- (NSString *)description {
|
|
return [NSString stringWithFormat:@"<%@ %p; %lu inserts; %lu deletes; %lu updates; %lu moves>",
|
|
NSStringFromClass(self.class), self, (unsigned long)self.inserts.count, (unsigned long)self.deletes.count, (unsigned long)self.updates.count, (unsigned long)self.moves.count];
|
|
}
|
|
|
|
@end
|