Android - How to check if your contacts has registered on your app?
Not sure if this is the solution to your problem, but it might be and it is too long for a comment to explain:
Let me write your code in a commpressed way with some comments:
// start a loop over every local contacts on the phone
while (cursor.moveToNext()) {
// create a local variable for this contact (the class name should be Contact. Singular.)
Contacts contacts = new Contacts(name, phone, "");
// get a contact with the same phone number from firebase.
// the sorting is executed for every iteration of the loop -> bad performance.
// -> I recommend to order the reference outside of the loop.
Query query = reference.orderByChild("number").equalTo(contacts.getNumber());
// now you add the ValueEventListener
// if onDataChanged is called, you iterate over all children of the new snapshot
// you create again a local Contacts object
// then there is this strange comparison of name and phone...don't know what that accomplishes, but ok
// now comes the part that might cause the wrong behaviour:
userList.add(contacts1);
adapter = new ContactsAdapter(getContext(), userList);
recyclerView.setAdapter(adapter);
}
So the last 3 lines seem error-prone:
userList.add(contacts1);
adapter = new ContactsAdapter(getContext(), userList);
recyclerView.setAdapter(adapter);
In general there is not much wrong with them, but keep in mind that you are basically still in the very first loop that you started in getContactList()
That means that you create a completely new adapter for every contact in the loop and assign this new adapter to the recyclerView.
Let's say you have 100 contacts on your phone. This leads to 100 adapters being created and set to the recyclerView.
I would suggest 2 major changes in your code:
override
onViewCreated
and put thegetContactList();
in there. The methodonCreateView
should only be concerned with basic view inflation and maye initializing simple View variables. Filling the View with data should be done afterwards inonViewCreated
.Make a clear separation between "preparing the data for the View" and "putting the prepared data into the View". In other words: First create your final
userList
and only if this list is done, create the adapter and set it to the recyclerView.
If you want to improve even further, have a look at the idea of ViewModels. In short: The ViewModel is preparing all the data that its View wants to display (including filtering, transforming, etc.). The View (in this case your Fragment) is as "stupid" as possible -> It waits until the ViewModel passes data into it and does nothing more than to display that data.
LiveData is very useful for this approach and it will make your code much much cleaner and easier to understand and to maintain :)
Good luck with your project!
I'll not give you any code but an architectural hint on how to achieve your goal. From details provided in your question, I assume your clients are registering through OTP. When they sign in you can store their UID against their phone numbers. Take a look on how to save user details.
Here is how your firebase stored registered users should look like
registeredUsers : {
"23492hsdfs08fv9x9" : "0X20-342234...",
"34928304283438d8f" : "0X02-123123..."
}
Now when you access current user contact list, loop through the usrs list in firebase to see which phone numbers from contact list are also in firebase.
You could use just list of phone numbers without adding Firebase UIDs but I recommend you do it like above in case you need to map a contact number to it's UID and then you can fetch that user's details from UID as well.
Hope I've given you a hint on how to get along.