How to change a view after user clicked on a button and a Firestore document got updated?

Viewed 166

So basically when a user comes to the "premium" activity in my app he sees a button which says "Purchase premium". When he clicks on it a document in Firestore gets updated (a boolean from false to true) and the button disappears and instead of the button a TextView appears. When the user navigates back to the MainActivity and from there again to the "premium" activity he sees the "Purchase premium" button again which I don't want because he already "bought" it. The question is how can I achieve that when the user clicks on the button, the view of the layout changes for good, so when the user navigates back to the activity he will not be able to make the purchase again. I don't want him to see the button.

Should I use the boolean (premium member: true) that got changed in the document in Firestore after he clicked on it to do this? Like: If document says "premium membership: true" set button visibility gone etc. This would make a "read" to Firestore every time the user navigates to the Activity, is there a better way?

Here is my onClickListener of the purchase button:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_premium_purchase);

        purchase = findViewById(R.id.card_view_premium_purchase);
        premiumTextView = findViewById(R.id.premium_purchase_success);

        premiumTextView.setVisibility(View.GONE);

        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                String username = user.getDisplayName();

                DocumentReference userRef = db.collection("User").document(username);

                // Set the "pro membership" to true
                userRef
                        .update("pro membership", true).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "DocumentSnapshot successfully updated!");
                        Toast.makeText(PremiumPurchaseActivity.this, "Guess Premium successfully bought", Toast.LENGTH_SHORT).show();
                        premiumTextView.setVisibility(View.VISIBLE);
                        purchase.setVisibility(View.GONE);
                    }
                })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.w(TAG, "Error updating document", e);
                            }
                        });
            }
        });
    }

1 Answers

Actually, its very simple all you have to do is get the current value of "pro membership" field from DocumentRefernce and handle UI accordingly in your activity onResume Method.

@Override
public void onResume(){
super.onResume();
String username = user.getDisplayName();

DocumentReference userRef =  db.collection("User").document(username);

userRef.get().addOnCompleteListener(new 
                 OnCompleteListener<DocumentSnapshot>() {
              @Override
                 public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document != null) {

                        boolean membership = document.getBoolean("pro membership");
                        if(membership){
                           premiumTextView.setVisibility(View.VISIBLE);
                           purchase.setVisibility(View.GONE);
                            }

                           } else {
                            Log.d("LOGGER", "No such document");
                              }
                           } else {
                            Log.d("LOGGER", "get failed with ", 
                            task.getException());
                           }
                        }
                       });

                     }
Related