Last active
December 30, 2015 21:39
-
-
Save notxcain/7889166 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// QWListChange.h | |
// | |
// Created by Denis Mikhaylov on 04/12/13. | |
// Copyright (c) 2013 QIWI. All rights reserved. | |
// | |
// Header | |
typedef NS_ENUM(NSInteger, QWListChangeType) | |
{ | |
QWListChangeInsert = 1, | |
QWListChangeDelete = 2, | |
QWListChangeMove = 3, | |
QWListChangeUpdate = 4 | |
}; | |
@protocol QWSectionedList; | |
@interface QWSectionedListChange : NSObject | |
+ (instancetype)batchWithChanges:(NSArray *)changes; | |
+ (instancetype)changeOfItem:(id)item atIndexPath:(NSIndexPath *)indexPath type:(QWListChangeType)type newIndexPath:(NSIndexPath *)newIndexPath; | |
+ (instancetype)changeOfSectionAtIndex:(NSUInteger)index type:(QWListChangeType)type; | |
- (void)applyToList:(id <QWSectionedList>)list; | |
@end | |
@protocol QWSectionedList <NSObject> | |
- (void)beginUpdates; | |
- (void)changeSectionAtIndex:(NSUInteger)sectionIndex changeType:(QWListChangeType)type; | |
- (void)changeItem:(id)item atIndexPath:(NSIndexPath *)indexPath changeType:(QWListChangeType)type newIndexPath:(NSIndexPath *)newIndexPath; | |
- (void)endUpdates; | |
@end | |
// Implementation | |
@interface QWBlockBasedListChange : QWSectionedListChange | |
@property (nonatomic, copy, readonly) void (^block)(id <QWSectionedList>); | |
- (id)initWithBlock:(void (^)(id <QWSectionedList> list))block; | |
@end | |
@implementation QWSectionedListChange | |
+ (instancetype)batchWithChanges:(NSArray *)changes | |
{ | |
return [[QWBlockBasedListChange alloc] initWithBlock:^(id<QWSectionedList> list) { | |
[list beginUpdates]; | |
[changes each:^(id x) { | |
[x applyToList:list]; | |
}]; | |
[list endUpdates]; | |
}]; | |
} | |
+ (instancetype)changeOfItem:(id)item atIndexPath:(NSIndexPath *)indexPath type:(QWListChangeType)type newIndexPath:(NSIndexPath *)newIndexPath | |
{ | |
return [[QWBlockBasedListChange alloc] initWithBlock:^(id<QWSectionedList> list) { | |
[list changeItem:item atIndexPath:indexPath changeType:type newIndexPath:newIndexPath]; | |
}]; | |
} | |
+ (instancetype)changeOfSectionAtIndex:(NSUInteger)index type:(QWListChangeType)type | |
{ | |
return [[QWBlockBasedListChange alloc] initWithBlock:^(id<QWSectionedList> list) { | |
[list changeSectionAtIndex:index changeType:type]; | |
}]; | |
} | |
- (void)applyToList:(id<QWSectionedList>)list | |
{ | |
} | |
@end | |
@implementation QWBlockBasedListChange | |
- (id)initWithBlock:(void (^)(id<QWSectionedList>))block | |
{ | |
self = [super init]; | |
if (self) { | |
_block = [block copy]; | |
} | |
return self; | |
} | |
- (void)applyToList:(id<QWSectionedList>)list | |
{ | |
self.block(list); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment