Dagger2 + MVP上Kotlin

我正在学习Dagger2 + MVP并在Kotlin上做。 而且我在理解Dagger2或者MVP或者那里有一个问题。

构建一个应用程序和想法应该如何工作是非常简单的。 该应用程序由左侧导航MenuActivity和几个Fragments (比方说3),应该在activity_menu.xmlFrameLayout中进行更改。

我已经读了几篇文章,花了几天时间学习Dagger2。 本文我作为教程来构建我的示例: https : //proandroiddev.com/dagger-2-part-ii-custom-scopes-component-dependencies-subcomponents-697c1fa1cfc

在我的想法中,Dagger架构应该由三个@Component组成:(1)AppComponent,(2)MenuActivityComponent和(3)AccountFragmentComponent。 从我的理解和文章中的建筑图片我的架构可以是这样的:(3)取决于 – >(2)取决于 – >(1)

每个@Component具有@Module :(1)AppModule,(2)MenuActivityModule和(3)AccountFragmentModule。 (2)MenuActivityModule和(3)AccountFragmentModule应该@Provide Presenter从MVP意识形态的Presenter@InjectMenuActivity和其他Fragment ,如AccountFragment

的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

 @ActivityScope @Subcomponent(modules = arrayOf(MenuActivityModule::class)) interface MenuActivityComponent { fun inject(activity: MenuActivity) fun plus(accountsModule : AccountsFragmentModule) : AccountsFragmentComponent } 

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,据我所知,这将保证每个组件在应用程序中需要的时间只有一个组件的存在。

ActivityScope

 @Scope annotation class ActivityScope 

FragmentScope

 @Scope annotation class FragmentScope 

在Application类中,我创建了@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 } } 

我在这个最后部分的误解部分价值。 我来到的情况下,我需要plus MenuActivityComponent的子组件,并作为一个构造函数的变量MenuActivity,但我明白这是错误的,即使我希望实例应该只有一个在应用程序中,我不能创建另一个实例。

问题 :我哪里有错误? 在我的代码,架构,理解依赖注入或三个? 感谢您的帮助!

我会分开你的想法和个人的工作。 一旦你在这两个概念上变得流利/高超,你可以尝试把它们结合起来。

例如,尝试构建一个简单的多活动/片段应用程序MVP设计模式。 使用MVP,您将在Presenter(包含视图逻辑并控制视图的对象,以及处理视图收集和转发的行为)之间编写双向接口契约,而视图(视图对象,通常是像Fragment或Activity这样的本地组件,负责显示视图并处理用户输入,如触摸事件)。

使用Dagger2,您将学习依赖注入设计模式/架构风格。 您将构建组合成组件的模块,然后使用这些组件来注入对象。

把两者结合起来就可以自己理解每个概念。

查看Google Architectural Blueprint存储库以获取MVP和Dagger2的示例。 https://github.com/googlesamples/android-architecture

另外我有两个@Scopes:ActivityScope和FragmentScope,据我所知,这将保证只有一个组件的存在,每个组件需要在应用程序

匕首的范围不是一些神奇的仙尘,将为你管理生命。 范围的使用仅仅是验证帮助,它可以帮助你不依赖于不同的生命周期。 您仍然必须自己管理组件和模块对象,将正确的参数传递给它们的构造函数。 由不同组件实例管理的依赖关系图是独立的,并绑定到它们的@Component对象。 请注意,我在说某种意义上的“绑定”,它们是由它们(通过构造函数)创建的,并且可选地存储在里面,绝对没有其他魔法在幕后工作。 因此,如果您需要在应用程序的各个部分之间共享一堆依赖项,则可能需要传递一些组件和模块实例。

在碎片的情况下,对于复杂的生命周期(活动)有一个严格的依赖关系。 在onAttach期间你会收到这个依赖,并在onDetach期间丢失它。 所以,如果你真的想把一些依赖于Activity的东西传给Fragments,就必须传递这些依赖于Activity的组件/模块,就像在MVP不存在的情况下传递Activity实例一样。

同样,如果您的Activity通过Intent接收到一些依赖项,您将不得不从它反序列化该依赖项,并根据Intent内容创建一个Module …

活动和片段生命周期的复杂性加重了使用匕首注射时常遇到的问题。 Android框架是围绕假设建立的,活动和片段以序列化的形式接收所有的依赖关系。 最后,一个写得很好的应用程序不应该在模块之间有太多的依赖关系(查找“紧耦合”)。 由于这些原因,如果您不遵循严格的单一活动范例,在您的项目中使用Dagger在本地化的活动/ Fagment范围上可能并不值得。 许多人仍然这样做,即使这是不值得的,但国际海事组织,这只是个人喜好的问题。