mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Banner management
This commit is contained in:
@@ -6,9 +6,11 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
@@ -17,6 +19,7 @@ import android.widget.Toast;
|
|||||||
import com.firebase.ui.database.FirebaseRecyclerAdapter;
|
import com.firebase.ui.database.FirebaseRecyclerAdapter;
|
||||||
import com.firebase.ui.database.FirebaseRecyclerOptions;
|
import com.firebase.ui.database.FirebaseRecyclerOptions;
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
import com.google.firebase.database.ChildEventListener;
|
||||||
import com.google.firebase.database.DataSnapshot;
|
import com.google.firebase.database.DataSnapshot;
|
||||||
import com.google.firebase.database.DatabaseError;
|
import com.google.firebase.database.DatabaseError;
|
||||||
import com.google.firebase.database.DatabaseReference;
|
import com.google.firebase.database.DatabaseReference;
|
||||||
@@ -24,10 +27,12 @@ import com.google.firebase.database.FirebaseDatabase;
|
|||||||
import com.google.firebase.database.ValueEventListener;
|
import com.google.firebase.database.ValueEventListener;
|
||||||
import com.google.firebase.storage.FirebaseStorage;
|
import com.google.firebase.storage.FirebaseStorage;
|
||||||
import com.google.firebase.storage.StorageReference;
|
import com.google.firebase.storage.StorageReference;
|
||||||
import com.rengwuxian.materialedittext.MaterialEditText;
|
import com.jaredrummler.materialspinner.MaterialSpinner;
|
||||||
import com.squareup.picasso.Picasso;
|
import com.squareup.picasso.Picasso;
|
||||||
|
import com.waterbase.foodifyServer.Common.Common;
|
||||||
import com.waterbase.foodifyServer.Model.Banner;
|
import com.waterbase.foodifyServer.Model.Banner;
|
||||||
import com.waterbase.foodifyServer.Model.Food;
|
import com.waterbase.foodifyServer.Model.Food;
|
||||||
|
import com.waterbase.foodifyServer.Model.Request;
|
||||||
import com.waterbase.foodifyServer.ViewHolder.BannerViewHolder;
|
import com.waterbase.foodifyServer.ViewHolder.BannerViewHolder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -37,6 +42,9 @@ import info.hoang8f.widget.FButton;
|
|||||||
|
|
||||||
public class BannerActivity extends AppCompatActivity {
|
public class BannerActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
FirebaseDatabase database = FirebaseDatabase.getInstance();
|
||||||
|
DatabaseReference foods = database.getReference("Foods");
|
||||||
|
|
||||||
RecyclerView recyclerView;
|
RecyclerView recyclerView;
|
||||||
RecyclerView.LayoutManager layoutManager;
|
RecyclerView.LayoutManager layoutManager;
|
||||||
|
|
||||||
@@ -53,13 +61,13 @@ public class BannerActivity extends AppCompatActivity {
|
|||||||
FirebaseRecyclerAdapter<Banner, BannerViewHolder> adapter;
|
FirebaseRecyclerAdapter<Banner, BannerViewHolder> adapter;
|
||||||
|
|
||||||
//Add new Banner
|
//Add new Banner
|
||||||
MaterialEditText edtName, edtFoodId;
|
MaterialSpinner foodListSpinner;
|
||||||
FButton btnUpload, btnSelect;
|
|
||||||
|
|
||||||
Banner newBanner;
|
Banner newBanner;
|
||||||
Uri filePath;
|
Uri filePath;
|
||||||
|
|
||||||
List<Food> foodList = new ArrayList<>();
|
List<Food> foodList = new ArrayList<>();
|
||||||
|
List foodName = new ArrayList();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -83,6 +91,7 @@ public class BannerActivity extends AppCompatActivity {
|
|||||||
fab.setOnClickListener((view) -> { showAddBanner(); });
|
fab.setOnClickListener((view) -> { showAddBanner(); });
|
||||||
|
|
||||||
loadListBanner();
|
loadListBanner();
|
||||||
|
loadListFood();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadListBanner() {
|
private void loadListBanner() {
|
||||||
@@ -115,8 +124,6 @@ public class BannerActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadListFood() {
|
private void loadListFood() {
|
||||||
FirebaseDatabase database = FirebaseDatabase.getInstance();
|
|
||||||
DatabaseReference foods = database.getReference("Foods");
|
|
||||||
|
|
||||||
foods.addValueEventListener(new ValueEventListener() {
|
foods.addValueEventListener(new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -124,8 +131,12 @@ public class BannerActivity extends AppCompatActivity {
|
|||||||
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
|
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
|
||||||
Food food = snapshot.getValue(Food.class);
|
Food food = snapshot.getValue(Food.class);
|
||||||
foodList.add(food);
|
foodList.add(food);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foodName = new ArrayList();
|
||||||
|
|
||||||
|
for(Food food: foodList)
|
||||||
|
foodName.add(food.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -146,9 +157,243 @@ public class BannerActivity extends AppCompatActivity {
|
|||||||
alertDialog.setTitle("Thêm ảnh bìa");
|
alertDialog.setTitle("Thêm ảnh bìa");
|
||||||
alertDialog.setMessage("Chọn món ăn bạn muốn thêm:");
|
alertDialog.setMessage("Chọn món ăn bạn muốn thêm:");
|
||||||
|
|
||||||
loadListFood();
|
LayoutInflater inflater = this.getLayoutInflater();
|
||||||
|
View view = inflater.inflate(R.layout.add_new_banner, null);
|
||||||
|
|
||||||
|
foodListSpinner = view.findViewById(R.id.foodList);
|
||||||
|
|
||||||
|
foodListSpinner.setItems(foodName);
|
||||||
|
alertDialog.setView(view);
|
||||||
|
alertDialog.setIcon(R.drawable.ic_baseline_laptop_24);
|
||||||
|
|
||||||
|
alertDialog.setPositiveButton("Thêm", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
//Get Object Food has selected
|
||||||
|
Food foodSelectedName = foodList.get(foodListSpinner.getSelectedIndex());
|
||||||
|
|
||||||
|
//Get key Food Id from Firebase
|
||||||
|
foods.addChildEventListener(new ChildEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||||
|
if(dataSnapshot.exists())
|
||||||
|
{
|
||||||
|
String pushKey = dataSnapshot.getKey();
|
||||||
|
foods.child(pushKey).addChildEventListener(new ChildEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||||
|
if(dataSnapshot.exists())
|
||||||
|
{
|
||||||
|
if(dataSnapshot.getKey().equals("name"))
|
||||||
|
{
|
||||||
|
if(dataSnapshot.getValue().toString().equals(foodSelectedName.getName())){
|
||||||
|
//Create new Object banner
|
||||||
|
newBanner = new Banner();
|
||||||
|
newBanner.setId(pushKey);
|
||||||
|
newBanner.setName(foodSelectedName.getName());
|
||||||
|
newBanner.setImage(foodSelectedName.getImage());
|
||||||
|
|
||||||
|
if(newBanner != null)
|
||||||
|
//Push Banner to Firebase
|
||||||
|
banners.push().setValue(newBanner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
alertDialog.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
alertDialog.show();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onContextItemSelected(@NonNull MenuItem item) {
|
||||||
|
if(item.getTitle().equals(Common.UPDATE))
|
||||||
|
{
|
||||||
|
showUpdateBannerDialog(adapter.getRef(item.getOrder()).getKey(), adapter.getItem(item.getOrder()));
|
||||||
|
}
|
||||||
|
else if(item.getTitle().equals(Common.DELETE))
|
||||||
|
{
|
||||||
|
deleteBanner(adapter.getRef(item.getOrder()).getKey());
|
||||||
|
}
|
||||||
|
return super.onContextItemSelected(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteBanner(String key) {
|
||||||
|
banners.child(key).removeValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showUpdateBannerDialog(String key, Banner item) {
|
||||||
|
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(BannerActivity.this);
|
||||||
|
alertDialog.setTitle("Cập nhật ảnh bìa");
|
||||||
|
alertDialog.setMessage("Vui lòng chọn món ăn bạn muốn cập nhật:");
|
||||||
|
|
||||||
|
|
||||||
LayoutInflater inflater = this.getLayoutInflater();
|
LayoutInflater inflater = this.getLayoutInflater();
|
||||||
View add_menu_layout = inflater.inflate(R.layout.add_new_banner, null);
|
final View view = inflater.inflate(R.layout.add_new_banner, null);
|
||||||
|
|
||||||
|
foodListSpinner = view.findViewById(R.id.foodList);
|
||||||
|
foodListSpinner.setItems(foodName);
|
||||||
|
|
||||||
|
banners.orderByKey().equalTo(key)
|
||||||
|
.addValueEventListener(new ValueEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||||
|
for(DataSnapshot postSnapShot: dataSnapshot.getChildren()) {
|
||||||
|
Banner data = postSnapShot.getValue(Banner.class);
|
||||||
|
foodListSpinner.setSelectedIndex(foodName.indexOf(data.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
Toast.makeText(BannerActivity.this, "Đã có lỗi từ hệ thống, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
alertDialog.setView(view);
|
||||||
|
|
||||||
|
final String localKey = key;
|
||||||
|
|
||||||
|
alertDialog.setPositiveButton("Cập nhật", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
//Get Object Food has selected
|
||||||
|
Food foodSelectedName = foodList.get(foodListSpinner.getSelectedIndex());
|
||||||
|
|
||||||
|
//Get key Food Id from Firebase
|
||||||
|
foods.addChildEventListener(new ChildEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||||
|
if(dataSnapshot.exists())
|
||||||
|
{
|
||||||
|
String pushKey = dataSnapshot.getKey();
|
||||||
|
foods.child(pushKey).addChildEventListener(new ChildEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||||
|
if(dataSnapshot.exists())
|
||||||
|
{
|
||||||
|
if(dataSnapshot.getKey().equals("name"))
|
||||||
|
{
|
||||||
|
if(dataSnapshot.getValue().toString().equals(foodSelectedName.getName())){
|
||||||
|
item.setId(pushKey);
|
||||||
|
item.setName(foodSelectedName.getName());
|
||||||
|
item.setImage(foodSelectedName.getImage());
|
||||||
|
|
||||||
|
banners.child(localKey).setValue(item);
|
||||||
|
adapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
alertDialog.setNegativeButton("Huỷ", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
alertDialog.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +164,7 @@ public class OrderStatus extends AppCompatActivity {
|
|||||||
spinner = (MaterialSpinner) view.findViewById(R.id.statusSpinner);
|
spinner = (MaterialSpinner) view.findViewById(R.id.statusSpinner);
|
||||||
spinner.setItems("ĐÃ NHẬN ĐƠN", "ĐANG TRÊN ĐƯỜNG", "GIAO THÀNH CÔNG");
|
spinner.setItems("ĐÃ NHẬN ĐƠN", "ĐANG TRÊN ĐƯỜNG", "GIAO THÀNH CÔNG");
|
||||||
|
|
||||||
|
//Set value default spinner
|
||||||
requests.orderByKey().equalTo(key)
|
requests.orderByKey().equalTo(key)
|
||||||
.addValueEventListener(new ValueEventListener() {
|
.addValueEventListener(new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -15,78 +15,11 @@
|
|||||||
android:layout_margin="10dp"
|
android:layout_margin="10dp"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<com.rengwuxian.materialedittext.MaterialEditText
|
<com.jaredrummler.materialspinner.MaterialSpinner
|
||||||
android:id="@+id/edtFoodId"
|
android:id="@+id/foodList"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:backgroundTint="#CCCCCC"
|
/>
|
||||||
android:drawablePadding="13dp"
|
|
||||||
android:hint="Food Id"
|
|
||||||
android:inputType="text"
|
|
||||||
android:textColor="@color/black"
|
|
||||||
android:textColorHint="@color/black"
|
|
||||||
android:textSize="20sp"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintWidth_percent=".8" />
|
|
||||||
|
|
||||||
<com.rengwuxian.materialedittext.MaterialEditText
|
|
||||||
android:id="@+id/edtFoodName"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:backgroundTint="#CCCCCC"
|
|
||||||
android:drawablePadding="13dp"
|
|
||||||
android:hint="Food Name"
|
|
||||||
android:inputType="text"
|
|
||||||
android:textColor="@color/black"
|
|
||||||
android:textColorHint="@color/black"
|
|
||||||
android:textSize="18sp"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintWidth_percent=".8" />
|
|
||||||
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="horizontal"
|
|
||||||
android:weightSum="2">
|
|
||||||
|
|
||||||
<info.hoang8f.widget.FButton
|
|
||||||
android:id="@+id/btnSelect"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_alignParentBottom="true"
|
|
||||||
android:layout_margin="8dp"
|
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="Chọn ảnh..."
|
|
||||||
android:textColor="@color/white"
|
|
||||||
app:cornerRadius="4dp"
|
|
||||||
app:fButtonColor="@color/colorPrimary"
|
|
||||||
app:shadowColor="@color/black"
|
|
||||||
app:shadowEnabled="true"
|
|
||||||
app:shadowHeight="5dp" />
|
|
||||||
|
|
||||||
<info.hoang8f.widget.FButton
|
|
||||||
android:id="@+id/btnUpload"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_alignParentBottom="true"
|
|
||||||
android:layout_margin="8dp"
|
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="Tải ảnh lên"
|
|
||||||
android:textColor="@color/white"
|
|
||||||
app:cornerRadius="4dp"
|
|
||||||
app:fButtonColor="@color/btnSignActive"
|
|
||||||
app:shadowColor="@color/black"
|
|
||||||
app:shadowEnabled="true"
|
|
||||||
app:shadowHeight="5dp" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</androidx.core.widget.NestedScrollView>
|
</androidx.core.widget.NestedScrollView>
|
||||||
|
|||||||
Reference in New Issue
Block a user