Skip to content

Commit 8e87621

Browse files
committed
使用dagger-android 完成注入
1 parent 04db04d commit 8e87621

File tree

15 files changed

+235
-23
lines changed

15 files changed

+235
-23
lines changed

RECORD.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848

4949
*This module should be installed in the component that is used to inject the {android.app.Application} class.*
5050

51-
你还可以用 AndroidSupportInjectionModule
51+
你还可以用 `AndroidSupportInjectionModule` 可以额外支持V4包下的`Fragment`
5252

5353
2、MainActivityModule
5454

5555
*@ContributesAndroidInjector(modules = [FragmentBuildersModule::class])*
5656

57+
理解上是替换了原本 MainActivity注入需要的Component,在 MainActivityModule 中通过 `ContributesAndroidInjector` 并增加需要的其他Module(对Activity内的Fragmentmodule提供注入),实现MainActivity的注入:`AndroidInjection.inject(activity)`
58+
5759
3、AppModule
5860

5961
*提供 Service、Db、Dao的单例注入,依赖 `ViewModelModule`
@@ -63,6 +65,20 @@ ViewModelModule 是 VM相关的*
6365

6466
*通过 @Component.Builder 增加builder方法,提供Application 注入方法。*
6567

68+
引用上述所有Module
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
6679

80+
# Naviagiton:
6781

82+
https://blog.csdn.net/cekiasoo/article/details/80739805
6883

84+
https://blog.csdn.net/weixin_42215792/article/details/80326637

app/build.gradle

+18-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,26 @@ android {
4343

4444
dependencies {
4545
implementation fileTree(dir: 'libs', include: ['*.jar'])
46+
47+
//kotlin plugin
4648
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
49+
//kotlin ktx (used 0.3 now , 1.0.0 need api28)
50+
implementation "androidx.core:core-ktx:$ktx_version"
51+
52+
//support
4753
implementation "com.android.support:appcompat-v7:$android_support"
4854
implementation "com.android.support:support-v4:${android_support}"
4955
implementation "com.android.support:recyclerview-v7:${android_support}"
5056
implementation "com.android.support:design:${android_support}"
5157
implementation "com.android.support:cardview-v7:${android_support}"
52-
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
58+
59+
//constraint
60+
implementation "com.android.support.constraint:constraint-layout:${constraint_version}"
61+
62+
//navigation
63+
implementation "android.arch.navigation:navigation-fragment:${navigation_version}"
64+
implementation "android.arch.navigation:navigation-ui:${navigation_version}"
65+
5366

5467
//dagger
5568
implementation "com.google.dagger:dagger:${dagger_version}"
@@ -58,10 +71,10 @@ dependencies {
5871
kapt "com.google.dagger:dagger-android-processor:${dagger_version}"
5972

6073
//HTTP retrofit2
74+
implementation "com.squareup.okhttp3:logging-interceptor:${okhttp3_version}"
6175
implementation "com.squareup.retrofit2:retrofit:${retrofit_version}"
6276
implementation "com.squareup.retrofit2:converter-gson:${retrofit_version}"
6377
implementation "com.squareup.retrofit2:adapter-rxjava2:${retrofit_version}"
64-
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
6578
implementation ("com.squareup.retrofit2:converter-simplexml:${rx_version}"){
6679
exclude group: 'xpp3', module: 'xpp3'
6780
exclude group: 'stax', module: 'stax-api'
@@ -87,8 +100,8 @@ dependencies {
87100
implementation "devlight.io:navigationtabbar:${tabbar_version}"
88101

89102
//iconics 3.1.0开始使用了androidx
90-
implementation "com.mikepenz:iconics-core:3.0.4@aar"
91-
implementation "com.mikepenz:iconics-views:3.0.4@aar"
103+
implementation "com.mikepenz:iconics-core:${iconics_version}"
104+
implementation "com.mikepenz:iconics-views:${iconics_version}"
92105
implementation 'com.mikepenz:material-design-iconic-typeface:2.2.0.5@aar'
93106
implementation 'com.mikepenz:fontawesome-typeface:5.3.1.1@aar'
94107
implementation 'com.mikepenz:octicons-typeface:3.2.0.5@aar'
@@ -97,5 +110,5 @@ dependencies {
97110
implementation 'com.mikepenz:ionicons-typeface:2.0.1.5@aar'
98111

99112
//EDIT TEXT
100-
implementation 'com.github.HITGIF:TextFieldBoxes:1.4.4'
113+
implementation "com.github.HITGIF:TextFieldBoxes:${textfiled_box_version}"
101114
}

app/src/main/AndroidManifest.xml

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020

2121
<activity
2222
android:name=".module.main.MainActivity"
23-
android:screenOrientation="portrait">
23+
android:screenOrientation="portrait" />
24+
<activity
25+
android:name=".module.StartActivity"
26+
android:screenOrientation="portrait"
27+
android:theme="@style/AppFullTheme">
28+
2429
<intent-filter>
2530
<action android:name="android.intent.action.MAIN" />
2631
<category android:name="android.intent.category.LAUNCHER" />
2732
</intent-filter>
2833
</activity>
29-
<activity
30-
android:name=".module.login.LoginActivity"
31-
android:screenOrientation="portrait">
32-
</activity>
34+
3335

3436
</application>
3537

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
package com.shuyu.github.kotlin.module.login
1+
package com.shuyu.github.kotlin.module
22

33
import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import com.shuyu.github.kotlin.R
66

77
/**
88
* Created by guoshuyu
9-
* Date: 2018-09-28
9+
* Date: 2018-09-30
1010
*/
11-
class LoginActivity : AppCompatActivity() {
11+
class StartActivity : AppCompatActivity() {
12+
1213
override fun onCreate(savedInstanceState: Bundle?) {
1314
super.onCreate(savedInstanceState)
14-
setContentView(R.layout.activity_login)
15+
setContentView(R.layout.activity_start)
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.shuyu.github.kotlin.module.base
2+
3+
import android.os.Bundle
4+
import android.support.v4.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import androidx.navigation.NavOptions
9+
import androidx.navigation.Navigation
10+
11+
/**
12+
* Created by guoshuyu
13+
* Date: 2018-09-30
14+
*/
15+
abstract class BaseFragment : Fragment() {
16+
17+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
18+
val mainView = inflater.inflate(getLayoutId(), container, false)
19+
onCreateView(mainView)
20+
return mainView
21+
}
22+
23+
abstract fun onCreateView(mainView: View)
24+
25+
abstract fun getLayoutId(): Int
26+
27+
28+
fun navigationPopUpTo(view: View, args: Bundle?, actionId: Int, finishStack: Boolean) {
29+
val controller = Navigation.findNavController(view)
30+
controller.navigate(actionId,
31+
args, NavOptions.Builder().setPopUpTo(controller.graph.id, true).build())
32+
if (finishStack) {
33+
activity?.finish()
34+
}
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.shuyu.github.kotlin.module.login
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import com.shuyu.github.kotlin.R
6+
import com.shuyu.github.kotlin.module.base.BaseFragment
7+
import kotlinx.android.synthetic.main.fragment_login.*
8+
9+
/**
10+
* Created by guoshuyu
11+
* Date: 2018-09-28
12+
*/
13+
class LoginFragment : BaseFragment() {
14+
15+
override fun onCreateView(mainView: View) {
16+
}
17+
18+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
19+
super.onViewCreated(view, savedInstanceState)
20+
login_submit_btn.setOnClickListener {
21+
///去主页需要finish
22+
navigationPopUpTo(view, null, R.id.action_nav_login_to_main, true)
23+
}
24+
}
25+
26+
override fun getLayoutId(): Int {
27+
return R.layout.fragment_login
28+
}
29+
}

app/src/main/java/com/shuyu/github/kotlin/module/main/dynamic/DynamicFragment.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package com.shuyu.github.kotlin.module.main.dynamic
22

33
import android.os.Bundle
4-
import android.support.v4.app.Fragment
5-
import android.view.LayoutInflater
64
import android.view.View
7-
import android.view.ViewGroup
85
import com.shuyu.github.kotlin.R
6+
import com.shuyu.github.kotlin.module.base.BaseFragment
97

108
/**
119
* Created by guoshuyu
1210
* Date: 2018-09-28
1311
*/
1412

15-
class DynamicFragment : Fragment() {
13+
class DynamicFragment : BaseFragment() {
1614

17-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
18-
val mainView = inflater.inflate(R.layout.fragment_dynamic, container, false)
19-
return mainView
15+
override fun onCreateView(mainView: View) {
16+
17+
}
18+
19+
override fun getLayoutId(): Int {
20+
return R.layout.fragment_dynamic
2021
}
2122

2223
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.shuyu.github.kotlin.module.welcome
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import com.shuyu.github.kotlin.R
6+
import com.shuyu.github.kotlin.module.base.BaseFragment
7+
8+
/**
9+
* Created by guoshuyu
10+
* Date: 2018-09-30
11+
*/
12+
class WelcomeFragment : BaseFragment() {
13+
14+
15+
override fun onCreateView(mainView: View) {
16+
17+
}
18+
19+
override fun getLayoutId(): Int {
20+
return R.layout.fragment_welcome
21+
}
22+
23+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
24+
super.onViewCreated(view, savedInstanceState)
25+
26+
//TODO 判断是否登录
27+
view.postDelayed({
28+
///去登录页不需要finish
29+
navigationPopUpTo(view, null, R.id.action_nav_wel_to_login,false)
30+
}, 2000)
31+
32+
}
33+
34+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:ignore="MergeRootFrame">
9+
10+
<fragment
11+
android:id="@+id/nav_fragment_container"
12+
android:name="androidx.navigation.fragment.NavHostFragment"
13+
android:layout_width="match_parent"
14+
android:layout_height="match_parent"
15+
app:defaultNavHost="true"
16+
app:navGraph="@navigation/gsy_navigation" />
17+
</FrameLayout>

app/src/main/res/layout/activity_login.xml renamed to app/src/main/res/layout/fragment_login.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
app:radius="5dp"
129129
app:round="false"
130130
app:unableBackgroundColor="@color/colorPrimaryDark"
131-
app:unableTextColor="@android:color/white" />
131+
app:unableTextColor="@android:color/white"
132+
app:navGraph="@navigation/gsy_navigation" />
132133

133134
</LinearLayout>
134135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<TextView
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:gravity="center"
10+
android:text="@string/app_name" />
11+
12+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/gsy_navigation"
6+
app:startDestination="@id/welcomeFragment">
7+
<fragment
8+
android:id="@+id/welcomeFragment"
9+
android:name="com.shuyu.github.kotlin.module.welcome.WelcomeFragment"
10+
android:label="WelcomeFragment"
11+
tools:layout="@layout/fragment_welcome">
12+
<action
13+
android:id="@+id/action_nav_wel_to_main"
14+
app:destination="@id/mainActivity" />
15+
<action
16+
android:id="@+id/action_nav_wel_to_login"
17+
app:destination="@id/loginFragment" />
18+
</fragment>
19+
<fragment
20+
android:id="@+id/loginFragment"
21+
android:name="com.shuyu.github.kotlin.module.login.LoginFragment"
22+
android:label="LoginActivity"
23+
tools:layout="@layout/fragment_login">
24+
<action
25+
android:id="@+id/action_nav_login_to_main"
26+
app:destination="@id/mainActivity" />
27+
</fragment>
28+
<activity
29+
android:id="@+id/mainActivity"
30+
android:name="com.shuyu.github.kotlin.module.main.MainActivity"
31+
android:label="activity_main"
32+
tools:layout="@layout/activity_main">
33+
34+
35+
</activity>
36+
</navigation>

app/src/main/res/values/styles.xml

+7
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@
88
<item name="colorAccent">@color/colorAccent</item>
99
</style>
1010

11+
<style name="AppFullTheme" parent="AppTheme">
12+
<item name="android:windowActionBar">false</item>
13+
<item name="android:windowIsTranslucent">false</item>
14+
<item name="android:windowNoTitle">true</item>
15+
<item name="android:windowFullscreen">true</item>
16+
</style>
17+
1118
</resources>

build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
buildscript {
44
ext.kotlin_version = '1.2.50'
5+
ext.ktx_version = '0.3'
56
ext.android_support = '27.1.1'
7+
ext.navigation_version = '1.0.0-alpha06'
8+
ext.constraint_version = '2.0.0-alpha2'
69
ext.retrofit_version = '2.4.0'
710
ext.rx_version = '2.1.0'
811
ext.event_bus = '3.1.1'
@@ -11,6 +14,9 @@ buildscript {
1114
ext.agentweb_version = '4.0.2'
1215
ext.tabbar_version = '1.2.5'
1316
ext.dagger_version = '2.17'
17+
ext.iconics_version = '3.0.4@aar'
18+
ext.okhttp3_version = '3.10.0'
19+
ext.textfiled_box_version = '1.4.4'
1420

1521
repositories {
1622
google()

img/Navigation.png

236 KB
Loading

0 commit comments

Comments
 (0)