diff --git a/FoodifyServer/app/src/main/AndroidManifest.xml b/FoodifyServer/app/src/main/AndroidManifest.xml index 9c5baa9..c329a0a 100644 --- a/FoodifyServer/app/src/main/AndroidManifest.xml +++ b/FoodifyServer/app/src/main/AndroidManifest.xml @@ -13,6 +13,9 @@ android:supportsRtl="true" android:theme="@style/Theme.FoodifyServer" tools:targetApi="31"> + 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() { + @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() { + @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() { + @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.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!"); + } + } +} \ No newline at end of file diff --git a/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Home.java b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Home.java index d810c24..4d067a0 100644 --- a/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Home.java +++ b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Home.java @@ -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); } }); } diff --git a/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Model/Food.java b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Model/Food.java new file mode 100644 index 0000000..468e7c4 --- /dev/null +++ b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/Model/Food.java @@ -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; + } +} \ No newline at end of file diff --git a/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/ViewHolder/FoodViewHolder.java b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/ViewHolder/FoodViewHolder.java new file mode 100644 index 0000000..306d4f6 --- /dev/null +++ b/FoodifyServer/app/src/main/java/com/waterbase/foodifyServer/ViewHolder/FoodViewHolder.java @@ -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); + } +} \ No newline at end of file diff --git a/FoodifyServer/app/src/main/res/layout/activity_food_list.xml b/FoodifyServer/app/src/main/res/layout/activity_food_list.xml new file mode 100644 index 0000000..3048519 --- /dev/null +++ b/FoodifyServer/app/src/main/res/layout/activity_food_list.xml @@ -0,0 +1,28 @@ + + + + + + + + \ No newline at end of file diff --git a/FoodifyServer/app/src/main/res/layout/add_new_food_layout.xml b/FoodifyServer/app/src/main/res/layout/add_new_food_layout.xml new file mode 100644 index 0000000..e333257 --- /dev/null +++ b/FoodifyServer/app/src/main/res/layout/add_new_food_layout.xml @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FoodifyServer/app/src/main/res/layout/add_new_menu_layout.xml b/FoodifyServer/app/src/main/res/layout/add_new_menu_layout.xml index 5c9a155..f95e97d 100644 --- a/FoodifyServer/app/src/main/res/layout/add_new_menu_layout.xml +++ b/FoodifyServer/app/src/main/res/layout/add_new_menu_layout.xml @@ -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" /> + + + + + + + + + + \ No newline at end of file