我正在学习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,但我明白这是错误的,即使我希望实例应该只有一个在应用程序中,我也不能创建另一个实例。
问题:我哪里有错?在我的代码中,在架构中,在理解依赖注入方面还是在所有三方面?感谢您的帮助!