我正在學習Dagger2 + MVP並在Kotlin上做。而且我在理解Dagger2或MVP或那裏的組合方面存在問題。Kotlin上的Dagger2 + MVP
構建一個應用程序和想法應該如何工作非常簡單。該應用程序由MenuActivity
與左側導航和幾個Fragments
(比方說3)應改變在FrameLayout
在activity_menu.xml
。
我已經閱讀了幾篇文章,花了幾天的時間學習Dagger2。 (1)AppComponent,(2)MenuActivityComponent和(3)AccountFragmentComponent。在我的想法中,Dagger體系結構應該由三個@Component
s組成:(1)AppComponent,(2)MenuActivityComponent和(3)AccountFragmentComponent。 而從我的理解和架構的文章中的圖片我的體系結構可以是這樣的:(3)取決於 - >(2)取決於 - >(1)
每個@Component
具有@Module
:(1 )AppModule,(2)MenuActivityModule和(3)AccountFragmentModule。就我所知,從MVP的意識形態角度來看,(2)MenuActivityModule和(3)AccountFragmentModule都應該是@Provide
Presenter
,而在MenuActivity
和其他Fragment
s,比如AccountFragment
中應該是@Inject
。
的AppModule
@Module
class AppModule(val app : App){
@Provides @Singleton
fun provideApp() = app
}
AppComponent
@Singleton @Component(modules = arrayOf(AppModule::class))
interface AppComponent{
fun inject(app : App)
fun plus(menuActivityModule: MenuActivityModule): MenuActivityComponent
}
MenuActivityModule
@Module
class MenuActivityModule(val activity : MenuActivity) {
@Provides
@ActivityScope
fun provideMenuActivityPresenter() =
MenuActivityPresenter(activity)
@Provides
fun provideActivity() = activity
}
MenuActivityComponent
AccountsFragmentModule
@Module
class AccountsFragmentModule(val fragment: AccountsFragment){
@FragmentScope
@Provides
fun provideAccountsFragmentPresenter() =
AccountsFragmentPresenter(fragment)
}
AccountsFragmentComponent
@FragmentScope
@Subcomponent(modules = arrayOf(AccountsFragmentModule::class))
interface AccountsFragmentComponent {
fun inject(fragment: AccountsFragment)
}
而且我有兩個@Scope
S:ActivityScope和FragmentScope,使我明白,這將保證在應用程序中需要每個組件的時間僅存在一個Component。
ActivityScope
@Scope
annotation class ActivityScope
FragmentScope
@Scope
annotation class FragmentScope
在應用I類創建的@Singleton
依賴性的曲線圖。
class App : Application(){
val component : AppComponent by lazy {
DaggerAppComponent
.builder()
.appModule(AppModule(this))
.build()
}
companion object {
lateinit var instance : App
private set
}
override fun onCreate() {
super.onCreate()
component.inject(this)
}
}
在MenuActivity:
class MenuActivity: AppCompatActivity()
@Inject lateinit var presenter : MenuActivityPresenter
val Activity.app : App
get() = application as App
val component by lazy {
app.component.plus(MenuActivityModule(this))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu)
/* setup dependency injection */
component.inject(this)
/* setup UI */
setupMenu()
presenter.init()
}
private fun setupMenu(){
navigationView.setNavigationItemSelectedListener({
menuItem: MenuItem -> selectDrawerItem(menuItem)
true
})
/* Hamburger icon for left-side menu */
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(drawerToggle as ActionBarDrawerToggle)
}
private fun selectDrawerItem(menuItem: MenuItem){
presenter.menuItemSelected(menuItem)
// Highlight the selected item has been done by NavigationView
menuItem.isChecked = true
// Set action bar title
title = menuItem.title
// Close the navigation drawer
drawerLayout.closeDrawers()
}
@SuppressLint("CommitTransaction")
override fun showFragment(fragment: Fragment, isReplace: Boolean,
backStackTag: String?, isEnabled: Boolean)
{
/* Defining fragment transaction */
with(supportFragmentManager.beginTransaction()){
/* Select if to replace or add a fragment */
if(isReplace)
replace(R.id.frameLayoutContent, fragment, backStackTag)
else
add(R.id.frameLayoutContent, fragment)
backStackTag?.let { this.addToBackStack(it) }
commit()
}
enableDrawer(isEnabled)
}
private fun enableDrawer(isEnabled: Boolean) {
drawerLayout.setDrawerLockMode(if(isEnabled) DrawerLayout.LOCK_MODE_UNLOCKED
else DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
drawerToggle?.onDrawerStateChanged(if(isEnabled) DrawerLayout.LOCK_MODE_UNLOCKED
else DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
drawerToggle?.isDrawerIndicatorEnabled = isEnabled
drawerToggle?.syncState()
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (drawerToggle!!.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
drawerToggle?.syncState()
}
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
drawerToggle?.onConfigurationChanged(newConfig)
}
}
MainActivityPresenter
class MenuActivityPresenter(val menuActivity: MenuActivity){
fun init(){
menuActivity.showFragment(AccountsFragment.newInstance(), isReplace = false)
}
fun menuItemSelected(menuItem: MenuItem){
val fragment = when(menuItem.itemId){
R.id.nav_accounts_fragment -> {
AccountsFragment.newInstance()
}
R.id.nav_income_fragment -> {
IncomeFragment.newInstance()
}
R.id.nav_settings -> {
IncomeFragment.newInstance()
}
R.id.nav_feedback -> {
OutcomeFragment.newInstance()
}
else -> {
IncomeFragment.newInstance()
}
}
menuActivity.showFragment(fragment)
}
}
activity_menu.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/frameLayoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/main_menu"
app:headerLayout="@layout/nav_header"
/>
</android.support.v4.widget.DrawerLayout>
而且在那裏我有在我的理解一個突破點的地方:
class AccountsFragment : Fragment() {
companion object {
fun newInstance() = AccountsFragment()
}
val Activity.app : App
get() = application as App
val component by lazy {
app.component
.plus(MenuActivityModule(activity as MenuActivity))
.plus(AccountsFragmentModule(this))
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.fragment_accounts, container, false)
setHasOptionsMenu(true)
component.inject(this)
return view
}
}
我在component
值這最後一部分的誤解。我來到這種情況,我需要plus
MenuActivityComponent的子組件,並給出一個構造函數變量MenuActivity,但我明白這是錯誤的,即使我希望實例應該只有一個在應用程序中,我也不能創建另一個實例。
問題:我哪裏有錯?在我的代碼中,在架構中,在理解依賴注入方面還是在所有三方面?感謝您的幫助!