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() { | |
// 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( | |
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 |
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 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
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
@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
@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 |
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 |
NewerOlder