Skip to content

Commit c8eda2b

Browse files
committed
apply dagger2 to inject configuration module.
1 parent 9a644cb commit c8eda2b

32 files changed

+356
-108
lines changed

app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ android {
3232
apt libraries.butterKnifeCompiler
3333

3434
compile libraries.fastJson
35+
36+
// Dagger dependencies
37+
apt libraries.daggerCompiler
38+
provided libraries.javaxAnnotation
39+
compile libraries.dagger
3540
}
3641

3742
lintOptions {
@@ -88,5 +93,30 @@ android {
8893
// // Set the severity of the given issues to ignore (same as disabling the check)
8994
// ignore 'TypographyQuotes'
9095
}
96+
97+
// If you need to add more flavors, consider using flavor dimensions.
98+
productFlavors {
99+
mock {
100+
applicationIdSuffix = ".mock"
101+
}
102+
prod {
103+
104+
}
105+
}
106+
107+
// Remove mockRelease as it's not needed.
108+
android.variantFilter { variant ->
109+
if(variant.buildType.name.equals('release')
110+
&& variant.getFlavors().get(0).name.equals('mock')) {
111+
variant.setIgnore(true);
112+
}
113+
}
114+
115+
// Always show the result of every unit test, even if it passes.
116+
testOptions.unitTests.all {
117+
testLogging {
118+
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
119+
}
120+
}
91121
}
92122

app/src/androidTest/java/com/cleaner/home/ApplicationTest.java renamed to app/src/androidTest/java/com/cleaner/main/ApplicationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
import android.app.Application;
44
import android.test.ApplicationTestCase;

app/src/main/AndroidManifest.xml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest
3-
package="com.cleaner.home"
3+
package="com.cleaner.main"
44
xmlns:android="http://schemas.android.com/apk/res/android">
55

66
<application
77
android:allowBackup="true"
88
android:hardwareAccelerated="true"
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
11-
android:theme="@style/AppTheme">
11+
android:theme="@style/AppTheme"
12+
android:name="com.cleaner.CleanCodeApplication"
13+
>
1214

13-
<activity android:name="com.cleaner.home.MainActivity"
15+
<activity android:name="com.cleaner.main.MainActivity"
1416
android:configChanges="orientation|keyboardHidden|screenSize">
1517
<intent-filter>
1618
<action android:name="android.intent.action.MAIN"/>
1719
<category android:name="android.intent.category.LAUNCHER"/>
1820
</intent-filter>
1921
</activity>
2022

21-
<activity android:name=".SummaryActivity"/>
22-
<activity android:name=".RecyclerSummaryActivity"/>
23-
<activity android:name=".RecyclerSummaryV2Activity"/>
24-
<activity android:name=".Message_TabActivity" />
25-
<activity android:name=".TabSearchDoctorActivity"/>
26-
<activity android:name=".MineActivity"/>
27-
<activity android:name=".DiscoveryActivity" />
23+
<activity android:name="com.cleaner.main.SummaryActivity"/>
24+
<activity android:name="com.cleaner.main.RecyclerSummaryActivity"/>
25+
<activity android:name="com.cleaner.main.RecyclerSummaryV2Activity"/>
26+
<activity android:name="com.cleaner.main.Message_TabActivity" />
27+
<activity android:name="com.cleaner.main.TabSearchDoctorActivity"/>
28+
<activity android:name="com.cleaner.main.MineActivity"/>
29+
<activity android:name="com.cleaner.main.DiscoveryActivity" />
2830
</application>
2931

3032
</manifest>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cleaner;
2+
3+
import android.content.Context;
4+
5+
import dagger.Module;
6+
import dagger.Provides;
7+
8+
/**
9+
* This is a Dagger module. We use this to pass in the Context dependency to the
10+
*/
11+
@Module
12+
public final class ApplicationModule {
13+
14+
private final Context mContext;
15+
16+
ApplicationModule(Context context) {
17+
mContext = context;
18+
}
19+
20+
@Provides
21+
Context provideContext() {
22+
return mContext;
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cleaner;
2+
3+
import android.app.Application;
4+
5+
import com.cleaner.config.ConfigurationRepositoryComponent;
6+
import com.cleaner.config.DaggerConfigurationRepositoryComponent;
7+
import com.cleaner.config.ConfigurationRepositoryModule;
8+
9+
public class CleanCodeApplication extends Application {
10+
11+
private ConfigurationRepositoryComponent mRepositoryComponent;
12+
13+
@Override
14+
public void onCreate() {
15+
super.onCreate();
16+
17+
mRepositoryComponent = DaggerConfigurationRepositoryComponent.builder()
18+
.applicationModule(new ApplicationModule((getApplicationContext())))
19+
.configurationRepositoryModule(new ConfigurationRepositoryModule()).build();
20+
21+
}
22+
23+
public ConfigurationRepositoryComponent getConfigurationRepositoryComponent() {
24+
return mRepositoryComponent;
25+
}
26+
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
package com.cleaner.config;
3+
4+
import javax.inject.Inject;
5+
import javax.inject.Singleton;
6+
7+
@Singleton
8+
public class ConfigurationRepository implements PageConfig {
9+
10+
private final PageConfig pageConfig;
11+
12+
@Inject
13+
ConfigurationRepository(/*@Remote*/ PageConfig pageConfig) {
14+
this.pageConfig = pageConfig;
15+
}
16+
17+
@Override
18+
public boolean hasSummary() {
19+
return pageConfig.hasSummary();
20+
}
21+
22+
@Override
23+
public boolean hasRecyclerSummary() {
24+
return pageConfig.hasRecyclerSummary();
25+
}
26+
27+
@Override
28+
public boolean hasRecyclerSummaryV2() {
29+
return pageConfig.hasRecyclerSummaryV2();
30+
}
31+
32+
@Override
33+
public boolean hasMessage() {
34+
return pageConfig.hasMessage();
35+
}
36+
37+
@Override
38+
public boolean hasDoctorPage() {
39+
return pageConfig.hasDoctorPage();
40+
}
41+
42+
@Override
43+
public boolean hasMinePage() {
44+
return pageConfig.hasMinePage();
45+
}
46+
47+
@Override
48+
public boolean hasDiscoveryPage() {
49+
return pageConfig.hasDiscoveryPage();
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cleaner.config;
2+
3+
4+
import com.cleaner.ApplicationModule;
5+
6+
import javax.inject.Singleton;
7+
8+
import dagger.Component;
9+
10+
@Singleton
11+
@Component(modules = {ConfigurationRepositoryModule.class, ApplicationModule.class})
12+
public interface ConfigurationRepositoryComponent {
13+
14+
ConfigurationRepository getConfigRepository();
15+
}

app/src/main/java/com/cleaner/config/MainPageConfig.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

app/src/main/java/com/cleaner/home/MainPageConfigImpl.java renamed to app/src/main/java/com/cleaner/config/MainPageConfigImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
package com.cleaner.home;
1+
package com.cleaner.config;
22

33
import android.text.TextUtils;
44

5-
import com.cleaner.config.BaseConstantDef;
6-
75
import java.util.Arrays;
86
import java.util.List;
97

108
/**
119
* Created by yangfeng on 16-6-29.
1210
*/
13-
public class MainPageConfigImpl implements MainConfigContracts.PageConfig {
11+
public class MainPageConfigImpl implements PageConfig {
1412
private int summaryVersion = VERSION_NONE;
1513
private int conversationVersion = VERSION_NONE;
1614
private int contactVersion = VERSION_NONE;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.cleaner.config;
2+
3+
/**
4+
* Created by yangfeng on 16-6-27.
5+
*/
6+
public interface PageConfig {
7+
int VERSION_NONE = 0;
8+
int VERSION_FIRST = 1;
9+
int VERSION_SECOND = 2;
10+
int VERSION_THIRD = 3;
11+
12+
boolean hasSummary();
13+
boolean hasRecyclerSummary();
14+
boolean hasRecyclerSummaryV2();
15+
16+
boolean hasMessage();
17+
18+
boolean hasDoctorPage();
19+
20+
boolean hasMinePage();
21+
22+
boolean hasDiscoveryPage();
23+
}

app/src/main/java/com/cleaner/home/BaseTabActivity.java renamed to app/src/main/java/com/cleaner/main/BaseTabActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
import android.os.Bundle;
44
import android.support.v4.app.FragmentActivity;

app/src/main/java/com/cleaner/home/DiscoveryActivity.java renamed to app/src/main/java/com/cleaner/main/DiscoveryActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
/**
44
* Created by yangfeng on 16-7-5.

app/src/main/java/com/cleaner/home/ExitingTrigger.java renamed to app/src/main/java/com/cleaner/main/ExitingTrigger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
/**
44
* Created by yangfeng on 16-6-28.

app/src/main/java/com/cleaner/home/KeyEventHelper.java renamed to app/src/main/java/com/cleaner/main/KeyEventHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
import android.view.KeyEvent;
44

app/src/main/java/com/cleaner/home/MainActivity.java renamed to app/src/main/java/com/cleaner/main/MainActivity.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cleaner.home;
1+
package com.cleaner.main;
22

33
import android.app.TabActivity;
44
import android.content.Intent;
@@ -11,9 +11,11 @@
1111
import android.widget.TabHost.TabSpec;
1212
import android.widget.Toast;
1313

14-
import com.cleaner.config.PatientUtil;
14+
import com.cleaner.CleanCodeApplication;
1515
import com.cleaner.view.BadgeRadioButton;
1616

17+
import javax.inject.Inject;
18+
1719
import butterknife.BindView;
1820
import butterknife.ButterKnife;
1921
import butterknife.Unbinder;
@@ -69,13 +71,22 @@ public class MainActivity extends TabActivity implements MainConfigContracts.Con
6971

7072
private final RadioClickListener listener = new RadioClickListener();
7173

74+
75+
@Inject MainConfigPresenterImpl configPresenter;
76+
7277
@Override
7378
protected void onCreate(Bundle savedInstanceState) {
7479
super.onCreate(savedInstanceState);
7580
if (savedInstanceState != null) {
7681
setActiveIndex(savedInstanceState.getInt(ACTIVE_INDEX_KEY, INDEX_SUMMARY));
7782
}
7883

84+
// Create the presenter
85+
DaggerMainConfigComponent.builder()
86+
.configurationRepositoryComponent(((CleanCodeApplication) getApplication()).getConfigurationRepositoryComponent())
87+
.mainConfigPresenterModule(new MainConfigPresenterModule(this)).build()
88+
.inject(this);
89+
7990
setContentView(R.layout.activity_main);
8091
}
8192

@@ -89,7 +100,8 @@ public void onContentChanged() {
89100
// checkAndLoginXbkpIM();
90101
// PatientApplication.getInstance().addActivity(this);
91102

92-
MainConfigContracts.ConfigPresenter configPresenter = new MainConfigPresenterImpl(this, parsePageConfig());
103+
// MainConfigContracts.ConfigPresenter configPresenter = new MainConfigPresenterImpl(this, pageConfig);
104+
93105
initSelectTab(getIntent());
94106
initRadioButtons();
95107
configPresenter.initTabs();
@@ -177,11 +189,11 @@ protected void onResume() {
177189
// }
178190
// }
179191

180-
private MainConfigContracts.PageConfig parsePageConfig() {
181-
String pageConfigKey = MainPageConfigImpl.getPageConfigKey();
182-
String preferredConfig = PatientUtil.loadConfigItem(this, pageConfigKey);
183-
return new MainPageConfigImpl(preferredConfig);
184-
}
192+
// private PageConfig parsePageConfig() {
193+
// String pageConfigKey = MainPageConfigImpl.getPageConfigKey();
194+
// String preferredConfig = PatientUtil.loadConfigItem(this, pageConfigKey);
195+
// return new MainPageConfigImpl(preferredConfig);
196+
// }
185197

186198
@Override
187199
public void addSummaryTab() {

0 commit comments

Comments
 (0)