I need to implement typing indicator in chat application using firebase. I have a logic like
Then i think when a value change it will notify the value change listener
if any one has a code implementation please share.
Please check code below: `
make structure something like: (it may vary as per your need I am just referring something to you)
I have an object "isTyping" with Two Keys Let's say "first_user" and "second_user". If you want to check for "second_user" is typing or not this should be the code.
private void checkForOtherUserTypingStatus() {
mMessageThread.child("isTyping").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("true")) {
updateTypingViewVisibility(true);
} else if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("false")) {
updateTypingViewVisibility(false);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("true")) {
updateTypingViewVisibility(true);
} else if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("false")) {
updateTypingViewVisibility(false);
}
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
To update value of "isTyping" > "first_user" (assume that you are typing)
mBinder.etChatMessage.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Map<String, Object> params = new HashMap<>();
params.put("first_user", charSequence.length() > 0);
mMessageThread.child("isTyping").updateChildren(params);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
Yes . you got it right. in user node maintain one boolean flag like isTyping
.
on textwatcherListener
change the value to true and yeah onValueChangeListner
will also trigger so that you can set Typing
on recipients screen by getting that boolean Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With