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_InjectingSavedStateViewModelFactory.kt
Last active May 4, 2020
Connecting The Dots :: InjectingViewModelFactory.kt
View Connecting_InjectingSavedStateViewModelFactory.kt
@Reusable
class InjectingSavedStateViewModelFactory @Inject constructor(
private val assistedFactories: Map<Class<out ViewModel>, @JvmSuppressWildcards AssistedSavedStateViewModelFactory<out ViewModel>>,
private val viewModelProviders: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) {
/**
* Creates instance of ViewModel either annotated with @AssistedInject or @Inject and passes dependencies it needs.
*/
fun create(owner: SavedStateRegistryOwner, defaultArgs: Bundle? = null) =
object : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
@mlykotom
mlykotom / appkill.sh
Last active Apr 24, 2020
Script for killing Android app (as system would)
View appkill.sh
#!/bin/bash
# Provide package of your application (com.example.myapp)
PACKAGE=$1
# First, put your app to background and then run this script
echo "Killing $PACKAGE"
adb shell ps | grep $PACKAGE | awk '{print $2}' | xargs adb shell run-as $PACKAGE kill
@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_InjectingSavedStateViewModelFactory.kt
Last active Jan 3, 2020
Connecting the dots :: InjectingSavedStateViewModelFactory.kt
View Connecting_InjectingSavedStateViewModelFactory.kt
@Reusable
class InjectingSavedStateViewModelFactory @Inject constructor(
private val assistedFactories: Map<Class<out ViewModel>, @JvmSuppressWildcards AssistedSavedStateViewModelFactory<out ViewModel>>
) {
fun create(owner: SavedStateRegistryOwner, defaultArgs: Bundle? = null): AbstractSavedStateViewModelFactory {
return object : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
// Attempt to get ViewModel from assisted inject factories
@mlykotom
mlykotom / Connecting_BuilderModule.kt
Last active Jan 3, 2020
Connecting the dots : BuilderModule
View Connecting_BuilderModule.kt
@AssistedModule
@Module(includes=[AssistedInject_BuilderModule::class])
abstract class BuilderModule {
@Binds
@IntoMap
@ViewModelKey(SomeViewModel::class)
abstract fun bindVMFactory(f: SomeViewModel.Factory): AssistedSavedStateViewModelFactory<out ViewModel>
}
@mlykotom
mlykotom / Connecting_SomeViewModel.kt
Last active Jan 3, 2020
Connecting the dots :: SomeViewModel.kt
View Connecting_SomeViewModel.kt
class SomeViewModel @AssistedInject constructor(
private val appContext: Context,
private val someDaggerDependency: SomeDaggerDependency,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
@AssistedInject.Factory
interface Factory : AssistedSavedStateViewModelFactory<SomeViewModel>
}
@mlykotom
mlykotom / Connecting_SomeFragment.kt
Last active Jan 3, 2020
Connecting the dots :: SomeFragment.kt
View Connecting_SomeFragment.kt
class SomeFragment() : Fragment() {
// ...
@Inject
lateinit var abstractFactory: InjectingSavedStateViewModelFactory
lateinit var viewModel: SomeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
You can’t perform that action at this time.