...
This commit is contained in:
@@ -21,6 +21,7 @@ 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 com.google.firebase.auth.FirebaseAuth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -86,9 +87,12 @@ public class GerirMesasActivity extends AppCompatActivity {
|
||||
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||
mesas.clear();
|
||||
adapter.clear();
|
||||
String currentUserEmail = FirebaseAuth.getInstance().getCurrentUser() != null
|
||||
? FirebaseAuth.getInstance().getCurrentUser().getEmail()
|
||||
: "";
|
||||
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
|
||||
Mesa mesa = postSnapshot.getValue(Mesa.class);
|
||||
if (mesa != null) {
|
||||
if (mesa != null && (mesa.getRestauranteEmail() == null || mesa.getRestauranteEmail().equals(currentUserEmail))) {
|
||||
// Ensure the ID is set from the snapshot key if it's missing in the value
|
||||
mesa.setId(postSnapshot.getKey());
|
||||
mesas.add(mesa);
|
||||
@@ -184,10 +188,13 @@ public class GerirMesasActivity extends AppCompatActivity {
|
||||
|
||||
Mesa existente = findMesa(numero);
|
||||
String mesaId;
|
||||
String currentUserEmail = FirebaseAuth.getInstance().getCurrentUser() != null
|
||||
? FirebaseAuth.getInstance().getCurrentUser().getEmail()
|
||||
: "";
|
||||
|
||||
if (existente == null) {
|
||||
mesaId = mDatabase.push().getKey();
|
||||
Mesa novaMesa = new Mesa(mesaId, numero, capacidade, estado);
|
||||
Mesa novaMesa = new Mesa(mesaId, numero, capacidade, estado, currentUserEmail);
|
||||
if (mesaId != null) {
|
||||
mDatabase.child(mesaId).setValue(novaMesa);
|
||||
}
|
||||
|
||||
@@ -164,10 +164,11 @@ public class NovaReservaActivity extends AppCompatActivity {
|
||||
|
||||
private void saveReservation() {
|
||||
android.widget.EditText etPartySize = findViewById(R.id.etPartySize);
|
||||
int partySize = 0;
|
||||
int val = 0;
|
||||
try {
|
||||
partySize = Integer.parseInt(etPartySize.getText().toString());
|
||||
val = Integer.parseInt(etPartySize.getText().toString());
|
||||
} catch (Exception e) {}
|
||||
final int partySize = val;
|
||||
|
||||
if (selectedDate == null || selectedTime == null || partySize == 0) {
|
||||
android.widget.Toast.makeText(this, "Por favor, selecione data, hora e número de pessoas.",
|
||||
@@ -175,6 +176,86 @@ public class NovaReservaActivity extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
String restEmail = selectedRestaurant.getEmail();
|
||||
|
||||
com.google.firebase.database.DatabaseReference mesasRef = com.google.firebase.database.FirebaseDatabase.getInstance().getReference("Mesas");
|
||||
|
||||
mesasRef.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@androidx.annotation.NonNull com.google.firebase.database.DataSnapshot snapshot) {
|
||||
int totalMesas = 0;
|
||||
for (com.google.firebase.database.DataSnapshot ds : snapshot.getChildren()) {
|
||||
com.example.pap_teste.models.Mesa m = ds.getValue(com.example.pap_teste.models.Mesa.class);
|
||||
if (m != null && restEmail.equals(m.getRestauranteEmail())) {
|
||||
totalMesas++;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalMesas == 0) {
|
||||
proceedWithReservation(partySize);
|
||||
return;
|
||||
}
|
||||
|
||||
checkReservationsAndSave(totalMesas, partySize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@androidx.annotation.NonNull com.google.firebase.database.DatabaseError error) {
|
||||
proceedWithReservation(partySize);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkReservationsAndSave(int totalMesas, final int partySize) {
|
||||
String restEmail = selectedRestaurant.getEmail();
|
||||
com.google.firebase.database.DatabaseReference reservasRef = com.google.firebase.database.FirebaseDatabase.getInstance().getReference("reservas");
|
||||
|
||||
reservasRef.orderByChild("restauranteEmail").equalTo(restEmail).addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@androidx.annotation.NonNull com.google.firebase.database.DataSnapshot snapshot) {
|
||||
int ocupadas = 0;
|
||||
java.util.Map<String, Integer> ocupacaoPorHora = new java.util.HashMap<>();
|
||||
|
||||
for (com.google.firebase.database.DataSnapshot ds : snapshot.getChildren()) {
|
||||
com.example.pap_teste.models.Reserva r = ds.getValue(com.example.pap_teste.models.Reserva.class);
|
||||
if (r != null && selectedDate.equals(r.getData()) && !"Cancelada".equals(r.getEstado()) && !"Recusada".equals(r.getEstado())) {
|
||||
int count = ocupacaoPorHora.getOrDefault(r.getHora(), 0) + 1;
|
||||
ocupacaoPorHora.put(r.getHora(), count);
|
||||
if (selectedTime.equals(r.getHora())) {
|
||||
ocupadas++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ocupadas >= totalMesas) {
|
||||
String sugestao = "";
|
||||
String[] horasComuns = {"12:00", "12:30", "13:00", "13:30", "14:00", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00"};
|
||||
for (String h : horasComuns) {
|
||||
if (ocupacaoPorHora.getOrDefault(h, 0) < totalMesas && !h.equals(selectedTime)) {
|
||||
sugestao = h;
|
||||
break; // Encontramos a primeira sugestão livre
|
||||
}
|
||||
}
|
||||
String msg = "Não há mesas disponíveis para as " + selectedTime + ".";
|
||||
if (!sugestao.isEmpty()) {
|
||||
msg += " Sugestão: tente reservar para as " + sugestao + ".";
|
||||
} else {
|
||||
msg += " Tente para outro dia.";
|
||||
}
|
||||
android.widget.Toast.makeText(NovaReservaActivity.this, msg, android.widget.Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
proceedWithReservation(partySize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@androidx.annotation.NonNull com.google.firebase.database.DatabaseError error) {
|
||||
proceedWithReservation(partySize);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void proceedWithReservation(int partySize) {
|
||||
String userEmail = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser() != null
|
||||
? com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser().getEmail()
|
||||
: "cliente@teste.com";
|
||||
@@ -196,11 +277,11 @@ public class NovaReservaActivity extends AppCompatActivity {
|
||||
ref.child(id).setValue(reserva).addOnCompleteListener(task -> {
|
||||
if (task.isSuccessful()) {
|
||||
android.widget.Toast
|
||||
.makeText(this, "Reserva solicitada com sucesso!", android.widget.Toast.LENGTH_SHORT)
|
||||
.makeText(NovaReservaActivity.this, "Reserva solicitada com sucesso!", android.widget.Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
finish();
|
||||
} else {
|
||||
android.widget.Toast.makeText(this, "Erro ao salvar reserva.", android.widget.Toast.LENGTH_SHORT)
|
||||
android.widget.Toast.makeText(NovaReservaActivity.this, "Erro ao salvar reserva.", android.widget.Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,16 +5,18 @@ public class Mesa {
|
||||
private int numero;
|
||||
private int capacidade;
|
||||
private String estado;
|
||||
private String restauranteEmail;
|
||||
|
||||
public Mesa() {
|
||||
// Default constructor required for calls to DataSnapshot.getValue(Mesa.class)
|
||||
}
|
||||
|
||||
public Mesa(String id, int numero, int capacidade, String estado) {
|
||||
public Mesa(String id, int numero, int capacidade, String estado, String restauranteEmail) {
|
||||
this.id = id;
|
||||
this.numero = numero;
|
||||
this.capacidade = capacidade;
|
||||
this.estado = estado;
|
||||
this.restauranteEmail = restauranteEmail;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
@@ -49,6 +51,14 @@ public class Mesa {
|
||||
this.estado = estado;
|
||||
}
|
||||
|
||||
public String getRestauranteEmail() {
|
||||
return restauranteEmail;
|
||||
}
|
||||
|
||||
public void setRestauranteEmail(String restauranteEmail) {
|
||||
this.restauranteEmail = restauranteEmail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Mesa " + numero;
|
||||
|
||||
9
app/src/main/res/drawable/ic_visibility.xml
Normal file
9
app/src/main/res/drawable/ic_visibility.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#757575"
|
||||
android:pathData="M12,4.5C7,4.5 2.73,7.61 1,12c1.73,4.39 5.99,7.5 11,7.5s9.27,-3.11 11,-7.5c-1.73,-4.39 -6,-7.5 -11,-7.5zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5zM12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3z"/>
|
||||
</vector>
|
||||
9
app/src/main/res/drawable/ic_visibility_off.xml
Normal file
9
app/src/main/res/drawable/ic_visibility_off.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#757575"
|
||||
android:pathData="M12,7c2.76,0 5,2.24 5,5 0,0.65 -0.13,1.26 -0.36,1.83l2.92,2.92c1.51,-1.26 2.7,-2.89 3.43,-4.75 -1.73,-4.39 -6,-7.5 -11,-7.5 -1.4,0 -2.74,0.25 -3.98,0.7l2.16,2.16C10.74,7.13 11.35,7 12,7zM2,4.27l2.28,2.28 0.46,0.46C3.08,8.3 1.78,10.02 1,12c1.73,4.39 5.99,7.5 11,7.5 1.55,0 3.03,-0.3 4.38,-0.84l0.42,0.42L19.73,22 21,20.73 3.27,3 2,4.27zM7.53,9.8l1.55,1.55c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.66 1.34,3 3,3 0.22,0 0.44,-0.03 0.65,-0.08l1.55,1.55c-0.67,0.33 -1.41,0.53 -2.2,0.53 -2.76,0 -5,-2.24 -5,-5 0,-0.79 0.2,-1.53 0.53,-2.2zM11.84,9.02l3.15,3.15 0.02,-0.16c0,-1.66 -1.34,-3 -3,-3l-0.17,0.01z"/>
|
||||
</vector>
|
||||
Reference in New Issue
Block a user