2019-12-19 17:32:49 +00:00
|
|
|
/*
|
2023-04-06 09:44:16 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-09-07 22:37:59 +00:00
|
|
|
*
|
2018-05-01 21:33:50 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-09-07 22:37:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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]) {
|
2018-02-14 00:46:10 +00:00
|
|
|
_inserts = inserts;
|
|
|
|
|
_deletes = deletes;
|
|
|
|
|
_updates = updates;
|
|
|
|
|
_moves = moves;
|
|
|
|
|
_oldIndexMap = oldIndexMap;
|
|
|
|
|
_newIndexMap = newIndexMap;
|
2016-09-07 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)hasChanges {
|
2017-07-12 19:40:16 +00:00
|
|
|
return self.changeCount > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)changeCount {
|
|
|
|
|
return self.inserts.count + self.deletes.count + self.updates.count + self.moves.count;
|
2016-09-07 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-26 18:47:08 +00:00
|
|
|
- (IGListIndexSetResult *)resultForBatchUpdates {
|
2016-09-07 22:37:59 +00:00
|
|
|
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];
|
|
|
|
|
|
2016-10-26 18:47:08 +00:00
|
|
|
// convert all update+move to delete+insert
|
2017-01-22 22:34:32 +00:00
|
|
|
const NSInteger moveCount = moves.count;
|
2016-09-07 22:37:59 +00:00
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 18:47:08 +00:00
|
|
|
// 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]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
return [[IGListIndexSetResult alloc] initWithInserts:inserts
|
|
|
|
|
deletes:deletes
|
2016-10-26 18:47:08 +00:00
|
|
|
updates:[NSIndexSet new]
|
2016-09-07 22:37:59 +00:00
|
|
|
moves:filteredMoves
|
|
|
|
|
oldIndexMap:_oldIndexMap
|
|
|
|
|
newIndexMap:_newIndexMap];
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 23:15:55 +00:00
|
|
|
- (NSInteger)oldIndexForIdentifier:(id<NSObject>)identifier {
|
2016-09-07 22:37:59 +00:00
|
|
|
NSNumber *index = [_oldIndexMap objectForKey:identifier];
|
|
|
|
|
return index == nil ? NSNotFound : [index integerValue];
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 23:15:55 +00:00
|
|
|
- (NSInteger)newIndexForIdentifier:(id<NSObject>)identifier {
|
2016-09-07 22:37:59 +00:00
|
|
|
NSNumber *index = [_newIndexMap objectForKey:identifier];
|
|
|
|
|
return index == nil ? NSNotFound : [index integerValue];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)description {
|
Minimum fixes for Xcode 9.3 build errors
Summary:
Issue fixed: #1141
This is a set of minimum fixes for Xcode 9.3 rather than #1142. Much smaller diffs 😃
The changes here were done by Xcode's FixIt. See also https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW5.
> Platform Dependencies
>
> OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.
- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
Closes https://github.com/Instagram/IGListKit/pull/1143
Differential Revision: D7551617
Pulled By: rnystrom
fbshipit-source-id: b27ebe2b1a1c93ebfd1218dc38b7621fefee2e6b
2018-04-09 15:06:47 +00:00
|
|
|
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];
|
2016-09-07 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|