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 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() | |
this@onLayout.viewTreeObserver.removeOnGlobalLayoutListener(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 | |
/** @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
addon-google_apis-google-10 | |
addon-google_apis-google-11 | |
addon-google_apis-google-12 | |
addon-google_apis-google-13 | |
addon-google_apis-google-14 | |
addon-google_apis-google-15 | |
addon-google_apis-google-16 | |
addon-google_apis-google-17 | |
addon-google_apis-google-18 | |
addon-google_apis-google-19 |
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 LifecycleAwareDisposable( | |
private val disposable: Disposable | |
) : LifecycleObserver { | |
@OnLifecycleEvent(ON_DESTROY) | |
fun dispose() = disposable.dispose() | |
} | |
/** @author Aidan Follestad (afollestad) */ |
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.Transformations | |
fun <X, Y> LiveData<X>.map(mapper: (X) -> Y) = | |
Transformations.map(this, mapper)!! | |
fun <X, Y> LiveData<X>.switchMap(mapper: (X) -> LiveData<Y>) = | |
Transformations.switchMap(this, mapper)!! |
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.OnAttachStateChangeListener | |
import androidx.lifecycle.LifecycleOwner | |
/** @author Aidan Follestad (@afollestad) */ | |
class ViewLifecycleOwner(view: View) : LifecycleOwner, OnAttachStateChangeListener { | |
private val lifecycle = SimpleLifecycle(this) | |
init { |
OlderNewer