提交 564d51a3 编写于 作者: J jessyan

add RepositoryManager

上级 46a9efc4
......@@ -135,7 +135,7 @@ Name: Contract
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import com.jess.arms.mvp.BaseView;
import com.jess.arms.mvp.IView;
import com.jess.arms.mvp.IModel;
/**
......
package common;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.mvp.Presenter;
import com.jess.arms.mvp.IPresenter;
/**
* Created by jess on 8/5/16 13:13
* contact with jess.yan.effort@gmail.com
*/
public abstract class WEActivity<P extends Presenter> extends BaseActivity<P> {
public abstract class WEActivity<P extends IPresenter> extends BaseActivity<P> {
protected WEApplication mWeApplication;
@Override
protected void ComponentInject() {
......
......@@ -101,9 +101,9 @@ public class WEApplication extends BaseApplication {
@Override
protected GlobeConfigModule getGlobeConfigModule() {
return GlobeConfigModule
.buidler()
.builder()
.baseurl(Api.APP_DOMAIN)
.globeHttpHandler(new GlobeHttpHandler() {// 这里可以提供一个全局处理http响应结果的处理类,
.globeHttpHandler(new GlobeHttpHandler() {// 这里可以提供一个全局处理Http请求和响应结果的处理类,
// 这里可以比客户端提前一步拿到服务器返回的结果,可以做一些操作,比如token超时,重新获取
@Override
public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
......@@ -139,7 +139,7 @@ public class WEApplication extends BaseApplication {
return response;
}
// 这里可以在请求服务器之前可以拿到request,做一些操作比如给request统一添加token或者header以及数据加密等操作
// 这里可以在请求服务器之前可以拿到request,做一些操作比如给request统一添加token或者header以及参数加密等操作
@Override
public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {
/* 如果需要再请求服务器之前做一些操作,则重新返回一个做过操作的的requeat如增加header,不做操作则直接返回request参数
......
package common;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.mvp.Presenter;
import com.jess.arms.mvp.IPresenter;
import com.squareup.leakcanary.RefWatcher;
/**
* Created by jess on 8/5/16 14:11
* contact with jess.yan.effort@gmail.com
*/
public abstract class WEFragment<P extends Presenter> extends BaseFragment<P> {
public abstract class WEFragment<P extends IPresenter> extends BaseFragment<P> {
protected WEApplication mWeApplication;
@Override
protected void ComponentInject() {
......
package me.jessyan.mvparms.demo.mvp.contract;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.mvp.BaseView;
import com.jess.arms.mvp.IView;
import com.jess.arms.mvp.IModel;
import com.tbruyelle.rxpermissions.RxPermissions;
......@@ -16,7 +16,7 @@ import rx.Observable;
*/
public interface UserContract {
//对于经常使用的关于UI的方法可以定义到BaseView中,如显示隐藏进度条,和显示文字消息
interface View extends BaseView {
interface View extends IView {
void setAdapter(DefaultAdapter adapter);
void startLoadMore();
void endLoadMore();
......
......@@ -7,7 +7,7 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.jess.arms.mvp.Presenter;
import com.jess.arms.mvp.IPresenter;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoLinearLayout;
......@@ -20,7 +20,7 @@ import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public abstract class BaseActivity<P extends Presenter> extends RxAppCompatActivity {
public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActivity {
protected final String TAG = this.getClass().getSimpleName();
protected BaseApplication mApplication;
private Unbinder mUnbinder;
......
......@@ -6,7 +6,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jess.arms.mvp.Presenter;
import com.jess.arms.mvp.IPresenter;
import com.trello.rxlifecycle.components.support.RxFragment;
import org.simple.eventbus.EventBus;
......@@ -19,7 +19,7 @@ import butterknife.Unbinder;
/**
* Created by jess on 2015/12/8.
*/
public abstract class BaseFragment<P extends Presenter> extends RxFragment {
public abstract class BaseFragment<P extends IPresenter> extends RxFragment {
protected BaseActivity mActivity;
protected View mRootView;
protected final String TAG = this.getClass().getSimpleName();
......
......@@ -36,16 +36,16 @@ public class GlobeConfigModule {
* @date 8/5/16 11:03 AM
* @description: 设置baseurl
*/
private GlobeConfigModule(Buidler buidler) {
this.mApiUrl = buidler.apiUrl;
this.mHandler = buidler.handler;
this.mInterceptors = buidler.interceptors;
this.mErroListener = buidler.responseErroListener;
this.mCacheFile = buidler.cacheFile;
private GlobeConfigModule(Builder builder) {
this.mApiUrl = builder.apiUrl;
this.mHandler = builder.handler;
this.mInterceptors = builder.interceptors;
this.mErroListener = builder.responseErroListener;
this.mCacheFile = builder.cacheFile;
}
public static Buidler buidler() {
return new Buidler();
public static Builder builder() {
return new Builder();
}
......@@ -92,17 +92,17 @@ public class GlobeConfigModule {
}
public static final class Buidler {
public static final class Builder {
private HttpUrl apiUrl = HttpUrl.parse("https://api.github.com/");
private GlobeHttpHandler handler;
private List<Interceptor> interceptors = new ArrayList<>();
private ResponseErroListener responseErroListener;
private File cacheFile;
private Buidler() {
private Builder() {
}
public Buidler baseurl(String baseurl) {//基础url
public Builder baseurl(String baseurl) {//基础url
if (TextUtils.isEmpty(baseurl)) {
throw new IllegalArgumentException("baseurl can not be empty");
}
......@@ -110,24 +110,24 @@ public class GlobeConfigModule {
return this;
}
public Buidler globeHttpHandler(GlobeHttpHandler handler) {//用来处理http响应结果
public Builder globeHttpHandler(GlobeHttpHandler handler) {//用来处理http响应结果
this.handler = handler;
return this;
}
public Buidler addInterceptor(Interceptor interceptor) {//动态添加任意个interceptor
public Builder addInterceptor(Interceptor interceptor) {//动态添加任意个interceptor
this.interceptors.add(interceptor);
return this;
}
public Buidler responseErroListener(ResponseErroListener listener) {//处理所有Rxjava的onError逻辑
public Builder responseErroListener(ResponseErroListener listener) {//处理所有Rxjava的onError逻辑
this.responseErroListener = listener;
return this;
}
public Buidler cacheFile(File cacheFile) {
public Builder cacheFile(File cacheFile) {
this.cacheFile = cacheFile;
return this;
}
......
package com.jess.arms.integration;
/**
* Created by jess on 17/03/2017 11:15
* Contact with jess.yan.effort@gmail.com
*/
public interface IRepositoryManager {
/**
* 注入RetrofitService,在框架全局配置类中进行注入
* @param services
*/
void injectRetrofitService(Class<?>... services);
/**
* 注入CacheService,在框架全局配置类中进行注入
* @param services
*/
void injectCacheService(Class<?>... services);
/**
* 根据传入的Class获取对应的Retrift service
*
* @param service
* @param <T>
* @return
*/
<T> T obtainRetrofitService(Class<T> service);
/**
* 根据传入的Class获取对应的RxCache service
*
* @param cache
* @param <T>
* @return
*/
<T> T obtainCacheService(Class<T> cache);
}
package com.jess.arms.integration;
import com.jess.arms.utils.Preconditions;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.rx_cache.internal.RxCache;
import retrofit2.Retrofit;
/**
* 用来管理网络请求层,以及数据缓存层,以后可以添加数据库请求层
* 需要在{link ConfigModule}中先inject需要的服务
* Created by jess on 13/04/2017 09:52
* Contact with jess.yan.effort@gmail.com
*/
@Singleton
public class RepositoryManager implements IRepositoryManager {
private Retrofit mRetrofit;
private RxCache mRxCache;
private final Map<String, Object> mRetrofitServiceCache = new LinkedHashMap<>();
private final Map<String, Object> mCacheServiceCache = new LinkedHashMap<>();
@Inject
public RepositoryManager(Retrofit retrofit, RxCache rxCache) {
this.mRetrofit = retrofit;
this.mRxCache = rxCache;
}
@Override
public void injectRetrofitService(Class<?>... services) {
for (Class<?> service : services) {
if (mRetrofitServiceCache.containsKey(service.getName())) continue;
mRetrofitServiceCache.put(service.getName(), mRetrofit.create(service));
}
}
@Override
public void injectCacheService(Class<?>... services) {
for (Class<?> service : services) {
if (mCacheServiceCache.containsKey(service.getName())) continue;
mCacheServiceCache.put(service.getName(), mRxCache.using(service));
}
}
@Override
public <T> T obtainRetrofitService(Class<T> service) {
Preconditions.checkState(mRetrofitServiceCache.containsKey(service.getName())
,"Unable to find %s,first call injectRetrofitService(%s) in ConfigModule",service.getName(),service.getSimpleName());
return (T) mRetrofitServiceCache.get(service.getName());
}
@Override
public <T> T obtainCacheService(Class<T> cache) {
Preconditions.checkState(mCacheServiceCache.containsKey(cache.getName())
,"Unable to find %s,first call injectCacheService(%s) in ConfigModule",cache.getName(),cache.getSimpleName());
return (T) mCacheServiceCache.get(cache.getName());
}
}
......@@ -8,7 +8,7 @@ import rx.subscriptions.CompositeSubscription;
/**
* Created by jess on 16/4/28.
*/
public class BasePresenter<M extends IModel, V extends BaseView> implements Presenter {
public class BasePresenter<M extends IModel, V extends IView> implements IPresenter {
protected final String TAG = this.getClass().getSimpleName();
protected CompositeSubscription mCompositeSubscription;
......
......@@ -3,7 +3,7 @@ package com.jess.arms.mvp;
/**
* Created by jess on 16/4/28.
*/
public interface Presenter {
public interface IPresenter {
void onStart();
void onDestroy();
}
......@@ -5,7 +5,7 @@ import android.content.Intent;
/**
* Created by jess on 16/4/22.
*/
public interface BaseView {
public interface IView {
/**
* 显示加载
......
......@@ -2,7 +2,7 @@ package com.jess.arms.utils;
import android.Manifest;
import com.jess.arms.mvp.BaseView;
import com.jess.arms.mvp.IView;
import com.tbruyelle.rxpermissions.RxPermissions;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
......@@ -27,7 +27,7 @@ public class PermissionUtil {
/**
* 请求摄像头权限
*/
public static void launchCamera(final RequestPermission requestPermission, RxPermissions rxPermissions, final BaseView view, RxErrorHandler errorHandler) {
public static void launchCamera(final RequestPermission requestPermission, RxPermissions rxPermissions, final IView view, RxErrorHandler errorHandler) {
//先确保是否已经申请过摄像头,和写入外部存储的权限
boolean isPermissionsGranted =
rxPermissions
......@@ -60,7 +60,7 @@ public class PermissionUtil {
/**
* 请求外部存储的权限
*/
public static void externalStorage(final RequestPermission requestPermission, RxPermissions rxPermissions, final BaseView view, RxErrorHandler errorHandler) {
public static void externalStorage(final RequestPermission requestPermission, RxPermissions rxPermissions, final IView view, RxErrorHandler errorHandler) {
//先确保是否已经申请过摄像头,和写入外部存储的权限
boolean isPermissionsGranted =
rxPermissions
......@@ -90,7 +90,7 @@ public class PermissionUtil {
/**
* 请求发送短信权限
*/
public static void sendSms(final RequestPermission requestPermission, RxPermissions rxPermissions, final BaseView view, RxErrorHandler errorHandler) {
public static void sendSms(final RequestPermission requestPermission, RxPermissions rxPermissions, final IView view, RxErrorHandler errorHandler) {
//先确保是否已经申请过权限
boolean isPermissionsGranted =
rxPermissions
......@@ -119,7 +119,7 @@ public class PermissionUtil {
/**
* 请求打电话权限
*/
public static void callPhone(final RequestPermission requestPermission, RxPermissions rxPermissions, final BaseView view, RxErrorHandler errorHandler) {
public static void callPhone(final RequestPermission requestPermission, RxPermissions rxPermissions, final IView view, RxErrorHandler errorHandler) {
//先确保是否已经申请过权限
boolean isPermissionsGranted =
rxPermissions
......
......@@ -2,7 +2,7 @@ package com.jess.arms.utils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.mvp.BaseView;
import com.jess.arms.mvp.IView;
import com.trello.rxlifecycle.LifecycleTransformer;
import rx.Observable;
......@@ -17,7 +17,7 @@ import rx.schedulers.Schedulers;
public class RxUtils {
public static <T> Observable.Transformer<T, T> applySchedulers(final BaseView view) {
public static <T> Observable.Transformer<T, T> applySchedulers(final IView view) {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> observable) {
......@@ -41,7 +41,7 @@ public class RxUtils {
}
public static <T> LifecycleTransformer<T> bindToLifecycle(BaseView view) {
public static <T> LifecycleTransformer<T> bindToLifecycle(IView view) {
if (view instanceof BaseActivity) {
return ((BaseActivity) view).<T>bindToLifecycle();
} else if (view instanceof BaseFragment) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册