-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileActivity.java
More file actions
85 lines (70 loc) · 3.05 KB
/
Copy pathProfileActivity.java
File metadata and controls
85 lines (70 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.assignment_5;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.*;
public class ProfileActivity extends AppCompatActivity {
private EditText etName, etBloodType, etCity, etPhone;
private Button btnUpdate, btnLogout;
private FirebaseAuth auth;
private DatabaseReference databaseReference;
private FirebaseUser currentUser;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
etName = findViewById(R.id.etName);
etBloodType = findViewById(R.id.etBloodType);
etCity = findViewById(R.id.etCity);
etPhone = findViewById(R.id.etPhone);
btnUpdate = findViewById(R.id.btnUpdate);
btnLogout = findViewById(R.id.btnLogout);
auth = FirebaseAuth.getInstance();
currentUser = auth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
if (currentUser != null) {
loadUserProfile();
}
btnUpdate.setOnClickListener(view -> updateProfile());
btnLogout.setOnClickListener(view -> {
auth.signOut();
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
finish();
});
}
private void loadUserProfile() {
String userId = currentUser.getUid();
databaseReference.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
etName.setText(snapshot.child("name").getValue(String.class));
etBloodType.setText(snapshot.child("bloodType").getValue(String.class));
etCity.setText(snapshot.child("city").getValue(String.class));
etPhone.setText(snapshot.child("phone").getValue(String.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(ProfileActivity.this, "Failed to load profile", Toast.LENGTH_SHORT).show();
}
});
}
private void updateProfile() {
String userId = currentUser.getUid();
String updatedCity = etCity.getText().toString();
String updatedPhone = etPhone.getText().toString();
databaseReference.child(userId).child("city").setValue(updatedCity);
databaseReference.child(userId).child("phone").setValue(updatedPhone);
Toast.makeText(this, "Profile Updated Successfully", Toast.LENGTH_SHORT).show();
}
}