-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Hilt AssistedInject (#271)
* Add support for Hilt AssistedInject * Review changes Co-authored-by: Natan Vieira <[email protected]> * Fix lint erors * Annotate function with`@ExperimentalVoyagerApi` --------- Co-authored-by: Natan Vieira <[email protected]>
- Loading branch information
Showing
4 changed files
with
63 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
.../android/src/main/java/cafe/adriel/voyager/sample/hiltIntegration/HiltDetailsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package cafe.adriel.voyager.sample.hiltIntegration | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedFactory | ||
import dagger.assisted.AssistedInject | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
|
||
// Until now, there is no support to assisted injection. | ||
// Follow the thread here: https://github.com/google/dagger/issues/2287 | ||
class HiltDetailsViewModel( | ||
val index: Int | ||
@HiltViewModel(assistedFactory = HiltDetailsViewModel.Factory::class) | ||
class HiltDetailsViewModel @AssistedInject constructor( | ||
@Assisted val index: Int | ||
) : ViewModel() { | ||
companion object { | ||
fun provideFactory( | ||
index: Int | ||
): ViewModelProvider.Factory = object : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T = HiltDetailsViewModel(index) as T | ||
} | ||
|
||
@AssistedFactory | ||
interface Factory { | ||
fun create(index: Int): HiltDetailsViewModel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters