Skip to content

Commit

Permalink
add setExecutor to ActionRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed May 10, 2018
1 parent d880300 commit b7e5fb7
Show file tree
Hide file tree
Showing 15 changed files with 629 additions and 37 deletions.
421 changes: 401 additions & 20 deletions README.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions routerlib/src/main/java/com/lzh/nonview/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.lzh.nonview.router.tools.Constants;
import com.lzh.nonview.router.tools.Utils;

import java.util.concurrent.Executor;


/**
* Entry of Router。
Expand Down Expand Up @@ -123,6 +125,11 @@ public Router addExtras(Bundle extras) {
return this;
}

public Router setExecutor(Executor executor) {
this.internalCallback.getExtras().putValue(Constants.KEY_ACTION_EXECUTOR, executor);
return this;
}

/**
* Restore a Routing event from last uri and extras.
* @param uri last uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.lzh.nonview.router.module.ActionRouteRule;
import com.lzh.nonview.router.tools.Cache;
import com.lzh.nonview.router.tools.Constants;

import java.util.concurrent.Executor;

Expand All @@ -33,6 +34,10 @@ public abstract class ActionLauncher extends Launcher<ActionRouteRule> {
* @return returns a executor instance to switching thread.
*/
protected Executor getExecutor() {
return Cache.findOrCreateExecutor(rule.getExecutor());
Executor executor = extras.getValue(Constants.KEY_ACTION_EXECUTOR);
if (executor == null) {
executor = Cache.findOrCreateExecutor(rule.getExecutor());
}
return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.lzh.nonview.router.launcher.ActionLauncher;
import com.lzh.nonview.router.launcher.Launcher;
import com.lzh.nonview.router.module.ActionRouteRule;
import com.lzh.nonview.router.tools.Constants;

import java.util.concurrent.Executor;

public class ActionRoute extends BaseRoute<IActionRoute> implements IActionRoute {

Expand All @@ -31,4 +34,9 @@ protected Launcher obtainLauncher() throws Exception{
}
return launcher.newInstance();
}

@Override
public void setExecutor(Executor executor) {
callback.getExtras().putValue(Constants.KEY_ACTION_EXECUTOR, executor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
*/
package com.lzh.nonview.router.route;

import java.util.concurrent.Executor;

/**
* <p>
* Base on the {@link IBaseRoute}
* </p>
*/
public interface IActionRoute extends IBaseRoute<IActionRoute>{

void setExecutor(Executor executor);

class EmptyActionRoute extends EmptyBaseRoute<IActionRoute> implements IActionRoute {

public EmptyActionRoute(InternalCallback internal) {
super(internal);
}

@Override
public void setExecutor(Executor executor) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface Constants {
String KEY_RESUME_CONTEXT = "ROUTER-KEY:RESUME_CONTEXT";
String KEY_RESULT_CALLBACK = "ROUTER-KEY:RESULT_CALLBACK";
String KEY_ACTION_EXECUTOR = "ROUTER-KEY:ACTION_EXECUTOR";
}
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ android {
}

def parceler_version="1.3.9"
def router_version="2.6.1"
def router_version="2.7.0"
def butterknife_version='8.8.1'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/java/com/lzh/nonview/router/demo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.lzh.nonview.router.Router
import com.lzh.nonview.router.RouterConfiguration
import com.lzh.nonview.router.anno.RouteConfig
import com.lzh.nonview.router.demo.interceptors.DefaultInterceptor
import com.lzh.nonview.router.demo.tools.SingleCache

@RouteConfig(baseUrl = "haoge://page/", pack = "com.haoge.studio")
class App : Application() {
Expand All @@ -25,5 +26,7 @@ class App : Application() {

// 配置Parceler转换器
Parceler.setDefaultConverter(FastJsonConverter::class.java)

SingleCache.init(this)
}
}
31 changes: 26 additions & 5 deletions sample/src/main/java/com/lzh/nonview/router/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ import com.lzh.nonview.router.exception.NotFoundException
import com.lzh.nonview.router.launcher.Launcher
import com.lzh.nonview.router.module.RouteRule
import com.lzh.nonview.router.route.RouteCallback
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

@RouterRule("main")
class MainActivity : BaseActivity() {

val pool: ExecutorService = Executors.newSingleThreadExecutor { runnable ->
val thread:Thread = Thread(runnable)
thread.name = "action_executor"
return@newSingleThreadExecutor thread
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -82,11 +91,6 @@ class MainActivity : BaseActivity() {

}

@OnClick(R.id.launchActionRoute)
fun launchActionRoute() {
Router.create("haoge://page/simple-action").open(this)
}

@OnClick(R.id.toResultActivity)
fun toResultActivity() {
Router.create("haoge://page/result")
Expand Down Expand Up @@ -118,4 +122,21 @@ class MainActivity : BaseActivity() {

Router.create(url).open(this)
}

@OnClick(R.id.launchActionRoute)
fun launchActionRoute() {
Router.create("haoge://page/say/hello").open(this)
}

@OnClick(R.id.launchActionRouteWithExecutorAnnotation)
fun launchActionRouteWithExecutorAnnotation() {
Router.create("haoge://page/executor/switcher").open(this)
}

@OnClick(R.id.launchActionRouteWithExecutorConfig)
fun launchActionRouteWithExecutorConfig() {
Router.create("haoge://page/executor/switcher")
.setExecutor(pool)
.open(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lzh.nonview.router.demo.action

import android.content.Context
import android.os.Bundle
import com.lzh.nonview.router.anno.RouteExecutor
import com.lzh.nonview.router.anno.RouterRule
import com.lzh.nonview.router.demo.executor.TestExecutor
import com.lzh.nonview.router.demo.tools.SingleCache
import com.lzh.nonview.router.route.ActionSupport

/**
* @author haoge on 2018/5/10
*/
@RouteExecutor(TestExecutor::class)
@RouterRule("executor/switcher")
class ExecutorAction : ActionSupport(){
override fun onRouteTrigger(context: Context?, bundle: Bundle?) {
SingleCache.toast?.show("线程切换器测试动作路由被启动,启动线程为:${Thread.currentThread().name}" )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package com.lzh.nonview.router.demo.action

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import com.lzh.nonview.router.anno.RouterRule
import com.lzh.nonview.router.demo.tools.SingleCache
import com.lzh.nonview.router.route.ActionSupport

/**
* DATE: 2018/5/8
* AUTHOR: haoge
* @author haoge on 2018/5/10
*/
@RouterRule("simple-action")
class SimpleAction : ActionSupport(){
@RouterRule("say/hello")
class SayHelloAction:ActionSupport() {
override fun onRouteTrigger(context: Context?, bundle: Bundle?) {
Toast.makeText(context, "simple-action被触发", Toast.LENGTH_SHORT).show()
SingleCache.toast?.show("Hello! this is an action route!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lzh.nonview.router.demo.executor

import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
* @author haoge on 2018/5/10
*/
class TestExecutor : Executor{

val pool:ExecutorService = Executors.newSingleThreadExecutor { runnable ->
val thread:Thread = Thread(runnable)
thread.name = "action_annotation_executor"
return@newSingleThreadExecutor thread
}

override fun execute(command: Runnable?) {
pool.execute(command)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.lzh.nonview.router.demo.tools

import android.annotation.SuppressLint
import android.os.Looper
import android.text.TextUtils
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast

/**
* 一个简单易用的Toast封装类。用于提供易用的、多样式的Toast组件进行使用
*
* DATE: 2018/5/9
*
* AUTHOR: haoge
*/
class EasyToast private constructor(private val toast: Toast, private val tv: TextView?, private val isDefault: Boolean) {

fun show(resId:Int) {
show(SingleCache.context!!.getString(resId))
}

fun show(message:String) {
if (TextUtils.isEmpty(message)) {
return
}

if (Looper.myLooper() == Looper.getMainLooper()) {
showInternal(message)
} else {
SingleCache.mainHandler.post { showInternal(message) }
}
}

private fun showInternal(message: String) {
if (isDefault) {
toast.setText(message)
toast.show()
} else {
tv?.text = message
toast.show()
}
}

companion object {
/**
* 默认提供的Toast实例,在首次使用时进行加载。
*/
@JvmStatic
val DEFAULT: EasyToast by lazy { default() }

@JvmStatic
private fun default(): EasyToast {
checkThread()
@SuppressLint("ShowToast")
val toast = Toast.makeText(SingleCache.context, "", Toast.LENGTH_SHORT)
return EasyToast(toast, null, true)
}

@JvmStatic
fun create(layoutId: Int, tvId: Int, duration: Int): EasyToast {
checkThread()
val container = LayoutInflater.from(SingleCache.context).inflate(layoutId, null)
val tv:TextView = container.findViewById(tvId) as TextView
val toast = Toast(SingleCache.context)
toast.view = container
toast.duration = duration
return EasyToast(toast, tv, false)
}

// Toast限制:在执行了Looper.prepare()的线程中才能创建Toast实例
// 这里加强限制,仅限主线程创建
private fun checkThread() {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw RuntimeException("the toast-create method must called on main-thread")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.lzh.nonview.router.demo.tools

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper


/**
* 提供一些全局的常用数据进行使用。
*
* DATE: 2018/5/9
*
* AUTHOR: haoge
*/
@SuppressLint("StaticFieldLeak")
object SingleCache {
internal var toast:EasyToast? = null
internal var context:Context? = null
var mainHandler: Handler = Handler(Looper.getMainLooper())

fun init(context: Context) {
if (this.context == null) {
this.context = context.applicationContext
toast = EasyToast.DEFAULT
}
}
}
20 changes: 16 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,36 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/toResultActivity"
android:text="使用ActivityResult回调"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/toArgsActivity"
android:text="跳转到Arg参数自动解析示例页"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/launchActionRoute"
android:text="启动一个简单的动作路由"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/toResultActivity"
android:text="使用ActivityResult回调"
android:id="@+id/launchActionRouteWithExecutorAnnotation"
android:text="启动一个简单的动作路由(使用注解进行线程指定)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/toArgsActivity"
android:text="跳转到Arg参数自动解析示例页"
android:id="@+id/launchActionRouteWithExecutorConfig"
android:text="启动一个简单的动作路由(使用执行器进行线程指定)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</LinearLayout>

</ScrollView>
Expand Down

0 comments on commit b7e5fb7

Please sign in to comment.