Skip to content

Instantly share code, notes, and snippets.

View macsystems's full-sized avatar
🏠
Working from home

Jens Hohl macsystems

🏠
Working from home
View GitHub Profile
@macsystems
macsystems / ProgressBarExt.kt
Last active January 22, 2020 11:47
Using sealed class to change progressbar visibility
fun ProgressBar.handleState(resource: Resource<Any>) {
visibility = when (resource) {
is Loading -> View.VISIBLE
is Error -> View.GONE
is Success -> View.GONE
}
}
@macsystems
macsystems / ViewDebounceClickListenerExt.kt
Created January 28, 2020 15:33
Debounce Click Listener Extension which prevents user invoking some function to often
private const val defaultDebounce: Long = 500
/**
* Prevents that the click events gets called too often by fast clicking user
*/
@UiThread
fun View.setDebounceOnClickListener(@IntRange(from = 0, to = Integer.MAX_VALUE.toLong()) debounceTime: Long = defaultDebounce, listener: (View) -> Unit) {
this.setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0
override fun onClick(v: View) {
@macsystems
macsystems / FlowExt.kt
Last active May 14, 2024 15:50
Flow debounce operator which debounces matching events and also ignores when it gets emited multiple times
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.launch
import kotlin.time.Duration
/**
* Debouncing when predicate is [true]