提交 e6693259 编写于 作者: J jessyan

use ConfigModule

上级 564d51a3
......@@ -11,7 +11,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:name="common.WEApplication"
android:name=".app.WEApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
......@@ -39,6 +39,11 @@
android:name="com.jess.arms.widget.imageloader.glide.GlideConfiguration"
android:value="GlideModule"/>
<!--arms配置-->
<meta-data
android:name="me.jessyan.mvparms.demo.app.GlobalConfiguration"
android:value="ConfigModule"/>
</application>
</manifest>
\ No newline at end of file
package common;
import com.jess.arms.base.BaseActivity;
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 IPresenter> extends BaseActivity<P> {
protected WEApplication mWeApplication;
@Override
protected void ComponentInject() {
mWeApplication = (WEApplication) getApplication();
setupActivityComponent(mWeApplication.getAppComponent());
}
//提供AppComponent(提供所有的单例对象)给子类,进行Component依赖
protected abstract void setupActivityComponent(AppComponent appComponent);
@Override
protected void onDestroy() {
super.onDestroy();
this.mWeApplication = null;
}
}
package common;
import com.jess.arms.base.BaseFragment;
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 IPresenter> extends BaseFragment<P> {
protected WEApplication mWeApplication;
@Override
protected void ComponentInject() {
mWeApplication = (WEApplication)mActivity.getApplication();
setupFragmentComponent(mWeApplication.getAppComponent());
}
//提供AppComponent(提供所有的单例对象)给子类,进行Component依赖
protected abstract void setupFragmentComponent(AppComponent appComponent);
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher watcher = WEApplication.getRefWatcher(getActivity());//使用leakCanary检测fragment的内存泄漏
if (watcher != null) {
watcher.watch(this);
}
this.mWeApplication =null;
}
}
package common;
package me.jessyan.mvparms.demo.app;
import android.content.Context;
import android.text.TextUtils;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.module.GlobeConfigModule;
import com.jess.arms.http.GlobeHttpHandler;
import com.jess.arms.http.RequestInterceptor;
import com.jess.arms.integration.ConfigModule;
import com.jess.arms.utils.UiUtils;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import me.jessyan.mvparms.demo.BuildConfig;
import me.jessyan.mvparms.demo.di.module.CacheModule;
import me.jessyan.mvparms.demo.di.module.ServiceModule;
import me.jessyan.mvparms.demo.mvp.model.api.Api;
import okhttp3.Interceptor;
import okhttp3.Request;
......@@ -25,84 +20,16 @@ import okhttp3.Response;
import timber.log.Timber;
/**
* Created by jess on 8/5/16 11:07
* contact with jess.yan.effort@gmail.com
* app的全局配置信息在此配置,需要将此实现类声明到AndroidManifest中
* Created by jess on 12/04/2017 17:25
* Contact with jess.yan.effort@gmail.com
*/
public class WEApplication extends BaseApplication {
private AppComponent mAppComponent;
private RefWatcher mRefWatcher;//leakCanary观察器
@Override
public void onCreate() {
super.onCreate();
mAppComponent = DaggerAppComponent
.builder()
.appModule(getAppModule())//baseApplication提供
.clientModule(getClientModule())//baseApplication提供
.imageModule(getImageModule())//baseApplication提供
.globeConfigModule(getGlobeConfigModule())//全局配置
.serviceModule(new ServiceModule())//需自行创建
.cacheModule(new CacheModule())//需自行创建
.build();
if (BuildConfig.LOG_DEBUG) {//Timber日志打印
Timber.plant(new Timber.DebugTree());
}
installLeakCanary();//leakCanary内存泄露检查
}
public class GlobalConfiguration implements ConfigModule {
@Override
public void onTerminate() {
super.onTerminate();
if (mAppComponent != null)
this.mAppComponent = null;
if (mRefWatcher != null)
this.mRefWatcher = null;
}
/**
* 安装leakCanary检测内存泄露
*/
protected void installLeakCanary() {
this.mRefWatcher = BuildConfig.USE_CANARY ? LeakCanary.install(this) : RefWatcher.DISABLED;
}
/**
* 获得leakCanary观察器
*
* @param context
* @return
*/
public static RefWatcher getRefWatcher(Context context) {
WEApplication application = (WEApplication) context.getApplicationContext();
return application.mRefWatcher;
}
/**
* 将AppComponent返回出去,供其它地方使用, AppComponent接口中声明的方法返回的实例,在getAppComponent()拿到对象后都可以直接使用
*
* @return
*/
public AppComponent getAppComponent() {
return mAppComponent;
}
/**
* app的全局配置信息封装进module(使用Dagger注入到需要配置信息的地方)
* GlobeHttpHandler是在NetworkInterceptor中拦截数据
* 如果想将请求参数加密,则必须在Interceptor中对参数进行处理,GlobeConfigModule.addInterceptor可以添加Interceptor
*
* @return
*/
@Override
protected GlobeConfigModule getGlobeConfigModule() {
return GlobeConfigModule
.builder()
.baseurl(Api.APP_DOMAIN)
public void applyOptions(Context context, GlobeConfigModule.Builder builder) {
builder.baseurl(Api.APP_DOMAIN)
.globeHttpHandler(new GlobeHttpHandler() {// 这里可以提供一个全局处理Http请求和响应结果的处理类,
// 这里可以比客户端提前一步拿到服务器返回的结果,可以做一些操作,比如token超时,重新获取
@Override
......@@ -115,7 +42,7 @@ public class WEApplication extends BaseApplication {
JSONObject object = (JSONObject) array.get(0);
String login = object.getString("login");
String avatar_url = object.getString("avatar_url");
Timber.tag(TAG).w("Result ------> " + login + " || Avatar_url------> " + avatar_url);
Timber.w("Result ------> " + login + " || Avatar_url------> " + avatar_url);
}
} catch (JSONException e) {
......@@ -124,17 +51,17 @@ public class WEApplication extends BaseApplication {
}
/* 这里如果发现token过期,可以先请求最新的token,然后在拿新的token放入request里去重新请求
注意在这个回调之前已经调用过proceed,所以这里必须自己去建立网络请求,如使用okhttp使用新的request去请求
create a new request and modify it accordingly using the new token
Request newRequest = chain.request().newBuilder().header("token", newToken)
.build();
/* 这里如果发现token过期,可以先请求最新的token,然后在拿新的token放入request里去重新请求
注意在这个回调之前已经调用过proceed,所以这里必须自己去建立网络请求,如使用okhttp使用新的request去请求
create a new request and modify it accordingly using the new token
Request newRequest = chain.request().newBuilder().header("token", newToken)
.build();
retry the request
retry the request
response.body().close();
如果使用okhttp将新的请求,请求成功后,将返回的response return出去即可
如果不需要返回新的结果,则直接把response参数返回出去 */
response.body().close();
如果使用okhttp将新的请求,请求成功后,将返回的response return出去即可
如果不需要返回新的结果,则直接把response参数返回出去 */
return response;
}
......@@ -148,14 +75,11 @@ public class WEApplication extends BaseApplication {
return request;
}
})
.responseErroListener((context, e) -> {
.responseErroListener((context1, e) -> {
/* 用来提供处理所有错误的监听
rxjava必要要使用ErrorHandleSubscriber(默认实现Subscriber的onError方法),此监听才生效 */
Timber.tag(TAG).w("------------>" + e.getMessage());
Timber.w("------------>" + e.getMessage());
UiUtils.SnackbarText("net error");
}).build();
});
}
}
package me.jessyan.mvparms.demo.app;
import android.content.Context;
import com.jess.arms.base.BaseApplication;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import me.jessyan.mvparms.demo.BuildConfig;
import timber.log.Timber;
/**
* Created by jess on 8/5/16 11:07
* contact with jess.yan.effort@gmail.com
*/
public class WEApplication extends BaseApplication {
private RefWatcher mRefWatcher;//leakCanary观察器
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.LOG_DEBUG) {//Timber日志打印
Timber.plant(new Timber.DebugTree());
}
installLeakCanary();//leakCanary内存泄露检查
}
@Override
public void onTerminate() {
super.onTerminate();
this.mRefWatcher = null;
}
/**
* 安装leakCanary检测内存泄露
*/
protected void installLeakCanary() {
this.mRefWatcher = BuildConfig.USE_CANARY ? LeakCanary.install(this) : RefWatcher.DISABLED;
}
/**
* 获得leakCanary观察器
*
* @param context
* @return
*/
public static RefWatcher getRefWatcher(Context context) {
WEApplication application = (WEApplication) context.getApplicationContext();
return application.mRefWatcher;
}
}
......@@ -2,7 +2,7 @@ package me.jessyan.mvparms.demo.di.component;
import com.jess.arms.di.scope.ActivityScope;
import common.AppComponent;
import com.jess.arms.di.component.AppComponent;
import dagger.Component;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.ui.activity.UserActivity;
......
package me.jessyan.mvparms.demo.di.module;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import io.rx_cache.internal.RxCache;
import me.jessyan.mvparms.demo.mvp.model.api.cache.CommonCache;
/**
* Created by zhiyicx on 2016/3/30.
*/
@Module
public class CacheModule {
@Singleton
@Provides
CommonCache provideCommonService(RxCache rxCache) {
return rxCache.using(CommonCache.class);
}
}
package me.jessyan.mvparms.demo.di.module;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import me.jessyan.mvparms.demo.mvp.model.api.service.CommonService;
import me.jessyan.mvparms.demo.mvp.model.api.service.UserService;
import retrofit2.Retrofit;
/**
* Created by zhiyicx on 2016/3/30.
*/
@Module
public class ServiceModule {
@Singleton
@Provides
CommonService provideCommonService(Retrofit retrofit) {
return retrofit.create(CommonService.class);
}
@Singleton
@Provides
UserService provideUserService(Retrofit retrofit) {
return retrofit.create(UserService.class);
}
}
package me.jessyan.mvparms.demo.mvp.model;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import java.util.List;
......@@ -11,8 +12,8 @@ import io.rx_cache.DynamicKey;
import io.rx_cache.EvictDynamicKey;
import io.rx_cache.Reply;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.api.cache.CacheManager;
import me.jessyan.mvparms.demo.mvp.model.api.service.ServiceManager;
import me.jessyan.mvparms.demo.mvp.model.api.cache.CommonCache;
import me.jessyan.mvparms.demo.mvp.model.api.service.UserService;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import rx.Observable;
import rx.functions.Func1;
......@@ -22,21 +23,20 @@ import rx.functions.Func1;
* Contact with jess.yan.effort@gmail.com
*/
@ActivityScope
public class UserModel extends BaseModel<ServiceManager, CacheManager> implements UserContract.Model {
public class UserModel extends BaseModel implements UserContract.Model {
public static final int USERS_PER_PAGE = 10;
@Inject
public UserModel(ServiceManager serviceManager, CacheManager cacheManager) {
super(serviceManager, cacheManager);
public UserModel(IRepositoryManager repositoryManager) {
super(repositoryManager);
}
@Override
public Observable<List<User>> getUsers(int lastIdQueried, boolean update) {
Observable<List<User>> users = mServiceManager.getUserService()
Observable<List<User>> users = mRepositoryManager.obtainRetrofitService(UserService.class)
.getUsers(lastIdQueried, USERS_PER_PAGE);
//使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
return mCacheManager.getCommonCache()
return mRepositoryManager.obtainCacheService(CommonCache.class)
.getUsers(users
, new DynamicKey(lastIdQueried)
, new EvictDynamicKey(update))
......
package me.jessyan.mvparms.demo.mvp.model.api.cache;
import com.jess.arms.http.BaseCacheManager;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Created by jess on 8/30/16 13:54
* Contact with jess.yan.effort@gmail.com
*/
@Singleton
public class CacheManager implements BaseCacheManager {
private CommonCache mCommonCache;
/**
* 如果需要添加Cache只需在构造方法中添加对应的Cache,
* 在提供get方法返回出去,只要在CacheModule提供了该Cache Dagger2会自行注入
* @param commonCache
*/
@Inject
public CacheManager(CommonCache commonCache) {
this.mCommonCache = commonCache;
}
public CommonCache getCommonCache() {
return mCommonCache;
}
/**
* 这里可以释放一些资源(注意这里是单例,即不需要在activity的生命周期调用)
*/
@Override
public void onDestroy() {
}
}
package me.jessyan.mvparms.demo.mvp.model.api.service;
import com.jess.arms.http.BaseServiceManager;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Created by jess on 8/5/16 13:01
* contact with jess.yan.effort@gmail.com
*/
@Singleton
public class ServiceManager implements BaseServiceManager {
private CommonService mCommonService;
private UserService mUserService;
/**
* 如果需要添加service只需在构造方法中添加对应的service,在提供get方法返回出去,只要在ServiceModule提供了该service
* Dagger2会自行注入
* @param commonService
*/
@Inject public ServiceManager(CommonService commonService,UserService userService){
this.mCommonService = commonService;
this.mUserService = userService;
}
public CommonService getCommonService() {
return mCommonService;
}
public UserService getUserService() {
return mUserService;
}
/**
* 这里可以释放一些资源(注意这里是单例,即不需要在activity的生命周期调用)
*/
@Override
public void onDestroy() {
}
}
......@@ -8,14 +8,14 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.paginate.Paginate;
import com.tbruyelle.rxpermissions.RxPermissions;
import butterknife.BindView;
import common.AppComponent;
import common.WEActivity;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
......@@ -26,7 +26,7 @@ import rx.android.schedulers.AndroidSchedulers;
import timber.log.Timber;
public class UserActivity extends WEActivity<UserPresenter> implements UserContract.View, SwipeRefreshLayout.OnRefreshListener {
public class UserActivity extends BaseActivity<UserPresenter> implements UserContract.View, SwipeRefreshLayout.OnRefreshListener {
@Nullable
@BindView(R.id.recyclerView)
......
......@@ -6,12 +6,12 @@ import android.widget.ImageView;
import android.widget.TextView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.widget.imageloader.ImageLoader;
import com.jess.arms.widget.imageloader.glide.GlideImageConfig;
import butterknife.BindView;
import common.WEApplication;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import rx.Observable;
......@@ -29,12 +29,12 @@ public class UserItemHolder extends BaseHolder<User> {
@BindView(R.id.tv_name)
TextView mName;
private ImageLoader mImageLoader;//用于加载图片的管理类,默认使用glide,使用策略模式,可替换框架
private final WEApplication mApplication;
private final BaseApplication mApplication;
public UserItemHolder(View itemView) {
super(itemView);
//可以在任何可以拿到Application的地方,拿到AppComponent,从而得到用Dagger管理的单例对象
mApplication = (WEApplication) itemView.getContext().getApplicationContext();
mApplication = (BaseApplication) itemView.getContext().getApplicationContext();
mImageLoader = mApplication.getAppComponent().imageLoader();
}
......
......@@ -7,6 +7,7 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IPresenter;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.zhy.autolayout.AutoFrameLayout;
......@@ -64,14 +65,15 @@ public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActi
setContentView(initView());
//绑定到butterknife
mUnbinder = ButterKnife.bind(this);
ComponentInject();//依赖注入
setupActivityComponent(mApplication.getAppComponent());//依赖注入
initData();
}
/**
* 依赖注入的入口
* 提供AppComponent(提供所有的单例对象)给子类,进行Component依赖
* @param appComponent
*/
protected abstract void ComponentInject();
protected abstract void setupActivityComponent(AppComponent appComponent);
public void FullScreencall() {
......
......@@ -3,17 +3,19 @@ package com.jess.arms.base;
import android.app.Application;
import android.content.Context;
import com.jess.arms.di.component.DaggerBaseComponent;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.di.component.DaggerAppComponent;
import com.jess.arms.di.module.AppModule;
import com.jess.arms.di.module.ClientModule;
import com.jess.arms.di.module.GlobeConfigModule;
import com.jess.arms.di.module.ImageModule;
import com.jess.arms.integration.ActivityLifecycle;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.ConfigModule;
import com.jess.arms.integration.ManifestParser;
import javax.inject.Inject;
import java.util.List;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import javax.inject.Inject;
/**
* 本项目由
......@@ -26,12 +28,7 @@ import static com.jess.arms.utils.Preconditions.checkNotNull;
*/
public abstract class BaseApplication extends Application {
static private BaseApplication mApplication;
private ClientModule mClientModule;
private AppModule mAppModule;
private ImageModule mImageModule;
private GlobeConfigModule mGlobeConfigModule;
@Inject
protected AppManager mAppManager;
private AppComponent mAppComponent;
@Inject
protected ActivityLifecycle mActivityLifecycle;
protected final String TAG = this.getClass().getSimpleName();
......@@ -41,15 +38,15 @@ public abstract class BaseApplication extends Application {
public void onCreate() {
super.onCreate();
mApplication = this;
this.mAppModule = new AppModule(this);//提供application
DaggerBaseComponent
mAppComponent = DaggerAppComponent
.builder()
.appModule(mAppModule)
.build()
.inject(this);
this.mImageModule = new ImageModule();//图片加载框架默认使用glide
this.mClientModule = new ClientModule(mAppManager);//用于提供okhttp和retrofit的单例
this.mGlobeConfigModule = checkNotNull(getGlobeConfigModule(), "lobeConfigModule is required");
.appModule(new AppModule(this))////提供application
.clientModule(new ClientModule())//用于提供okhttp和retrofit的单例
.imageModule(new ImageModule())//图片加载框架默认使用glide
.globeConfigModule(getGlobeConfigModule(this))//全局配置
.build();
mAppComponent.inject(this);
registerActivityLifecycleCallbacks(mActivityLifecycle);
}
......@@ -59,47 +56,44 @@ public abstract class BaseApplication extends Application {
@Override
public void onTerminate() {
super.onTerminate();
if (mClientModule != null)
this.mClientModule = null;
if (mAppModule != null)
this.mAppModule = null;
if (mImageModule != null)
this.mImageModule = null;
if (mActivityLifecycle != null) {
unregisterActivityLifecycleCallbacks(mActivityLifecycle);
}
if (mAppManager != null) {//释放资源
this.mAppManager.release();
this.mAppManager = null;
}
if (mApplication != null)
this.mApplication = null;
this.mAppComponent = null;
this.mActivityLifecycle = null;
this.mApplication = null;
}
/**
* 将app的全局配置信息封装进module(使用Dagger注入到需要配置信息的地方)
* 需要在AndroidManifest中声明{@link ConfigModule}的实现类,和Glide的配置方式相似
*
* @return
*/
protected abstract GlobeConfigModule getGlobeConfigModule();
private GlobeConfigModule getGlobeConfigModule(Application context) {
List<ConfigModule> modules = new ManifestParser(context).parse();
GlobeConfigModule.Builder builder = GlobeConfigModule
.builder()
.baseurl("https://api.github.com");//为了防止用户没有通过GlobeConfigModule配置baseurl,而导致报错,所以提前配置个默认baseurl
public ClientModule getClientModule() {
return mClientModule;
}
public AppModule getAppModule() {
return mAppModule;
}
for (ConfigModule module : modules) {
module.applyOptions(context, builder);
}
public ImageModule getImageModule() {
return mImageModule;
return builder.build();
}
public AppManager getAppManager() {
return mAppManager;
/**
* 将AppComponent返回出去,供其它地方使用, AppComponent接口中声明的方法返回的实例,在getAppComponent()拿到对象后都可以直接使用
*
* @return
*/
public AppComponent getAppComponent() {
return mAppComponent;
}
......
......@@ -6,6 +6,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IPresenter;
import com.trello.rxlifecycle.components.support.RxFragment;
......@@ -42,14 +43,17 @@ public abstract class BaseFragment<P extends IPresenter> extends RxFragment {
mActivity = (BaseActivity) getActivity();
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().register(this);//注册到事件主线
ComponentInject();
setupFragmentComponent(mActivity.mApplication.getAppComponent());
initData();
}
/**
* 依赖注入的入口
* 提供AppComponent(提供所有的单例对象)给子类,进行Component依赖
* @param appComponent
*/
protected abstract void ComponentInject();
protected abstract void setupFragmentComponent(AppComponent appComponent);
@Override
......
package common;
package com.jess.arms.di.component;
import android.app.Application;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.module.AppModule;
import com.jess.arms.di.module.ClientModule;
import com.jess.arms.di.module.GlobeConfigModule;
import com.jess.arms.di.module.ImageModule;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.widget.imageloader.ImageLoader;
import java.io.File;
import javax.inject.Singleton;
import dagger.Component;
import me.jessyan.mvparms.demo.di.module.CacheModule;
import me.jessyan.mvparms.demo.di.module.ServiceModule;
import me.jessyan.mvparms.demo.mvp.model.api.cache.CacheManager;
import me.jessyan.mvparms.demo.mvp.model.api.service.ServiceManager;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import okhttp3.OkHttpClient;
......@@ -24,16 +24,12 @@ import okhttp3.OkHttpClient;
* Created by jess on 8/4/16.
*/
@Singleton
@Component(modules = {AppModule.class, ClientModule.class, ServiceModule.class, ImageModule.class,
CacheModule.class, GlobeConfigModule.class})
@Component(modules = {AppModule.class, ClientModule.class, ImageModule.class, GlobeConfigModule.class})
public interface AppComponent {
Application Application();
//服务管理器,retrofitApi
ServiceManager serviceManager();
//缓存管理器
CacheManager cacheManager();
//用于管理网络请求层,以及数据缓存层
IRepositoryManager repositoryManager();
//Rxjava错误处理管理类
RxErrorHandler rxErrorHandler();
......@@ -47,6 +43,11 @@ public interface AppComponent {
//gson
Gson gson();
//缓存文件根目录(RxCache和Glide的的缓存都已经作为子文件夹在这个目录里),应该将所有缓存放到这个根目录里,便于管理和清理,可在GlobeConfigModule里配置
File cacheFile();
//用于管理所有activity
AppManager appManager();
void inject(BaseApplication application);
}
package com.jess.arms.di.component;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.module.AppModule;
import javax.inject.Singleton;
import dagger.Component;
/**
* Created by jess on 14/12/2016 13:58
* Contact with jess.yan.effort@gmail.com
*/
@Singleton
@Component(modules={AppModule.class})
public interface BaseComponent {
void inject(BaseApplication application);
}
......@@ -3,6 +3,8 @@ package com.jess.arms.di.module;
import android.app.Application;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.integration.RepositoryManager;
import javax.inject.Singleton;
......@@ -30,4 +32,9 @@ public class AppModule {
@Provides
public Gson provideGson(){return new Gson();}
@Singleton
@Provides
public IRepositoryManager provideRepositoryManager(RepositoryManager repositoryManager) {
return repositoryManager;
}
}
......@@ -2,7 +2,6 @@ package com.jess.arms.di.module;
import android.app.Application;
import com.jess.arms.integration.AppManager;
import com.jess.arms.http.GlobeHttpHandler;
import com.jess.arms.http.RequestInterceptor;
import com.jess.arms.utils.DataHelper;
......@@ -33,13 +32,8 @@ import retrofit2.converter.gson.GsonConverterFactory;
@Module
public class ClientModule {
private static final int TIME_OUT = 10;
private AppManager mAppManager;
public ClientModule(AppManager appManager) {
this.mAppManager = appManager;
}
/**
* @param builder
* @param client
......@@ -147,17 +141,6 @@ public class ClientModule {
}
/**
* 提供管理所有activity的管理类
*
* @return
*/
@Singleton
@Provides
AppManager provideAppManager() {
return mAppManager;
}
// .addNetworkInterceptor(new Interceptor() {
// @Override
......
package com.jess.arms.http;
/**
* Created by jess on 8/30/16 16:00
* Contact with jess.yan.effort@gmail.com
*/
public interface BaseCacheManager {
void onDestroy();
}
package com.jess.arms.http;
/**
* Created by jess on 8/30/16 15:59
* Contact with jess.yan.effort@gmail.com
*/
public interface BaseServiceManager {
void onDestroy();
}
package com.jess.arms.integration;
import android.content.Context;
import com.jess.arms.di.module.GlobeConfigModule;
/**
* 此接口可以给框架配置一些参数,需要实现类实现后,并在AndroidManifest中声明该实现类
* Created by jess on 12/04/2017 11:37
* Contact with jess.yan.effort@gmail.com
*/
public interface ConfigModule {
void applyOptions(Context context, GlobeConfigModule.Builder builder);
}
package com.jess.arms.integration;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jess on 12/04/2017 14:41
* Contact with jess.yan.effort@gmail.com
*/
public final class ManifestParser {
private static final String MODULE_VALUE = "ConfigModule";
private final Context context;
public ManifestParser(Application context) {
this.context = context;
}
public List<ConfigModule> parse() {
List<ConfigModule> modules = new ArrayList<ConfigModule>();
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData != null) {
for (String key : appInfo.metaData.keySet()) {
if (MODULE_VALUE.equals(appInfo.metaData.get(key))) {
modules.add(parseModule(key));
}
}
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException("Unable to find metadata to parse ConfigModule", e);
}
return modules;
}
private static ConfigModule parseModule(String className) {
Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Unable to find ConfigModule implementation", e);
}
Object module;
try {
module = clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Unable to instantiate ConfigModule implementation for " + clazz, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to instantiate ConfigModule implementation for " + clazz, e);
}
if (!(module instanceof ConfigModule)) {
throw new RuntimeException("Expected instanceof ConfigModule, but found: " + module);
}
return (ConfigModule) module;
}
}
\ No newline at end of file
package com.jess.arms.mvp;
import com.jess.arms.http.BaseCacheManager;
import com.jess.arms.http.BaseServiceManager;
import com.jess.arms.integration.IRepositoryManager;
/**
* Created by jess on 8/5/16 12:55
* contact with jess.yan.effort@gmail.com
*/
public class BaseModel<S extends BaseServiceManager, C extends BaseCacheManager> implements IModel{
protected S mServiceManager;//服务管理类,用于网络请求
protected C mCacheManager;//缓存管理类,用于管理本地或者内存缓存
public class BaseModel implements IModel {
protected IRepositoryManager mRepositoryManager;//用于管理网络请求层,以及数据缓存层
public BaseModel(S serviceManager, C cacheManager) {
this.mServiceManager = serviceManager;
this.mCacheManager = cacheManager;
public BaseModel(IRepositoryManager repositoryManager) {
this.mRepositoryManager = repositoryManager;
}
@Override
public void onDestroy() {
if (mServiceManager != null) {
mServiceManager = null;
}
if (mCacheManager != null) {
mCacheManager = null;
}
mRepositoryManager = null;
}
}
......@@ -43,10 +43,9 @@ public class BasePresenter<M extends IModel, V extends IView> implements IPresen
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().unregister(this);//解除注册eventbus
unSubscribe();//解除订阅
if (mModel != null) {
if (mModel != null)
mModel.onDestroy();
this.mModel = null;
}
this.mModel = null;
this.mRootView = null;
this.mCompositeSubscription = null;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册