Skip to content

Commit

Permalink
Add some animation
Browse files Browse the repository at this point in the history
  • Loading branch information
lexs committed Sep 12, 2013
1 parent 971d445 commit efb4ee0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
13 changes: 4 additions & 9 deletions sample/src/main/java/com/example/flow/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import butterknife.InjectView;
import butterknife.Views;
import com.example.flow.model.Conversation;
import com.example.flow.model.User;
import com.example.flow.view.ContainerView;
import com.squareup.flow.Backstack;
import com.squareup.flow.Flow;
import com.squareup.flow.Screen;
Expand All @@ -42,7 +42,7 @@
public class MainActivity extends Activity implements Flow.Listener {
private static final String BUNDLE_BACKSTACK = "backstack";

@InjectView(R.id.container) FrameLayout containerView;
@InjectView(R.id.container) ContainerView containerView;

private MenuItem friendsMenu;

Expand Down Expand Up @@ -99,7 +99,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override public void go(Backstack backstack, Flow.Direction direction) {
Screen screen = backstack.current().getScreen();
displayView(getView(screen));
containerView.displayView(getView(screen), direction);

setTitle(screen.getClass().getSimpleName());

Expand All @@ -116,7 +116,7 @@ private Backstack getInitialBackstack(Bundle savedInstanceState) {
if (savedInstanceState != null) {
return savedInstanceState.getParcelable(BUNDLE_BACKSTACK);
} else {
return Backstack.single(new App.FriendList());
return Backstack.single(new App.ConversationList());
}
}

Expand All @@ -126,11 +126,6 @@ private View getView(Screen screen) {
return Screens.createView(scopedContext, screen);
}

private void displayView(View view) {
containerView.removeAllViews();
containerView.addView(view);
}

@Module(library = true) class ActivityModule {
@Provides @App Flow provideAppFlow() {
return flow;
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/com/example/flow/ScopedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import dagger.ObjectGraph;

public class ScopedContext extends ContextWrapper implements Injector {
public final ObjectGraph graph;
private final ObjectGraph graph;

private LayoutInflater inflater;

Expand Down
26 changes: 26 additions & 0 deletions sample/src/main/java/com/example/flow/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,34 @@

import android.content.Context;
import android.view.View;
import android.view.ViewTreeObserver;

public class Utils {
public interface OnMeasuredCallback {
void onMeasured(View view, int width, int height);
}

public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
int width = view.getWidth();
int height = view.getHeight();

if (width > 0 && height > 0) {
callback.onMeasured(view, width, height);
return;
}

final ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override public boolean onPreDraw() {
if (observer.isAlive()) observer.removeOnPreDrawListener(this);

callback.onMeasured(view, view.getWidth(), view.getHeight());

return true;
}
});
}

public static void inject(Context context, View view) {
((Injector) context).inject(view);
}
Expand Down
85 changes: 85 additions & 0 deletions sample/src/main/java/com/example/flow/view/ContainerView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2013 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.flow.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import com.example.flow.Utils;
import com.squareup.flow.Flow;

public class ContainerView extends FrameLayout {
private boolean disabled;

private View activeView;

public ContainerView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override public boolean dispatchTouchEvent(MotionEvent ev) {
return !disabled && super.dispatchTouchEvent(ev);
}

public void displayView(View view, final Flow.Direction direction) {
addView(view);

if (activeView != null) {
final View from = activeView;
final View to = view;
Utils.waitForMeasure(to, new Utils.OnMeasuredCallback() {
@Override public void onMeasured(View view, int width, int height) {
runAnimation(from, to, direction);
}
});
}

activeView = view;
}

private void runAnimation(final View from, final View to, Flow.Direction direction) {
disabled = true;

Animator animator = createSegue(from, to, direction);
animator.addListener(new AnimatorListenerAdapter() {
@Override public void onAnimationEnd(Animator animation) {
removeView(from);
disabled = false;
}
});
animator.start();
}

private Animator createSegue(View from, View to, Flow.Direction direction) {
boolean backward = direction == Flow.Direction.BACKWARD;
int fromTranslation = backward ? from.getWidth() : -from.getWidth();
int toTranslation = backward ? -to.getWidth() : to.getWidth();

AnimatorSet set = new AnimatorSet();

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

return set;
}
}
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~ limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.example.flow.view.ContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
Expand Down

0 comments on commit efb4ee0

Please sign in to comment.