mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Add favorite Food
This commit is contained in:
Binary file not shown.
@@ -59,4 +59,29 @@ public class Database extends SQLiteAssetHelper {
|
||||
String query = String.format("DELETE FROM OrderDetail");
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
//Favorites
|
||||
public void addToFavorites(String foodId){
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("INSERT INTO Favorites(FoodId) VALUES('%s');", foodId);
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
public void removeFromFavorites(String foodId){
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("DELETE FROM Favorites WHERE FoodId='%s';", foodId);
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
public boolean isFavorite(String foodId){
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("SELECT * FROM Favorites WHERE FoodId='%s'", foodId);
|
||||
Cursor cursor = db.rawQuery(query, null);
|
||||
if(cursor.getCount() <= 0){
|
||||
cursor.close();
|
||||
return false;
|
||||
}
|
||||
cursor.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.google.firebase.database.ValueEventListener;
|
||||
import com.mancj.materialsearchbar.MaterialSearchBar;
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.ViewHolder.FoodViewHolder;
|
||||
@@ -47,21 +48,21 @@ public class FoodList extends AppCompatActivity {
|
||||
List<String> suggestList = new ArrayList<>();
|
||||
MaterialSearchBar materialSearchBar;
|
||||
|
||||
//Favorites
|
||||
Database localDB;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_food_list);
|
||||
|
||||
// calling the action bar
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
// showing the back button in action bar
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
//Firebase
|
||||
database = FirebaseDatabase.getInstance();
|
||||
foodList = database.getReference("Foods");
|
||||
|
||||
//Local DB
|
||||
localDB = new Database(this);
|
||||
|
||||
recyclerView = (RecyclerView) findViewById(R.id.recyler_food);
|
||||
recyclerView.setHasFixedSize(true);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
@@ -70,7 +71,7 @@ public class FoodList extends AppCompatActivity {
|
||||
//Get Intent here
|
||||
if(getIntent() != null)
|
||||
categoryId = getIntent().getStringExtra("CategoryId");
|
||||
categoryName = getIntent().getStringExtra("CategoryName");
|
||||
categoryName = getIntent().getStringExtra("CategoryName");
|
||||
if(!categoryId.isEmpty() && categoryId != null) {
|
||||
if(Common.isConnectedToInternet(getBaseContext()))
|
||||
loadListFood(categoryId);
|
||||
@@ -132,6 +133,12 @@ public class FoodList extends AppCompatActivity {
|
||||
});
|
||||
|
||||
setTitle(categoryName);
|
||||
|
||||
// calling the action bar
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
// showing the back button in action bar
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
private void startSearch(CharSequence text) {
|
||||
@@ -179,28 +186,47 @@ public class FoodList extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void loadListFood(String categoryId){
|
||||
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class, R.layout.food_item,
|
||||
FoodViewHolder.class, foodList.orderByChild("menuId").equalTo(categoryId) // like: Select * from Foods where MenuId = 'categoryId'
|
||||
) {
|
||||
@Override
|
||||
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
|
||||
viewHolder.food_name.setText(model.getName());
|
||||
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
|
||||
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class, R.layout.food_item,
|
||||
FoodViewHolder.class, foodList.orderByChild("menuId").equalTo(categoryId) // like: Select * from Foods where MenuId = 'categoryId'
|
||||
) {
|
||||
@Override
|
||||
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
|
||||
viewHolder.food_name.setText(model.getName());
|
||||
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
|
||||
|
||||
final Food local = model;
|
||||
viewHolder.setItemClickListener(new ItemClickListener() {
|
||||
@Override
|
||||
public void onClick(View view, int position, boolean isLongClick) {
|
||||
//Start Activity
|
||||
Intent foodDetail = new Intent(FoodList.this, FoodDetail.class);
|
||||
foodDetail.putExtra("FoodId", adapter.getRef(position).getKey()); // Send Food Id to new activity
|
||||
startActivity(foodDetail);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
//Add Favorites
|
||||
if(localDB.isFavorite(adapter.getRef(position).getKey()))
|
||||
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_24);
|
||||
|
||||
//Set Adapter
|
||||
//Click to change state of Favorites
|
||||
viewHolder.fav_image.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(!localDB.isFavorite(adapter.getRef(position).getKey())){
|
||||
localDB.addToFavorites(adapter.getRef(position).getKey());
|
||||
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_24);
|
||||
Toast.makeText(FoodList.this, model.getName() + " đã thêm vào danh sách yêu thích", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
localDB.removeFromFavorites(adapter.getRef(position).getKey());
|
||||
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_border_24);
|
||||
Toast.makeText(FoodList.this, model.getName() + " đã xoá khỏi danh sách yêu thích", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
viewHolder.setItemClickListener(new ItemClickListener() {
|
||||
@Override
|
||||
public void onClick(View view, int position, boolean isLongClick) {
|
||||
//Start Activity
|
||||
Intent foodDetail = new Intent(FoodList.this, FoodDetail.class);
|
||||
foodDetail.putExtra("FoodId", adapter.getRef(position).getKey()); // Send Food Id to new activity
|
||||
startActivity(foodDetail);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//Set Adapter
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import com.waterbase.foodify.R;
|
||||
public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
|
||||
public TextView food_name;
|
||||
public ImageView food_image;
|
||||
public ImageView food_image, fav_image;
|
||||
|
||||
private ItemClickListener itemClickListener;
|
||||
|
||||
@@ -26,6 +26,7 @@ public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnCl
|
||||
|
||||
food_name = (TextView) itemView.findViewById(R.id.food_name);
|
||||
food_image = (ImageView) itemView.findViewById(R.id.food_image);
|
||||
fav_image = (ImageView) itemView.findViewById(R.id.fav);
|
||||
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
|
||||
</vector>
|
||||
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"/>
|
||||
</vector>
|
||||
@@ -18,16 +18,36 @@
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerCrop" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_name"
|
||||
|
||||
<LinearLayout
|
||||
android:weightSum="10"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:background="#4f0e0d0e"
|
||||
android:gravity="center"
|
||||
android:text="Name of Food"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="20sp" />
|
||||
>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_name"
|
||||
android:layout_width="0dp"
|
||||
android:layout_weight="9"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#4f0e0d0e"
|
||||
android:gravity="center"
|
||||
android:text="Name of Food"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/fav"
|
||||
android:src="@drawable/ic_baseline_favorite_border_24"
|
||||
android:layout_weight="1"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
</RelativeLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
Reference in New Issue
Block a user