mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Create New Category
This commit is contained in:
@@ -47,6 +47,12 @@ dependencies {
|
||||
//Add library
|
||||
implementation 'com.google.firebase:firebase-core:10.2.0'
|
||||
implementation 'com.google.firebase:firebase-database:10.2.0'
|
||||
implementation 'com.firebaseui:firebase-ui-database:1.2.0'
|
||||
implementation 'com.squareup.picasso:picasso:2.5.2'
|
||||
implementation 'com.google.firebase:firebase-auth:10.2.0'
|
||||
implementation 'com.google.firebase:firebase-storage:10.2.0'
|
||||
implementation 'com.github.jd-alexander:android-flat-button:v1.1'
|
||||
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
|
||||
}
|
||||
|
||||
apply plugin: 'com.google.gms.google-services'
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.waterbase.foodifyServer.Common;
|
||||
|
||||
public class Category {
|
||||
private String Name;
|
||||
private String Image;
|
||||
|
||||
public Category() {
|
||||
}
|
||||
|
||||
public Category(String name, String image) {
|
||||
Name = name;
|
||||
Image = image;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return Name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return Image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
Image = image;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,27 @@
|
||||
package com.waterbase.foodifyServer;
|
||||
|
||||
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.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.Menu;
|
||||
import android.widget.TextView;
|
||||
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.android.material.navigation.NavigationView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.view.GravityCompat;
|
||||
import androidx.navigation.NavController;
|
||||
@@ -20,14 +30,52 @@ import androidx.navigation.ui.AppBarConfiguration;
|
||||
import androidx.navigation.ui.NavigationUI;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
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.ViewHolder.MenuViewHolder;
|
||||
import com.waterbase.foodifyServer.databinding.ActivityHomeBinding;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import info.hoang8f.widget.FButton;
|
||||
|
||||
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
|
||||
|
||||
TextView txtFullName;
|
||||
|
||||
//Firebase
|
||||
FirebaseDatabase database;
|
||||
DatabaseReference categories;
|
||||
FirebaseStorage storage;
|
||||
StorageReference storageReference;
|
||||
FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter;
|
||||
|
||||
//View
|
||||
RecyclerView recycler_menu;
|
||||
RecyclerView.LayoutManager layoutManager;
|
||||
|
||||
//Add new Menu Layout
|
||||
MaterialEditText edtName;
|
||||
FButton btnUpload, btnSelect;
|
||||
|
||||
Category newCategory;
|
||||
|
||||
Uri saveUri;
|
||||
private final int PICK_IMAGE_REQUEST = 71;
|
||||
|
||||
DrawerLayout drawer;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -38,7 +86,18 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
toolbar.setTitle("Menu Management");
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
//Init database
|
||||
database = FirebaseDatabase.getInstance();
|
||||
categories = database.getReference("Category");
|
||||
storage = FirebaseStorage.getInstance();
|
||||
storageReference = storage.getReference();
|
||||
|
||||
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
|
||||
fab.setOnClickListener((view) -> {
|
||||
showDialog();
|
||||
});
|
||||
|
||||
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.app_name, R.string.app_name);
|
||||
drawer.setDrawerListener(toggle);
|
||||
toggle.syncState();
|
||||
@@ -49,9 +108,144 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
View headerView = navigationView.getHeaderView(0);
|
||||
txtFullName = (TextView) headerView.findViewById(R.id.txtFullName);
|
||||
txtFullName.setText(Common.currentUser.getName());
|
||||
|
||||
//Init View
|
||||
recycler_menu = (RecyclerView) findViewById(R.id.recyler_menu);
|
||||
recycler_menu.setHasFixedSize(true);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recycler_menu.setLayoutManager(layoutManager);
|
||||
|
||||
loadMenu();
|
||||
|
||||
}
|
||||
|
||||
private void showDialog() {
|
||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
|
||||
alertDialog.setTitle("Thêm danh mục:");
|
||||
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_menu_layout, null);
|
||||
|
||||
edtName = add_menu_layout.findViewById(R.id.edtName);
|
||||
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(newCategory != null) {
|
||||
categories.push().setValue(newCategory);
|
||||
Snackbar.make(drawer, "Danh mục " + newCategory.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(Home.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
|
||||
newCategory = new Category(edtName.getText().toString(), uri.toString());
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.addOnFailureListener((e) -> {
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(Home.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 + "%");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
|
||||
&& data != null && data.getData() != null) {
|
||||
saveUri = data.getData();
|
||||
btnSelect.setText("Ảnh đã được chọn!");
|
||||
}
|
||||
}
|
||||
|
||||
private void chooseImage() {
|
||||
Intent intent = new Intent();
|
||||
intent.setType("image/*");
|
||||
intent.setAction(Intent.ACTION_GET_CONTENT);
|
||||
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
|
||||
}
|
||||
|
||||
private void loadMenu() {
|
||||
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(
|
||||
Category.class,
|
||||
R.layout.menu_item,
|
||||
MenuViewHolder.class,
|
||||
categories
|
||||
) {
|
||||
@Override
|
||||
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int i) {
|
||||
viewHolder.txtMenuName.setText(model.getName());
|
||||
Picasso.with(Home.this).load(model.getImage()).into(viewHolder.imageView);
|
||||
}
|
||||
};
|
||||
|
||||
adapter.notifyDataSetChanged(); //Refresh data if have data changed
|
||||
recycler_menu.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.waterbase.foodifyServer.Interface;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
public interface ItemClickListener {
|
||||
void onClick(View view, int position, boolean isLongClick);
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.waterbase.foodifyServer.ViewHolder;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.waterbase.foodifyServer.Interface.ItemClickListener;
|
||||
import com.waterbase.foodifyServer.R;
|
||||
|
||||
public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
|
||||
public TextView txtMenuName;
|
||||
public ImageView imageView;
|
||||
|
||||
private ItemClickListener itemClickListener;
|
||||
|
||||
public MenuViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
txtMenuName = (TextView) itemView.findViewById(R.id.menu_name);
|
||||
imageView = (ImageView) itemView.findViewById(R.id.menu_image);
|
||||
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public void setItemClickListener(ItemClickListener itemClickListener) {
|
||||
this.itemClickListener = itemClickListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
itemClickListener.onClick(v, getAdapterPosition(), false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?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:drawableLeft="@drawable/ic_baseline_vpn_key_24"
|
||||
android:drawablePadding="13dp"
|
||||
android:hint="Tên"
|
||||
android:inputType="text"
|
||||
android:textColor="@color/black"
|
||||
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
|
||||
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"
|
||||
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="Upload"
|
||||
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>
|
||||
@@ -23,6 +23,7 @@
|
||||
<include layout="@layout/content_home" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|end"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?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"
|
||||
>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/menu_image"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/menu_name"
|
||||
android:text="Name of Menu"
|
||||
android:textColor="@color/white"
|
||||
android:background="#4f0e0d0e"
|
||||
android:textSize="20sp"
|
||||
android:gravity="center"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
</RelativeLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
@@ -8,6 +8,13 @@
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
|
||||
<color name="colorPrimary">#FF5353</color>
|
||||
<color name="colorPrimaryDark">#00251a</color>
|
||||
<color name="colorAccent">#39796b</color>
|
||||
|
||||
<color name="btnSignUp">#39796b</color>
|
||||
<color name="btnSignActive">#A9d440</color>
|
||||
|
||||
<color name="overlayBackground">#7f333639</color>
|
||||
<color name="overlayActionBar">#0e0d0e</color>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user