Created
May 18, 2017 17:10
-
-
Save harshvishu/4986b4f74e2cd95db9eb1f690826046a to your computer and use it in GitHub Desktop.
Custom TextWatcher with debounce operator (Notify text changes after delay)
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
package com.brotherpowers.your_package_name.Controller; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Created by harsh_vishwakarma on 5/18/17. | |
* <p> | |
* A debounce version {@link TextWatcher} | |
* using {@link ScheduledExecutorService} | |
* <p> | |
* Aim: To achieve this result | |
* http://reactivex.io/documentation/operators/debounce.html | |
*/ | |
public abstract class DebounceTextWatcher implements TextWatcher { | |
private static final int DEFAULT_DELAY = 400; // MILLISECONDS | |
// Service to execute after delay | |
private ScheduledExecutorService service = Executors.newScheduledThreadPool(1); | |
private ScheduledFuture<?> scheduledFuture; | |
private final int delay; // Delay for debounce operation | |
// public empty default constructor | |
@SuppressWarnings("WeakerAccess") | |
public DebounceTextWatcher() { | |
delay = DEFAULT_DELAY; | |
} | |
// Specify delay | |
@SuppressWarnings("WeakerAccess") | |
public DebounceTextWatcher(int delay) { | |
this.delay = delay; | |
} | |
/** | |
* Available for override | |
*/ | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
/** | |
* Available for override | |
*/ | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
/** | |
* Call onDebounceTextChanged from Main Thread | |
*/ | |
@Override | |
public final void afterTextChanged(final Editable s) { | |
cancelDebounce(); // Cancel any existing call to execute service | |
// Schedule a new task | |
scheduledFuture = service.schedule(new Runnable() { | |
@Override | |
public void run() { | |
new Handler(Looper.getMainLooper()).post(new Runnable() { | |
@Override | |
public void run() { | |
onDebounceTextChanged(s); | |
} | |
}); | |
} | |
}, delay, TimeUnit.MILLISECONDS); | |
} | |
/** | |
* Override this method to be notified of the text changes after delay | |
*/ | |
public abstract void onDebounceTextChanged(Editable s); | |
/** | |
* Cancel task | |
*/ | |
private void cancelDebounce() { | |
if (scheduledFuture != null) { | |
scheduledFuture.cancel(true); | |
} | |
} | |
/** | |
* Call this from on Activity's /Fragment's onDestroy method | |
* to avoid memory leaks | |
*/ | |
public void onDestroy() { | |
cancelDebounce(); | |
service.shutdown(); | |
scheduledFuture = null; | |
service = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment