diff --git a/TGRDataSource.xcodeproj/project.pbxproj b/TGRDataSource.xcodeproj/project.pbxproj index 83e3c76..e731a02 100644 --- a/TGRDataSource.xcodeproj/project.pbxproj +++ b/TGRDataSource.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 990C568719017CB400E382F1 /* TGRDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568619017CB400E382F1 /* TGRDataSource.m */; }; 990C568A19017DAD00E382F1 /* NSObject+Abstract.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568919017DAD00E382F1 /* NSObject+Abstract.m */; }; 990C568D19017EBA00E382F1 /* TGRArrayDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */; }; + 993DB7E71901990000653166 /* TGRFetchedResultsDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -57,6 +58,8 @@ 990C568919017DAD00E382F1 /* NSObject+Abstract.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+Abstract.m"; sourceTree = ""; }; 990C568B19017EBA00E382F1 /* TGRArrayDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TGRArrayDataSource.h; sourceTree = ""; }; 990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TGRArrayDataSource.m; sourceTree = ""; }; + 993DB7E51901990000653166 /* TGRFetchedResultsDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TGRFetchedResultsDataSource.h; sourceTree = ""; }; + 993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TGRFetchedResultsDataSource.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -121,6 +124,8 @@ 990C568619017CB400E382F1 /* TGRDataSource.m */, 990C568B19017EBA00E382F1 /* TGRArrayDataSource.h */, 990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */, + 993DB7E51901990000653166 /* TGRFetchedResultsDataSource.h */, + 993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */, ); path = TGRDataSource; sourceTree = ""; @@ -234,6 +239,7 @@ buildActionMask = 2147483647; files = ( 990C568719017CB400E382F1 /* TGRDataSource.m in Sources */, + 993DB7E71901990000653166 /* TGRFetchedResultsDataSource.m in Sources */, 990C568A19017DAD00E382F1 /* NSObject+Abstract.m in Sources */, 990C568D19017EBA00E382F1 /* TGRArrayDataSource.m in Sources */, ); @@ -416,6 +422,7 @@ 990C568119017A3400E382F1 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 990C568219017A3400E382F1 /* Build configuration list for PBXNativeTarget "TGRDataSourceTests" */ = { isa = XCConfigurationList; @@ -424,6 +431,7 @@ 990C568419017A3400E382F1 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/TGRDataSource/TGRFetchedResultsDataSource.h b/TGRDataSource/TGRFetchedResultsDataSource.h new file mode 100644 index 0000000..33223d6 --- /dev/null +++ b/TGRDataSource/TGRFetchedResultsDataSource.h @@ -0,0 +1,50 @@ +// TGRFetchedResultsDataSource.h +// +// Copyright (c) 2014 Guillermo Gonzalez +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import "TGRDataSource.h" + +/** + A `TGRDataSource` subclass that takes data from a `NSFetchedResultsController`. + */ +@interface TGRFetchedResultsDataSource : TGRDataSource + +/** + The `NSFetchedResultsController` object managed by this data source. + */ +@property (strong, nonatomic, readonly) NSFetchedResultsController *fetchedResultsController; + +/** + Initializes the data source. + + @param controller The `NSFetchedResultsController` object managed by this data source. + @param reuseIdentifier The cell reuse identifier. + @param configureCellBlock A block that will be called when the view asks for a cell in a particular location. + + @return An initialized data source. + */ +- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller + cellReuseIdentifier:(NSString *)reuseIdentifier + configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock; + +@end diff --git a/TGRDataSource/TGRFetchedResultsDataSource.m b/TGRDataSource/TGRFetchedResultsDataSource.m new file mode 100644 index 0000000..d5b6d13 --- /dev/null +++ b/TGRDataSource/TGRFetchedResultsDataSource.m @@ -0,0 +1,72 @@ +// TGRFetchedResultsDataSource.m +// +// Copyright (c) 2014 Guillermo Gonzalez +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "TGRFetchedResultsDataSource.h" + +@implementation TGRFetchedResultsDataSource + +- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller + cellReuseIdentifier:(NSString *)reuseIdentifier + configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock +{ + self = [super initWithCellReuseIdentifier:reuseIdentifier configureCellBlock:configureCellBlock]; + + if (self) { + _fetchedResultsController = controller; + } + + return self; +} + +- (id)itemAtIndexPath:(NSIndexPath *)indexPath { + return [self.fetchedResultsController objectAtIndexPath:indexPath]; +} + +- (NSIndexPath *)indexPathForItem:(id)item { + return [self.fetchedResultsController indexPathForObject:item]; +} + +#pragma mark - UITableViewDataSource + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return [self.fetchedResultsController.sections count]; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + id sectionInfo = self.fetchedResultsController.sections[section]; + return [sectionInfo numberOfObjects]; +} + +#pragma mark - UICollectionViewDataSource + +- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { + return [self.fetchedResultsController.sections count]; +} + +- (NSInteger)collectionView:(UICollectionView *)collectionView + numberOfItemsInSection:(NSInteger)section +{ + id sectionInfo = self.fetchedResultsController.sections[section]; + return [sectionInfo numberOfObjects]; +} + +@end