Android路由框架AnnoRouter:使用Java接口來定義路由跳轉

介紹

AnnoRouter是一個用於協助Android組件化的路由框架。它參考了Retrofit的接口設計,將路由跳轉API轉化爲Java接口,使用註解來配置跳轉信息。android

AnnoRouter示例

Usage

初始化全局Router

Router.Builder builder = new Router.Builder()
        .application(this)
        ...
       
Router.init(builder);

攔截過濾urlgit

new Router.Builder()
    .routerUrlFilter(new IRouterUrlFilter() {
        @Override
        public String filter(String url) {
            ...
            return url;
        }
    })
    ...

異常錯誤處理github

new Router.Builder()
    .exceptionHandler(new IExceptionHandler() {
        @Override
        public void handler(String url, Exception e) {
            
        }
    })
    ...

定義路由Api

使用 @RouterScheme, @RouterHost, @Path@Param 來定義一個路由地址。app

@RouterScheme("scheme")
@RouterHost("host")
public interface RouterApi {
     
    @Path("path")
    ...
    void jump(@Param("paramName") int paramValue);
        
}
 
public interface RouterApi {
     
    @RouterScheme("scheme")
    @RouterHost("host")
    @Path("path")
    ...
    void jump(@Param("paramName") int paramValue);
        
}

有時候,存在 scheme://host/path相同但參數不同的路由地址,就須要使用註解 @Strict 來做區別。 框架

e.g.ide

@RouterScheme("scheme")
@RouterHost("host")
public interface RouterApi {
     
    @Path("path")
    ...
    void jumpToActivity1(@Param("param1") String param1);
    
    
    @Strict
    @Path("path")
    ...
    void jumpToActivity2(@Param("param1") String param1, @Param("param2") int param2);
        
}
  • scheme://host/path?param1=a 將會匹配上方法 jumpToActivity1
  • scheme://host/path?param1=a&param2=1 將會匹配上方法 jumpToActivity2
  • scheme://host/path?param1=a&param2=1&param3=1 將會匹配上方法 jumpToActivity1, 但 param2=1&param3=1 會被忽略掉。

處理前置校驗或準備任務組件化

public interface RouterApi {
     
    @Task(CustomRouterTask.class)
    ...
    void jumpToActivity();
        
}
 
// ----------------
     
public class CustomRouterTask implements IRouterTask {
                                     
    @Override
    public void execute(Context context, RouterInfo routerInfo, OnTaskResult onTaskResult) {
        // do something...
        onTaskResult.success();
    }

}

跳轉到Activity或自定義處理動畫

public interface RouterApi {
     
    ...
    @Activity(LoginActivity.class)
    void jumpToLogin();
    
    ...
    @RouterHandler(CustomRouterHandler.class)
    void jumpToLogin();
        
}
 
// ----------------
 
public class CustomRouterHandler implements IRouterHandler {

    @Override
    public void applyRouter(Context context, RouterInfo routerInfo, OnRouterResult routerResult) {
        
        // do what you want to do.
 
        if(routerResult != null) {
            routerResult.onSuccess();
        }
    }
}

自定義Activity轉場動畫ui

public interface RouterApi {
     
    ...
    @Transition(CustomeTransition.class)
    void jumpToLogin();
        
}
 
// ----------------
 
public class CustomeTransition implements IActivityTransition {
                     
    @Override
    public int enterAnim() {
        return R.anim.fade_in;
    }
 
    @Override
    public int exitAnim() {
        return R.anim.fade_out;
    }
}

設置Activity launchModethis

public interface RouterApi {
     
    ...
    @Flags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    void jump(@Flags int flags);
    
    
    ...
    void jump(@Flags int flags);
        
}
 
// ----------------
 
Router.create(RouterApi.class).jump(Intent.FLAG_ACTIVITY_CLEAR_TOP);

添加路由Api

須要將定義好的路由Api添加至Router。若是隻經過接口而不經過url跳轉,能夠不添加。

Router.addRouterIndex(RouterApi.class);

自定義協議處理

主要處理一些特殊url協議,好比 http, https

public class HttpSchemeHandler implements ISchemeHandler {
 
    @Override
    public void applyRouter(Context context, String url, OnRouterResult routerResult) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        context.startActivity(intent);
 
        if(routerResult != null) {
            routerResult.onSuccess();
        }
    }
}

// ----------------
 
HttpSchemeHandler httpSchemeHandler = new HttpSchemeHandler();
Router.addSchemeHandler("https", httpSchemeHandler);
Router.addSchemeHandler("http", httpSchemeHandler);

使用路由Api進行跳轉

兩種跳轉調用方式:接口跳轉,url跳轉。

// The Router class generates an implementation of the RouterApi interface.
RouterApi routerApi = Router.create(RouterApi .class);
routerApi.jump("value");
 
// or use url instead.
Router.execute("scheme://host/path?param=value");

獲取Activity Result

@RouterScheme("app")
@RouterHost("usercenter")
public interface LoginRouterApi {
     
    @Path("login")
    @Activity(LoginActivity.class)
    @RequestCode(1001)
    void jumpToLogin(@Param("mobile") String mobile);
        
    @Activity(LoginActivity.class)
    @RequestCode(1001)
    void jumpToLogin(@Param("mobile") String mobile, OnActivityResult onActivityResult);
}
 
// ----------------
 
OnActivityResult onActivityResult = new OnActivityResult() {
 
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         
    }
    
    @Override
    public void onSuccess() {
        
    }
 
    @Override
    public void onFailure(Exception e) {

    }
};
 
Router.execute("app://usercenter/login?mobile=0123456789", onActivityResult);
// or
Router.create(LoginRouterApi.class).jumpToLogin("0123456789", onActivityResult);

Get it

AnnoAdapter is now available on JCentral.

implementation 'com.eastwood.common:anno-router:1.0.2'

結語

AnnoRouter已上傳Github,歡迎Star交流
https://github.com/EastWoodYang/AnnoRouter

相關文章
相關標籤/搜索