Skip to content

Commit

Permalink
第四部分 依赖注入dagger2
Browse files Browse the repository at this point in the history
  • Loading branch information
ditclear committed May 13, 2018
1 parent 5f696a7 commit bfbbc45
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 179 deletions.
435 changes: 296 additions & 139 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ dependencies {
implementation 'android.arch.persistence.room:rxjava2:1.0.0'
kapt 'android.arch.persistence.room:compiler:1.0.0'

//dagger2 di
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'

//不重要
implementation 'us.feras.mdv:markdownview:1.1.0'
implementation 'org.jsoup:jsoup:1.10.3'//处理html中的image
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/io/ditclear/app/di/component/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.ditclear.app.di.component

import dagger.Component
import io.ditclear.app.di.module.AppModule
import io.ditclear.app.view.PaoActivity
import javax.inject.Singleton

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent{

fun inject(activity: PaoActivity)
}
36 changes: 36 additions & 0 deletions app/src/main/java/io/ditclear/app/di/module/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.ditclear.app.di.module

import android.content.Context
import dagger.Module
import dagger.Provides
import io.ditclear.app.helper.Constants
import io.ditclear.app.model.local.AppDatabase
import io.ditclear.app.model.remote.PaoService
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
class AppModule(val applicationContext: Context){

//提供 Retrofit 实例
@Provides @Singleton
fun provideRemoteClient(): Retrofit = Retrofit.Builder()
.baseUrl(Constants.HOST_API)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()

//提供 PaoService 实例
@Provides @Singleton
fun providePaoService(client:Retrofit) =client.create(PaoService::class.java)

//提供 数据库 实例
@Provides @Singleton
fun provideAppDataBase():AppDatabase = AppDatabase.getInstance(applicationContext)

//提供PaoDao 实例
@Provides @Singleton
fun providePaoDao(dataBase:AppDatabase)=dataBase.paoDao()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package io.ditclear.app.model.repository

import io.ditclear.app.model.local.dao.PaoDao
import io.ditclear.app.model.remote.PaoService
import javax.inject.Inject

/**
* 页面描述:PaoRepo
*
* Created by ditclear on 2018/4/14.
*/
class PaoRepo constructor(private val remote:PaoService,private val local :PaoDao){
class PaoRepo @Inject constructor(private val remote:PaoService, private val local :PaoDao){

fun getArticleDetail(id:Int)= local.getArticleById(id)
.onErrorResumeNext {
Expand Down
34 changes: 13 additions & 21 deletions app/src/main/java/io/ditclear/app/view/PaoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import io.ditclear.app.BuildConfig
import io.ditclear.app.R
import io.ditclear.app.databinding.PaoActivityBinding
import io.ditclear.app.helper.Constants
import io.ditclear.app.model.local.AppDatabase
import io.ditclear.app.model.remote.PaoService
import io.ditclear.app.model.repository.PaoRepo
import io.ditclear.app.di.component.DaggerAppComponent
import io.ditclear.app.di.module.AppModule
import io.ditclear.app.viewmodel.PaoViewModel
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Inject

class PaoActivity : RxAppCompatActivity() {

lateinit var mBinding : PaoActivityBinding
lateinit var mViewMode : PaoViewModel

@Inject
lateinit var mViewModel : PaoViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -33,21 +31,15 @@ class PaoActivity : RxAppCompatActivity() {
mBinding=DataBindingUtil.setContentView(this,R.layout.pao_activity)
setSupportActionBar(mBinding.toolbar)
mBinding.webView.setOnLongClickListener { true }
//////model
val remote=Retrofit.Builder()
.baseUrl(Constants.HOST_API)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(PaoService::class.java)

val local=AppDatabase.getInstance(applicationContext).paoDao()

/////ViewModel
mViewMode= PaoViewModel(PaoRepo(remote, local))
//////di
getComponent().inject(this)
////binding
mBinding.vm=mViewMode
mBinding.vm=mViewModel
}

fun getComponent()=DaggerAppComponent.builder()
.appModule(AppModule(applicationContext)).build()

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menu?.let {
menuInflater.inflate(R.menu.detail_menu,it)
Expand All @@ -58,7 +50,7 @@ class PaoActivity : RxAppCompatActivity() {
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
item?.let {
when(it.itemId){
R.id.action_refresh -> mViewMode.loadArticle().compose(bindToLifecycle())
R.id.action_refresh -> mViewModel.loadArticle().compose(bindToLifecycle())
.subscribe { _, error -> dispatchError(error) }
else -> { }
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/ditclear/app/viewmodel/PaoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import io.ditclear.app.helper.async
import io.ditclear.app.model.data.Article
import io.ditclear.app.model.repository.PaoRepo
import io.reactivex.Single
import javax.inject.Inject

/**
* 页面描述:PaoViewModel
* @param animal 数据源Model(MVVM 中的M),负责提供ViewModel中需要处理的数据
* Created by ditclear on 2017/11/17.
*/
class PaoViewModel(private val repo: PaoRepo) {
class PaoViewModel @Inject constructor(private val repo: PaoRepo) {

//////////////////data//////////////
val loading=ObservableBoolean(false)
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/io/ditclear/app/ExampleUnitTest.java

This file was deleted.

17 changes: 17 additions & 0 deletions app/src/test/java/io/ditclear/app/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.ditclear.app

import org.junit.Assert.assertEquals
import org.junit.Test

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
class ExampleUnitTest {
@Test
@Throws(Exception::class)
fun addition_isCorrect() {
assertEquals(4, (2 + 2).toLong())
}
}
Loading

0 comments on commit bfbbc45

Please sign in to comment.