Skip to content

Commit

Permalink
🐞fixed crash in collection view supplementaries reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksana Kovalchuk committed Nov 18, 2016
1 parent 2883c99 commit de88c78
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "ANListViewInterface.h"
#import "ANListCollectionView.h"
#import "ANListController+Interitance.h"
#import "Alister.h"

@implementation ANCollectionController

Expand Down Expand Up @@ -105,4 +106,18 @@ - (BOOL)_isExistMappingForSection:(NSInteger)section kind:(NSString*)kind
return (model != nil);
}

//- (NSString*)_handleStorageKind:(NSString*)existingKind
//{
// if ([existingKind isEqualToString:UICollectionElementKindSectionHeader])
// {
// existingKind = ANListDefaultHeaderKind;
// }
// else if ([existingKind isEqualToString:UICollectionElementKindSectionFooter])
// {
// existingKind = ANListDefaultFooterKind;
// }
//
// return existingKind;
//}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
- (ANListControllerItemsHandler*)itemsHandler;

- (instancetype)initWithListView:(id<ANListViewInterface>)listView;
- (void)storageWasAttached:(ANStorage*)storage;

@end
3 changes: 3 additions & 0 deletions Alister/ANListController/Core/Controller/ANListController.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ - (void)_attachStorage:(ANStorage*)storage
{
storage.updatesHandler = self.updateService;
[self.updateService storageNeedsReloadWithIdentifier:storage.identifier animated:NO];

[storage updateHeaderKind:[self.listView defaultHeaderKind]
footerKind:[self.listView defaultFooterKind]];
}

- (ANListControllerSearchManager*)searchManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ - (instancetype)initWithListView:(id<ANListViewInterface>)listView

- (void)registerFooterClass:(Class)viewClass forModelClass:(Class)modelClass
{
[self registerSupplementaryClass:viewClass forModelClass:modelClass kind:ANListDefaultFooterKind];
[self registerSupplementaryClass:viewClass forModelClass:modelClass kind:[self.listView defaultFooterKind]];
}

- (void)registerHeaderClass:(Class)viewClass forModelClass:(Class)modelClass
{
[self registerSupplementaryClass:viewClass forModelClass:modelClass kind:ANListDefaultHeaderKind];
[self registerSupplementaryClass:viewClass forModelClass:modelClass kind:[self.listView defaultHeaderKind]];
}

- (void)registerSupplementaryClass:(Class)supplementaryClass forModelClass:(Class)modelClass kind:(NSString*)kind
Expand Down
11 changes: 11 additions & 0 deletions Alister/ANListController/Core/ViewWrappers/ANListCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ - (id)defaultSupplementary
return [UICollectionReusableView new];
}

- (NSString*)defaultHeaderKind
{
return UICollectionElementKindSectionHeader;
}

- (NSString*)defaultFooterKind
{
return UICollectionElementKindSectionFooter;
}

- (void)performUpdate:(ANStorageUpdateModel*)update animated:(BOOL)animated //TODO: handle animation
{
UICollectionView* collectionView = self.collectionView;
Expand Down Expand Up @@ -155,6 +165,7 @@ - (void)performUpdate:(ANStorageUpdateModel*)update animated:(BOOL)animated //TO
}
}


#pragma mark - workarounds

// This is to prevent a bug in UICollectionView from occurring.
Expand Down
11 changes: 11 additions & 0 deletions Alister/ANListController/Core/ViewWrappers/ANListTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "ANListTableView.h"
#import "ANStorageUpdateModel.h"
#import "ANTableUpdateConfigurationModel.h"
#import "Alister.h"

@interface ANListTableView ()

Expand Down Expand Up @@ -101,6 +102,16 @@ - (id)defaultSupplementary
return [UITableViewHeaderFooterView new];
}

- (NSString*)defaultFooterKind
{
return ANListDefaultFooterKind;
}

- (NSString*)defaultHeaderKind
{
return ANListDefaultHeaderKind;
}

- (void)performUpdate:(ANStorageUpdateModel*)update animated:(BOOL)animated
{
UITableView* tableView = self.tableView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@

- (void)performUpdate:(ANStorageUpdateModel*)update animated:(BOOL)animated;

- (NSString*)defaultHeaderKind;
- (NSString*)defaultFooterKind;

@end
2 changes: 2 additions & 0 deletions Alister/ANStorage/Models/Section/ANStorageSectionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)replaceItemAtIndex:(NSInteger)index withItem:(id)item;

- (void)replaceSupplementaryKind:(NSString*)kind onKind:(NSString*)newKind;

@end

NS_ASSUME_NONNULL_END
14 changes: 14 additions & 0 deletions Alister/ANStorage/Models/Section/ANStorageSectionModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,18 @@ - (NSDictionary*)supplementaryObjects
return [self.supplementaries copy];
}

- (void)replaceSupplementaryKind:(NSString*)kind onKind:(NSString*)newKind
{
NSMutableDictionary* supplementaries = [self.supplementaries mutableCopy];

[[self.supplementaries allKeys] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

if ([obj isEqualToString:kind])
{
[supplementaries setObject:[supplementaries objectForKey:kind] forKey:newKind];
[supplementaries removeObjectForKey:kind];
}
}];
}

@end
19 changes: 19 additions & 0 deletions Alister/ANStorage/Models/Storage/ANStorageModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ - (instancetype)init
return self;
}

- (void)setHeaderKind:(NSString*)headerKind
{
__block NSString* oldKind = _headerKind;
[self.sectionModels enumerateObjectsUsingBlock:^(ANStorageSectionModel* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj replaceSupplementaryKind:oldKind onKind:headerKind];
}];
_headerKind = headerKind;
}

- (void)setFooterKind:(NSString*)footerKind
{
__block NSString* oldKind = _footerKind;
[self.sectionModels enumerateObjectsUsingBlock:^(ANStorageSectionModel* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj replaceSupplementaryKind:oldKind onKind:footerKind];
}];
_footerKind = footerKind;

}

- (NSArray*)sections
{
return [self.sectionModels copy];
Expand Down
2 changes: 1 addition & 1 deletion Alister/ANStorage/Storage/ANStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ - (void)updateSectionFooterModel:(id)footerModel forSectionIndex:(NSInteger)sect
[self.updater updateSectionFooterModel:footerModel forSectionIndex:sectionIndex];
}

- (NSString *)debugDescription
- (NSString*)debugDescription
{
NSMutableString* string = [NSMutableString string];
[string appendFormat:@"ID: %@\n", self.identifier];
Expand Down
Loading

0 comments on commit de88c78

Please sign in to comment.