Skip to content

Commit

Permalink
add view interface for progress callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zoonooz committed Jan 14, 2018
1 parent 3fe495b commit 9c77180
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void prepare(CoordinatorLayout parent, View child, View dependency) {

@Override
void updateViewWithPercent(View child, float percent) {
super.updateViewWithPercent(child, percent);
if (mAnimation != null) {
// get the transform at the specify time in progress
Transformation transformation = new Transformation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zoonref.viewbehavior;

/**
* Created by zoonooz on 15/1/2018 AD.
* Interface for child view to let behavior send the progress
*/

public interface PercentageChildView {

/**
* This will be called on behavior updated.
* @param behavior Changing behavior object
* @param progress 0.0 to 1.0 progress of behavior animation
*/
void onPercentageBehaviorChange(PercentageViewBehavior behavior, float progress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Created by zoonooz on 5/1/2018 AD.
* Base behavior for percentage driven behaviors.
*/
public abstract class PercentageViewBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {
public class PercentageViewBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

static final int UNSPECIFIED_INT = Integer.MAX_VALUE;
static final float UNSPECIFIED_FLOAT = Float.MAX_VALUE;
Expand Down Expand Up @@ -164,7 +164,11 @@ void updateView(V child, View dependency) {
* @param child child view
* @param percent progress of dependency changed 0.0f to 1.0f
*/
abstract void updateViewWithPercent(V child, float percent);
void updateViewWithPercent(V child, float percent) {
if (child instanceof PercentageChildView) {
((PercentageChildView) child).onPercentageBehaviorChange(this, percent);
}
}

/**
* Builder class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void prepare(CoordinatorLayout parent, View child, View dependency) {

@Override
void updateViewWithPercent(View child, float percent) {
super.updateViewWithPercent(child, percent);

float newX = targetX == UNSPECIFIED_INT ? 0 : (targetX - mStartX) * percent;
float newY = targetY == UNSPECIFIED_INT ? 0 : (targetY - mStartY) * percent;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
package com.zoonref.viewbehavior;

import android.app.Activity;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;
import android.view.animation.ScaleAnimation;

import com.zoonref.viewbehavior.shadow.ShadowAnimation;

import static junit.framework.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;

/**
* Created by zoonooz on 6/1/2018 AD.
* Test {@link AnimationViewBehavior}
*/
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowAnimation.class})
public class AnimationViewBehaviorTest {

private CoordinatorLayout coordinatorLayout;
private View firstView;
private View secondView;

@Before
public void setup() {
Activity activity = Robolectric.setupActivity(Activity.class);
activity.setTheme(R.style.Theme_AppCompat);
coordinatorLayout = new CoordinatorLayout(activity);
activity.setContentView(coordinatorLayout);
firstView = new View(activity);
secondView = new View(activity);

CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
coordinatorLayout.addView(firstView, params);
coordinatorLayout.addView(secondView, params);
}
public class AnimationViewBehaviorTest extends BehaviorTest {

@Test
public void animations() {
Expand Down
38 changes: 38 additions & 0 deletions library/src/test/java/com/zoonref/viewbehavior/BehaviorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.zoonref.viewbehavior;

import android.app.Activity;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;

import com.zoonref.viewbehavior.view.TestPercentageChildView;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

/**
* Created by zoonooz on 15/1/2018 AD.
* Best test class
*/
@RunWith(RobolectricTestRunner.class)
public abstract class BehaviorTest {

CoordinatorLayout coordinatorLayout;
View firstView;
TestPercentageChildView secondView;

@Before
public void setup() {
Activity activity = Robolectric.setupActivity(Activity.class);
activity.setTheme(R.style.Theme_AppCompat);
coordinatorLayout = new CoordinatorLayout(activity);
activity.setContentView(coordinatorLayout);
firstView = new View(activity);
secondView = new TestPercentageChildView(activity);

CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
coordinatorLayout.addView(firstView, params);
coordinatorLayout.addView(secondView, params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.zoonref.viewbehavior;

import android.support.design.widget.CoordinatorLayout;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;

/**
* Created by zoonooz on 15/1/2018 AD.
* Test {@link PercentageViewBehavior}
*/
public class PercentageViewBehaviorTest extends BehaviorTest {

@Test
public void dependsOnX() {
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder()
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_X)
.targetValue(100)
.targetX(100)
.targetY(200)
.build();
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
params.setBehavior(behavior);
secondView.setLayoutParams(params);

// move first view by 50%
firstView.setX(50);
coordinatorLayout.requestLayout();
assertEquals(0.5f, secondView.getCurrentPercent());
}

@Test
public void dependsOnY() {
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder()
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_Y)
.targetValue(100)
.targetX(100)
.targetY(200)
.build();
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
params.setBehavior(behavior);
secondView.setLayoutParams(params);

// move first view by 50%
firstView.setY(50);
coordinatorLayout.requestLayout();
assertEquals(0.5f, secondView.getCurrentPercent());
}

@Test
public void dependsOnWidth() {
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder()
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_WIDTH)
.targetValue(160)
.targetX(100)
.targetY(200)
.build();
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
params.setBehavior(behavior);
secondView.setLayoutParams(params);

// resize first view's width
CoordinatorLayout.LayoutParams resizeParams = new CoordinatorLayout.LayoutParams(firstView.getLayoutParams());
resizeParams.width = 160;
firstView.setLayoutParams(resizeParams);
coordinatorLayout.requestLayout();
assertEquals(1.0f, secondView.getCurrentPercent());
}

@Test
public void dependsOnHeight() {
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder()
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_HEIGHT)
.targetValue(100)
.targetX(100)
.targetY(200)
.build();
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
params.setBehavior(behavior);
secondView.setLayoutParams(params);

// resize first view's height
CoordinatorLayout.LayoutParams resizeParams = new CoordinatorLayout.LayoutParams(firstView.getLayoutParams());
resizeParams.height = 100;
firstView.setLayoutParams(resizeParams);
coordinatorLayout.requestLayout();
assertEquals(1.0f, secondView.getCurrentPercent());
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
package com.zoonref.viewbehavior;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;

import static junit.framework.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

/**
* Created by zoonooz on 5/1/2018 AD.
* Test {@link SimpleViewBehavior}
*/
@RunWith(RobolectricTestRunner.class)
public class SimpleViewBehaviorTest {

private CoordinatorLayout coordinatorLayout;
private View firstView;
private View secondView;

@Before
public void setup() {
Activity activity = Robolectric.setupActivity(Activity.class);
activity.setTheme(R.style.Theme_AppCompat);
coordinatorLayout = new CoordinatorLayout(activity);
activity.setContentView(coordinatorLayout);
firstView = new View(activity);
secondView = new View(activity);

CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
coordinatorLayout.addView(firstView, params);
coordinatorLayout.addView(secondView, params);
}
public class SimpleViewBehaviorTest extends BehaviorTest {

@Test
public void dependsOnX_targetXAndY() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.zoonref.viewbehavior.view;

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

import com.zoonref.viewbehavior.PercentageChildView;
import com.zoonref.viewbehavior.PercentageViewBehavior;

/**
* Created by zoonooz on 15/1/2018 AD.
* {@link PercentageChildView} implementation for testing
*/

public class TestPercentageChildView extends View implements PercentageChildView {

private float currentPercent;

public TestPercentageChildView(Context context) {
super(context);
}

@Override
public void onPercentageBehaviorChange(PercentageViewBehavior behavior, float progress) {
currentPercent = progress;
}

public float getCurrentPercent() {
return currentPercent;
}
}

0 comments on commit 9c77180

Please sign in to comment.