Skip to content

Commit bd013bd

Browse files
edit dependency configurations for modules
1 parent bda8629 commit bd013bd

File tree

8 files changed

+156
-36
lines changed

8 files changed

+156
-36
lines changed

app/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ dependencies {
107107
implementation(project(Modules.AndroidLibrary.CORE))
108108

109109
implementation(project(Modules.AndroidLibrary.DOMAIN))
110-
// TODO Solve Why doesn't work when DATA module is not added to dagger Hilt?
111110
implementation(project(Modules.AndroidLibrary.DATA))
112111

113112
addAppModuleDependencies()
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
package com.smarttoolfactory.post_detail
22

33
import android.os.Bundle
4-
import android.view.LayoutInflater
5-
import android.view.View
6-
import android.view.ViewGroup
7-
import androidx.databinding.DataBindingUtil
8-
import androidx.fragment.app.Fragment
4+
import com.smarttoolfactory.core.ui.base.DynamicNavigationFragment
5+
import com.smarttoolfactory.domain.di.DomainModuleDependencies
96
import com.smarttoolfactory.domain.model.Post
107
import com.smarttoolfactory.post_detail.databinding.FragmentPostDetailBinding
8+
import com.smarttoolfactory.post_detail.di.DaggerPostDetailComponent
9+
import dagger.hilt.android.EntryPointAccessors
1110

12-
class PostDetailFragment : Fragment() {
11+
class PostDetailFragment : DynamicNavigationFragment<FragmentPostDetailBinding>() {
1312

14-
private lateinit var dataBinding: FragmentPostDetailBinding
13+
// private val viewModel: PostDetailViewModel by viewModels()
1514

16-
override fun onCreateView(
17-
inflater: LayoutInflater,
18-
container: ViewGroup?,
19-
savedInstanceState: Bundle?
20-
): View? {
21-
dataBinding =
22-
DataBindingUtil.inflate(inflater, R.layout.fragment_post_detail, container, false)
23-
return dataBinding.root
15+
override fun getLayoutRes(): Int = R.layout.fragment_post_detail
16+
17+
override fun bindViews() {
18+
// Get Post from navigation component arguments
19+
val post = arguments?.get("post") as Post
20+
dataBinding.item = post
21+
// viewModel.updatePostStatus(post)
22+
}
23+
24+
override fun onCreate(savedInstanceState: Bundle?) {
25+
initCoreDependentInjection()
26+
super.onCreate(savedInstanceState)
2427
}
2528

26-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
27-
super.onViewCreated(view, savedInstanceState)
28-
// Get RepoListItem from navigation component arguments
29-
val post = arguments?.get("post") as? Post?
29+
private fun initCoreDependentInjection() {
3030

31-
dataBinding.item = post
31+
val coreModuleDependencies = EntryPointAccessors.fromApplication(
32+
requireActivity().applicationContext,
33+
DomainModuleDependencies::class.java
34+
)
35+
36+
DaggerPostDetailComponent.factory().create(
37+
coreModuleDependencies,
38+
requireActivity().application
39+
)
40+
.inject(this)
3241
}
3342
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.smarttoolfactory.post_detail
2+
3+
import androidx.hilt.lifecycle.ViewModelInject
4+
import androidx.lifecycle.ViewModel
5+
import com.smarttoolfactory.domain.model.Post
6+
import com.smarttoolfactory.domain.usecase.GetPostsWithStatusUseCaseFlow
7+
import kotlinx.coroutines.CoroutineScope
8+
import kotlinx.coroutines.flow.catch
9+
import kotlinx.coroutines.flow.launchIn
10+
import kotlinx.coroutines.flow.onCompletion
11+
import kotlinx.coroutines.flow.onStart
12+
13+
class PostDetailViewModel @ViewModelInject constructor(
14+
private val coroutineScope: CoroutineScope,
15+
private val getPostsUseCase: GetPostsWithStatusUseCaseFlow
16+
) : ViewModel() {
17+
18+
fun updatePostStatus(post: Post) {
19+
20+
post.displayCount++
21+
22+
getPostsUseCase.updatePostStatus(post)
23+
.onStart { println("⏰ PostStatusViewModel updatePostStatus() catch() onStart") }
24+
.catch { throwable ->
25+
println("❌ PostStatusViewModel updatePostStatus() catch(): ${throwable.message}")
26+
}
27+
.onCompletion { cause: Throwable? ->
28+
println(
29+
"💀 PostStatusViewModel updatePostStatus() onCompletion()" +
30+
" error: ${cause != null}"
31+
)
32+
}
33+
.launchIn(coroutineScope)
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.smarttoolfactory.post_detail.di
2+
3+
import android.app.Application
4+
import com.smarttoolfactory.domain.di.DomainModuleDependencies
5+
import com.smarttoolfactory.post_detail.PostDetailFragment
6+
import dagger.BindsInstance
7+
import dagger.Component
8+
9+
@Component(
10+
dependencies = [DomainModuleDependencies::class],
11+
modules = [PostDetailModule::class]
12+
)
13+
interface PostDetailComponent {
14+
15+
fun inject(postDetailFragment: PostDetailFragment)
16+
17+
@Component.Factory
18+
interface Factory {
19+
fun create(
20+
dataModuleDependencies: DomainModuleDependencies,
21+
@BindsInstance application: Application
22+
): PostDetailComponent
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.smarttoolfactory.post_detail.di
2+
3+
import android.app.Application
4+
import android.content.Context
5+
import dagger.Binds
6+
import dagger.Module
7+
import dagger.hilt.InstallIn
8+
import dagger.hilt.android.components.FragmentComponent
9+
10+
@InstallIn(FragmentComponent::class)
11+
@Module(includes = [PostDetailBindModule::class])
12+
class PostDetailModule
13+
14+
@InstallIn(FragmentComponent::class)
15+
@Module
16+
abstract class PostDetailBindModule {
17+
@Binds
18+
abstract fun bindContext(application: Application): Context
19+
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// package com.smarttoolfactory.core.di
2-
//
3-
// import com.smarttoolfactory.core.CoreDependency
4-
// import dagger.Module
5-
// import dagger.Provides
6-
// import dagger.hilt.InstallIn
7-
// import dagger.hilt.android.components.ApplicationComponent
8-
// import javax.inject.Singleton
9-
//
10-
// @InstallIn(ApplicationComponent::class)
11-
// @Module
12-
// class CoreModule {
13-
//
14-
//
15-
// }
1+
package com.smarttoolfactory.core.di
2+
3+
import com.smarttoolfactory.core.CoreDependency
4+
import dagger.Module
5+
import dagger.Provides
6+
import dagger.hilt.InstallIn
7+
import dagger.hilt.android.components.ApplicationComponent
8+
9+
@InstallIn(ApplicationComponent::class)
10+
@Module
11+
class CoreModule {
12+
13+
@Provides
14+
fun provideCoreDependency() = CoreDependency()
15+
}

libraries/core/src/main/java/com/smarttoolfactory/core/di/CoreModuleDependencies.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//
12
// package com.smarttoolfactory.core.di
23
//
34
// import com.smarttoolfactory.core.CoreDependency
5+
// import com.smarttoolfactory.data.repository.PostStatusRepository
6+
// import com.smarttoolfactory.domain.usecase.GetPostsWithStatusUseCaseFlow
47
// import dagger.hilt.EntryPoint
58
// import dagger.hilt.InstallIn
69
// import dagger.hilt.android.components.ApplicationComponent
@@ -15,5 +18,12 @@
1518
// /*
1619
// Provision methods to provide dependencies to components that depend on this component
1720
// */
21+
// fun coreDependency(): CoreDependency
22+
//
23+
// /*
24+
// Provision methods to provide dependencies to components that depend on this component
25+
// */
26+
// fun postStatusRepository(): PostStatusRepository
1827
//
28+
// fun getPostsWithStatusUseCaseFlow(): GetPostsWithStatusUseCaseFlow
1929
// }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.smarttoolfactory.domain.di
2+
3+
import com.smarttoolfactory.data.repository.PostStatusRepository
4+
import com.smarttoolfactory.domain.usecase.GetPostListUseCaseFlow
5+
import com.smarttoolfactory.domain.usecase.GetPostListUseCaseRxJava3
6+
import com.smarttoolfactory.domain.usecase.GetPostsWithStatusUseCaseFlow
7+
import dagger.hilt.EntryPoint
8+
import dagger.hilt.InstallIn
9+
import dagger.hilt.android.components.ApplicationComponent
10+
11+
@EntryPoint
12+
@InstallIn(ApplicationComponent::class)
13+
interface DomainModuleDependencies {
14+
15+
/*
16+
Provision methods to provide dependencies to components that depend on this component
17+
*/
18+
19+
fun postStatusRepository(): PostStatusRepository
20+
21+
fun getPostListUseCaseFlow(): GetPostListUseCaseFlow
22+
fun getPostListUseCaseRxJava3(): GetPostListUseCaseRxJava3
23+
fun getPostsWithStatusUseCaseFlow(): GetPostsWithStatusUseCaseFlow
24+
}

0 commit comments

Comments
 (0)