Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from lyft/demo-broken-dialog-transitions
Browse files Browse the repository at this point in the history
Fix dialog transitions
  • Loading branch information
Alan Chiu committed Mar 27, 2016
2 parents b790f21 + 1b29663 commit af89633
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,28 @@ public class SlideDownTransition implements ScreenTransition {

@Override
public void transition(final ViewGroup root, final View from, final View to, final TransitionListener transitionListener) {
Animator animator = createAnimator(from);
if (to == null) {
root.removeView(from);
return;
}

Animator animator = createAnimator(to);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
((ViewGroup) from.getParent()).removeView(from);
root.removeView(from);
transitionListener.onTransitionCompleted();
}
});
animator.start();
}

private Animator createAnimator(View from) {
int fromTranslation = from.getHeight();
private Animator createAnimator(View to) {
int fromTranslation = to.getHeight();

AnimatorSet set = new AnimatorSet();

set.play(ObjectAnimator.ofFloat(from, View.TRANSLATION_Y, fromTranslation));
set.play(ObjectAnimator.ofFloat(to, View.TRANSLATION_Y, fromTranslation));

return set;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ public class SlideUpTransition extends ObjectAnimatorTransition {

@Override
public void performTranslate(final ViewGroup root, final View from, View to, final TransitionListener transitionListener) {
if (to == null) {
root.removeView(from);
return;
}

Animator animator = createAnimator(to);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
root.removeView(from);
transitionListener.onTransitionCompleted();
}
});
animator.start();
}

private Animator createAnimator(View to) {
int toTranslation = to.getHeight();
private Animator createAnimator(View from) {
int toTranslation = from.getHeight();

AnimatorSet set = new AnimatorSet();

set.play(ObjectAnimator.ofFloat(to, View.TRANSLATION_Y, toTranslation, 0));
set.play(ObjectAnimator.ofFloat(from, View.TRANSLATION_Y, toTranslation, 0));

return set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Controller(DialogController.class)
@DaggerModule(DialogModule.class)
@EnterTransition(SlideUpTransition.class)
@ExitTransition(SlideDownTransition.class)
@EnterTransition(SlideDownTransition.class)
@ExitTransition(SlideUpTransition.class)
public class Dialog extends Screen {
}
57 changes: 30 additions & 27 deletions scoop/src/main/java/com/lyft/scoop/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public Router(ScreenScooper screenScooper, boolean allowEmptyStack) {
this.allowEmptyStack = allowEmptyStack;
}

public void onCreate(Scoop root) {
this.root = root;
}

protected abstract void onScoopChanged(RouteChange routeChange);

public boolean goBack() {
if (!backStack.isEmpty()) {
Scoop previousScoop = backStack.peek();
Expand All @@ -31,10 +37,10 @@ public boolean goBack() {
if (!backStack.isEmpty()) {
Scoop nextScoop = backStack.peek();
Screen nextScreen = Screen.fromScoop(nextScoop);
performScoopChange(nextScoop, nextScreen, previousScreen, TransitionDirection.EXIT);
performScoopChange(nextScoop, previousScreen, nextScreen, TransitionDirection.EXIT);
return true;
} else if (allowEmptyStack) {
performScoopChange(previousScoop, null, previousScreen, TransitionDirection.EXIT);
performScoopChange(previousScoop, previousScreen, null, TransitionDirection.EXIT);
return true;
}
}
Expand All @@ -55,7 +61,7 @@ public void goTo(Screen screen) {

Scoop nextScoop = screenScooper.createScreenScoop(screen, previousScoop);
backStack.push(nextScoop);
performScoopChange(nextScoop, screen, previousScreen, TransitionDirection.ENTER);
performScoopChange(nextScoop, previousScreen, screen, TransitionDirection.ENTER);
}

public void replaceWith(Screen screen) {
Expand All @@ -82,34 +88,37 @@ public void replaceWith(Screen screen) {
}

backStack.push(nextScoop);
performScoopChange(nextScoop, screen, previousScreen, TransitionDirection.ENTER);
}

public void resetTo(Screen screen) {
if (tryHandleEmptyBackstack(screen)) {
return;
}
resetTo(screen, TransitionDirection.EXIT);
performScoopChange(nextScoop, previousScreen, screen, TransitionDirection.ENTER);
}

public void replaceAllWith(Screen... screens) {
replaceAllWith(Arrays.asList(screens));
}

public void replaceAllWith(List<Screen> screens) {
Screen previousScreen = Screen.fromScoop(backStack.peek());

backStack.clear();

Scoop previousScoop = root;
Scoop scoop = root;
for (final Screen screen : screens) {
final Scoop newScoop = screenScooper.createScreenScoop(screen, previousScoop);
final Scoop newScoop = screenScooper.createScreenScoop(screen, scoop);
backStack.push(newScoop);
previousScoop = newScoop;
scoop = newScoop;
}
Screen lastScreen = null;

Screen currentScreen = null;
if (!screens.isEmpty()) {
lastScreen = screens.get(screens.size() - 1);
currentScreen = screens.get(screens.size() - 1);
}
performScoopChange(backStack.peek(), previousScreen, currentScreen, TransitionDirection.ENTER);
}

public void resetTo(Screen screen) {
if (tryHandleEmptyBackstack(screen)) {
return;
}
performScoopChange(backStack.peek(), lastScreen, null, TransitionDirection.ENTER);
resetTo(screen, TransitionDirection.EXIT);
}

public void resetTo(Screen screen, TransitionDirection direction) {
Expand All @@ -120,7 +129,7 @@ public void resetTo(Screen screen, TransitionDirection direction) {
Scoop topScoop = backStack.peek();

if (sameScreen(screen, Screen.fromScoop(topScoop))) {
performScoopChange(topScoop, screen, previousScreen, direction);
performScoopChange(topScoop, previousScreen, screen, direction);
return;
}

Expand All @@ -129,11 +138,7 @@ public void resetTo(Screen screen, TransitionDirection direction) {

Scoop nextScoop = screenScooper.createScreenScoop(screen, root);
backStack.push(nextScoop);
performScoopChange(nextScoop, screen, previousScreen, direction);
}

public void onCreate(Scoop root) {
this.root = root;
performScoopChange(nextScoop, previousScreen, screen, direction);
}

public boolean hasActiveScreen() {
Expand All @@ -144,18 +149,16 @@ private boolean tryHandleEmptyBackstack(final Screen screen) {
if (backStack.isEmpty()) {
final Scoop newScoop = screenScooper.createScreenScoop(screen, root);
backStack.push(newScoop);
performScoopChange(backStack.peek(), screen, null, TransitionDirection.ENTER);
performScoopChange(backStack.peek(), null, screen, TransitionDirection.ENTER);
return true;
}
return false;
}

private void performScoopChange(Scoop scoop, Screen next, Screen previous, TransitionDirection direction) {
private void performScoopChange(Scoop scoop, Screen previous, Screen next, TransitionDirection direction) {
onScoopChanged(new RouteChange(scoop, previous, next, direction));
}

protected abstract void onScoopChanged(RouteChange routeChange);

static boolean sameScreen(Screen previous, Screen next) {

if (previous == null || next == null) {
Expand Down
9 changes: 3 additions & 6 deletions scoop/src/test/java/com/lyft/scoop/RouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public void replaceToSameController() {
@Test
public void hasActiveScreen() {


router = new TestRouter(false);
Screen screen1 = new Screen1();

Expand All @@ -229,18 +228,16 @@ public void hasActiveScreen() {
}

@Test
public void replaceAllWithEmptyList() {

public void replaceAllWithEmptyListOnDisallowedEmptyStack() {

router = new TestRouter(false);
router = new TestRouter(true);

router.replaceAllWith(Collections.EMPTY_LIST);
router.replaceAllWith(Collections.<Screen>emptyList());
Assert.assertFalse(router.hasActiveScreen());
}

private void checkIfRouterBackstackIsEmpty() {Assert.assertEquals(false, router.goBack());}


@Test
public void sameController() {

Expand Down

0 comments on commit af89633

Please sign in to comment.