Skip to content

Commit

Permalink
Merge pull request #162 from ramblex/master
Browse files Browse the repository at this point in the history
Allow elements to be moved to start of FirebaseArray (potential fix for #129)
  • Loading branch information
morganchen12 authored Oct 10, 2016
2 parents 4fd5e28 + d92efb6 commit 6a403fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion FirebaseDatabaseUI/FirebaseArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ - (void)initListeners {
NSUInteger fromIndex = [self indexForKey:snapshot.key];
[self.snapshots removeObjectAtIndex:fromIndex];

NSUInteger toIndex = [self indexForKey:previousChildKey] + 1;
NSUInteger toIndex = 0;
if (previousChildKey != nil) {
NSUInteger prevIndex = [self indexForKey:previousChildKey];
if (prevIndex != NSNotFound) {
toIndex = prevIndex + 1;
}
}
[self.snapshots insertObject:snapshot atIndex:toIndex];

if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
Expand Down
64 changes: 64 additions & 0 deletions FirebaseDatabaseUITests/FirebaseArrayTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,68 @@ - (void)testFirebaseArrayCanMoveElement {
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
}

- (void)testFirebaseArrayCanMoveElementToStart {
[self.observable populateWithCount:10];
self.snap.key = @"8";

// Test delegate
__block BOOL delegateWasCalled = NO;
__block BOOL expectedParametersWereCorrect = NO;
self.arrayDelegate.didMoveObject = ^(FirebaseArray *array, id object, NSUInteger from, NSUInteger to) {
// Xcode complains about retain cycles if an XCTAssert is placed in here.
delegateWasCalled = YES;
expectedParametersWereCorrect = (array == self.firebaseArray &&
object == self.snap &&
from == 8 && to == 0);
};

// Move 8 to the start
[self.observable sendEvent:FIRDataEventTypeChildMoved withObject:self.snap previousKey:@"" error:nil];

// Array expectation
NSArray *items = self.firebaseArray.items;
NSArray *expected = @[@"8", @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"9"];
NSMutableArray *result = [NSMutableArray array];
for (FUIFakeSnapshot *snapshot in items) {
[result addObject:snapshot.key];
}
XCTAssert([result isEqual:expected], @"expected firebaseArray contents to equal %@, got %@", expected, [result copy]);

// Delegate expectations
XCTAssert(delegateWasCalled, @"expected delegate to receive callback for deletion");
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
}

- (void)testFirebaseArrayMovesElementToStartWithNilPreviousKey {
[self.observable populateWithCount:10];
self.snap.key = @"6";

// Test delegate
__block BOOL delegateWasCalled = NO;
__block BOOL expectedParametersWereCorrect = NO;
self.arrayDelegate.didMoveObject = ^(FirebaseArray *array, id object, NSUInteger from, NSUInteger to) {
// Xcode complains about retain cycles if an XCTAssert is placed in here.
delegateWasCalled = YES;
expectedParametersWereCorrect = (array == self.firebaseArray &&
object == self.snap &&
from == 6 && to == 0);
};

// Move 8 to the start
[self.observable sendEvent:FIRDataEventTypeChildMoved withObject:self.snap previousKey:nil error:nil];

// Array expectation
NSArray *items = self.firebaseArray.items;
NSArray *expected = @[@"6", @"0", @"1", @"2", @"3", @"4", @"5", @"7", @"8", @"9"];
NSMutableArray *result = [NSMutableArray array];
for (FUIFakeSnapshot *snapshot in items) {
[result addObject:snapshot.key];
}
XCTAssert([result isEqual:expected], @"expected firebaseArray contents to equal %@, got %@", expected, [result copy]);

// Delegate expectations
XCTAssert(delegateWasCalled, @"expected delegate to receive callback for deletion");
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
}

@end

0 comments on commit 6a403fa

Please sign in to comment.