Files
NaMesa/app/src/main/java/com/example/pap_teste/ClientDashboardActivity.java
2026-03-18 10:40:19 +00:00

134 lines
6.9 KiB
Java

package com.example.pap_teste;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pap_teste.models.FoodCategory;
import java.util.ArrayList;
import java.util.List;
public class ClientDashboardActivity extends AppCompatActivity {
private String email, displayName, role;
private TextView txtGreeting;
private androidx.activity.result.ActivityResultLauncher<Intent> profileLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_client_dashboard);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.clientRoot), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
txtGreeting = findViewById(R.id.txtClientGreeting);
email = getIntent().getStringExtra(MainActivity.EXTRA_EMAIL);
displayName = getIntent().getStringExtra(MainActivity.EXTRA_DISPLAY_NAME);
role = getIntent().getStringExtra(MainActivity.EXTRA_ROLE);
profileLauncher = registerForActivityResult(
new androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
String updatedName = result.getData()
.getStringExtra(MainActivity.EXTRA_DISPLAY_NAME);
if (updatedName != null) {
displayName = updatedName;
updateGreeting();
}
}
});
updateGreeting();
fetchProfilePicture();
setupCategories();
setupActions();
}
private void updateGreeting() {
txtGreeting.setText(String.format("Olá, %s", displayName != null ? displayName : "convidado"));
}
private void fetchProfilePicture() {
if (email == null) return;
String documentId = email.replace(".", "_").replace("@", "_at_");
com.google.firebase.database.FirebaseDatabase.getInstance().getReference()
.child("users").child(documentId).child("photoUrl")
.get().addOnSuccessListener(snapshot -> {
if (!isDestroyed() && snapshot.exists()) {
String photoUrl = snapshot.getValue(String.class);
if (photoUrl != null && !photoUrl.isEmpty()) {
android.widget.ImageView imgProfile = findViewById(R.id.imgProfile);
com.bumptech.glide.Glide.with(this).load(photoUrl).circleCrop().into(imgProfile);
}
}
});
}
private void setupCategories() {
RecyclerView rv = findViewById(R.id.rvCategories);
List<FoodCategory> cats = new ArrayList<>();
cats.add(new FoodCategory("Carnes", R.drawable.cat_carnes));
cats.add(new FoodCategory("Massas", R.drawable.cat_massas));
cats.add(new FoodCategory("Sushi", R.drawable.cat_sushi));
// Using circle_bg as placeholder for missing images
cats.add(new FoodCategory("Pizzas", R.drawable.circle_bg));
cats.add(new FoodCategory("Sobremesas", R.drawable.circle_bg));
FoodCategoryAdapter adapter = new FoodCategoryAdapter(cats, category -> {
Intent intent = new Intent(this, ExplorarRestaurantesActivity.class);
intent.putExtra("category_filter", category.getName());
startActivity(intent);
});
rv.setAdapter(adapter);
}
private void setupActions() {
findViewById(R.id.cardProfile).setOnClickListener(v -> {
Intent intent = new Intent(this, ProfileDashboardActivity.class);
intent.putExtra(MainActivity.EXTRA_EMAIL, email);
intent.putExtra(MainActivity.EXTRA_DISPLAY_NAME, displayName);
profileLauncher.launch(intent);
});
findViewById(R.id.btnVoltar).setOnClickListener(v -> finish());
findViewById(R.id.btnCheckIn).setOnClickListener(v -> {
boolean hasLocationPermission = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == android.content.pm.PackageManager.PERMISSION_GRANTED;
if (!hasLocationPermission) {
android.widget.Toast.makeText(this, "Dê permissões de localização para aceder ao check-in.", android.widget.Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(this, CheckInAntecipadoActivity.class);
intent.putExtra("restaurant_email", "sabor_arte@restaurante.com");
startActivity(intent);
}
});
findViewById(R.id.btnPartilhar).setOnClickListener(
v -> startActivity(new Intent(this, PartilharReservaActivity.class)));
findViewById(R.id.btnNovaReserva)
.setOnClickListener(v -> startActivity(new Intent(this, NovaReservaActivity.class)));
findViewById(R.id.btnMinhasReservas)
.setOnClickListener(v -> {
Intent intent = new Intent(this, MinhasReservasActivity.class);
intent.putExtra(MainActivity.EXTRA_EMAIL, email);
startActivity(intent);
});
}
}