forked from Andrenalin/ViewModelKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVMKMutableArrayDataSource.m
More file actions
91 lines (69 loc) · 2.22 KB
/
Copy pathVMKMutableArrayDataSource.m
File metadata and controls
91 lines (69 loc) · 2.22 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
//
// VMKMutableArrayDataSource.m
// ViewModelKit
//
// Created by Daniel Rinser on 09.11.16.
// Copyright © 2016 Andre Trettin. All rights reserved.
//
#import "VMKMutableArrayDataSource+Private.h"
@implementation VMKMutableArrayDataSource
#pragma mark - init
- (instancetype)initWithArrayModel:(VMKArrayModel *)arrayModel factory:(nullable id<VMKCellViewModelFactory>)factory {
self = [super init];
if (self) {
_arrayModel = arrayModel;
_factory = factory;
}
return self;
}
- (void)setUpBinding {
[self.arrayUpdater bindArray];
}
#pragma mark - Accessors
- (VMKViewModelCache *)viewModelCache {
if (!_viewModelCache) {
_viewModelCache = [[VMKViewModelCache alloc] init];
}
return _viewModelCache;
}
- (VMKArrayUpdater *)arrayUpdater {
if (_arrayUpdater) {
return _arrayUpdater;
}
_arrayUpdater = [[VMKArrayUpdater alloc] initWithArrayModel:self.arrayModel delegate:self];
return _arrayUpdater;
}
#pragma mark - VMKDataSourceType
- (NSInteger)sections {
return 1;
}
- (NSInteger)rowsInSection:(NSInteger)section {
return (NSInteger)self.arrayModel.count;
}
- (__kindof VMKViewModel<VMKCellType> *)viewModelAtIndexPath:(NSIndexPath *)indexPath {
VMKViewModel<VMKCellType> *vm = [self.viewModelCache viewModelAtIndexPath:indexPath];
if (vm) {
return vm;
}
return [self insertedViewModelInCacheAtIndexPath:indexPath];
}
#pragma mark - cache
- (nullable __kindof VMKViewModel<VMKCellType> *)insertedViewModelInCacheAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.arrayModel objectAtIndex:(NSUInteger)indexPath.row];
if (!object) {
return nil;
}
__kindof VMKViewModel<VMKCellType> *vm = [self.factory cellViewModelForDataSource:self object:object];
if (vm) {
[self.viewModelCache setViewModel:vm atIndexPath:indexPath];
}
return vm;
}
#pragma mark - VMKArrayUpdaterDelegate
- (void)arrayUpdater:(VMKArrayUpdater *)arrayUpdater didChangeWithChangeSet:(VMKChangeSet *)changeSet {
if (changeSet.history.count > 0) {
[self.viewModelCache changeCacheWithChangeSet:changeSet];
[self.delegate dataSource:self didUpdateChangeSet:changeSet];
}
}
@end