Add food function

This commit is contained in:
phanhuuloi27
2022-06-05 20:36:27 +07:00
parent 255d3559c6
commit a045d0da72
10 changed files with 551 additions and 5 deletions
@@ -13,6 +13,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.FoodifyServer"
tools:targetApi="31">
<activity
android:name=".FoodList"
android:exported="false" />
<activity
android:name=".Home"
android:exported="false"
@@ -7,4 +7,5 @@ public class Common {
public static final String UPDATE = "Cập nhật";
public static final String DELETE = "Xoá";
public static final int PICK_IMAGE_REQUEST = 71;
}
@@ -0,0 +1,243 @@
package com.waterbase.foodifyServer;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.picasso.Picasso;
import com.waterbase.foodifyServer.Common.Category;
import com.waterbase.foodifyServer.Common.Common;
import com.waterbase.foodifyServer.Interface.ItemClickListener;
import com.waterbase.foodifyServer.Model.Food;
import com.waterbase.foodifyServer.ViewHolder.FoodViewHolder;
import java.util.UUID;
import info.hoang8f.widget.FButton;
public class FoodList extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RelativeLayout rootLayout;
FloatingActionButton fab;
//Firebase
FirebaseDatabase db;
DatabaseReference foodList;
FirebaseStorage storage;
StorageReference storageReference;
String categoryId ="";
FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter;
//Add new Food Layout
MaterialEditText edtName, edtDescription, edtPrice, edtDiscount;
FButton btnSelect, btnUpload;
Food newFood;
Uri saveUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
//Firebase
db = FirebaseDatabase.getInstance();
foodList = db.getReference("Foods");
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
//Init
recyclerView = (RecyclerView) findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddFoodDialog();
}
});
if(getIntent() != null) {
categoryId = getIntent().getStringExtra("CategoryId");
}
if(!categoryId.isEmpty())
loadListFood(categoryId);
}
private void showAddFoodDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(FoodList.this);
alertDialog.setTitle("Thêm món ăn");
alertDialog.setMessage("Vui lòng điền đầy đủ thông tin");
LayoutInflater inflater = this.getLayoutInflater();
View add_menu_layout = inflater.inflate(R.layout.add_new_food_layout, null);
edtName = add_menu_layout.findViewById(R.id.edtName);
edtDescription = add_menu_layout.findViewById(R.id.edtDescription);
edtPrice = add_menu_layout.findViewById(R.id.edtPrice);
edtDiscount = add_menu_layout.findViewById(R.id.edtDiscount);
btnSelect = add_menu_layout.findViewById(R.id.btnSelect);
btnUpload = add_menu_layout.findViewById(R.id.btnUpload);
//Event for button
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage(); //Let user select image from Gallery and save Uri of this image
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
alertDialog.setView(add_menu_layout);
alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24);
alertDialog.setPositiveButton("Thêm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//Upload new category
if(newFood != null) {
foodList.push().setValue(newFood);
Snackbar.make(rootLayout, "Món ăn " + newFood.getName() + " đã được thêm", Snackbar.LENGTH_SHORT)
.show();
}
}
});
alertDialog.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void uploadImage() {
if(saveUri != null) {
ProgressDialog mDialog = new ProgressDialog(this);
mDialog.setMessage("Đang tải lên....");
mDialog.show();
String imageName = UUID.randomUUID().toString();
StorageReference imageFolder = storageReference.child("/images/" + imageName);
imageFolder.putFile(saveUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mDialog.dismiss();
Toast.makeText(FoodList.this, "Đã tải lên thành công!", Toast.LENGTH_SHORT).show();
imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//Set value for new Category if image upload and we can get download link
newFood = new Food();
newFood.setName(edtName.getText().toString());
newFood.setDescription(edtDescription.getText().toString());
newFood.setPrice(edtPrice.getText().toString());
newFood.setDiscount(edtDiscount.getText().toString());
newFood.setMenuId(categoryId);
newFood.setImage(uri.toString());
}
});
}
})
.addOnFailureListener((e) -> {
mDialog.dismiss();
Toast.makeText(FoodList.this, e.getMessage(), Toast.LENGTH_SHORT).show();
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
mDialog.setMessage("Đang tải " + progress + "%");
}
});
}
}
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Common.PICK_IMAGE_REQUEST);
}
private void loadListFood(String categoryId) {
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("menuId").equalTo(categoryId)
) {
@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int i) {
viewHolder.txtFoodName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//Code later
}
});
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Common.PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
saveUri = data.getData();
btnSelect.setText("Ảnh đã được chọn!");
}
}
}
@@ -73,7 +73,7 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
Category newCategory;
Uri saveUri;
private final int PICK_IMAGE_REQUEST = 71;
DrawerLayout drawer;
@@ -215,7 +215,7 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
if(requestCode == Common.PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
saveUri = data.getData();
btnSelect.setText("Ảnh đã được chọn!");
@@ -226,7 +226,7 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Common.PICK_IMAGE_REQUEST);
}
private void loadMenu() {
@@ -244,7 +244,10 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//send Category Id and Start new Activity
Intent foodList = new Intent(Home.this, FoodList.class);
foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
@@ -0,0 +1,64 @@
package com.waterbase.foodifyServer.Model;
public class Food {
private String Name, Image, Description, Price, Discount, MenuId;
public Food() {}
public Food(String name, String image, String description, String price, String discount, String menuId) {
Name = name;
Image = image;
Description = description;
Price = price;
Discount = discount;
MenuId = menuId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getDiscount() {
return Discount;
}
public void setDiscount(String discount) {
Discount = discount;
}
public String getMenuId() {
return MenuId;
}
public void setMenuId(String menuId) {
MenuId = menuId;
}
}
@@ -0,0 +1,46 @@
package com.waterbase.foodifyServer.ViewHolder;
import android.view.ContextMenu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.waterbase.foodifyServer.Common.Common;
import com.waterbase.foodifyServer.Interface.ItemClickListener;
import com.waterbase.foodifyServer.R;
public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener{
public TextView txtFoodName;
public ImageView imageView;
private ItemClickListener itemClickListener;
public FoodViewHolder(View itemView) {
super(itemView);
txtFoodName = (TextView) itemView.findViewById(R.id.food_name);
imageView = (ImageView) itemView.findViewById(R.id.food_image);
itemView.setOnCreateContextMenuListener(this);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), false);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Tuỳ chọn:");
menu.add(0,0, getAdapterPosition(), Common.UPDATE);
menu.add(0,1, getAdapterPosition(), Common.DELETE);
}
}
@@ -0,0 +1,28 @@
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
tools:context=".FoodList">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_food"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:backgroundTint="@color/white"
app:srcCompat="@drawable/ic_baseline_playlist_add_24"
/>
</RelativeLayout>
@@ -0,0 +1,126 @@
<?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="4dp"
>
<LinearLayout
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#CCCCCC"
android:drawablePadding="13dp"
android:textSize="28sp"
android:hint="Tên món ăn"
android:inputType="text"
android:textColor="@color/black"
android:textColorHint="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent=".8" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#CCCCCC"
android:drawablePadding="13dp"
android:hint="Mô tả về món ăn này"
android:inputType="textMultiLine"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent=".8" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#CCCCCC"
android:drawablePadding="13dp"
android:hint="Giá"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent=".8" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#CCCCCC"
android:drawablePadding="13dp"
android:hint="Giảm giá"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent=".8" />
<LinearLayout
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<info.hoang8f.widget.FButton
android:id="@+id/btnSelect"
android:text="Chọn ảnh..."
android:textColor="@color/white"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_margin="8dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
app:fButtonColor="@color/colorPrimary"
app:shadowColor="@color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
<info.hoang8f.widget.FButton
android:id="@+id/btnUpload"
android:text="Tải ảnh lên"
android:textColor="@color/white"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_margin="8dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
app:fButtonColor="@color/btnSignActive"
app:shadowColor="@color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
@@ -26,7 +26,6 @@
android:textColorHint="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtPhone"
app:layout_constraintWidth_percent=".8" />
<LinearLayout
@@ -0,0 +1,33 @@
<?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="200dp"
app:cardElevation="4dp"
android:layout_marginBottom="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/food_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/food_name"
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" />
</RelativeLayout>
</androidx.cardview.widget.CardView>