From e582e7ce6b0d0a228b2de6ad9621c5674e6070da Mon Sep 17 00:00:00 2001 From: 230409 <230409@epvc.pt> Date: Tue, 3 Feb 2026 09:59:28 +0000 Subject: [PATCH] ... --- .idea/deviceManager.xml | 13 +++ .../com/example/pap_teste/MainActivity.java | 99 +++++++++---------- 2 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 .idea/deviceManager.xml diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xml new file mode 100644 index 0000000..91f9558 --- /dev/null +++ b/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/pap_teste/MainActivity.java b/app/src/main/java/com/example/pap_teste/MainActivity.java index 5c8c4da..9e6a5e6 100644 --- a/app/src/main/java/com/example/pap_teste/MainActivity.java +++ b/app/src/main/java/com/example/pap_teste/MainActivity.java @@ -19,8 +19,11 @@ import androidx.core.view.WindowInsetsCompat; import com.google.firebase.FirebaseApp; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; -import com.google.firebase.firestore.FirebaseFirestore; -import com.google.firebase.firestore.SetOptions; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.ValueEventListener; import java.util.HashMap; import java.util.Map; @@ -60,7 +63,7 @@ public class MainActivity extends AppCompatActivity { private EditText inputEstablishmentPhone; private boolean hasCreatedAccount; private FirebaseAuth firebaseAuth; - private FirebaseFirestore firestore; + private DatabaseReference databaseReference; @Override protected void onCreate(Bundle savedInstanceState) { @@ -75,7 +78,7 @@ public class MainActivity extends AppCompatActivity { FirebaseApp.initializeApp(this); firebaseAuth = FirebaseAuth.getInstance(); - firestore = FirebaseFirestore.getInstance(); + databaseReference = FirebaseDatabase.getInstance().getReference(); bindViews(); setupTypeToggle(); @@ -266,7 +269,7 @@ public class MainActivity extends AppCompatActivity { } private boolean ensureFirebaseReady() { - boolean ready = firebaseAuth != null && firestore != null; + boolean ready = firebaseAuth != null && databaseReference != null; if (!ready) { Toast.makeText(this, "Ligue-se ao Firebase para continuar.", Toast.LENGTH_SHORT).show(); } @@ -335,61 +338,51 @@ public class MainActivity extends AppCompatActivity { } private void fetchAccountAndNavigate(String email, String fallbackName, String resolvedRole) { - if (firestore == null) { + if (databaseReference == null) { Toast.makeText(this, "Firebase indisponível.", Toast.LENGTH_SHORT).show(); navigateToDashboard(email, fallbackName, resolvedRole); return; } String documentId = buildDocumentId(email); - firestore.collection("users") - .document(documentId) - .get() - .addOnSuccessListener(snapshot -> { - if (snapshot == null || !snapshot.exists()) { - Toast.makeText(this, "Conta sem perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT) - .show(); - navigateToDashboard(email, fallbackName, resolvedRole); - return; - } + databaseReference.child("users").child(documentId).get().addOnCompleteListener(task -> { + if (!task.isSuccessful()) { + android.util.Log.e("LoginError", "Database check failed", task.getException()); + Toast.makeText(this, "Falha ao validar perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT) + .show(); + navigateToDashboard(email, fallbackName, resolvedRole); + return; + } - String accountTypeInFirebase = snapshot.getString("accountType"); - if (accountTypeInFirebase != null - && !accountTypeInFirebase.equalsIgnoreCase(selectedAccountType.name())) { - Toast.makeText(this, "Tipo de conta não corresponde ao registo no Firebase.", - Toast.LENGTH_SHORT).show(); - return; - } + DataSnapshot snapshot = task.getResult(); + if (snapshot == null || !snapshot.exists()) { + Toast.makeText(this, "Conta sem perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT).show(); + navigateToDashboard(email, fallbackName, resolvedRole); + return; + } - String displayNameFromDb = snapshot.getString("displayName"); - String establishmentName = snapshot.getString("establishmentName"); - String ownerName = snapshot.getString("ownerName"); - String roleFromDb = snapshot.getString("role"); + String accountTypeInFirebase = snapshot.child("accountType").getValue(String.class); + if (accountTypeInFirebase != null + && !accountTypeInFirebase.equalsIgnoreCase(selectedAccountType.name())) { + Toast.makeText(this, "Tipo de conta não corresponde ao registo no Firebase.", Toast.LENGTH_SHORT) + .show(); + return; + } - String finalDisplayName = !TextUtils.isEmpty(establishmentName) - ? establishmentName - : !TextUtils.isEmpty(displayNameFromDb) ? displayNameFromDb - : !TextUtils.isEmpty(ownerName) ? ownerName - : fallbackName; + String displayNameFromDb = snapshot.child("displayName").getValue(String.class); + String establishmentName = snapshot.child("establishmentName").getValue(String.class); + String ownerName = snapshot.child("ownerName").getValue(String.class); + String roleFromDb = snapshot.child("role").getValue(String.class); - String finalRole = !TextUtils.isEmpty(roleFromDb) ? roleFromDb : resolvedRole; - navigateToDashboard(email, finalDisplayName, finalRole); - }) - .addOnFailureListener(e -> { - android.util.Log.e("LoginError", "Firestore check failed", e); - if (e instanceof com.google.firebase.firestore.FirebaseFirestoreException) { - com.google.firebase.firestore.FirebaseFirestoreException fe = (com.google.firebase.firestore.FirebaseFirestoreException) e; - if (fe.getCode() == com.google.firebase.firestore.FirebaseFirestoreException.Code.UNAVAILABLE || - fe.getCode() == com.google.firebase.firestore.FirebaseFirestoreException.Code.FAILED_PRECONDITION) { - Toast.makeText(this, "Sem internet. A entrar em modo offline...", Toast.LENGTH_LONG).show(); - // Não fazemos return, deixamos cair no navigateToDashboard abaixo - } - } - Toast.makeText(this, "Falha ao validar perfil na cloud. A entrar em modo básico.", - Toast.LENGTH_SHORT) - .show(); - navigateToDashboard(email, fallbackName, resolvedRole); - }); + String finalDisplayName = !TextUtils.isEmpty(establishmentName) + ? establishmentName + : !TextUtils.isEmpty(displayNameFromDb) ? displayNameFromDb + : !TextUtils.isEmpty(ownerName) ? ownerName + : fallbackName; + + String finalRole = !TextUtils.isEmpty(roleFromDb) ? roleFromDb : resolvedRole; + navigateToDashboard(email, finalDisplayName, finalRole); + }); } private void navigateToDashboard(String email, String displayName, String role) { @@ -415,7 +408,7 @@ public class MainActivity extends AppCompatActivity { String establishmentPhone, String uid, Runnable onSuccess) { - if (firestore == null) { + if (databaseReference == null) { Toast.makeText(this, "Firebase indisponível.", Toast.LENGTH_SHORT).show(); return; } @@ -440,9 +433,7 @@ public class MainActivity extends AppCompatActivity { payload.put("establishmentPhone", establishmentPhone); } - firestore.collection("users") - .document(documentId) - .set(payload, SetOptions.merge()) + databaseReference.child("users").child(documentId).updateChildren(payload) .addOnSuccessListener(unused -> { Toast.makeText(this, "Conta guardada na cloud.", Toast.LENGTH_SHORT).show(); if (onSuccess != null) {