FirebaseAuth.getInstance().getCurrentUser().getUid() always pointing to same ID

Viewed 32

I'm trying to make a feature in my Android studio app which allows users to book appointment with a fixed list of doctors through Firebase realtime database. The problem is that FirebaseAuth.getInstance().getCurrentUser().getUid() is always pointing to same ID and so instead of new appointment details being added via children nodes in the parent node, the existing details in children nodes are being overwritten. Here's my code-

DoctorList.java-

public class DoctorList extends AppCompatActivity {


    TextView doc1,doc2,doc3,doc4;
    FirebaseAuth mAuth;
    DatabaseReference patUser;
    ProgressDialog loader;
    FirebaseDatabase database= FirebaseDatabase.getInstance();
    String Date;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_doctor_list);

            Intent intent = getIntent();

            // receive the value by getStringExtra() method
            // and key must be same which is send by first
            // activity
            String email = intent.getStringExtra("message_key");//patient email

            doc1=findViewById(R.id.doc1);
            doc2=findViewById(R.id.doc2);
            doc3=findViewById(R.id.doc3);
            doc4=findViewById(R.id.doc4);

            loader = new ProgressDialog(this);
            mAuth = FirebaseAuth.getInstance();

            FirebaseUser user= mAuth.getInstance().getCurrentUser();
            patUser= database.getReference().child("Patient Appointments");

            doc1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loader.setMessage("Please wait....");
                    loader.setCanceledOnTouchOutside(false);
                    loader.show();

                    Intent intent= new Intent( DoctorList.this, Book.class);
                    intent.putExtra("message_key1", "elise@doc.com");
                    intent.putExtra("message_key2", email);
                    startActivity(intent);

                    // create the get Intent object
                    Intent intent1 = getIntent();

                    // receive the value by getStringExtra() method
                    // and key must be same which is send by first
                    // activity
                    Date = intent1.getStringExtra("message");
                    //Toast.makeText(PatientPage.this, str, Toast.LENGTH_SHORT).show();

                    String currentUserId = mAuth.getCurrentUser().getUid();
                    patUser= database.getReference().child("Patient Appointments").child(currentUserId);
                                //HashMap userInfo = new HashMap();
                                HashMap userInfo = new HashMap();
                                userInfo.put("Date",Date);
                                userInfo.put("Patient",email);
                                userInfo.put("Doctor","Dr.Elise Heather");
                                userInfo.put("Phone","5925866");
                                userInfo.put("Status","Pending");

                                patUser.updateChildren(userInfo).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()){
                                            Toast.makeText(DoctorList.this, "Little more to go....", Toast.LENGTH_SHORT).show();

                                        }

                                        else{
                                            Toast.makeText(DoctorList.this, task.getException().toString(), Toast.LENGTH_SHORT).show();

                                        }

                                    }

                    });


                    loader.dismiss();
                    //Intent intent2= new Intent( DoctorList.this, PatientPage.class);
                    //startActivity(intent2);

                }

            });
        }
    }

Book.java-

public class Book extends AppCompatActivity {

TextView selectedDate;
Button calenderButton,ok;
FirebaseAuth mAuth;
DatabaseReference docUser;
ProgressDialog loader;
FirebaseDatabase database= FirebaseDatabase.getInstance();
String Date;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book);

    Intent intent = getIntent();

    // receive the value by getStringExtra() method
    // and key must be same which is send by first
    // activity
    String docEmail = intent.getStringExtra("message_key1");
    String patEmail = intent.getStringExtra("message_key2");

    selectedDate=findViewById(R.id.text);
    calenderButton=findViewById(R.id.calender);
    ok=findViewById(R.id.ok);

    loader = new ProgressDialog(this);
    mAuth = FirebaseAuth.getInstance();

    FirebaseUser user= mAuth.getInstance().getCurrentUser();
    docUser= database.getReference().child("Doctor Schedule");

    MaterialDatePicker materialDatePicker=MaterialDatePicker.Builder.datePicker().
            setTitleText("Select date").setSelection(MaterialDatePicker.todayInUtcMilliseconds()).build();

    calenderButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            materialDatePicker.show(getSupportFragmentManager(),"Tag_Picker");
            materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
                @Override
                public void onPositiveButtonClick(Object selection) {
                    selectedDate.setText(materialDatePicker.getHeaderText());
                    Date=materialDatePicker.getHeaderText();
                }
            });
        }
    });

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (Date != null) {

                String currentUserId = mAuth.getCurrentUser().getUid();
                docUser = database.getReference().child("Doctor Schedule").child(currentUserId);
                //HashMap userInfo = new HashMap();
                HashMap userInfo = new HashMap();
                userInfo.put("Date", Date);
                userInfo.put("Patient", patEmail);
                userInfo.put("Doctor", docEmail);
                userInfo.put("Status", "Pending");

                docUser.updateChildren(userInfo).addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(Book.this, "Appointment booked", Toast.LENGTH_SHORT).show();
                            Intent intent= new Intent( Book.this, DoctorList.class);
                            intent.putExtra("message", Date);
                            startActivity(intent);

                        } else {
                            Toast.makeText(Book.this, task.getException().toString(), Toast.LENGTH_SHORT).show();

                        }

                    }

                });


            }

            else {
                Toast.makeText(Book.this, "Select date!", Toast.LENGTH_SHORT).show();

            }
        }
    });
}

}

Suppose a user with email athy@gmail.com books appointment and in Firebase database there's already details of booking by another user nivi@gm.com . Then the details of the latter user in the parent node (ID) gets overwritten by user athy@gmail.com and it looks like this. I don't think I've written code for overwriting instead of updating since it works fine in other Android studio projects, so I'm guessing FirebaseAuth.getInstance().getCurrentUser().getUid() is the one always pointing to same ID which I've circled in the image.

How do I fix this?

0 Answers
Related