This commit is contained in:
2026-02-03 09:59:28 +00:00
parent 2e7e22c89a
commit e582e7ce6b
2 changed files with 58 additions and 54 deletions

13
.idea/deviceManager.xml generated Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DeviceTable">
<option name="columnSorters">
<list>
<ColumnSorterState>
<option name="column" value="Name" />
<option name="order" value="ASCENDING" />
</ColumnSorterState>
</list>
</option>
</component>
</project>

View File

@@ -19,8 +19,11 @@ import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser; import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.database.DataSnapshot;
import com.google.firebase.firestore.SetOptions; 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.HashMap;
import java.util.Map; import java.util.Map;
@@ -60,7 +63,7 @@ public class MainActivity extends AppCompatActivity {
private EditText inputEstablishmentPhone; private EditText inputEstablishmentPhone;
private boolean hasCreatedAccount; private boolean hasCreatedAccount;
private FirebaseAuth firebaseAuth; private FirebaseAuth firebaseAuth;
private FirebaseFirestore firestore; private DatabaseReference databaseReference;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -75,7 +78,7 @@ public class MainActivity extends AppCompatActivity {
FirebaseApp.initializeApp(this); FirebaseApp.initializeApp(this);
firebaseAuth = FirebaseAuth.getInstance(); firebaseAuth = FirebaseAuth.getInstance();
firestore = FirebaseFirestore.getInstance(); databaseReference = FirebaseDatabase.getInstance().getReference();
bindViews(); bindViews();
setupTypeToggle(); setupTypeToggle();
@@ -266,7 +269,7 @@ public class MainActivity extends AppCompatActivity {
} }
private boolean ensureFirebaseReady() { private boolean ensureFirebaseReady() {
boolean ready = firebaseAuth != null && firestore != null; boolean ready = firebaseAuth != null && databaseReference != null;
if (!ready) { if (!ready) {
Toast.makeText(this, "Ligue-se ao Firebase para continuar.", Toast.LENGTH_SHORT).show(); 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) { 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(); Toast.makeText(this, "Firebase indisponível.", Toast.LENGTH_SHORT).show();
navigateToDashboard(email, fallbackName, resolvedRole); navigateToDashboard(email, fallbackName, resolvedRole);
return; return;
} }
String documentId = buildDocumentId(email); String documentId = buildDocumentId(email);
firestore.collection("users") databaseReference.child("users").child(documentId).get().addOnCompleteListener(task -> {
.document(documentId) if (!task.isSuccessful()) {
.get() android.util.Log.e("LoginError", "Database check failed", task.getException());
.addOnSuccessListener(snapshot -> { Toast.makeText(this, "Falha ao validar perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT)
if (snapshot == null || !snapshot.exists()) { .show();
Toast.makeText(this, "Conta sem perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT) navigateToDashboard(email, fallbackName, resolvedRole);
.show(); return;
navigateToDashboard(email, fallbackName, resolvedRole); }
return;
}
String accountTypeInFirebase = snapshot.getString("accountType"); DataSnapshot snapshot = task.getResult();
if (accountTypeInFirebase != null if (snapshot == null || !snapshot.exists()) {
&& !accountTypeInFirebase.equalsIgnoreCase(selectedAccountType.name())) { Toast.makeText(this, "Conta sem perfil na cloud. A entrar em modo básico.", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Tipo de conta não corresponde ao registo no Firebase.", navigateToDashboard(email, fallbackName, resolvedRole);
Toast.LENGTH_SHORT).show(); return;
return; }
}
String displayNameFromDb = snapshot.getString("displayName"); String accountTypeInFirebase = snapshot.child("accountType").getValue(String.class);
String establishmentName = snapshot.getString("establishmentName"); if (accountTypeInFirebase != null
String ownerName = snapshot.getString("ownerName"); && !accountTypeInFirebase.equalsIgnoreCase(selectedAccountType.name())) {
String roleFromDb = snapshot.getString("role"); Toast.makeText(this, "Tipo de conta não corresponde ao registo no Firebase.", Toast.LENGTH_SHORT)
.show();
return;
}
String finalDisplayName = !TextUtils.isEmpty(establishmentName) String displayNameFromDb = snapshot.child("displayName").getValue(String.class);
? establishmentName String establishmentName = snapshot.child("establishmentName").getValue(String.class);
: !TextUtils.isEmpty(displayNameFromDb) ? displayNameFromDb String ownerName = snapshot.child("ownerName").getValue(String.class);
: !TextUtils.isEmpty(ownerName) ? ownerName String roleFromDb = snapshot.child("role").getValue(String.class);
: fallbackName;
String finalRole = !TextUtils.isEmpty(roleFromDb) ? roleFromDb : resolvedRole; String finalDisplayName = !TextUtils.isEmpty(establishmentName)
navigateToDashboard(email, finalDisplayName, finalRole); ? establishmentName
}) : !TextUtils.isEmpty(displayNameFromDb) ? displayNameFromDb
.addOnFailureListener(e -> { : !TextUtils.isEmpty(ownerName) ? ownerName
android.util.Log.e("LoginError", "Firestore check failed", e); : fallbackName;
if (e instanceof com.google.firebase.firestore.FirebaseFirestoreException) {
com.google.firebase.firestore.FirebaseFirestoreException fe = (com.google.firebase.firestore.FirebaseFirestoreException) e; String finalRole = !TextUtils.isEmpty(roleFromDb) ? roleFromDb : resolvedRole;
if (fe.getCode() == com.google.firebase.firestore.FirebaseFirestoreException.Code.UNAVAILABLE || navigateToDashboard(email, finalDisplayName, finalRole);
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);
});
} }
private void navigateToDashboard(String email, String displayName, String role) { private void navigateToDashboard(String email, String displayName, String role) {
@@ -415,7 +408,7 @@ public class MainActivity extends AppCompatActivity {
String establishmentPhone, String establishmentPhone,
String uid, String uid,
Runnable onSuccess) { Runnable onSuccess) {
if (firestore == null) { if (databaseReference == null) {
Toast.makeText(this, "Firebase indisponível.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Firebase indisponível.", Toast.LENGTH_SHORT).show();
return; return;
} }
@@ -440,9 +433,7 @@ public class MainActivity extends AppCompatActivity {
payload.put("establishmentPhone", establishmentPhone); payload.put("establishmentPhone", establishmentPhone);
} }
firestore.collection("users") databaseReference.child("users").child(documentId).updateChildren(payload)
.document(documentId)
.set(payload, SetOptions.merge())
.addOnSuccessListener(unused -> { .addOnSuccessListener(unused -> {
Toast.makeText(this, "Conta guardada na cloud.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Conta guardada na cloud.", Toast.LENGTH_SHORT).show();
if (onSuccess != null) { if (onSuccess != null) {