Skip to content

Commit

Permalink
加入dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
wosojadfjgo committed Aug 30, 2019
1 parent 5ab02f5 commit c39f76d
Show file tree
Hide file tree
Showing 24 changed files with 731 additions and 217 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ dependencies {
implementation "com.meituan.android.walle:library:${versions.walle}"
//Arouter路由
kapt "com.alibaba:arouter-compiler:${versions.arouterCompiler}"
//dagger
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand Down
3 changes: 3 additions & 0 deletions base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ dependencies {
api "com.alibaba:arouter-annotation:${versions.arouterAnnotation}"
api "com.alibaba:arouter-api:${versions.arouterApi}"
kapt "com.alibaba:arouter-compiler:${versions.arouterCompiler}"
//dagger
api "com.google.dagger:dagger:${versions.dagger}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
//解决dex超出方法数限制的问题
api "com.android.support:multidex:${versions.multidex}"
//工具类
Expand Down
28 changes: 28 additions & 0 deletions base/src/main/java/com/heyongrui/base/app/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
import com.heyongrui.base.BuildConfig;
import com.heyongrui.base.assist.AppManager;
import com.heyongrui.base.assist.NetStateChangeReceiver;
import com.heyongrui.base.dagger.AppComponent;
import com.heyongrui.base.dagger.AppModule;
import com.heyongrui.base.dagger.DaggerAppComponent;
import com.tencent.smtt.sdk.QbSdk;

public class BaseApplication extends Application {

private static BaseApplication instance;
private static AppComponent appComponent;

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
Expand All @@ -25,6 +32,8 @@ protected void attachBaseContext(Context base) {
@Override
public void onCreate() {
super.onCreate();
instance = this;
initializeInjector();
//工具类初始化
Utils.init(this);
//初始化Arouter
Expand Down Expand Up @@ -93,4 +102,23 @@ public void onActivityDestroyed(Activity activity) {
}
});
}

public static BaseApplication getInstance() {
return instance;
}

public static Context getContext() {
return getInstance().getApplicationContext();
}

public static AppComponent getAppComponent() {
return appComponent;
}

//初始化component
private void initializeInjector() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
}
54 changes: 33 additions & 21 deletions base/src/main/java/com/heyongrui/base/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.NetworkUtils;
import com.heyongrui.base.app.BaseApplication;
import com.heyongrui.base.assist.NetStateChangeObserver;
import com.heyongrui.base.assist.NetStateChangeReceiver;
import com.heyongrui.base.dagger.AppComponent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -28,7 +30,7 @@
*/
public abstract class BaseActivity<T extends BasePresenter> extends SupportActivity implements NetStateChangeObserver {

protected String TAG = "BaseFragment";
protected String TAG = "BaseActivity";
protected T mPresenter;

@Override
Expand All @@ -39,6 +41,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
Log.i("onCreate", "onCreate fixOrientation when Oreo, result = " + result);
}
super.onCreate(savedInstanceState);
initializeInjector();
if (isImmersionBarEnabled()) {
initImmersionBar();
}
Expand All @@ -49,10 +52,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}
ARouter.getInstance().inject(this);
mPresenter = setPresenter();
if (mPresenter != null) {
if (this instanceof BaseView) {
mPresenter.attchView(this, (BaseView) this);
}
if (null != mPresenter && this instanceof BaseView) {
mPresenter.attchView(this, (BaseView) this);
}
init(savedInstanceState);
if (!NetworkUtils.isConnected()) {
Expand Down Expand Up @@ -90,24 +91,12 @@ protected void onStop() {

@Override
protected void onDestroy() {
if (mPresenter != null) {
if (null != mPresenter) {
mPresenter.detachView();
}
super.onDestroy();
}

protected boolean needRegisterNetworkChangeObserver() {
return false;
}

@Override
public void onNetDisconnected() {
}

@Override
public void onNetConnected(NetworkUtils.NetworkType networkType) {
}

protected boolean fixOrientation() {
try {
Field field = Activity.class.getDeclaredField("mActivityInfo");
Expand Down Expand Up @@ -161,6 +150,20 @@ protected void addOnLongClickListeners(View.OnLongClickListener onClickListener,
}
}

protected AppComponent getAppComponent() {
return BaseApplication.getAppComponent();
}

protected void initializeInjector() {
}

protected boolean isImmersionBarEnabled() {//是否启用沉浸式
return true;
}

protected void initImmersionBar() {
}

protected abstract int getLayoutId();

protected T setPresenter() {
Expand All @@ -169,10 +172,19 @@ protected T setPresenter() {

protected abstract void init(Bundle savedInstanceState);

protected boolean isImmersionBarEnabled() {//是否启用沉浸式
return true;

/**
* 是否需要注册网络变化的Observer,如果不需要监听网络变化,则返回false;否则返回true.默认返回false
*/
protected boolean needRegisterNetworkChangeObserver() {
return false;
}

protected void initImmersionBar() {
@Override
public void onNetDisconnected() {//网络断开
}

@Override
public void onNetConnected(NetworkUtils.NetworkType networkType) {//网络连接
}
}
67 changes: 38 additions & 29 deletions base/src/main/java/com/heyongrui/base/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import androidx.fragment.app.FragmentActivity;

import com.blankj.utilcode.util.NetworkUtils;
import com.heyongrui.base.app.BaseApplication;
import com.heyongrui.base.assist.NetStateChangeObserver;
import com.heyongrui.base.assist.NetStateChangeReceiver;
import com.heyongrui.base.dagger.AppComponent;

import me.yokeyword.fragmentation.SupportFragment;

Expand All @@ -33,6 +35,7 @@ public abstract class BaseFragment<T extends BasePresenter> extends SupportFragm
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeInjector();
TAG = this.getClass().getSimpleName();
mActivity = getActivity();
mContext = getContext();
Expand All @@ -46,21 +49,14 @@ public void onSupportVisible() {
}
}

public void initImmersionBar() {
}

private boolean isImmersionBarEnabled() {
return true;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mInflater = inflater;
mPresenter = setPresenter();
if (mPresenter != null) {
if (this instanceof BaseView) mPresenter.attchView(mContext, (BaseView) this);
if (null != mPresenter && this instanceof BaseView) {
mPresenter.attchView(mContext, (BaseView) this);
}
if (mView == null) {
if (null == mView) {
mView = inflater.inflate(getLayoutId(), container, false);
initView(savedInstanceState);
} else {
Expand Down Expand Up @@ -107,27 +103,12 @@ public void onStop() {

@Override
public void onDestroy() {
if (mPresenter != null) {
if (null != mPresenter) {
mPresenter.detachView();
}
super.onDestroy();
}

/**
* 是否需要注册网络变化的Observer,如果不需要监听网络变化,则返回false;否则返回true.默认返回false
*/
protected boolean needRegisterNetworkChangeObserver() {
return false;
}

@Override
public void onNetDisconnected() {//网络断开
}

@Override
public void onNetConnected(NetworkUtils.NetworkType networkType) {//网络连接
}

protected void addOnClickListeners(View.OnClickListener onClickListener, View... views) {
if (views != null) {
for (View view : views) {
Expand All @@ -137,7 +118,7 @@ protected void addOnClickListeners(View.OnClickListener onClickListener, View...
}

protected void addOnClickListeners(View.OnClickListener onClickListener, @IdRes int... ids) {
if (mView == null) return;
if (null == mView) return;
if (ids != null) {
for (@IdRes int id : ids) {
mView.findViewById(id).setOnClickListener(onClickListener);
Expand All @@ -146,22 +127,50 @@ protected void addOnClickListeners(View.OnClickListener onClickListener, @IdRes
}

protected void addOnLongClickListeners(View.OnLongClickListener onClickListener, @IdRes int... ids) {
if (mView == null) return;
if (null == mView) return;
if (ids != null) {
for (@IdRes int id : ids) {
mView.findViewById(id).setOnLongClickListener(onClickListener);
}
}
}

protected AppComponent getAppComponent() {
return BaseApplication.getAppComponent();
}

protected abstract int getLayoutId();
protected void initializeInjector() {
}

protected T setPresenter() {
return null;
}

protected abstract int getLayoutId();

protected abstract void initView(Bundle savedInstanceState);

protected abstract void initData(Bundle savedInstanceState);

protected void initImmersionBar() {
}

protected boolean isImmersionBarEnabled() {
return true;
}

/**
* 是否需要注册网络变化的Observer,如果不需要监听网络变化,则返回false;否则返回true.默认返回false
*/
protected boolean needRegisterNetworkChangeObserver() {
return false;
}

@Override
public void onNetDisconnected() {//网络断开
}

@Override
public void onNetConnected(NetworkUtils.NetworkType networkType) {//网络连接
}
}
22 changes: 22 additions & 0 deletions base/src/main/java/com/heyongrui/base/dagger/AppComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.heyongrui.base.dagger;


import com.heyongrui.base.assist.AppData;

import javax.inject.Singleton;

import dagger.Component;

/**
* 2019/8/26
* lambert
*/

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
/*
* 暴露给依赖者Component(依赖者若使用@Component,此处必须暴露,否则依赖者无法@Inject AppData;依赖者若使用@Subcomponent,此处可以不暴露依赖者也可@Inject AppData)
*/
AppData appData();
}
40 changes: 40 additions & 0 deletions base/src/main/java/com/heyongrui/base/dagger/AppModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.heyongrui.base.dagger;


import com.heyongrui.base.app.BaseApplication;
import com.heyongrui.base.assist.AppData;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

/**
* 2019/8/26
* lambert
*/

@Module
public class AppModule {
private BaseApplication baseApplication;

public AppModule(BaseApplication baseApplication) {
this.baseApplication = baseApplication;
}

@Provides
@Singleton
BaseApplication providesApplication() {
return baseApplication;
}


/*
* @Inject AppData使用单例,由于AppModule在BaseApplication中初始,所以@Inject AppData是全局单例(而且AppComponent也必须加上@Singleton)
*/
@Provides
@Singleton
AppData provideAppData() {
return new AppData();
}
}
17 changes: 17 additions & 0 deletions base/src/main/java/com/heyongrui/base/dagger/PerActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.heyongrui.base.dagger;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Scope;

/**
* 2019/8/26
* lambert
*/
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}
Loading

0 comments on commit c39f76d

Please sign in to comment.