forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASMutableElementMap.m
More file actions
114 lines (94 loc) · 3.84 KB
/
ASMutableElementMap.m
File metadata and controls
114 lines (94 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// ASMutableElementMap.m
// AsyncDisplayKit
//
// Created by Adlai Holler on 2/23/17.
// Copyright © 2017 Facebook. All rights reserved.
//
#import "ASMutableElementMap.h"
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASDataController.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASTwoDimensionalArrayUtils.h>
#import <AsyncDisplayKit/NSIndexSet+ASHelpers.h>
typedef NSMutableArray<NSMutableArray<ASCollectionElement *> *> ASMutableCollectionElementTwoDimensionalArray;
typedef NSMutableDictionary<NSString *, NSMutableDictionary<NSIndexPath *, ASCollectionElement *> *> ASMutableSupplementaryElementDictionary;
@implementation ASMutableElementMap {
ASMutableSupplementaryElementDictionary *_supplementaryElements;
NSMutableArray<ASSection *> *_sections;
ASMutableCollectionElementTwoDimensionalArray *_sectionsOfItems;
}
- (instancetype)initWithSections:(NSArray<ASSection *> *)sections items:(ASCollectionElementTwoDimensionalArray *)items supplementaryElements:(ASSupplementaryElementDictionary *)supplementaryElements
{
if (self = [super init]) {
_sections = [sections mutableCopy];
_sectionsOfItems = (id)ASTwoDimensionalArrayDeepMutableCopy(items);
_supplementaryElements = [ASMutableElementMap deepMutableCopyOfElementsDictionary:supplementaryElements];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
return [[ASElementMap alloc] initWithSections:_sections items:_sectionsOfItems supplementaryElements:_supplementaryElements];
}
- (void)removeAllSectionContexts
{
[_sections removeAllObjects];
}
- (void)insertSection:(ASSection *)section atIndex:(NSInteger)index
{
[_sections insertObject:section atIndex:index];
}
- (void)removeItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
ASDeleteElementsInTwoDimensionalArrayAtIndexPaths(_sectionsOfItems, indexPaths);
}
- (void)removeSectionContextsAtIndexes:(NSIndexSet *)indexes
{
[_sections removeObjectsAtIndexes:indexes];
}
- (void)removeAllElements
{
[_sectionsOfItems removeAllObjects];
[_supplementaryElements removeAllObjects];
}
- (void)removeSectionsOfItems:(NSIndexSet *)itemSections
{
[_sectionsOfItems removeObjectsAtIndexes:itemSections];
}
- (void)removeSupplementaryElementsInSections:(NSIndexSet *)sections
{
[_supplementaryElements enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSMutableDictionary<NSIndexPath *,ASCollectionElement *> * _Nonnull supplementariesForKind, BOOL * _Nonnull stop) {
[supplementariesForKind removeObjectsForKeys:[sections as_filterIndexPathsBySection:supplementariesForKind]];
}];
}
- (void)insertEmptySectionsOfItemsAtIndexes:(NSIndexSet *)sections
{
[sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
[_sectionsOfItems insertObject:[NSMutableArray array] atIndex:idx];
}];
}
- (void)insertElement:(ASCollectionElement *)element atIndexPath:(NSIndexPath *)indexPath
{
NSString *kind = element.supplementaryElementKind;
if (kind == nil) {
[_sectionsOfItems[indexPath.section] insertObject:element atIndex:indexPath.item];
} else {
NSMutableDictionary *supplementariesForKind = _supplementaryElements[kind];
if (supplementariesForKind == nil) {
supplementariesForKind = [NSMutableDictionary dictionary];
_supplementaryElements[kind] = supplementariesForKind;
}
supplementariesForKind[indexPath] = element;
}
}
#pragma mark - Helpers
+ (ASMutableSupplementaryElementDictionary *)deepMutableCopyOfElementsDictionary:(ASSupplementaryElementDictionary *)originalDict
{
NSMutableDictionary *deepCopy = [NSMutableDictionary dictionaryWithCapacity:originalDict.count];
[originalDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSDictionary<NSIndexPath *,ASCollectionElement *> * _Nonnull obj, BOOL * _Nonnull stop) {
deepCopy[key] = [obj mutableCopy];
}];
return deepCopy;
}
@end