Skip to content

Commit

Permalink
06-05 加入Dagger2
Browse files Browse the repository at this point in the history
  • Loading branch information
tmwuzhen committed Jun 5, 2017
1 parent d9ba5de commit b9fc989
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@ import android.support.v7.widget.RecyclerView
import android.util.Log
import com.example.komoriwu.kotlintest.R
import com.example.komoriwu.kotlintest.article.presenter.ArticlePresenter
import com.example.komoriwu.kotlintest.article.presenter.ArticlePresenterImpl
import com.example.komoriwu.kotlintest.article.view.ArticleView
import com.example.komoriwu.kotlintest.base.BaseActivity
import com.example.komoriwu.kotlintest.bean.ArticleBody
import com.example.komoriwu.kotlintest.component.DaggerArticleComponent
import com.example.komoriwu.kotlintest.module.ArticleModule
import org.jetbrains.anko.find
import javax.inject.Inject

class MainActivity : BaseActivity(R.layout.activity_main), ArticleView {

@Inject
lateinit var mArticlePresenter: ArticlePresenter
private var mRecyclerView: RecyclerView? = null
private var mArticleAdapter: ArticleAdapter? = null
private var mArticlePresenter: ArticlePresenter? = null

override fun initView() {
mRecyclerView = find(R.id.recycle_view)
}

override fun initData() {
initToolbar(com.example.komoriwu.kotlintest.R.string.app_name)
mArticlePresenter = ArticlePresenterImpl(this, this)
mArticlePresenter?.loaderArticle("1", "8")
mRecyclerView?.layoutManager = LinearLayoutManager(this, LinearLayoutManager.
VERTICAL, false)
mArticleAdapter=ArticleAdapter()
mRecyclerView?.adapter = mArticleAdapter
mArticleAdapter = ArticleAdapter()
mRecyclerView?.adapter = mArticleAdapter

DaggerArticleComponent.builder().articleModule(ArticleModule(this, this))
.build().inject(this)
// mArticlePresenter = ArticlePresenterImpl(this, this)
mArticlePresenter.loaderArticle("1", "8")
}

override fun addArticleList(articleBody: ArticleBody?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.example.http.listener.HttpOnNextListener
import com.example.komoriwu.kotlintest.bean.ArticleBody
import com.example.komoriwu.kotlintest.http.api.ArticleApi
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import dagger.Module


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.example.komoriwu.kotlintest.bean.ArticleBody
* Created by KomoriWu on 2017/6/2.
*/
class ArticlePresenterImpl(private var mContext: Context,
private var mArticleView: ArticleView?) :
private var mArticleView: ArticleView) :
ArticlePresenter, ArticleModelImpl.OnLoadListListener {

private var mArticleModel: ArticleModel? = null
Expand All @@ -24,7 +24,7 @@ class ArticlePresenterImpl(private var mContext: Context,
}

override fun onSuccess(t: ArticleBody?) {
mArticleView?.addArticleList(t)
mArticleView.addArticleList(t)
}

override fun onFail(e: Throwable?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.komoriwu.kotlintest.component

import android.content.Context
import com.example.komoriwu.kotlintest.article.MainActivity
import com.example.komoriwu.kotlintest.article.view.ArticleView
import com.example.komoriwu.kotlintest.module.ArticleModule
import dagger.Component

/**
* Created by KomoriWu
* on 2017-06-05.
*/

@Component(modules = arrayOf(ArticleModule::class))
interface ArticleComponent {
fun inject(mainActivity: MainActivity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.komoriwu.kotlintest.module

import android.content.Context
import com.example.komoriwu.kotlintest.article.presenter.ArticlePresenter
import com.example.komoriwu.kotlintest.article.presenter.ArticlePresenterImpl
import com.example.komoriwu.kotlintest.article.view.ArticleView
import dagger.Module
import dagger.Provides

/**
* Created by KomoriWu
* on 2017-06-05.
*/
@Module
class ArticleModule(private var mContext: Context,
private var mArticleView: ArticleView) {
@Provides
fun providePresenter(): ArticlePresenter {
return ArticlePresenterImpl(mContext, mArticleView)
}
}

0 comments on commit b9fc989

Please sign in to comment.