This repository has been archived by the owner on May 24, 2024. It is now read-only.
/
NIMutableTableViewModel.m
195 lines (161 loc) · 7.29 KB
/
NIMutableTableViewModel.m
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Copyright 2011-2014 NimbusKit
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NIMutableTableViewModel.h"
#import "NITableViewModel+Private.h"
#import "NIMutableTableViewModel+Private.h"
#import "NimbusCore.h"
@implementation NIMutableTableViewModel
@dynamic delegate;
#pragma mark - Public
- (NSArray *)addObject:(id)object {
NITableViewModelSection* section = self.sections.count == 0 ? [self _appendSection] : self.sections.lastObject;
[section.mutableRows addObject:object];
return [NSArray arrayWithObject:[NSIndexPath indexPathForRow:section.mutableRows.count - 1
inSection:self.sections.count - 1]];
}
- (NSArray *)addObject:(id)object toSection:(NSUInteger)sectionIndex {
NIDASSERT(sectionIndex >= 0 && sectionIndex < self.sections.count);
NITableViewModelSection *section = [self.sections objectAtIndex:sectionIndex];
[section.mutableRows addObject:object];
return [NSArray arrayWithObject:[NSIndexPath indexPathForRow:section.mutableRows.count - 1
inSection:sectionIndex]];
}
- (NSArray *)addObjectsFromArray:(NSArray *)array {
NSMutableArray* indices = [NSMutableArray array];
for (id object in array) {
[indices addObject:[[self addObject:object] objectAtIndex:0]];
}
return indices;
}
- (NSArray *)insertObject:(id)object atRow:(NSUInteger)row inSection:(NSUInteger)sectionIndex {
NIDASSERT(sectionIndex >= 0 && sectionIndex < self.sections.count);
NITableViewModelSection *section = [self.sections objectAtIndex:sectionIndex];
[section.mutableRows insertObject:object atIndex:row];
return [NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
}
- (NSArray *)removeObjectAtIndexPath:(NSIndexPath *)indexPath {
NIDASSERT(indexPath.section < (NSInteger)self.sections.count);
if (indexPath.section >= (NSInteger)self.sections.count) {
return nil;
}
NITableViewModelSection* section = [self.sections objectAtIndex:indexPath.section];
NIDASSERT(indexPath.row < (NSInteger)section.mutableRows.count);
if (indexPath.row >= (NSInteger)section.mutableRows.count) {
return nil;
}
[section.mutableRows removeObjectAtIndex:indexPath.row];
return [NSArray arrayWithObject:indexPath];
}
- (NSIndexSet *)addSectionWithTitle:(NSString *)title {
NITableViewModelSection* section = [self _appendSection];
section.headerTitle = title;
return [NSIndexSet indexSetWithIndex:self.sections.count - 1];
}
- (NSIndexSet *)insertSectionWithTitle:(NSString *)title atIndex:(NSUInteger)index {
NITableViewModelSection* section = [self _insertSectionAtIndex:index];
section.headerTitle = title;
return [NSIndexSet indexSetWithIndex:index];
}
- (NSIndexSet *)removeSectionAtIndex:(NSUInteger)index {
NIDASSERT(index >= 0 && index < self.sections.count);
[self.sections removeObjectAtIndex:index];
return [NSIndexSet indexSetWithIndex:index];
}
- (void)updateSectionIndex {
[self _compileSectionIndex];
}
#pragma mark - Private
- (NITableViewModelSection *)_appendSection {
if (nil == self.sections) {
[self _setSectionsWithArray:[NSMutableArray array]];
}
NITableViewModelSection* section = nil;
section = [[NITableViewModelSection alloc] init];
section.rows = [NSMutableArray array];
[self.sections addObject:section];
return section;
}
- (NITableViewModelSection *)_insertSectionAtIndex:(NSUInteger)index {
if (nil == self.sections) {
[self _setSectionsWithArray:[NSMutableArray array]];
}
NITableViewModelSection* section = nil;
section = [[NITableViewModelSection alloc] init];
section.rows = [NSMutableArray array];
NIDASSERT(index >= 0 && index <= self.sections.count);
[self.sections insertObject:section atIndex:index];
return section;
}
- (void)_setSectionsWithArray:(NSArray *)sectionsArray {
if ([sectionsArray isKindOfClass:[NSMutableArray class]]) {
self.sections = (NSMutableArray *)sectionsArray;
} else {
self.sections = [sectionsArray mutableCopy];
}
}
#pragma mark - UITableViewDataSource
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(tableViewModel:canEditObject:atIndexPath:inTableView:)]) {
id object = [self objectAtIndexPath:indexPath];
return [self.delegate tableViewModel:self canEditObject:object atIndexPath:indexPath inTableView:tableView];
} else {
return NO;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self objectAtIndexPath:indexPath];
if (editingStyle == UITableViewCellEditingStyleDelete) {
BOOL shouldDelete = YES;
if ([self.delegate respondsToSelector:@selector(tableViewModel:shouldDeleteObject:atIndexPath:inTableView:)]) {
shouldDelete = [self.delegate tableViewModel:self shouldDeleteObject:object atIndexPath:indexPath inTableView:tableView];
}
if (shouldDelete) {
NSArray *indexPaths = [self removeObjectAtIndexPath:indexPath];
UITableViewRowAnimation animation = UITableViewRowAnimationAutomatic;
if ([self.delegate respondsToSelector:@selector(tableViewModel:deleteRowAnimationForObject:atIndexPath:inTableView:)]) {
animation = [self.delegate tableViewModel:self deleteRowAnimationForObject:object atIndexPath:indexPath inTableView:tableView];
}
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(tableViewModel:canMoveObject:atIndexPath:inTableView:)]) {
id object = [self objectAtIndexPath:indexPath];
return [self.delegate tableViewModel:self canMoveObject:object atIndexPath:indexPath inTableView:tableView];
} else {
return NO;
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id object = [self objectAtIndexPath:sourceIndexPath];
BOOL shouldMove = YES;
if ([self.delegate respondsToSelector:@selector(tableViewModel:shouldMoveObject:atIndexPath:toIndexPath:inTableView:)]) {
shouldMove = [self.delegate tableViewModel:self shouldMoveObject:object atIndexPath:sourceIndexPath toIndexPath:destinationIndexPath inTableView:tableView];
}
if (shouldMove) {
[self removeObjectAtIndexPath:sourceIndexPath];
[self insertObject:object atRow:destinationIndexPath.row inSection:destinationIndexPath.section];
}
}
@end
@implementation NITableViewModelSection (Mutable)
- (NSMutableArray *)mutableRows {
NIDASSERT([self.rows isKindOfClass:[NSMutableArray class]] || nil == self.rows);
self.rows = nil == self.rows ? [NSMutableArray array] : self.rows;
return (NSMutableArray *)self.rows;
}
@end