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
@Component( | |
dependencies = [ | |
FeatureComponent::class | |
] | |
) | |
@Singleton | |
interface AppComponent { | |
@Component.Factory | |
interface Factory { |
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
class SomeViewModel @Inject constructor( | |
private val depFromDagger1: Dependency, | |
private val depFromDagger2: Dependency2 | |
) : ViewModel() |
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
class SomeViewModel( | |
private val dep1: Dependency, | |
private val dep2: Dependency2 | |
) : ViewModel() |
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
class SomeViewModel @AssistedInject constructor( | |
private val application: Application, | |
@Assisted private val savedStateHandle: SavedStateHandle | |
){ | |
// must be inside of the ViewModel class! | |
@AssistedFactory | |
interface Factory : AssistedSavedStateViewModelFactory<SomeViewModel> { | |
override fun create(savedStateHandle: SavedStateHandle): SomeViewModel // may be ommited prior kotlin 1.3.60 or after PR #121 in AssistedInject lib | |
} | |
} |
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
class SomeViewModel @AssistedInject constructor( | |
private val application: Application, | |
@Assisted private val savedStateHandle: SavedStateHandle | |
){ | |
// ... | |
} |
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
/** | |
* Base interface for all ViewModel factories | |
*/ | |
interface AssistedSavedStateViewModelFactory<T : ViewModel> { | |
fun create(savedStateHandle: SavedStateHandle): T | |
} |
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
class SomeViewModel(/* ..ommited.. */){ | |
val counter = savedStateHandle.getLiveData("counter", 0) | |
fun onPlusClick() { | |
counter.value = (counter.value ?: 0) + 1 | |
} | |
} |
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
class SomeViewModel(/* ..ommited.. */){ | |
val counter = MutableLiveData<Int>(0) | |
init { | |
counter.value = savedStateHandle.get("counter") ?: 0 | |
counter.observeForever { newValue -> | |
savedStateHandle.set("counter", newValue) | |
} | |
} |
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
class SomeViewModel( | |
private val application: Application, | |
private val savedStateHandle: SavedStateHandle | |
){ | |
// ... | |
} |
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
class SomeFragment : Fragment() { | |
lateinit var viewModel : SomeViewModel | |
override fun onCreate(savedState: Bundle?){ | |
super.onCreate(savedState) | |
// default arguments, so you can set something dynamically | |
val defaultArgs: Bundle? = bundleOf("id" to 5) // may be null | |
// default factory for ViewModel creation |
NewerOlder