From 02e95ed15add76797faada5ec1c62bf2dc09fb6c Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Mon, 19 Jun 2017 12:19:25 -0700 Subject: [PATCH] add snapshotAtIndex to indexed data sources --- FirebaseDatabaseUI/FUIIndexArray.h | 2 +- FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h | 8 ++++++++ FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m | 4 ++++ FirebaseDatabaseUI/FUIIndexTableViewDataSource.h | 8 ++++++++ FirebaseDatabaseUI/FUIIndexTableViewDataSource.m | 4 ++++ .../FUIIndexCollectionViewDataSourceTest.m | 6 ++++++ FirebaseDatabaseUITests/FUIIndexTableViewDataSourceTest.m | 6 ++++++ 7 files changed, 37 insertions(+), 1 deletion(-) diff --git a/FirebaseDatabaseUI/FUIIndexArray.h b/FirebaseDatabaseUI/FUIIndexArray.h index c33f3f0093a..2a87571a2ac 100644 --- a/FirebaseDatabaseUI/FUIIndexArray.h +++ b/FirebaseDatabaseUI/FUIIndexArray.h @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN @optional /** - * Delegate method called when the database reference at an index has + * Delegate method called when the database reference at an index has * finished loading its contents. * @param array The array containing the reference. * @param ref The reference that was loaded. diff --git a/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h b/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h index 9f0991af5c8..b6f9e40074a 100644 --- a/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h +++ b/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h @@ -91,6 +91,14 @@ didFailLoadAtIndex:(NSUInteger)index NSIndexPath *indexPath, FIRDataSnapshot *_Nullable snap))populateCell NS_DESIGNATED_INITIALIZER; +/** + * Returns the snapshot at the given index, if it has loaded. + * Raises a fatal error if the index is out of bounds. + * @param index The index of the requested snapshot. + * @return A snapshot, or nil if one has not yet been loaded. + */ +- (nullable FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index; + @end @interface UICollectionView (FUIIndexCollectionViewDataSource) diff --git a/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m b/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m index 97cd70aba57..a919f489cd9 100644 --- a/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m +++ b/FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m @@ -54,6 +54,10 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery return self.array.indexes; } +- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index { + return [self.array objectAtIndex:index]; +} + #pragma mark - FUIIndexArrayDelegate - (void)array:(FUIIndexArray *)array diff --git a/FirebaseDatabaseUI/FUIIndexTableViewDataSource.h b/FirebaseDatabaseUI/FUIIndexTableViewDataSource.h index f73ca2487f8..b68555f2fbd 100644 --- a/FirebaseDatabaseUI/FUIIndexTableViewDataSource.h +++ b/FirebaseDatabaseUI/FUIIndexTableViewDataSource.h @@ -91,6 +91,14 @@ didFailLoadAtIndex:(NSUInteger)index NSIndexPath *indexPath, FIRDataSnapshot *_Nullable snap))populateCell NS_DESIGNATED_INITIALIZER; +/** + * Returns the snapshot at the given index, if it has loaded. + * Raises a fatal error if the index is out of bounds. + * @param index The index of the requested snapshot. + * @return A snapshot, or nil if one has not yet been loaded. + */ +- (nullable FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index; + @end @interface UITableView (FUIIndexTableViewDataSource) diff --git a/FirebaseDatabaseUI/FUIIndexTableViewDataSource.m b/FirebaseDatabaseUI/FUIIndexTableViewDataSource.m index 4ff9df08abc..f87e5be6741 100644 --- a/FirebaseDatabaseUI/FUIIndexTableViewDataSource.m +++ b/FirebaseDatabaseUI/FUIIndexTableViewDataSource.m @@ -62,6 +62,10 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery return self.array.indexes; } +- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index { + return [self.array objectAtIndex:index]; +} + #pragma mark - FUIIndexArrayDelegate - (void)array:(FUIIndexArray *)array diff --git a/FirebaseDatabaseUITests/FUIIndexCollectionViewDataSourceTest.m b/FirebaseDatabaseUITests/FUIIndexCollectionViewDataSourceTest.m index 6941cd12090..b7f0201059c 100644 --- a/FirebaseDatabaseUITests/FUIIndexCollectionViewDataSourceTest.m +++ b/FirebaseDatabaseUITests/FUIIndexCollectionViewDataSourceTest.m @@ -183,4 +183,10 @@ - (void)testItUpdatesOnMove { XCTAssertEqualObjects(cell.accessibilityValue, @"3"); } +- (void)testItReturnsSnapshotsFromItsIndexArray { + FIRDataSnapshot *snap = [self.dataSource snapshotAtIndex:0]; + XCTAssertEqualObjects(snap.key, @"data", @"expected snap's key to equal 'data', got %@ instead", snap.key); + XCTAssertEqualObjects(snap.value, @"1", @"expected snap's key to equal '1', got %@ instead", snap.value); +} + @end diff --git a/FirebaseDatabaseUITests/FUIIndexTableViewDataSourceTest.m b/FirebaseDatabaseUITests/FUIIndexTableViewDataSourceTest.m index 3072df7439d..31117323c7a 100644 --- a/FirebaseDatabaseUITests/FUIIndexTableViewDataSourceTest.m +++ b/FirebaseDatabaseUITests/FUIIndexTableViewDataSourceTest.m @@ -178,4 +178,10 @@ - (void)testItUpdatesOnMove { XCTAssertEqualObjects(cell.accessibilityValue, @"3"); } +- (void)testItReturnsSnapshotsFromItsIndexArray { + FIRDataSnapshot *snap = [self.dataSource snapshotAtIndex:0]; + XCTAssertEqualObjects(snap.key, @"data", @"expected snap's key to equal 'data', got %@ instead", snap.key); + XCTAssertEqualObjects(snap.value, @"1", @"expected snap's key to equal '1', got %@ instead", snap.value); +} + @end