Skip to content

Commit

Permalink
Feature added: the default transition
Browse files Browse the repository at this point in the history
  • Loading branch information
belinwu committed Dec 19, 2015
1 parent 24ffced commit d195d5a
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 32 deletions.
52 changes: 52 additions & 0 deletions doorbell/src/main/java/com/wujilin/doorbell/Doorbell.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Doorbell {

/**
* The default transition to override when launching activities
*/
public static Transition defaultTransition;

/**
* The door
*/
Expand Down Expand Up @@ -176,6 +181,53 @@ public static void ring(Door door) {
door.onBlock();
}

/**
* Return the default transition to use for the incoming activity.
*
* @return The resource ID of the animation resource to use for the incoming activity.
*/
public static int getDefaultEnter() {
return defaultTransition == null ? 0 : defaultTransition.getEnter();
}

/**
* Return the default transition to use for the outgoing activity.
*
* @return The resource ID of the animation resource to use for the outgoing activity.
*/
public static int getDefaultExit() {
return defaultTransition == null ? 0 : defaultTransition.getExit();
}

/**
* Setup the default transition to override when launching the activities.
*
* @param transition The default transition
*/
public static void defaultTransition(Transition transition) {
defaultTransition = transition;
}

/**
* Setup the default resource ID of the animation resource to use for the incoming/outgoing activity.
*
* @param enter The resource ID of the animation resource to use for the incoming activity.
* @param exit The resource ID of the animation resource to use for the outgoing activity.
*/
public static void defaultTransition(final int enter, final int exit) {
defaultTransition = new Transition() {
@Override
public int getEnter() {
return enter;
}

@Override
public int getExit() {
return exit;
}
};
}

/**
* The builder class to build the doorbell.
*/
Expand Down
2 changes: 1 addition & 1 deletion doorbell/src/main/java/com/wujilin/doorbell/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ of this software and associated documentation files (the "Software"), to deal
* @see com.wujilin.doorbell.starter.ActivityStarter
* @see com.wujilin.doorbell.starter.FragmentStarter
*/
public interface Starter {
public interface Starter extends Transition {

/**
* Starts the activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ of this software and associated documentation files (the "Software"), to deal
import com.wujilin.doorbell.Transition;

import java.io.Serializable;
import java.util.ArrayList;

import static com.wujilin.doorbell.starter.Starters.newActivityStarter;
import static com.wujilin.doorbell.starter.Starters.newContextStarter;
import static com.wujilin.doorbell.starter.Starters.newFragmentStarter;
import static com.wujilin.doorbell.starter.Starters.newStarter;
import static com.wujilin.doorbell.starter.Starters.newStarter;
import static com.wujilin.doorbell.starter.Starters.newStarter;
import static com.wujilin.doorbell.util.Objects.requireNonNull;

/**
Expand All @@ -67,7 +66,7 @@ public class ActivityDoorbell extends Doorbell {
private int requestCode;

/**
* The intent for strating activities
* The intent for starting activities
*/
private Intent intent;

Expand All @@ -84,7 +83,7 @@ public class ActivityDoorbell extends Doorbell {
/**
* Constructs a new activity doorbell.
*
* @param builder The builder to build the actvity doorbell
* @param builder The builder to build the activity doorbell
*/
private ActivityDoorbell(final Builder builder) {
super(builder);
Expand Down Expand Up @@ -123,15 +122,9 @@ protected void onAllow() {
return;
}

int enterId = 0;
int exitId = 0;

// if the starter is a transition
if (starter instanceof Transition) {
Transition transition = (Transition) starter;
enterId = transition.getEnter();
exitId = transition.getExit();
}
// get the transition of the starter
int enterId = starter.getEnter();
int exitId = starter.getExit();

// if the transition of the doorbell is given
if (transition != null) {
Expand Down Expand Up @@ -183,7 +176,7 @@ public static class Builder extends Doorbell.Builder {
* @param context The context to start activities
*/
public Builder(Context context) {
this(newContextStarter(context));
this(newStarter(context));
}

/**
Expand All @@ -192,7 +185,7 @@ public Builder(Context context) {
* @param activity The activity to start activities
*/
public Builder(Activity activity) {
this(newActivityStarter(activity));
this(newStarter(activity));
}

/**
Expand All @@ -201,7 +194,7 @@ public Builder(Activity activity) {
* @param fragment The fragment to start acitvities
*/
public Builder(Fragment fragment) {
this(newFragmentStarter(fragment));
this(newStarter(fragment));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
The MIT License (MIT)
Copyright (c) 2015 Belin Wu (http://wujilin.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.wujilin.doorbell.starter;

import com.wujilin.doorbell.Starter;
import com.wujilin.doorbell.Transition;

import static com.wujilin.doorbell.Doorbell.getDefaultEnter;
import static com.wujilin.doorbell.Doorbell.getDefaultExit;

/**
* The abstract implementation of the Starter interface.
*/
public abstract class AbstractStarter implements Starter {
private int enterId = getDefaultEnter();
private int exitId = getDefaultExit();

public AbstractStarter() {

}

public AbstractStarter(Transition transition) {
if (transition == null) {
return;
}
this.enterId = transition.getEnter();
this.exitId = transition.getExit();
}

public AbstractStarter(int enterId, int exitId) {
this.enterId = enterId;
this.exitId = exitId;
}

@Override
public int getEnter() {
return enterId;
}

@Override
public int getExit() {
return exitId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ of this software and associated documentation files (the "Software"), to deal
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import com.wujilin.doorbell.Starter;

import java.lang.ref.WeakReference;

/**
* The activity starter to start activities.
*/
class ActivityStarter implements Starter {
class ActivityStarter extends AbstractStarter {

/**
* The weak reference to the activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ of this software and associated documentation files (the "Software"), to deal
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import com.wujilin.doorbell.Starter;

import java.lang.ref.WeakReference;

import static android.os.Build.VERSION.SDK_INT;
Expand All @@ -40,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
/**
* The context starter to start activities.
*/
class ContextStarter implements Starter {
class ContextStarter extends AbstractStarter {

/**
* The weak reference to the context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ of this software and associated documentation files (the "Software"), to deal
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.wujilin.doorbell.Starter;

import java.lang.ref.WeakReference;

/**
* The fragment starter to start activities.
*/
class FragmentStarter implements Starter {
class FragmentStarter extends AbstractStarter {
/**
* The weak refrence to the fragment.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private Starters() {
* @param context The context to start activities
* @return The context starter or null if context is null
*/
public static Starter newContextStarter(Context context) {
public static Starter newStarter(Context context) {
if (context == null) {
return null;
}
Expand All @@ -62,7 +62,7 @@ public static Starter newContextStarter(Context context) {
* @param activity The activity to start activities
* @return The activity starter or null if context is null
*/
public static Starter newActivityStarter(Activity activity) {
public static Starter newStarter(Activity activity) {
if (activity == null) {
return null;
}
Expand All @@ -76,9 +76,9 @@ public static Starter newActivityStarter(Activity activity) {
* Creates a new fragment starter.
*
* @param fragment The fragment to start activities
* @return The fragment starter or null if fragmetn is null
* @return The fragment starter or null if fragment is null
*/
public static Starter newFragmentStarter(Fragment fragment) {
public static Starter newStarter(Fragment fragment) {
if (fragment == null) {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions doorbell/src/main/res/anim/app_navigtor_pop_right_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200" android:fromXDelta="-100.0%p" android:toXDelta="0.0" />
<alpha android:duration="200" android:fromAlpha="0.1" android:toAlpha="1.0" />
</set>
6 changes: 6 additions & 0 deletions doorbell/src/main/res/anim/app_navigtor_pop_right_out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200" android:fromXDelta="0.0" android:toXDelta="100.0%p" />
<alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.1" />
</set>
5 changes: 5 additions & 0 deletions doorbell/src/main/res/anim/app_push_left_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200" android:fromXDelta="100.0%p" android:toXDelta="0.0" />
<alpha android:duration="200" android:fromAlpha="0.1" android:toAlpha="1.0" />
</set>
5 changes: 5 additions & 0 deletions doorbell/src/main/res/anim/app_push_left_out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200" android:fromXDelta="0.0" android:toXDelta="-100.0%p" />
<alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.1" />
</set>

0 comments on commit d195d5a

Please sign in to comment.