tou a editar o perfil e deixar a pp mais bonita
This commit is contained in:
@@ -13,6 +13,7 @@ public class User {
|
||||
public String password;
|
||||
public int age;
|
||||
public String utenteNumber;
|
||||
public String profilePictureUri;
|
||||
|
||||
public User(String name, String email, String password, int age, String utenteNumber) {
|
||||
this.name = name;
|
||||
|
||||
@@ -59,15 +59,47 @@ public class ProfileFragment extends Fragment {
|
||||
binding.profileEmail.setText(currentUser.email);
|
||||
binding.profileAge.setText(String.valueOf(currentUser.age));
|
||||
binding.profileUtente.setText(currentUser.utenteNumber != null ? currentUser.utenteNumber : "N/A");
|
||||
if (currentUser.profilePictureUri != null) {
|
||||
android.widget.ImageView profileImage = binding.getRoot().findViewById(R.id.profile_image);
|
||||
if (profileImage != null) {
|
||||
try {
|
||||
profileImage.setImageURI(android.net.Uri.parse(currentUser.profilePictureUri));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private android.net.Uri tempProfileUri;
|
||||
private android.widget.ImageView dialogImageView;
|
||||
|
||||
private final androidx.activity.result.ActivityResultLauncher<String> pickMedia = registerForActivityResult(
|
||||
new androidx.activity.result.contract.ActivityResultContracts.GetContent(), uri -> {
|
||||
if (uri != null) {
|
||||
tempProfileUri = uri;
|
||||
try {
|
||||
requireContext().getContentResolver().takePersistableUriPermission(uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (dialogImageView != null) {
|
||||
dialogImageView.setImageURI(uri);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
private void showEditDialog() {
|
||||
if (currentUser == null)
|
||||
return;
|
||||
|
||||
// Reset temp uri
|
||||
tempProfileUri = null;
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
|
||||
// Inflate custom layout
|
||||
@@ -81,8 +113,10 @@ public class ProfileFragment extends Fragment {
|
||||
EditText editName = dialogView.findViewById(R.id.edit_name);
|
||||
EditText editAge = dialogView.findViewById(R.id.edit_age);
|
||||
EditText editUtente = dialogView.findViewById(R.id.edit_utente);
|
||||
|
||||
EditText editEmail = dialogView.findViewById(R.id.edit_email);
|
||||
EditText editPassword = dialogView.findViewById(R.id.edit_password);
|
||||
dialogImageView = dialogView.findViewById(R.id.edit_profile_image);
|
||||
View btnChangePassword = dialogView.findViewById(R.id.button_change_password);
|
||||
View btnSave = dialogView.findViewById(R.id.button_save);
|
||||
View btnCancel = dialogView.findViewById(R.id.button_cancel);
|
||||
|
||||
@@ -91,17 +125,24 @@ public class ProfileFragment extends Fragment {
|
||||
editAge.setText(String.valueOf(currentUser.age));
|
||||
editUtente.setText(currentUser.utenteNumber);
|
||||
editEmail.setText(currentUser.email);
|
||||
editPassword.setText(currentUser.password);
|
||||
|
||||
if (currentUser.profilePictureUri != null) {
|
||||
dialogImageView.setImageURI(android.net.Uri.parse(currentUser.profilePictureUri));
|
||||
}
|
||||
|
||||
dialogImageView.setOnClickListener(v -> pickMedia.launch("image/*"));
|
||||
|
||||
btnChangePassword.setOnClickListener(v -> {
|
||||
showChangePasswordDialog();
|
||||
});
|
||||
|
||||
btnSave.setOnClickListener(v -> {
|
||||
String newName = editName.getText().toString();
|
||||
String ageStr = editAge.getText().toString();
|
||||
String newUtente = editUtente.getText().toString();
|
||||
String newEmail = editEmail.getText().toString();
|
||||
String newPassword = editPassword.getText().toString();
|
||||
|
||||
if (newName.isEmpty() || ageStr.isEmpty() || newUtente.isEmpty() || newEmail.isEmpty()
|
||||
|| newPassword.isEmpty()) {
|
||||
if (newName.isEmpty() || ageStr.isEmpty() || newUtente.isEmpty() || newEmail.isEmpty()) {
|
||||
Toast.makeText(getContext(), "Preencha todos os campos", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
@@ -113,7 +154,10 @@ public class ProfileFragment extends Fragment {
|
||||
currentUser.age = newAge;
|
||||
currentUser.utenteNumber = newUtente;
|
||||
currentUser.email = newEmail;
|
||||
currentUser.password = newPassword;
|
||||
// Password not updated here
|
||||
if (tempProfileUri != null) {
|
||||
currentUser.profilePictureUri = tempProfileUri.toString();
|
||||
}
|
||||
|
||||
AppDatabase.databaseWriteExecutor.execute(() -> {
|
||||
userDao.insert(currentUser);
|
||||
@@ -128,17 +172,52 @@ public class ProfileFragment extends Fragment {
|
||||
}
|
||||
|
||||
// UI update
|
||||
binding.profileName.setText(newName);
|
||||
binding.profileEmail.setText(newEmail);
|
||||
binding.profileAge.setText(String.valueOf(newAge));
|
||||
binding.profileUtente.setText(newUtente);
|
||||
loadUserData(); // Reload to show new image and data
|
||||
|
||||
Toast.makeText(getContext(), "Dados atualizados com sucesso!", Toast.LENGTH_SHORT).show();
|
||||
dialog.dismiss();
|
||||
dialogImageView = null; // Clear reference
|
||||
});
|
||||
|
||||
btnCancel.setOnClickListener(v -> {
|
||||
dialog.dismiss();
|
||||
dialogImageView = null;
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showChangePasswordDialog() {
|
||||
if (currentUser == null)
|
||||
return;
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
LayoutInflater inflater = requireActivity().getLayoutInflater();
|
||||
View dialogView = inflater.inflate(R.layout.dialog_change_password, null);
|
||||
builder.setView(dialogView);
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
EditText editNewPassword = dialogView.findViewById(R.id.new_password);
|
||||
View btnSave = dialogView.findViewById(R.id.button_save_password);
|
||||
View btnCancel = dialogView.findViewById(R.id.button_cancel_password);
|
||||
|
||||
btnSave.setOnClickListener(v -> {
|
||||
String newPass = editNewPassword.getText().toString();
|
||||
if (newPass.isEmpty()) {
|
||||
Toast.makeText(getContext(), "Insira a nova palavra-passe", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
currentUser.password = newPass;
|
||||
AppDatabase.databaseWriteExecutor.execute(() -> {
|
||||
userDao.insert(currentUser);
|
||||
});
|
||||
|
||||
Toast.makeText(getContext(), "Palavra-passe alterada!", Toast.LENGTH_SHORT).show();
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
btnCancel.setOnClickListener(v -> dialog.dismiss());
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user