Skip to content

Commit

Permalink
Added IntentHandler to start or handle Intents (#13)
Browse files Browse the repository at this point in the history
* Added IntentHandler to start or handle Intents

Closed #11

* Added setIntentHandler guide for README

* Fixed a code style issue
  • Loading branch information
drakeet authored Sep 21, 2017
1 parent b2ab784 commit 1018540
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class App extends Application {

Floo.configuration()
.setDebugEnabled(BuildConfig.DEBUG)
.setIntentHandler(new FragmentIntentHandler()) // DefaultIntentHandler by default
.addRequestInterceptor(new LogInterceptor("Request"))
.addTargetInterceptor(new LogInterceptor("Target"))
.addTargetNotFoundHandler(new WebHandler())
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class App extends Application {

Floo.configuration()
.setDebugEnabled(BuildConfig.DEBUG)
.setIntentHandler(new FragmentIntentHandler()) // DefaultIntentHandler by default
.addRequestInterceptor(new LogInterceptor("Request"))
.addTargetInterceptor(new LogInterceptor("Target"))
.addTargetNotFoundHandler(new WebHandler())
Expand Down
32 changes: 26 additions & 6 deletions library/src/main/java/me/drakeet/floo/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.List;
import java.util.Map;

import static me.drakeet.floo.Preconditions.checkNotNull;

/**
* A delegate for floo to hold the configuration fields
*
Expand All @@ -38,6 +40,7 @@ public class Configuration {
private @NonNull final List<TargetNotFoundHandler> targetNotFoundHandlers;
private @NonNull final List<Interceptor> requestInterceptors;
private @NonNull final List<Interceptor> targetInterceptors;
private @NonNull IntentHandler intentHandler = new DefaultIntentHandler();
private boolean debug = false;
private boolean stackObserverInitialized = false;

Expand All @@ -51,26 +54,30 @@ public class Configuration {


void apply(@NonNull final Map<String, Target> map) {
checkNotNull(map);
targetMap.set(new HashMap<>(map));
}


@NonNull
public Configuration addRequestInterceptor(@NonNull Interceptor requestInterceptor) {
checkNotNull(requestInterceptor);
requestInterceptors.add(requestInterceptor);
return this;
}


@NonNull
public Configuration addTargetInterceptor(@NonNull Interceptor responseInterceptor) {
checkNotNull(responseInterceptor);
targetInterceptors.add(responseInterceptor);
return this;
}


@NonNull
public Configuration addTargetNotFoundHandler(@NonNull TargetNotFoundHandler handler) {
checkNotNull(handler);
targetNotFoundHandlers.add(handler);
return this;
}
Expand All @@ -90,35 +97,48 @@ public Configuration setDebugEnabled(boolean debug) {
}


boolean isDebugEnabled() { return debug; }
public boolean isDebugEnabled() { return debug; }


public void setIntentHandler(@NonNull IntentHandler intentHandler) {
checkNotNull(intentHandler);
this.intentHandler = intentHandler;
}


@NonNull
public IntentHandler getIntentHandler() {
return intentHandler;
}


@NonNull
List<TargetNotFoundHandler> getTargetNotFoundHandlers() {
public List<TargetNotFoundHandler> getTargetNotFoundHandlers() {
return targetNotFoundHandlers;
}


@NonNull
List<Interceptor> getRequestInterceptors() {
public List<Interceptor> getRequestInterceptors() {
return requestInterceptors;
}


@NonNull
List<Interceptor> getTargetInterceptors() {
public List<Interceptor> getTargetInterceptors() {
return targetInterceptors;
}


@Nullable
Target getTarget(@NonNull String url) {
public Target getTarget(@NonNull String url) {
checkNotNull(url);
return targetMap.getTarget(url);
}


@NonNull
Map<? extends String, ? extends Target> getTargetMap() {
public Map<? extends String, ? extends Target> getTargetMap() {
return targetMap.get();
}

Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/me/drakeet/floo/DefaultIntentHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.drakeet.floo;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

/**
* @author drakeet
*/
public class DefaultIntentHandler implements IntentHandler {

@Override
public void onIntentCreated(@NonNull Context context, @NonNull Intent intent) {
context.startActivity(intent);
}
}
2 changes: 1 addition & 1 deletion library/src/main/java/me/drakeet/floo/Floo.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Navigation appendQueryParameter(@NonNull String key, @NonNull String valu
public void start() {
final Intent intent = getIntent();
if (intent != null) {
context.startActivity(intent);
configuration().getIntentHandler().onIntentCreated(context, intent);
} else {
log(WARN, "The target Intent is null, " +
"it may has been intercepted or dispatched to your TargetNotFoundHandlers.");
Expand Down
19 changes: 19 additions & 0 deletions library/src/main/java/me/drakeet/floo/IntentHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.drakeet.floo;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

/**
* @author drakeet
*/
public interface IntentHandler {

/**
* Called immediately after intent has created on {@link Floo#start()}.
*
* @param context The context.
* @param intent The intent.
*/
void onIntentCreated(@NonNull Context context, @NonNull Intent intent);
}
21 changes: 21 additions & 0 deletions library/src/main/java/me/drakeet/floo/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.drakeet.floo;

import android.support.annotation.NonNull;

/**
* @author drakeet
*/
public final class Preconditions {

@NonNull
@SuppressWarnings("ConstantConditions")
public static <T> T checkNotNull(@NonNull final T object) {
if (object == null) {
throw new NullPointerException();
}
return object;
}


private Preconditions() {}
}

0 comments on commit 1018540

Please sign in to comment.