mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Add show list favorites food
This commit is contained in:
@@ -20,6 +20,9 @@
|
||||
android:theme="@style/Theme.Foodify"
|
||||
tools:replace="android:theme"
|
||||
tools:targetApi="31">
|
||||
<activity
|
||||
android:name=".FavoritesActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".SearchActivity"
|
||||
android:exported="false"
|
||||
|
||||
Binary file not shown.
@@ -6,6 +6,7 @@ import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteQueryBuilder;
|
||||
|
||||
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
|
||||
import com.waterbase.foodify.Model.Favorites;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -114,12 +115,46 @@ public class Database extends SQLiteAssetHelper {
|
||||
}
|
||||
|
||||
//Favorites
|
||||
public void addToFavorites(String foodId, String userPhone) {
|
||||
public void addToFavorites(Favorites food) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("INSERT INTO Favorites(FoodId,UserPhone) VALUES('%s', '%s');", foodId, userPhone);
|
||||
String query = String.format("INSERT INTO Favorites" +
|
||||
"(FoodId,FoodName,FoodPrice,FoodMenuId,FoodImage,FoodDiscount,FoodDescription,UserPhone) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
|
||||
food.getFoodId(), food.getFoodName(), food.getFoodPrice(), food.getFoodMenuId(), food.getFoodImage(),
|
||||
food.getFoodDiscount(), food.getFoodDescription(), food.getUserPhone());
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
public List<Favorites> getAllFavorites(String userPhone)
|
||||
{
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
||||
|
||||
String[] sqlSelect = {"UserPhone", "FoodId", "FoodName", "FoodPrice", "FoodMenuId", "FoodImage", "FoodDiscount", "FoodDescription"};
|
||||
String sqlTable = "Favorites";
|
||||
|
||||
qb.setTables(sqlTable);
|
||||
Cursor c = qb.query(db, sqlSelect, "UserPhone=?", new String[]{userPhone}, null, null, null);
|
||||
|
||||
final List<Favorites> result = new ArrayList<>();
|
||||
if(c.moveToFirst())
|
||||
{
|
||||
do{
|
||||
result.add(new Favorites(
|
||||
c.getString(c.getColumnIndexOrThrow("FoodId")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodName")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodPrice")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodMenuId")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodImage")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodDiscount")),
|
||||
c.getString(c.getColumnIndexOrThrow("FoodDescription")),
|
||||
c.getString(c.getColumnIndexOrThrow("UserPhone"))
|
||||
));
|
||||
}while (c.moveToNext());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void removeFromFavorites(String foodId, String userPhone) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("DELETE FROM Favorites WHERE FoodId='%s' and UserPhone='%s';", foodId, userPhone);
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.waterbase.foodify;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.recyclerview.widget.ItemTouchHelper;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.animation.AnimationUtils;
|
||||
import android.view.animation.LayoutAnimationController;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Helper.RecyclerItemTouchHelper;
|
||||
import com.waterbase.foodify.Interface.RecyclerItemTouchHelperListener;
|
||||
import com.waterbase.foodify.Model.Favorites;
|
||||
import com.waterbase.foodify.ViewHolder.FavoritesAdapter;
|
||||
import com.waterbase.foodify.ViewHolder.FavoritesViewHolder;
|
||||
|
||||
public class FavoritesActivity extends AppCompatActivity implements RecyclerItemTouchHelperListener {
|
||||
|
||||
RecyclerView recyclerView;
|
||||
RecyclerView.LayoutManager layoutManager;
|
||||
|
||||
FavoritesAdapter adapter;
|
||||
|
||||
RelativeLayout rootLayout;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_favorites);
|
||||
|
||||
rootLayout = findViewById(R.id.rootLayout);
|
||||
|
||||
recyclerView = findViewById(R.id.recycler_fav);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recyclerView.setLayoutManager(layoutManager);
|
||||
|
||||
//Swipe to delete
|
||||
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
|
||||
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
|
||||
|
||||
loadFavorites();
|
||||
}
|
||||
|
||||
private void loadFavorites() {
|
||||
adapter = new FavoritesAdapter(this, new Database(this).getAllFavorites(Common.currentUser.getPhone()));
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
|
||||
if(viewHolder instanceof FavoritesViewHolder)
|
||||
{
|
||||
String name = ((FavoritesAdapter)recyclerView.getAdapter()).getItem(position).getFoodName();
|
||||
|
||||
Favorites deleteItem = ((FavoritesAdapter)recyclerView.getAdapter()).getItem(viewHolder.getAdapterPosition());
|
||||
int deleteIndex = viewHolder.getAdapterPosition();
|
||||
|
||||
adapter.removeItem(viewHolder.getAdapterPosition());
|
||||
new Database(getBaseContext()).removeFromFavorites(deleteItem.getFoodId(), Common.currentUser.getPhone());
|
||||
|
||||
//Make Snackbar
|
||||
Snackbar snackbar = Snackbar.make(rootLayout, name + " đã xoá khỏi danh sách yêu thích của bạn!", Snackbar.LENGTH_LONG);
|
||||
snackbar.setAction("Hoàn tác", new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
adapter.restoreItem(deleteItem, deleteIndex);
|
||||
new Database(getBaseContext()).addToFavorites(deleteItem);
|
||||
// updateTotalPrice();
|
||||
|
||||
}
|
||||
});
|
||||
snackbar.setActionTextColor(Color.YELLOW);
|
||||
snackbar.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ 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.Favorites;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.ViewHolder.FoodViewHolder;
|
||||
@@ -189,7 +190,7 @@ public class FoodList extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
recyclerView = (RecyclerView) findViewById(R.id.recyler_food);
|
||||
recyclerView = (RecyclerView) findViewById(R.id.recycler_food);
|
||||
recyclerView.setHasFixedSize(true);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recyclerView.setLayoutManager(layoutManager);
|
||||
@@ -342,8 +343,20 @@ public class FoodList extends AppCompatActivity {
|
||||
viewHolder.fav_image.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
Favorites favorites = new Favorites();
|
||||
|
||||
favorites.setFoodId(adapter.getRef(viewHolder.getAdapterPosition()).getKey());
|
||||
favorites.setFoodName(model.getName());
|
||||
favorites.setFoodDescription(model.getDescription());
|
||||
favorites.setFoodDiscount(model.getDiscount());
|
||||
favorites.setFoodImage(model.getImage());
|
||||
favorites.setFoodMenuId(model.getMenuId());
|
||||
favorites.setUserPhone(Common.currentUser.getPhone());
|
||||
favorites.setFoodPrice(model.getPrice());
|
||||
|
||||
if (!localDB.isFavorite(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone())) {
|
||||
localDB.addToFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone());
|
||||
localDB.addToFavorites(favorites);
|
||||
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 {
|
||||
|
||||
+34
-7
@@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.waterbase.foodify.Interface.RecyclerItemTouchHelperListener;
|
||||
import com.waterbase.foodify.ViewHolder.CartViewHolder;
|
||||
import com.waterbase.foodify.ViewHolder.FavoritesViewHolder;
|
||||
|
||||
public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
|
||||
|
||||
@@ -27,7 +28,7 @@ public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
|
||||
|
||||
@Override
|
||||
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
|
||||
if(listener != null)
|
||||
if (listener != null)
|
||||
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
|
||||
}
|
||||
|
||||
@@ -38,28 +39,54 @@ public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
|
||||
|
||||
@Override
|
||||
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
|
||||
View foregroundView = ((CartViewHolder)viewHolder).view_foreground;
|
||||
if (viewHolder instanceof CartViewHolder) {
|
||||
View foregroundView = ((CartViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().clearView(foregroundView);
|
||||
} else if(viewHolder instanceof FavoritesViewHolder)
|
||||
{
|
||||
View foregroundView = ((FavoritesViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().clearView(foregroundView);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
|
||||
View foregroundView = ((CartViewHolder)viewHolder).view_foreground;
|
||||
if (viewHolder instanceof CartViewHolder) {
|
||||
View foregroundView = ((CartViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
|
||||
} else if(viewHolder instanceof FavoritesViewHolder)
|
||||
{
|
||||
View foregroundView = ((FavoritesViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
|
||||
if(viewHolder != null)
|
||||
{
|
||||
View foregroundView = ((CartViewHolder)viewHolder).view_foreground;
|
||||
if (viewHolder != null) {
|
||||
if (viewHolder instanceof CartViewHolder) {
|
||||
View foregroundView = ((CartViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onSelected(foregroundView);
|
||||
} else if(viewHolder instanceof FavoritesViewHolder)
|
||||
{
|
||||
View foregroundView = ((FavoritesViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onSelected(foregroundView);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
|
||||
View foregroundView = ((CartViewHolder)viewHolder).view_foreground;
|
||||
if (viewHolder instanceof CartViewHolder) {
|
||||
View foregroundView = ((CartViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
|
||||
} else if(viewHolder instanceof FavoritesViewHolder)
|
||||
{
|
||||
View foregroundView = ((FavoritesViewHolder) viewHolder).view_foreground;
|
||||
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,6 +327,8 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
showHomeAddressDialog();
|
||||
} else if (id == R.id.nav_setting) {
|
||||
showSettingDialog();
|
||||
} else if (id == R.id.nav_favorites) {
|
||||
startActivity(new Intent(Home.this, FavoritesActivity.class));
|
||||
}
|
||||
else if (id == R.id.nav_log_out) {
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Favorites {
|
||||
|
||||
private String FoodId, FoodName, FoodPrice, FoodMenuId, FoodImage, FoodDiscount, FoodDescription, UserPhone;
|
||||
|
||||
public Favorites() {
|
||||
}
|
||||
|
||||
public Favorites(String foodId, String foodName, String foodPrice, String foodMenuId, String foodImage, String foodDiscount, String foodDescription, String userPhone) {
|
||||
FoodId = foodId;
|
||||
FoodName = foodName;
|
||||
FoodPrice = foodPrice;
|
||||
FoodMenuId = foodMenuId;
|
||||
FoodImage = foodImage;
|
||||
FoodDiscount = foodDiscount;
|
||||
FoodDescription = foodDescription;
|
||||
UserPhone = userPhone;
|
||||
}
|
||||
|
||||
public String getFoodId() {
|
||||
return FoodId;
|
||||
}
|
||||
|
||||
public void setFoodId(String foodId) {
|
||||
FoodId = foodId;
|
||||
}
|
||||
|
||||
public String getFoodName() {
|
||||
return FoodName;
|
||||
}
|
||||
|
||||
public void setFoodName(String foodName) {
|
||||
FoodName = foodName;
|
||||
}
|
||||
|
||||
public String getFoodPrice() {
|
||||
return FoodPrice;
|
||||
}
|
||||
|
||||
public void setFoodPrice(String foodPrice) {
|
||||
FoodPrice = foodPrice;
|
||||
}
|
||||
|
||||
public String getFoodMenuId() {
|
||||
return FoodMenuId;
|
||||
}
|
||||
|
||||
public void setFoodMenuId(String foodMenuId) {
|
||||
FoodMenuId = foodMenuId;
|
||||
}
|
||||
|
||||
public String getFoodImage() {
|
||||
return FoodImage;
|
||||
}
|
||||
|
||||
public void setFoodImage(String foodImage) {
|
||||
FoodImage = foodImage;
|
||||
}
|
||||
|
||||
public String getFoodDiscount() {
|
||||
return FoodDiscount;
|
||||
}
|
||||
|
||||
public void setFoodDiscount(String foodDiscount) {
|
||||
FoodDiscount = foodDiscount;
|
||||
}
|
||||
|
||||
public String getFoodDescription() {
|
||||
return FoodDescription;
|
||||
}
|
||||
|
||||
public void setFoodDescription(String foodDescription) {
|
||||
FoodDescription = foodDescription;
|
||||
}
|
||||
|
||||
public String getUserPhone() {
|
||||
return UserPhone;
|
||||
}
|
||||
|
||||
public void setUserPhone(String userPhone) {
|
||||
UserPhone = userPhone;
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ 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.Favorites;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.ViewHolder.FoodViewHolder;
|
||||
@@ -123,11 +124,11 @@ public class SearchActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
//Load add foods
|
||||
loadAddFood();
|
||||
//Load all foods
|
||||
loadAllFood();
|
||||
}
|
||||
|
||||
private void loadAddFood() {
|
||||
private void loadAllFood() {
|
||||
//Create query by category Id
|
||||
Query searchByName = foodList;
|
||||
//Create Options with query
|
||||
@@ -191,8 +192,20 @@ public class SearchActivity extends AppCompatActivity {
|
||||
viewHolder.fav_image.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
Favorites favorites = new Favorites();
|
||||
|
||||
favorites.setFoodId(adapter.getRef(position).getKey());
|
||||
favorites.setFoodName(model.getName());
|
||||
favorites.setFoodDescription(model.getDescription());
|
||||
favorites.setFoodDiscount(model.getDiscount());
|
||||
favorites.setFoodImage(model.getImage());
|
||||
favorites.setFoodMenuId(model.getMenuId());
|
||||
favorites.setUserPhone(Common.currentUser.getPhone());
|
||||
favorites.setFoodPrice(model.getPrice());
|
||||
|
||||
if (!localDB.isFavorite(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone())) {
|
||||
localDB.addToFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone());
|
||||
localDB.addToFavorites(favorites);
|
||||
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_24);
|
||||
Toast.makeText(SearchActivity.this, model.getName() + " đã thêm vào danh sách yêu thích", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.waterbase.foodify.ViewHolder;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.style.StrikethroughSpan;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.FoodDetail;
|
||||
import com.waterbase.foodify.FoodList;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Favorites;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FavoritesAdapter extends RecyclerView.Adapter<FavoritesViewHolder> {
|
||||
|
||||
private Context context;
|
||||
private List<Favorites> favoritesList;
|
||||
|
||||
public FavoritesAdapter(Context context, List<Favorites> favoritesList) {
|
||||
this.context = context;
|
||||
this.favoritesList = favoritesList;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public FavoritesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View itemView = LayoutInflater.from(context)
|
||||
.inflate(R.layout.favorite_item, parent, false);
|
||||
return new FavoritesViewHolder(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull FavoritesViewHolder viewHolder, int position) {
|
||||
Favorites model = favoritesList.get(position);
|
||||
|
||||
viewHolder.food_name.setText(model.getFoodName());
|
||||
|
||||
if (Integer.parseInt(model.getFoodDiscount()) > 0) {
|
||||
String foodPrice = model.getFoodPrice() + "đ";
|
||||
long newFoodPrice = Long.parseLong(model.getFoodPrice()) - Long.parseLong(model.getFoodPrice()) * Long.parseLong(model.getFoodDiscount()) / 100;
|
||||
SpannableStringBuilder spnBuilder = new SpannableStringBuilder(foodPrice);
|
||||
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
|
||||
spnBuilder.setSpan(strikethroughSpan, 0, foodPrice.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
viewHolder.food_price.setText(spnBuilder);
|
||||
viewHolder.newPrice.setText(newFoodPrice + "đ");
|
||||
viewHolder.discount.setText("- " + model.getFoodDiscount() + "%");
|
||||
} else {
|
||||
viewHolder.food_price.setText(String.format("%s đ", model.getFoodPrice()));
|
||||
viewHolder.discount.setVisibility(View.GONE);
|
||||
viewHolder.newPrice.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
Picasso.with(context).load(model.getFoodImage()).into(viewHolder.food_image);
|
||||
|
||||
//Quick Cart
|
||||
|
||||
viewHolder.quick_cart.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
boolean ifExists = new Database(context).checkFoodExists(model.getFoodId(), Common.currentUser.getPhone());
|
||||
if (!ifExists) {
|
||||
new Database(context).addToCart(new Order(
|
||||
Common.currentUser.getPhone(),
|
||||
model.getFoodId(),
|
||||
model.getFoodName(),
|
||||
"1",
|
||||
model.getFoodPrice(),
|
||||
model.getFoodDiscount(),
|
||||
model.getFoodImage()
|
||||
));
|
||||
} else {
|
||||
new Database(context).increaseCart(Common.currentUser.getPhone(), model.getFoodId());
|
||||
}
|
||||
|
||||
Toast.makeText(context, "Đã thêm vào giỏ hàng!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
|
||||
viewHolder.setItemClickListener(new ItemClickListener() {
|
||||
@Override
|
||||
public void onClick(View view, int position, boolean isLongClick) {
|
||||
//Start Activity
|
||||
Intent foodDetail = new Intent(context, FoodDetail.class);
|
||||
foodDetail.putExtra("FoodId", model.getFoodId()); // Send Food Id to new activity
|
||||
context.startActivity(foodDetail);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return favoritesList.size();
|
||||
}
|
||||
|
||||
public void removeItem(int position)
|
||||
{
|
||||
favoritesList.remove(position);
|
||||
notifyItemRemoved(position);
|
||||
}
|
||||
|
||||
public void restoreItem(Favorites item, int position)
|
||||
{
|
||||
favoritesList.add(position, item);
|
||||
notifyItemInserted(position);
|
||||
}
|
||||
|
||||
public Favorites getItem(int position)
|
||||
{
|
||||
return favoritesList.get(position);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.waterbase.foodify.ViewHolder;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
public class FavoritesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
|
||||
public TextView food_name, food_price, discount, newPrice;
|
||||
public ImageView food_image, fav_image, quick_cart;
|
||||
|
||||
public RelativeLayout view_background;
|
||||
public LinearLayout view_foreground;
|
||||
|
||||
private ItemClickListener itemClickListener;
|
||||
|
||||
public void setItemClickListener(ItemClickListener itemClickListener) {
|
||||
this.itemClickListener = itemClickListener;
|
||||
}
|
||||
|
||||
public FavoritesViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
|
||||
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);
|
||||
food_price = (TextView) itemView.findViewById(R.id.food_price);
|
||||
discount = (TextView) itemView.findViewById(R.id.discount);
|
||||
newPrice = (TextView) itemView.findViewById(R.id.new_Price);
|
||||
quick_cart = (ImageView) itemView.findViewById(R.id.btn_quick_cart);
|
||||
|
||||
view_background = (RelativeLayout) itemView.findViewById(R.id.view_background);
|
||||
view_foreground = (LinearLayout) itemView.findViewById(R.id.view_foreground);
|
||||
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
itemClickListener.onClick(v, getAdapterPosition(), false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/rootLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".FavoritesActivity">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_fav"
|
||||
android:scrollbars="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -24,7 +24,7 @@
|
||||
android:layout_below="@+id/searchBar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/recyler_food"
|
||||
android:id="@+id/recycler_food"
|
||||
android:scrollbars="vertical"
|
||||
/>
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.cardview.widget.CardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:cardElevation="10dp"
|
||||
app:cardCornerRadius="0dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/view_background"
|
||||
android:background="@color/bgRowBackground"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/delete_icon"
|
||||
android:src="@drawable/ic_baseline_delete_24"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_width="30dp"
|
||||
android:layout_height="30dp"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_toLeftOf="@id/delete_icon"
|
||||
android:text="Xoá"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="13sp"
|
||||
android:layout_margin="10dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/view_foreground"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/white"
|
||||
android:weightSum="5"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/food_image"
|
||||
android:layout_margin="5dp"
|
||||
android:src="@drawable/bg4"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="4"
|
||||
android:scaleType="centerCrop" />
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:weightSum="10"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_name"
|
||||
android:layout_width="0dp"
|
||||
android:layout_weight="9"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Name of Food"
|
||||
android:textSize="20sp" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_price"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="$100"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/discount"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="14dp"
|
||||
android:layout_marginLeft="30dp"
|
||||
android:layout_toEndOf="@+id/food_price"
|
||||
android:text="$100"
|
||||
android:textColor="@color/red"
|
||||
android:textSize="10sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/new_Price"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="5dp"
|
||||
android:layout_toEndOf="@+id/discount"
|
||||
android:textStyle="bold"
|
||||
android:text="$100"
|
||||
android:textSize="25sp" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/btn_quick_cart"
|
||||
android:src="@drawable/ic_baseline_shopping_cart_24"
|
||||
app:tint="@color/colorAccent"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginRight="7dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
@@ -10,6 +10,12 @@
|
||||
android:iconTint="@color/white"
|
||||
android:title="Địa chỉ mặc định"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/nav_favorites"
|
||||
android:icon="@drawable/ic_baseline_favorite_24"
|
||||
android:iconTint="@color/white"
|
||||
android:title="Danh sách yêu thích"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/nav_cart"
|
||||
android:icon="@drawable/ic_baseline_shopping_cart_24"
|
||||
|
||||
Reference in New Issue
Block a user