Skip to content

Instantly share code, notes, and snippets.

View DJDarkByte's full-sized avatar

Darryl van der Meij DJDarkByte

View GitHub Profile
@DJDarkByte
DJDarkByte / FragmentExtensions.kt
Created April 5, 2021 07:54
Hilt FragmentExtension
@MainThread
fun <VM : ViewModel> Fragment.viewModels(
clazz: KClass<VM>,
ownerProducer: () -> ViewModelStoreOwner = { this },
factoryProducer: (() -> ViewModelProvider.Factory)? = null
) = createViewModelLazy(clazz, { ownerProducer().viewModelStore }, factoryProducer)
@DJDarkByte
DJDarkByte / LifecycleRecyclerView.kt
Last active August 21, 2020 22:50
RecyclerView that cleans up it's mess instead of leaking
class LifecycleRecyclerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr), LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy() {
adapter = null
}
@DJDarkByte
DJDarkByte / SavedStateHandleNavGraphViewModel
Last active August 3, 2020 08:54
NavGraph scoped ViewModel with SavedStateHandle
Extension function:
fun <VM : ViewModel> Fragment.stateNavGraphViewModel(
clazz: KClass<VM>,
@IdRes navGraphId: Int,
qualifier: Qualifier? = null,
bundle: Bundle? = null,
parameters: ParametersDefinition? = null
) = lazy {
val owner = findNavController().getViewModelStoreOwner(navGraphId)