Skip to content

Instantly share code, notes, and snippets.

@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_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_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_InjectingSavedStateViewModelFactory.kt
Last active May 4, 2020 14:06
Connecting The Dots :: InjectingViewModelFactory.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 / 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_BuilderModule.kt
Last active February 7, 2021 20:43
Connecting the dots : BuilderModule
@Module
abstract class BuilderModule {
@Binds
@IntoMap
@ViewModelKey(SomeViewModel::class)
abstract fun bindVMFactory(f: SomeViewModel.Factory): AssistedSavedStateViewModelFactory<out ViewModel>
}
@mlykotom
mlykotom / Connecting_SomeFragment.kt
Last active February 7, 2021 20:47
Connecting the dots :: SomeFragment.kt
class SomeFragment() : Fragment() {
// ...
@Inject
lateinit var abstractFactory: dagger.Lazy<InjectingSavedStateViewModelFactory>
/**
* This method androidx uses for `by viewModels` method.
* We can set out injecting factory here and therefore don't touch it again later
@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 / appkill.sh
Last active July 5, 2021 18:26
Script for killing Android app (as system would)
#!/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