-
-
Save robotsquidward/3aed18f1296bfe445553ef4b189c8a2a to your computer and use it in GitHub Desktop.
Gist for Medium version of https://ajkueterman.dev/posts/android-viewmodel-manual-dependency-injection-made-easy/
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 MyActivity : AppCompatActivity() { | |
private val model: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
model.getUsers().observe(this, Observer<List<User>>{ users -> | |
// update UI | |
}) | |
} | |
} |
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
fun createWithFactory( | |
create: () -> ViewModel | |
): ViewModelProvider.Factory { | |
return object : ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
@Suppress("UNCHECKED_CAST")// Casting T as ViewModel | |
return create.invoke() as T | |
} | |
} | |
} |
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
private val model: MyViewModel by lazy { | |
ViewModelProvider( | |
this, | |
createWithFactory { | |
MyViewModel(repo = MyRepository()) | |
} | |
).get(MyViewModel::class.java) | |
} |
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
private val model: MyViewModel by activityViewModels { | |
createWithFactory { | |
MyViewModel(repo = MyRepository()) | |
} | |
} |
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
/** | |
* Get a [ViewModel] in an [ComponentActivity]. | |
*/ | |
@MainThread | |
inline fun <reified VM : ViewModel> ComponentActivity.viewModelBuilder( | |
noinline viewModelInitializer: () -> VM | |
): Lazy<VM> { | |
return ViewModelLazy( | |
viewModelClass = VM::class, | |
storeProducer = { viewModelStore }, | |
factoryProducer = { | |
return@ViewModelLazy object : ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
@Suppress("UNCHECKED_CAST")// Casting T as ViewModel | |
return viewModelInitializer.invoke() as T | |
} | |
} | |
} | |
) | |
} | |
/** | |
* Get a [ViewModel] in a [Fragment]. | |
*/ | |
@MainThread | |
inline fun <reified VM : ViewModel> Fragment.activityViewModelBuilder( | |
noinline viewModelInitializer: () -> VM | |
): Lazy<VM> { | |
return ViewModelLazy( | |
viewModelClass = VM::class, | |
storeProducer = { requireActivity().viewModelStore }, | |
factoryProducer = { | |
object : ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
@Suppress("UNCHECKED_CAST")// Casting T as ViewModel | |
return viewModelInitializer.invoke() as T | |
} | |
} | |
} | |
) | |
} |
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
private val model: MyViewModel by viewModelBuilder { | |
MyViewModel(repo = MyRepository()) | |
} |
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
private val model: MyViewModel by activityViewModelBuilder { | |
MyViewModel(repo = MyRepository()) | |
} |
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
dependencies { | |
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment