...
This commit is contained in:
@@ -3,10 +3,21 @@ package com.example.pap_teste;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.pap_teste.models.Restaurant;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
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.List;
|
||||
|
||||
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.ViewHolder> {
|
||||
@@ -25,7 +36,7 @@ public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.Vi
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@@ -40,6 +51,44 @@ public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.Vi
|
||||
listener.onRestaurantClick(restaurant);
|
||||
}
|
||||
});
|
||||
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
if (user != null && user.getEmail() != null && restaurant.getEmail() != null) {
|
||||
String encodedUserEmail = user.getEmail().replace(".", "_").replace("@", "_at_");
|
||||
String encodedRestEmail = restaurant.getEmail().replace(".", "_").replace("@", "_at_");
|
||||
DatabaseReference favRef = FirebaseDatabase.getInstance().getReference("users")
|
||||
.child(encodedUserEmail).child("favorites").child(encodedRestEmail);
|
||||
|
||||
favRef.addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||
if (snapshot.exists()) {
|
||||
holder.btnFavorite.setImageResource(android.R.drawable.btn_star_big_on);
|
||||
} else {
|
||||
holder.btnFavorite.setImageResource(android.R.drawable.btn_star_big_off);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@NonNull DatabaseError error) { }
|
||||
});
|
||||
|
||||
holder.btnFavorite.setOnClickListener(v -> {
|
||||
favRef.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||
if (snapshot.exists()) {
|
||||
favRef.removeValue();
|
||||
} else {
|
||||
favRef.setValue(restaurant);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@NonNull DatabaseError error) { }
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,10 +98,12 @@ public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.Vi
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView text1, text2;
|
||||
ImageButton btnFavorite;
|
||||
public ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
text1 = itemView.findViewById(android.R.id.text1);
|
||||
text2 = itemView.findViewById(android.R.id.text2);
|
||||
text1 = itemView.findViewById(R.id.txtRestaurantName);
|
||||
text2 = itemView.findViewById(R.id.txtRestaurantCategory);
|
||||
btnFavorite = itemView.findViewById(R.id.btnFavorite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user