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 "IGListIndexPathResult.h"
|
|
|
|
|
#import "IGListIndexPathResultInternal.h"
|
|
|
|
|
|
|
|
|
|
@implementation IGListIndexPathResult {
|
|
|
|
|
NSMapTable<id<NSObject>, NSIndexPath *> *_oldIndexPathMap;
|
|
|
|
|
NSMapTable<id<NSObject>, NSIndexPath *> *_newIndexPathMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithInserts:(NSArray<NSIndexPath *> *)inserts
|
|
|
|
|
deletes:(NSArray<NSIndexPath *> *)deletes
|
|
|
|
|
updates:(NSArray<NSIndexPath *> *)updates
|
|
|
|
|
moves:(NSArray<IGListMoveIndexPath *> *)moves
|
|
|
|
|
oldIndexPathMap:(NSMapTable<id<NSObject>, NSIndexPath *> *)oldIndexPathMap
|
|
|
|
|
newIndexPathMap:(NSMapTable<id<NSObject>, NSIndexPath *> *)newIndexPathMap {
|
|
|
|
|
if (self = [super init]) {
|
2018-02-14 00:46:10 +00:00
|
|
|
_inserts = inserts;
|
|
|
|
|
_deletes = deletes;
|
|
|
|
|
_updates = updates;
|
|
|
|
|
_moves = moves;
|
|
|
|
|
_oldIndexPathMap = oldIndexPathMap;
|
|
|
|
|
_newIndexPathMap = newIndexPathMap;
|
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
|
|
|
- (IGListIndexPathResult *)resultForBatchUpdates {
|
2016-11-21 18:15:08 +00:00
|
|
|
NSMutableSet<NSIndexPath *> *deletes = [NSMutableSet setWithArray:self.deletes];
|
|
|
|
|
NSMutableSet<NSIndexPath *> *inserts = [NSMutableSet setWithArray:self.inserts];
|
|
|
|
|
NSMutableSet<NSIndexPath *> *filteredUpdates = [NSMutableSet setWithArray:self.updates];
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
NSArray<IGListMoveIndexPath *> *moves = self.moves;
|
|
|
|
|
NSMutableArray<IGListMoveIndexPath *> *filteredMoves = [moves mutableCopy];
|
|
|
|
|
|
2016-10-26 18:47:08 +00:00
|
|
|
// convert move+update to delete+insert, respecting the from/to of the move
|
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--) {
|
|
|
|
|
IGListMoveIndexPath *move = moves[i];
|
|
|
|
|
if ([filteredUpdates containsObject:move.from]) {
|
|
|
|
|
[filteredMoves removeObjectAtIndex:i];
|
|
|
|
|
[filteredUpdates removeObject:move.from];
|
|
|
|
|
[deletes addObject:move.from];
|
|
|
|
|
[inserts addObject: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 [_oldIndexPathMap keyEnumerator]) {
|
|
|
|
|
NSIndexPath *indexPath = [_oldIndexPathMap objectForKey:key];
|
|
|
|
|
if ([filteredUpdates containsObject:indexPath]) {
|
|
|
|
|
[deletes addObject:indexPath];
|
|
|
|
|
[inserts addObject:(id)[_newIndexPathMap objectForKey:key]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
return [[IGListIndexPathResult alloc] initWithInserts:[inserts allObjects]
|
|
|
|
|
deletes:[deletes allObjects]
|
2016-10-26 18:47:08 +00:00
|
|
|
updates:[NSArray new]
|
2016-09-07 22:37:59 +00:00
|
|
|
moves:filteredMoves
|
|
|
|
|
oldIndexPathMap:_oldIndexPathMap
|
|
|
|
|
newIndexPathMap:_newIndexPathMap];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSIndexPath *)oldIndexPathForIdentifier:(id<NSObject>)identifier {
|
|
|
|
|
return [_oldIndexPathMap objectForKey:identifier];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSIndexPath *)newIndexPathForIdentifier:(id<NSObject>)identifier {
|
|
|
|
|
return [_newIndexPathMap objectForKey:identifier];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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
|