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 / 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_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?) {
@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 / Motivation_SomeViewModel.kt
Last active Jan 3, 2020
Connecting The Dots :: Motivation - manually added dependencies
View Motivation_SomeViewModel.kt
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
@mlykotom
mlykotom / Connecting_Motivation_SomeFragment.kt
Last active Jan 3, 2020
Connecting The Dots :: Motivation - manually added dependencies
View Connecting_Motivation_SomeFragment.kt
class SomeFragment : Fragment() {
// inject into this class omitted for brevity
@Inject
lateinit var dep1: Dependency
@Inject
lateinit var dep2: Dependency
lateinit var viewModel : SomeViewModel
@mlykotom
mlykotom / Connecting_Motivation_AfterInitViewModel.kt
Last active Jan 3, 2020
Connecting The Dots :: Motivation - parameters after initialization of ViewModel
View Connecting_Motivation_AfterInitViewModel.kt
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
@mlykotom
mlykotom / Connecting_SavedState_Fragment.kt
Last active Jan 3, 2020
Connecting The Dots :: Pure SavedState - Fragment
View Connecting_SavedState_Fragment.kt
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
You can’t perform that action at this time.