Skip to content

Commit

Permalink
Hilt dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahendran Vadivalagan committed Oct 1, 2021
1 parent 21806b8 commit 2ea5e38
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/ex2/hiltespresso/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.ex2.hiltespresso

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/ex2/hiltespresso/data/DataRepoImpl.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.ex2.hiltespresso.data

import com.ex2.hiltespresso.data.model.Profile
import javax.inject.Inject

class DataRepoImpl : DataRepository {
class DataRepoImpl @Inject constructor() : DataRepository {

override fun getProfile(): Profile =
Profile(name = "Bruce Wayne", age = 42)
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/ex2/hiltespresso/di/ProfileModule.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
package com.ex2.hiltespresso.di

class ProfileModule {
import com.ex2.hiltespresso.data.DataRepoImpl
import com.ex2.hiltespresso.data.DataRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent

@InstallIn(ViewModelComponent::class)
@Module
abstract class ProfileModule {

@Binds
abstract fun getProfileSource(repo: DataRepoImpl): DataRepository
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.ex2.hiltespresso.data.DataRepoImpl
import com.ex2.hiltespresso.R
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ProfileFragment : Fragment() {

val viewModel by viewModels<ProfileViewModel> {
ProfileViewModel.Factory(repo = DataRepoImpl())
}
private val viewModel by viewModels<ProfileViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.ex2.hiltespresso.ui.profile

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.ex2.hiltespresso.data.DataRepository
import com.ex2.hiltespresso.data.model.Profile
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

class ProfileViewModel(private val repo: DataRepository) : ViewModel() {
@HiltViewModel
class ProfileViewModel @Inject constructor(
private val repo: DataRepository
) : ViewModel() {

fun getProfile(): Profile = repo.getProfile()

class Factory(private val repo: DataRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return ProfileViewModel(repo) as T
}
}
}

0 comments on commit 2ea5e38

Please sign in to comment.