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
#!/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 |
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
@Component( | |
dependencies = [ | |
FeatureComponent::class | |
] | |
) | |
@Singleton | |
interface AppComponent { | |
@Component.Factory | |
interface Factory { |
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 | |
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 |
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
@Module | |
abstract class BuilderModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey(SomeViewModel::class) | |
abstract fun bindVMFactory(f: SomeViewModel.Factory): AssistedSavedStateViewModelFactory<out ViewModel> | |
} |
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 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 | |
} | |
} |
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
@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) { |
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() |
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 | |
) : ViewModel() |
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 application: Application, | |
@Assisted 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
@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 |
NewerOlder