Skip to content

Instantly share code, notes, and snippets.

@tmaxxdd
Created July 31, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmaxxdd/4deb74ae5ce43e542335f1a5a6506812 to your computer and use it in GitHub Desktop.
Save tmaxxdd/4deb74ae5ce43e542335f1a5a6506812 to your computer and use it in GitHub Desktop.
...
private Disposable textWatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatEditText input = findViewById(R.id.password);
AppCompatTextView message = findViewById(R.id.message);
textWatcher = RxTextView.textChanges(input) // Tworzy strumien na podstawie zmian EditText
.debounce(300, TimeUnit.MILLISECONDS) // Ogranicza emisje na odstepy co 300 milisekund
.map(this::isPasswordValid) // Zamienia wprowadzony tekst na rezultat logiczny
.subscribeOn(Schedulers.io()) // Powyzsze operacje wykonuje w tle na watku I/O
.observeOn(AndroidSchedulers.mainThread()) // Rezultat jest zwracany do UI
.subscribe( // Subskrybcja, ktora przechwytuje emisje czyli (true/false)
valid -> {
if (valid) { //Haslo zgodne ze schematem
message.setText("Haslo jest prawidłowe.");
message.setTextColor(Color.GREEN);
} else { //Haslo niezgodne ze schematem
message.setText("Haslo niezgodne z wymaganiami.");
message.setTextColor(Color.RED);
}
});
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment