From 187d8e64dc7189f63707d154166867084662dbe3 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 5 Aug 2017 23:10:28 -0700 Subject: Create debounce and update searchview (#27) * Prepare version * Create debounce base * Add debouncer and fix transition crash * Add debounce docs * Update links * Update searchview docs * Test without a ref * Add links to core components * Update links * Update to bullet points * Test core md * Test slash * Test slash * Specify implemented dependencies --- .../kotlin/ca/allanwang/kau/kotlin/Debouncer.kt | 124 +++++++++++++++++++++ .../ca/allanwang/kau/kotlin/LazyResettable.kt | 2 +- .../ca/allanwang/kau/logging/TimberLogger.kt | 3 + 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kotlin/Debouncer.kt (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Debouncer.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Debouncer.kt new file mode 100644 index 0000000..4fba2c8 --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Debouncer.kt @@ -0,0 +1,124 @@ +package ca.allanwang.kau.kotlin + +import ca.allanwang.kau.logging.KL +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + +/** + * Created by Allan Wang on 2017-08-05. + * + * Thread safe function wrapper to allow for debouncing + * With reference to Stack Overflow + */ + +/** + * The debouncer base + * Implements everything except for the callback, + * as the number of variables is different between implementations + * You may still use this without extending it, but you'll have to pass a callback each time + */ +open class Debouncer(var interval: Long) { + private val sched = Executors.newScheduledThreadPool(1) + private var task: DebounceTask? = null + + /** + * Generic invocation to pass a callback to the new task + * Pass a new callback for the task + * If another task is pending, it will be invalidated + */ + operator fun invoke(callback: () -> Unit) { + synchronized(this) { + task?.invalidate() + val newTask = DebounceTask(callback) + KL.v("Debouncer task created: $newTask in $this") + sched.schedule(newTask, interval, TimeUnit.MILLISECONDS) + task = newTask + } + } + + /** + * Call to cancel all pending requests and shutdown the thread pool + * The debouncer cannot be used after this + */ + fun terminate() = sched.shutdownNow() + + /** + * Invalidate any pending tasks + */ + fun cancel() { + synchronized(this) { + if (task != null) KL.v("Debouncer cancelled for $task in $this") + task?.invalidate() + task = null + } + } + +} + +/* + * Helper extensions for functions with 0 to 3 arguments + */ + +/** + * The debounced task + * Holds a callback to execute if the time has come and it is still valid + * All methods can be viewed as synchronous as the invocation is synchronous + */ +private class DebounceTask(inline val callback: () -> Unit) : Runnable { + private var valid = true + + fun invalidate() { + valid = false + } + + override fun run() { + if (!valid) return + valid = false + KL.v("Debouncer task executed $this") + try { + callback() + } catch (e: Exception) { + KL.e(e, "DebouncerTask exception") + } + } +} + +/** + * A zero input debouncer + */ +class Debouncer0 internal constructor(interval: Long, val callback: () -> Unit) : Debouncer(interval) { + operator fun invoke() = invoke(callback) +} + +fun debounce(interval: Long, callback: () -> Unit) = Debouncer0(interval, callback) +fun (() -> Unit).debounce(interval: Long) = debounce(interval, this) + +/** + * A one argument input debouncer + */ +class Debouncer1 internal constructor(interval: Long, val callback: (T) -> Unit) : Debouncer(interval) { + operator fun invoke(key: T) = invoke { callback(key) } +} + +fun debounce(interval: Long, callback: (T) -> Unit) = Debouncer1(interval, callback) +fun ((T) -> Unit).debounce(interval: Long) = debounce(interval, this) + +/** + * A two argument input debouncer + */ +class Debouncer2 internal constructor(interval: Long, val callback: (T, V) -> Unit) : Debouncer(interval) { + operator fun invoke(arg0: T, arg1: V) = invoke { callback(arg0, arg1) } +} + +fun debounce(interval: Long, callback: (T, V) -> Unit) = Debouncer2(interval, callback) +fun ((T, V) -> Unit).debounce(interval: Long) = debounce(interval, this) + +/** + * A three argument input debouncer + */ +class Debouncer3 internal constructor(interval: Long, val callback: (T, U, V) -> Unit) : Debouncer(interval) { + operator fun invoke(arg0: T, arg1: U, arg2: V) = invoke { callback(arg0, arg1, arg2) } +} + +fun debounce(interval: Long, callback: ((T, U, V) -> Unit)) = Debouncer3(interval, callback) +fun ((T, U, V) -> Unit).debounce(interval: Long) = debounce(interval, this) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt index 2ac5d2f..ceeaa30 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt @@ -13,7 +13,7 @@ internal object UNINITIALIZED fun lazyResettable(initializer: () -> T): LazyResettable = LazyResettable(initializer) -open class LazyResettable(private val initializer: () -> T, lock: Any? = null) : ILazyResettable, Serializable { +class LazyResettable(private val initializer: () -> T, lock: Any? = null) : ILazyResettable, Serializable { @Volatile private var _value: Any = UNINITIALIZED private val lock = lock ?: this diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt index 4c6d655..4cf566a 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt @@ -18,4 +18,7 @@ open class TimberLogger(tag: String) { inline fun i(s: String) = Timber.i(TAG, s) inline fun v(s: String) = Timber.v(TAG, s) inline fun eThrow(s: String) = e(Throwable(s)) +// fun plant() { +// Timber.plant(Timber.Tree()) +// } } \ No newline at end of file -- cgit v1.2.3