Skip to content

Instantly share code, notes, and snippets.

Tomáš Mlynarič mlykotom

Block or report user

Report or block mlykotom

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mlykotom
mlykotom / Connecting_Motivation_2.kt
Created Jan 3, 2020
Connecting The Dots :: Motivation - dependencies with Dagger
View Connecting_Motivation_2.kt
class SomeViewModel @Inject constructor(
private val depFromDagger1: Dependency,
private val depFromDagger2: Dependency2
) : ViewModel()
@mlykotom
mlykotom / Connecting_Motivation_1.kt
Created Jan 3, 2020
Connecting The Dots :: Motivation - manually added dependencies
View Connecting_Motivation_1.kt
class SomeViewModel(
private val dep1: Dependency,
private val dep2: Dependency2
) : ViewModel()
@mlykotom
mlykotom / Connecting_Assisted_ViewModel_Body.kt
Last active Jan 3, 2020
Connecting The Dots :: Assisted Saved State - ViewModel body
View Connecting_Assisted_ViewModel_Body.kt
class SomeViewModel @AssistedInject constructor(
private val application: Application,
@Assisted private val savedStateHandle: SavedStateHandle
){
// must be inside of the ViewModel class!
@AssistedInject.Factory
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 Jan 3, 2020
Connecting The Dots :: Assisted Saved State - ViewModel constructor
View Connecting_Assisted_ViewModel_Constructor.kt
class SomeViewModel @AssistedInject constructor(
private val application: Application,
@Assisted private val savedStateHandle: SavedStateHandle
){
// ...
}
@mlykotom
mlykotom / Connecting_AssistedSavedStateViewModelFactory.kt
Last active Jan 3, 2020
Connecting The Dots :: Assisted Saved State - Abstract Factory
View Connecting_AssistedSavedStateViewModelFactory.kt
/**
* 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 Jan 3, 2020
Connecting The Dots :: Pure SavedState - ViewModel body simple
View Connecting_SavedState_ViewModel_Body_Simple.kt
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 Jan 3, 2020
Connecting The Dots :: Pure SavedState - ViewModel body
View Connecting_SavedState_ViewModel_Body.kt
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 Jan 3, 2020
Connecting The Dots :: Pure SavedState - ViewModel constructor
View Connecting_SavedState_ViewModel_Constructor.kt
class SomeViewModel(
private val application: Application,
private val savedStateHandle: SavedStateHandle
){
// ...
}
@mlykotom
mlykotom / Connecting_SavedState_Fragment.kt
Last active Jan 3, 2020
Connecting The Dots :: Pure SavedState - Fragment
View Connecting_SavedState_Fragment.kt
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
@mlykotom
mlykotom / Connecting_Motivation_AfterInitViewModel.kt
Last active Jan 3, 2020
Connecting The Dots :: Motivation - parameters after initialization of ViewModel
View Connecting_Motivation_AfterInitViewModel.kt
class SomeViewModel @Inject constructor(
private val depFromDagger1: Dependency,
private val depFromDagger2: Dependency2
) : ViewModel(){
// may crash with UninitializedPropertyAccessException
lateinit var articleId: String
// must !! or ?. for every access
var fragmentParams: Bundle? = null
You can’t perform that action at this time.