Skip to content

Instantly share code, notes, and snippets.

@mlykotom
mlykotom / AppComponent_Encapsulated.kt
Last active March 22, 2021 20:56
Lock your Dagger In Gradle Modules
@Component(
dependencies = [
FeatureComponent::class
]
)
@Singleton
interface AppComponent {
@Component.Factory
interface Factory {
@mlykotom
mlykotom / Connecting_Motivation_2.kt
Created January 3, 2020 10:10
Connecting The Dots :: Motivation - dependencies with Dagger
class SomeViewModel @Inject constructor(
private val depFromDagger1: Dependency,
private val depFromDagger2: Dependency2
) : ViewModel()
@mlykotom
mlykotom / Connecting_Motivation_1.kt
Created January 3, 2020 10:09
Connecting The Dots :: Motivation - manually added dependencies
class SomeViewModel(
private val dep1: Dependency,
private val dep2: Dependency2
) : ViewModel()
@mlykotom
mlykotom / Connecting_Assisted_ViewModel_Body.kt
Last active February 7, 2021 20:36
Connecting The Dots :: Assisted Saved State - ViewModel body
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
}
}
@mlykotom
mlykotom / Connecting_Assisted_ViewModel_Constructor.kt
Created January 3, 2020 09:49
Connecting The Dots :: Assisted Saved State - ViewModel constructor
class SomeViewModel @AssistedInject constructor(
private val application: Application,
@Assisted private val savedStateHandle: SavedStateHandle
){
// ...
}
@mlykotom
mlykotom / Connecting_AssistedSavedStateViewModelFactory.kt
Last active January 3, 2020 09:44
Connecting The Dots :: Assisted Saved State - Abstract Factory
/**
* Base interface for all ViewModel factories
*/
interface AssistedSavedStateViewModelFactory<T : ViewModel> {
fun create(savedStateHandle: SavedStateHandle): T
}
@mlykotom
mlykotom / Connecting_SavedState_ViewModel_Body_Simple.kt
Last active January 3, 2020 09:45
Connecting The Dots :: Pure SavedState - ViewModel body simple
class SomeViewModel(/* ..ommited.. */){
val counter = savedStateHandle.getLiveData("counter", 0)
fun onPlusClick() {
counter.value = (counter.value ?: 0) + 1
}
}
@mlykotom
mlykotom / Connecting_SavedState_ViewModel_Body.kt
Last active January 3, 2020 09:45
Connecting The Dots :: Pure SavedState - ViewModel body
class SomeViewModel(/* ..ommited.. */){
val counter = MutableLiveData<Int>(0)
init {
counter.value = savedStateHandle.get("counter") ?: 0
counter.observeForever { newValue ->
savedStateHandle.set("counter", newValue)
}
}
@mlykotom
mlykotom / Connecting_SavedState_ViewModel_Constructor.kt
Last active January 3, 2020 09:45
Connecting The Dots :: Pure SavedState - ViewModel constructor
class SomeViewModel(
private val application: Application,
private val savedStateHandle: SavedStateHandle
){
// ...
}
@mlykotom
mlykotom / Connecting_SavedState_Fragment.kt
Last active January 3, 2020 09:46
Connecting The Dots :: Pure SavedState - Fragment
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