Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -75,7 +77,7 @@
<EnvironmentVariable
key = "OS_ACTIVITY_MODE"
value = "disable"
isEnabled = "YES">
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
Expand Down
20 changes: 17 additions & 3 deletions Example/LPDCollectionViewKit/LPDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ - (void)viewDidLoad {

UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
collectionViewFlowLayout.itemSize = CGSizeMake((UIScreen.width - 30) / 2, (UIScreen.width - 30) / 2);
collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
collectionViewFlowLayout.itemSize = CGSizeMake((UIScreen.width - 60) / 3 , (UIScreen.width - 60) / 3);
collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
collectionViewFlowLayout.minimumLineSpacing = 10;
collectionViewFlowLayout.minimumInteritemSpacing = 10;
collectionViewFlowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 30);
collectionViewFlowLayout.footerReferenceSize = CGSizeMake(self.view.bounds.size.width, 1);

Expand Down Expand Up @@ -68,6 +70,9 @@ - (void)viewDidLoad {

UIBarButtonItem *insertSectionBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"is" style:UIBarButtonItemStylePlain target:self action:@selector(insertSection)];

UIBarButtonItem *scrollToItemAtIndexPathBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"scroll" style:UIBarButtonItemStylePlain target:self action:@selector(scrollToItem)];

self.navigationController.toolbarHidden = NO;
[self setToolbarItems:@[addCellBarButtonItem,
Expand All @@ -77,7 +82,8 @@ - (void)viewDidLoad {
removeCellBarButtonItem,
removeCellsBarButtonItem,
replaceCellsBarButtonItem,
insertSectionBarButtonItem,]
insertSectionBarButtonItem,
scrollToItemAtIndexPathBarButtonItem]
animated:YES];

@weakify(self);
Expand Down Expand Up @@ -209,4 +215,12 @@ - (void)insertSection {
[self.collectionViewModel addSectionViewModel:sectionViewModel];
}

- (void)scrollToItem {
NSArray *sections = self.collectionViewModel.collectionViewModelSections;
NSArray *items = [[sections objectAtIndex:sections.count-1] items];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:items.count-1 inSection:sections.count-1];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
[self.collectionViewModel scrollToCollectionItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}

@end
141 changes: 74 additions & 67 deletions LPDCollectionViewKit/Classes/LPDCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,104 +20,111 @@ @interface LPDCollectionView ()
@implementation LPDCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
self = [super initWithFrame:frame collectionViewLayout:layout];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0.9214 green:0.9206 blue:0.9458 alpha:1.0];
}
return self;
self = [super initWithFrame:frame collectionViewLayout:layout];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0.9214 green:0.9206 blue:0.9458 alpha:1.0];
}
return self;
}

- (void)bindingTo:(__kindof id<LPDCollectionViewModelProtocol>)viewModel {
NSParameterAssert(viewModel);

self.viewModel = viewModel;
LPDCollectionViewModel *collectionViewModel = self.viewModel;
super.delegate = collectionViewModel.delegate;
super.dataSource = collectionViewModel.dataSource;


@weakify(self);
[[[collectionViewModel.reloadDataSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(id x) {
NSParameterAssert(viewModel);
self.viewModel = viewModel;
LPDCollectionViewModel *collectionViewModel = self.viewModel;
super.delegate = collectionViewModel.delegate;
super.dataSource = collectionViewModel.dataSource;
@weakify(self);
[[[collectionViewModel.reloadDataSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(id x) {
@strongify(self);
[self reloadData];
}];

[[[collectionViewModel.insertSectionsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSIndexSet *indexSet) {
}];

[[[collectionViewModel.scrollToItemAtIndexPathSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(RACTuple *tuple) {
@strongify(self);
[self scrollToItemAtIndexPath:tuple.first atScrollPosition:[tuple.second integerValue] animated:[tuple.third boolValue]];
}];

[[[collectionViewModel.insertSectionsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSIndexSet *indexSet) {
@strongify(self);
[self insertSections:indexSet];
}];

}];
[[[collectionViewModel.deleteSectionsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSIndexSet *indexSet) {
@strongify(self);
[self deleteSections:indexSet];
}];

}];
[[[collectionViewModel.replaceSectionsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSIndexSet *indexSet) {
@strongify(self);
[self performBatchUpdates:^{
[self deleteSections:indexSet];
[self insertSections:indexSet];
} completion:nil];
}];

[self performBatchUpdates:^{
[self deleteSections:indexSet];
[self insertSections:indexSet];
} completion:nil];
}];
[[[collectionViewModel.reloadSectionsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSIndexSet *indexSet) {
@strongify(self);
[self reloadSections:indexSet];
}];

}];
[[[collectionViewModel.insertItemsAtIndexPathsSignal takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(RACTuple *tuple) {
@strongify(self);
if (tuple.second) {
[self performBatchUpdates:^{
[self insertSections:tuple.second];
[self insertItemsAtIndexPaths:tuple.first];
} completion:nil];
} else {
[self insertItemsAtIndexPaths:tuple.first];
}
@strongify(self);
if (tuple.second) {
[self performBatchUpdates:^{
[self insertSections:tuple.second];
[self insertItemsAtIndexPaths:tuple.first];
} completion:nil];
} else {
[self insertItemsAtIndexPaths:tuple.first];
}
}];

[[[collectionViewModel.deleteItemsAtIndexPathsSignal
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSArray<NSIndexPath *> *indexPaths) {
@strongify(self);
[self deleteItemsAtIndexPaths:indexPaths];
}];

[[[collectionViewModel.reloadItemsAtIndexPathsSignal
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSArray<NSIndexPath *> *indexPaths) {
@strongify(self);
[self reloadItemsAtIndexPaths:indexPaths];
}];

[[[collectionViewModel.replaceItemsAtIndexPathsSignal
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]] deliverOnMainThread] subscribeNext:^(RACTuple *tuple) {
@strongify(self);
[self performBatchUpdates:^{
if (tuple.third) {
[self insertSections:tuple.third];
[self insertItemsAtIndexPaths:tuple.second];
} else {
[self deleteItemsAtIndexPaths:tuple.first];
[self insertItemsAtIndexPaths:tuple.second];
}
} completion:nil];
}];
}];
[[[collectionViewModel.reloadItemsAtIndexPathsSignal
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]]
deliverOnMainThread] subscribeNext:^(NSArray<NSIndexPath *> *indexPaths) {
@strongify(self);
[self reloadItemsAtIndexPaths:indexPaths];
}];
[[[collectionViewModel.replaceItemsAtIndexPathsSignal
takeUntil:[self rac_signalForSelector:@selector(removeFromSuperview)]] deliverOnMainThread] subscribeNext:^(RACTuple *tuple) {
@strongify(self);
[self performBatchUpdates:^{
if (tuple.third) {
[self insertSections:tuple.third];
[self insertItemsAtIndexPaths:tuple.second];
} else {
[self deleteItemsAtIndexPaths:tuple.first];
[self insertItemsAtIndexPaths:tuple.second];
}
} completion:nil];
}];
}

- (void)setDelegate:(id<UICollectionViewDelegate>)delegate {
[self.viewModel setScrollViewDelegate:delegate];
[self.viewModel setScrollViewDelegate:delegate];
}

- (void)setDataSource:(id<UICollectionViewDataSource>)dataSource {
// do nothing
// do nothing
}

@end

11 changes: 6 additions & 5 deletions LPDCollectionViewKit/Classes/LPDCollectionViewModel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ NS_ASSUME_NONNULL_BEGIN

#pragma mark - data signal

@property (nullable, nonatomic, strong, readonly) RACSignal *reloadDataSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *insertSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *deleteSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *replaceSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *reloadSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *reloadDataSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *scrollToItemAtIndexPathSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *insertSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *deleteSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *replaceSectionsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *reloadSectionsSignal; // 请勿订阅此信号

@property (nullable, nonatomic, strong, readonly) RACSignal *insertItemsAtIndexPathsSignal; // 请勿订阅此信号
@property (nullable, nonatomic, strong, readonly) RACSignal *deleteItemsAtIndexPathsSignal; // 请勿订阅此信号
Expand Down
2 changes: 2 additions & 0 deletions LPDCollectionViewKit/Classes/LPDCollectionViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface LPDCollectionViewModel : NSObject <LPDCollectionViewModelProtocol>

@property (readonly, nonatomic, getter = getSections) NSArray *collectionViewModelSections;

+ (instancetype) new NS_UNAVAILABLE;

@end
Expand Down
27 changes: 27 additions & 0 deletions LPDCollectionViewKit/Classes/LPDCollectionViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ @interface LPDCollectionViewModel ()
@property (nonatomic, strong) LPDCollectionViewFactory *collectionViewFactory;

@property (nonatomic, strong) RACSubject *reloadDataSubject;
@property (nonatomic, strong) RACSubject *scrollToItemAtIndexPathSubject;

@property (nonatomic, strong) RACSubject *insertSectionsSubject;
@property (nonatomic, strong) RACSubject *deleteSectionsSubject;
Expand Down Expand Up @@ -76,6 +77,9 @@ @implementation LPDCollectionViewModel {
id<UICollectionViewDataSource> _dataSource;
}

- (NSArray *)getSections {
return [NSArray arrayWithArray:_sections];
}

- (instancetype)init {
if (self = [super init]) {
Expand Down Expand Up @@ -176,6 +180,25 @@ - (NSInteger)sectionIndexForFooterViewModel:(__kindof id<LPDCollectionItemViewMo
return nil;
}

- (void)scrollToCollectionItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
BOOL isHave = NO;
for (NSUInteger section=0; section < self.sections.count; section++) {
LPDCollectionSectionViewModel *sectionViewModel = self.sections[section];
for (NSUInteger row=0; row < sectionViewModel.mutableItems.count; row++) {
NSIndexPath *indexPathItem = [NSIndexPath indexPathForRow:row inSection:section];
if (indexPathItem == indexPath) {
isHave = YES;
}
}
}
if (isHave) {
[self.scrollToItemAtIndexPathSubject sendNext:RACTuplePack(indexPath,@(scrollPosition),@(animated))];
} else {
NSLog(@"scroll indexPath 超出范围!!");
return;
}
}

- (void)addCellViewModel:(__kindof id<LPDCollectionItemViewModelProtocol>)cellViewModel {
NSUInteger sectionIndex = self.sections.count > 0 ? self.sections.count - 1 : 0;
[self addCellViewModel:cellViewModel toSection:sectionIndex];
Expand Down Expand Up @@ -554,6 +577,10 @@ - (void)replaceSectionWithCellViewModels:(NSArray<__kindof id<LPDCollectionItemV
- (RACSignal *)reloadDataSignal {
return _reloadDataSubject ?: (_reloadDataSubject = [[RACSubject subject] setNameWithFormat:@"reloadDataSignal"]);
}

- (RACSignal *)scrollToItemAtIndexPathSignal {
return _scrollToItemAtIndexPathSubject ?: (_scrollToItemAtIndexPathSubject = [[RACSubject subject] setNameWithFormat:@"scrollToItemAtIndexPathSignal"]);
}

- (RACSignal *)insertSectionsSignal {
return _insertSectionsSubject
Expand Down
4 changes: 4 additions & 0 deletions LPDCollectionViewKit/Classes/LPDCollectionViewModelProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable __kindof id<LPDCollectionItemViewModelProtocol>)footerViewModelFromSection:(NSInteger)sectionIndex;

#pragma mark - scrollToItem methods

- (void)scrollToCollectionItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;

#pragma mark - add cells methods

/**
Expand Down