mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 20:00:53 +00:00
Upload Foodify-Client
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
package com.waterbase.foodify;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.Model.Request;
|
||||
import com.waterbase.foodify.ViewHolder.CartAdapter;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import info.hoang8f.widget.FButton;
|
||||
|
||||
public class Cart extends AppCompatActivity {
|
||||
|
||||
RecyclerView recyclerView;
|
||||
RecyclerView.LayoutManager layoutManager;
|
||||
|
||||
FirebaseDatabase database;
|
||||
DatabaseReference requests;
|
||||
|
||||
TextView txtTotalPrice;
|
||||
FButton btnPlace;
|
||||
|
||||
List<Order> cart = new ArrayList<>();
|
||||
CartAdapter adapter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_cart);
|
||||
|
||||
//Firebase
|
||||
database = FirebaseDatabase.getInstance();
|
||||
requests = database.getReference("Requests");
|
||||
|
||||
//Init
|
||||
recyclerView = (RecyclerView) findViewById(R.id.listCart);
|
||||
recyclerView.setHasFixedSize(true);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recyclerView.setLayoutManager(layoutManager);
|
||||
|
||||
txtTotalPrice = (TextView) findViewById(R.id.total);
|
||||
btnPlace = (FButton) findViewById(R.id.btnPlaceOrder);
|
||||
|
||||
btnPlace.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showAlertDialog();
|
||||
}
|
||||
});
|
||||
|
||||
loadListFood();
|
||||
|
||||
setTitle("Giỏ hàng");
|
||||
// calling the action bar
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
// showing the back button in action bar
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
private void showAlertDialog() {
|
||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
|
||||
alertDialog.setTitle("One more step!");
|
||||
alertDialog.setMessage("Enter your address: ");
|
||||
|
||||
final EditText edtAddress = new EditText(Cart.this);
|
||||
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT
|
||||
);
|
||||
edtAddress.setLayoutParams(lp);
|
||||
alertDialog.setView(edtAddress); //Add edit text to Dialog
|
||||
alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24);
|
||||
|
||||
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
//Create new Request
|
||||
Request request = new Request(
|
||||
Common.currentUser.getPhone(),
|
||||
Common.currentUser.getName(),
|
||||
edtAddress.getText().toString(),
|
||||
txtTotalPrice.getText().toString(),
|
||||
cart
|
||||
);
|
||||
|
||||
//Summit to Firebase
|
||||
//We will using System.CurrentMilli to key
|
||||
requests.child(String.valueOf(System.currentTimeMillis())).setValue(request);
|
||||
|
||||
//Delete Cart
|
||||
new Database(getBaseContext()).cleanCart();
|
||||
Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
alertDialog.show();
|
||||
}
|
||||
|
||||
private void loadListFood() {
|
||||
cart = new Database(this).getCarts();
|
||||
adapter = new CartAdapter(cart, this);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
//Calculate total price
|
||||
float total = 0;
|
||||
for(Order order:cart)
|
||||
total += (Float.parseFloat(order.getPrice()))*(Float.parseFloat(order.getQuantity()));
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
txtTotalPrice.setText(fmt.format(total));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
this.finish();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user