Skip to content

Instantly share code, notes, and snippets.

@watabee
watabee / android-demo-mode.zsh
Last active November 6, 2023 09:28
Enter or exit demo mode for the Android System UI
function android-demo-mode() {
CMD=$1
if [[ $CMD != "on" && $CMD != "off" ]]; then
echo "Usage: $0 [on|off] [hhmm]" >&2
return 1
fi
if [[ "$2" != "" ]]; then
HHMM="$2"
@watabee
watabee / LiveDataExt.kt
Last active January 11, 2020 01:25
Extension functions for LiveData
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.map
import androidx.lifecycle.switchMap
import kotlinx.coroutines.delay
// Combine latest values
inline fun <X, Y, Z> LiveData<X>.combineLatest(data: LiveData<Y>, crossinline mapFunction: (X, Y) -> Z): LiveData<Z> {
return switchMap { x: X -> data.map { y: Y -> mapFunction(x, y) } }
}
@watabee
watabee / LiveDataTestUtils.kt
Created December 22, 2019 08:13
Get value safely from LiveData for testing using kotlin coroutines
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeoutOrNull
import kotlin.coroutines.resume
suspend fun <T : Any> LiveData<T>.awaitValue(): T? = withTimeoutOrNull(2000L) {
suspendCancellableCoroutine<T> { cont ->
val observer = object : Observer<T> {
override fun onChanged(t: T) {
@watabee
watabee / SharedOkHttp.kt
Created February 23, 2019 05:18
A ktor client engine for sharing OkHttpClient
import io.ktor.client.call.HttpClientCall
import io.ktor.client.call.HttpEngineCall
import io.ktor.client.call.UnsupportedContentTypeException
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.HttpClientEngineConfig
import io.ktor.client.engine.HttpClientEngineFactory
import io.ktor.client.engine.HttpClientJvmEngine
import io.ktor.client.engine.mergeHeaders
import io.ktor.client.request.DefaultHttpRequest
import io.ktor.client.request.HttpRequestData
import android.content.Context
import android.support.annotation.MainThread
import android.support.v7.app.AlertDialog
import io.reactivex.Observable
enum class RxDialogActionStyle {
POSITIVE, NEGATIVE, NEUTRAL
}
data class RxDialogAction(val title: CharSequence, val style: RxDialogActionStyle)
@watabee
watabee / HighlightableImageView.java
Created August 2, 2017 14:48
An ImageView which can highlight.
import android.content.Context;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class HighlightableImageView extends AppCompatImageView {
private static final ColorFilter COLOR_FILTER = new PorterDuffColorFilter(0x40000000, PorterDuff.Mode.SRC_ATOP);