Search code examples
javaandroidfirebasefirebase-realtime-databasefirebase-console

Android Firebase: user send data for manual inspection, make data available for everyone if inspection pass


I have a multiuser app. My users fill forms in my app that is sent to "uncheckedForms" in my firebase database. I need to manually check the forms they send.

A form is a Java object that has a boolean "isPassedInspection" instance variable that is set to false by default.

After my manual inspection, I set this instance variable to true directly from the firebase console, if by my standards this form has "passed"

On the other hand, I have a second Item in my database called "forms" that represents active forms that have already passed my manual inspection and their "isPassedInspection" is set to true.

All my app users read the same data. this data is retrieved from the "forms" and bound to a RecyclerView in my app.

This is an image of illustration

enter image description here

I am trying to figure out how to make the forms that I just checked and changed isPassedInspection to true, to automatically move from the "uncheckedForms" to "forms".

I would love to hear suggestions. Thank you :)


Solution

  • There are 2 ways in which you can achieve this. First solution would be client side, in which you can attach a ValueEventListener on the isPassedInspection property to check the value if it is true or false. If the value is true, then copy that object from uncheckedForms to forms. Obviously, don't forget to delete the form form uncheckedForms, once the form is successfully copied. The second approach would be to write a function in Firebase Cloud Functions, that has the same purpose, to attach a listener on the same location and check if the value of isPassedInspection has changed, if it is changed to true then to copy the form in the other part. Satish Rajbhar's function might work, i didn't try it, but keep in mind that the benefit of this solution is that a form can be copied in the other side even if the user has the application closed.

    If I were you, I would not have used this kind of database structure. I only have used a single node named forms and then query the database based on the isPassedInspection property. So, the main ideea is to display the user only the forms that are checked by you. The query should look like this:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef.child("forms").orderByChild("isPassedInspection").equalsTo(true);