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
import android.view.View | |
import java.lang.System.currentTimeMillis | |
private const val HALF_SECOND = 500L | |
/** @author Aidan Follestad (@afollestad) */ | |
abstract class DebouncedOnClickListener( | |
private val delayBetweenClicks: Long = HALF_SECOND | |
) : View.OnClickListener { |
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
import android.content.ContentValues | |
/** | |
* Returns a [ContentValues] instance which contains only values that have changed between | |
* the receiver (original) and parameter (new) instances. | |
*/ | |
fun ContentValues.diffFrom(contentValues: ContentValues): ContentValues { | |
val diff = ContentValues() | |
for ((name, oldValue) in this.valueSet()) { | |
val newValue = contentValues.get(name) |
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
import android.view.View | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Job | |
import kotlin.coroutines.CoroutineContext | |
fun View.scopeWhileAttached( | |
context: CoroutineContext, | |
exec: CoroutineScope.() -> Unit | |
) { | |
val job = Job(context[Job]) |
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
import kotlin.math.ceil | |
const val SECOND: Long = 1000 | |
const val MINUTE = SECOND * 60 | |
const val HOUR = MINUTE * 60 | |
const val DAY = HOUR * 24 | |
const val WEEK = DAY * 7 | |
const val MONTH = WEEK * 4 | |
fun Long.timeString() = when { |
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
import android.view.View | |
import android.view.ViewTreeObserver | |
fun View.onLayout(cb: () -> Unit) { | |
if (this.viewTreeObserver.isAlive) { | |
this.viewTreeObserver.addOnGlobalLayoutListener( | |
object : ViewTreeObserver.OnGlobalLayoutListener { | |
override fun onGlobalLayout() { | |
cb() | |
[email protected](this) |
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
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MediatorLiveData | |
import androidx.lifecycle.Observer | |
typealias Zipper<T, K, R> = (T, K) -> R | |
/** @author Aidan Follestad (@afollestad) */ | |
class ZipLiveData<T, K, R>( | |
source1: LiveData<T>, | |
source2: LiveData<K>, |
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
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MediatorLiveData | |
import androidx.lifecycle.Observer | |
/** @author Aidan Follestad (@afollestad) */ | |
class DistinctLiveData<T>(source1: LiveData<T>) : MediatorLiveData<T>() { | |
private var isInitialized = false | |
private var lastValue: T? = 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
/** @author Aidan Follestad (@afollestad) */ | |
class TestLiveData<T>(data: LiveData<T>) { | |
private val receivedValues = mutableListOf<T>() | |
private val observer = Observer<T> { emission -> | |
emission?.let { receivedValues.add(it) } | |
} | |
init { | |
data.observeForever(observer) |
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
import android.view.View | |
import android.view.View.GONE | |
import android.view.View.INVISIBLE | |
import android.view.View.VISIBLE | |
import android.view.ViewTreeObserver | |
import android.widget.AdapterView | |
import android.widget.Spinner | |
fun View.show() { | |
visibility = VISIBLE |
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
import android.content.Context | |
import android.content.Intent | |
import android.content.IntentFilter | |
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY | |
import androidx.lifecycle.Lifecycle.Event.ON_START | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.LifecycleOwner | |
import androidx.lifecycle.OnLifecycleEvent | |
import android.content.BroadcastReceiver as StockReceiver |
OlderNewer