I have a Firebase Database that looks like this:
I have a node named Products
Products | |--------prod1_id | | | |--name : "prod1" | --state: "free" | | |---------prod2_id | |--name : "prod2" --price: "not_free"
Then I have node for users
Users | |--------user1_id | | | |--name : "user1" | --e-mail: " user1@email.com " | | |---------user2_id | |--name : "user2" --e-mail: " user2@email.com "
And in my application, the user can buy, and only when he / she does, I create a new node called PurchasedItems , where I store User and Product
Purchased items | |--------user1_id | | | |--prod1: "any_value" | --prod2: "any_value" | | |---------user2_id | |--prod1: "any_value"
The thing is, in my query for objects, I do this:
products_ref= FirebaseDatabase.getInstance().getReference().child("products");
WHAT A PROBLEM?
The user who entered the application, even if he bought the product, he will show the productNotFree layout, because on the products I do not have a link to the user who bought this product, but I have something similar to my "TryToPurchase", because that I have a request to PurchasedProducts to find out if the user has this product or not, to make a purchase or not.
In my onClick() method, I have this to check if the user has paid for it or not.
root_ref.child("PurchasedProducts").child(currentuser).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.hasChild(model.getProduct_id())){ showToast("He has that product"); } else{ showToast("Purchasing");
So far I have had a switch() on setImageResource , but from a request in Products
NOTE: layoutFree is, for example, a field in which there is no lock, and layoutNotFree is the same field, but with a lock
switch (model.getState()) { case "free": holder.card_image.setImageResource(layoutFree); break; case "not_free": holder.card_image.setImageResource(layoutNotFree)); break; default: break; }
Now I am faced with the fact that if the user buys the goods, he / she will not be able to see the layout, even if he buys it, he will see the layout with a lock, even if he paid for it.
What did I think to do?
In the same onBindViewHolder , where I have a switch() , I could make another Firebase request, asking if currentUser this product, and if it has this product, I put freeLayout otherwise NotFreeLayout.
But I think this is a little dirty, so I put it here to see if there is another way to make it effective.
What would I like to see?
Well, if I am registered as a user who paid for one product with the status "not_free", I would like to see a product with a layout without a blocking element, and also, if I pay for an element, I create a table PurchasedItems and I do something like this:
root_ref.child("PurchasedProducts").child(currentuser).child(productId).setValue("paid");
And also I would like RecyclerView change layoutNotFree to layoutFree, is this some way to do this, instead of updating the Activity ? Updating Activity means I need to re-do the adapter, create a RecyclerView , etc.
I hope I explained this correctly and step by step to avoid confusion, if I did something wrong to the question, feel free to edit it or ask me about it, and I will edit it.