提交 8c88ef50 编写于 作者: J jessyan

improve appmanager

上级 18d3c24b
......@@ -292,10 +292,12 @@ import static com.jess.arms.utils.Preconditions.checkNotNull;
#parse("File Header.java")
@ActivityScope
public class ${NAME}Model extends BaseModel<ServiceManager,CacheManager> implements ${NAME}Contract.Model{
private Gson mGson;
private Application mApplication;
@Inject
public ${NAME}Model(ServiceManager serviceManager, CacheManager cacheManager, Gson gson, Application application) {
super(serviceManager, cacheManager);
this.mGson = gson;
......@@ -356,9 +358,8 @@ public class ${NAME}Module {
@ActivityScope
@Provides
${NAME}Contract.Model provide${NAME}Model(ServiceManager serviceManager, CacheManager cacheManager
,Gson gson,Application application){
return new ${NAME}Model(serviceManager,cacheManager,gson,application);
${NAME}Contract.Model provide${NAME}Model(${NAME}Model model){
return model;
}
}
--------------------------------------------------------------------------------------
......
......@@ -6,8 +6,6 @@ import dagger.Module;
import dagger.Provides;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.UserModel;
import me.jessyan.mvparms.demo.mvp.model.api.cache.CacheManager;
import me.jessyan.mvparms.demo.mvp.model.api.service.ServiceManager;
/**
* Created by jess on 9/4/16 11:10
......@@ -33,7 +31,7 @@ public class UserModule {
@ActivityScope
@Provides
UserContract.Model provideUserModel(ServiceManager serviceManager, CacheManager cacheManager){
return new UserModel(serviceManager,cacheManager);
UserContract.Model provideUserModel(UserModel model){
return model;
}
}
package me.jessyan.mvparms.demo.mvp.model;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BaseModel;
import java.util.List;
import javax.inject.Inject;
import io.rx_cache.DynamicKey;
import io.rx_cache.EvictDynamicKey;
import io.rx_cache.Reply;
......@@ -18,9 +21,11 @@ import rx.functions.Func1;
* Created by jess on 9/4/16 10:56
* Contact with jess.yan.effort@gmail.com
*/
@ActivityScope
public class UserModel extends BaseModel<ServiceManager, CacheManager> implements UserContract.Model {
public static final int USERS_PER_PAGE = 10;
@Inject
public UserModel(ServiceManager serviceManager, CacheManager cacheManager) {
super(serviceManager, cacheManager);
}
......
......@@ -16,6 +16,9 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import timber.log.Timber;
/**
......@@ -25,7 +28,7 @@ import timber.log.Timber;
* Created by jess on 14/12/2016 13:50
* Contact with jess.yan.effort@gmail.com
*/
@Singleton
public class AppManager {
protected final String TAG = this.getClass().getSimpleName();
public static final String APPMANAGER_MESSAGE = "appmanager_message";
......@@ -40,6 +43,7 @@ public class AppManager {
//当前在前台的activity
private BaseActivity mCurrentActivity;
@Inject
public AppManager(Application application) {
this.mApplication = application;
EventBus.getDefault().register(this);
......
......@@ -5,7 +5,6 @@ import android.content.Context;
import com.jess.arms.di.component.DaggerBaseComponent;
import com.jess.arms.di.module.AppModule;
import com.jess.arms.di.module.BaseModule;
import com.jess.arms.di.module.ClientModule;
import com.jess.arms.di.module.ImageModule;
import com.jess.arms.http.GlobeHttpHandler;
......@@ -38,20 +37,21 @@ public abstract class BaseApplication extends Application {
public void onCreate() {
super.onCreate();
mApplication = this;
this.mAppModule = new AppModule(this);//提供application
DaggerBaseComponent
.builder()
.baseModule(new BaseModule(this))
.appModule(mAppModule)
.build()
.inject(this);
this.mImagerModule = new ImageModule();//图片加载框架默认使用glide
this.mClientModule = ClientModule//用于提供okhttp和retrofit的单列
.buidler()
.baseurl(getBaseUrl())
.globeHttpHandler(getHttpHandler())
.interceptors(getInterceptors())
.appManager(mAppManager)//DaggerBaseComponent.inject()后,mAppManager才有值
.responseErroListener(getResponseErroListener())
.build();
this.mAppModule = new AppModule(this, mAppManager);//提供application
this.mImagerModule = new ImageModule();//图片加载框架默认使用glide
}
/**
......
package com.jess.arms.di.component;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.module.BaseModule;
import com.jess.arms.di.module.AppModule;
import javax.inject.Singleton;
......@@ -12,7 +12,7 @@ import dagger.Component;
* Contact with jess.yan.effort@gmail.com
*/
@Singleton
@Component(modules={BaseModule.class})
@Component(modules={AppModule.class})
public interface BaseComponent {
void inject(BaseApplication application);
}
......@@ -3,7 +3,6 @@ package com.jess.arms.di.module;
import android.app.Application;
import com.google.gson.Gson;
import com.jess.arms.base.AppManager;
import javax.inject.Singleton;
......@@ -16,11 +15,9 @@ import dagger.Provides;
@Module
public class AppModule {
private Application mApplication;
private AppManager mAppManager;
public AppModule(Application application, AppManager appManager) {
public AppModule(Application application) {
this.mApplication = application;
this.mAppManager = appManager;
}
@Singleton
......@@ -33,7 +30,4 @@ public class AppModule {
@Provides
public Gson provideGson(){return new Gson();}
@Singleton
@Provides
public AppManager provideAppManager(){return mAppManager;}
}
package com.jess.arms.di.module;
import android.app.Application;
import com.jess.arms.base.AppManager;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Created by jess on 14/12/2016 13:54
* Contact with jess.yan.effort@gmail.com
*/
@Module
public class BaseModule {
private Application mApplication;
public BaseModule(Application application) {
this.mApplication = application;
}
@Singleton
@Provides
public AppManager provideAppManager(){ return new AppManager(mApplication);}
}
......@@ -3,6 +3,7 @@ package com.jess.arms.di.module;
import android.app.Application;
import android.text.TextUtils;
import com.jess.arms.base.AppManager;
import com.jess.arms.http.GlobeHttpHandler;
import com.jess.arms.http.RequestIntercept;
import com.jess.arms.utils.DataHelper;
......@@ -38,6 +39,7 @@ public class ClientModule {
private GlobeHttpHandler mHandler;
private Interceptor[] mInterceptors;
private ResponseErroListener mErroListener;
private AppManager mAppManager;
/**
* @author: jess
......@@ -49,42 +51,72 @@ public class ClientModule {
this.mHandler = buidler.handler;
this.mInterceptors = buidler.interceptors;
this.mErroListener = buidler.responseErroListener;
this.mAppManager = buidler.appManager;
}
public static Buidler buidler() {
return new Buidler();
}
/**
* @param cache 缓存
* @param intercept 拦截器
* @param builder
* @param client
* @param httpUrl
* @return
* @author: jess
* @date 8/30/16 1:12 PM
* @description:提供OkhttpClient
* @date 8/30/16 1:15 PM
* @description:提供retrofit
*/
@Singleton
@Provides
OkHttpClient provideClient(Cache cache, Interceptor intercept) {
final OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
return configureClient(okHttpClient, cache, intercept);
Retrofit provideRetrofit(Retrofit.Builder builder, OkHttpClient client, HttpUrl httpUrl) {
return builder
.baseUrl(httpUrl)//域名
.client(client)//设置okhttp
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//使用rxjava
.addConverterFactory(GsonConverterFactory.create())//使用Gson
.build();
}
/**
* @param client
* @param httpUrl
* 提供OkhttpClient
*
* @param okHttpClient
* @return
* @author: jess
* @date 8/30/16 1:13 PM
* @description: 提供retrofit
*/
@Singleton
@Provides
Retrofit provideRetrofit(OkHttpClient client, HttpUrl httpUrl) {
final Retrofit.Builder builder = new Retrofit.Builder();
return configureRetrofit(builder, client, httpUrl);
OkHttpClient provideClient(OkHttpClient.Builder okHttpClient, Cache cache, Interceptor intercept) {
OkHttpClient.Builder builder = okHttpClient
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.readTimeout(TIME_OUT, TimeUnit.SECONDS)
.cache(cache)//设置缓存
.addNetworkInterceptor(intercept);
if (mInterceptors != null && mInterceptors.length > 0) {//如果外部提供了interceptor的数组则遍历添加
for (Interceptor interceptor : mInterceptors) {
builder.addInterceptor(interceptor);
}
}
return builder
.build();
}
@Singleton
@Provides
Retrofit.Builder provideRetrofitBuilder() {
return new Retrofit.Builder();
}
@Singleton
@Provides
OkHttpClient.Builder provideClientBuilder() {
return new OkHttpClient.Builder();
}
@Singleton
@Provides
HttpUrl provideBaseUrl() {
......@@ -147,6 +179,7 @@ public class ClientModule {
/**
* 提供权限管理类,用于请求权限,适配6.0的权限管理
*
* @param application
* @return
*/
......@@ -158,44 +191,13 @@ public class ClientModule {
/**
* @param builder
* @param client
* @param httpUrl
* @return
* @author: jess
* @date 8/30/16 1:15 PM
* @description:配置retrofit
*/
private Retrofit configureRetrofit(Retrofit.Builder builder, OkHttpClient client, HttpUrl httpUrl) {
return builder
.baseUrl(httpUrl)//域名
.client(client)//设置okhttp
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//使用rxjava
.addConverterFactory(GsonConverterFactory.create())//使用Gson
.build();
}
/**
* 配置okhttpclient
*
* @param okHttpClient
* 提供管理所有activity的管理类
* @return
*/
private OkHttpClient configureClient(OkHttpClient.Builder okHttpClient, Cache cache, Interceptor intercept) {
OkHttpClient.Builder builder = okHttpClient
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.readTimeout(TIME_OUT, TimeUnit.SECONDS)
.cache(cache)//设置缓存
.addNetworkInterceptor(intercept);
if (mInterceptors != null && mInterceptors.length > 0) {//如果外部提供了interceptor的数组则遍历添加
for (Interceptor interceptor : mInterceptors) {
builder.addInterceptor(interceptor);
}
}
return builder
.build();
@Singleton
@Provides
AppManager provideAppManager() {
return mAppManager;
}
......@@ -204,6 +206,7 @@ public class ClientModule {
private GlobeHttpHandler handler;
private Interceptor[] interceptors;
private ResponseErroListener responseErroListener;
private AppManager appManager;
private Buidler() {
}
......@@ -231,11 +234,18 @@ public class ClientModule {
return this;
}
public Buidler appManager(AppManager appManager) {//管理所有activity的管理类
this.appManager = appManager;
return this;
}
public ClientModule build() {
if (apiUrl == null) {
if (apiUrl == null)
throw new IllegalStateException("baseurl is required");
}
if (appManager == null)
throw new IllegalStateException("appManager is required");
return new ClientModule(this);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册