Skip to content

Instantly share code, notes, and snippets.

@SkiFire13
SkiFire13 / DaggerViewModelFactory.kt
Last active February 27, 2024 13:12
Example setup for an android app with Fragment constructor injection and ViewModel injection
import android.os.Bundle
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.savedstate.SavedStateRegistryOwner
// The actual ViewModelFactory.
class DaggerViewModelFactory(
private val assistedViewModelFactory: AssistedViewModelFactory<ViewModel>,
owner: SavedStateRegistryOwner,
@jivimberg
jivimberg / takeWhileInclusive.kt
Last active December 18, 2020 10:36
Inclusive implementation of takeWhile for Kotlin. Inspired from this gist: https://gist.github.com/matklad/54776705250e3b375618f59a8247a237 . Read more about this implementation on my blog: https://jivimberg.io/blog/2018/06/02/implementing-takewhileinclusive-in-kotlin/
// kotlin.collections
inline fun <T> Array<out T>.takeWhileInclusive(
predicate: (T) -> Boolean
): List<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = predicate(it)
result