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 appContext: Context, | |
private val someDaggerDependency: SomeDaggerDependency, | |
@Assisted private val savedStateHandle: SavedStateHandle | |
) : ViewModel() { | |
@AssistedInject.Factory | |
interface Factory : AssistedSavedStateViewModelFactory<SomeViewModel> | |
} |
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() { | |
// inject into this class omitted for brevity | |
@Inject | |
lateinit var dep1: Dependency | |
@Inject | |
lateinit var dep2: Dependency | |
lateinit var viewModel : SomeViewModel |
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(){ | |
// may crash with UninitializedPropertyAccessException | |
lateinit var articleId: String | |
// must !! or ?. for every access | |
var fragmentParams: Bundle? = null |
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 |
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 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(/* ..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
/** | |
* 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( | |
private val dep1: Dependency, | |
private val dep2: Dependency2, | |
private val articleId: String | |
) : ViewModel() { | |
class Factory constructor( | |
private val dep1: Dependency, | |
private val dep2: Dependency2, | |
private val articleId: String |
NewerOlder