mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 20:00:53 +00:00
249 lines
9.1 KiB
Java
249 lines
9.1 KiB
Java
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.util.Log;
|
|
import android.view.LayoutInflater;
|
|
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.DataSnapshot;
|
|
import com.google.firebase.database.DatabaseError;
|
|
import com.google.firebase.database.DatabaseReference;
|
|
import com.google.firebase.database.FirebaseDatabase;
|
|
import com.google.firebase.database.Query;
|
|
import com.google.firebase.database.ValueEventListener;
|
|
import com.rengwuxian.materialedittext.MaterialEditText;
|
|
import com.waterbase.foodify.Common.Common;
|
|
import com.waterbase.foodify.Database.Database;
|
|
import com.waterbase.foodify.Model.MyResponse;
|
|
import com.waterbase.foodify.Model.Notification;
|
|
import com.waterbase.foodify.Model.Order;
|
|
import com.waterbase.foodify.Model.Request;
|
|
import com.waterbase.foodify.Model.Sender;
|
|
import com.waterbase.foodify.Model.Token;
|
|
import com.waterbase.foodify.Remote.APIService;
|
|
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;
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
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;
|
|
|
|
APIService mService;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_cart);
|
|
|
|
//Init
|
|
mService = Common.getFCMService();
|
|
|
|
//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) {
|
|
if (cart.size() > 0)
|
|
showAlertDialog();
|
|
else
|
|
Toast.makeText(Cart.this, "Giỏ hàng của bạn đang trống!!!", Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
|
|
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("Còn 1 bước nữa!");
|
|
alertDialog.setMessage("Xin vui lòng điền thông tin dưới đây: ");
|
|
|
|
LayoutInflater inflater = this.getLayoutInflater();
|
|
View order_address_comment = inflater.inflate(R.layout.order_address_comment, null);
|
|
|
|
MaterialEditText edtAddress = (MaterialEditText) order_address_comment.findViewById(R.id.edtAddress);
|
|
MaterialEditText edtComment = (MaterialEditText) order_address_comment.findViewById(R.id.edtComment);
|
|
|
|
|
|
alertDialog.setView(order_address_comment);
|
|
alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24);
|
|
|
|
alertDialog.setPositiveButton("Đặt", 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(),
|
|
"0",
|
|
edtComment.getText().toString(),
|
|
cart
|
|
);
|
|
|
|
//Summit to Firebase
|
|
//We will using System.CurrentMilli to key
|
|
String order_number = String.valueOf(System.currentTimeMillis());
|
|
requests.child(order_number).setValue(request);
|
|
|
|
sendNotificationOrder(order_number);
|
|
|
|
//Delete Cart
|
|
// new Database(getBaseContext()).cleanCart();
|
|
// Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
|
// finish();
|
|
}
|
|
});
|
|
|
|
alertDialog.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
|
|
alertDialog.show();
|
|
}
|
|
|
|
private void sendNotificationOrder(String order_number) {
|
|
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
|
|
Query data = tokens.orderByChild("serverToken").equalTo(true); //Get all node with isServerToken is true;
|
|
data.addValueEventListener(new ValueEventListener() {
|
|
@Override
|
|
public void onDataChange(DataSnapshot dataSnapshot) {
|
|
for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
|
|
|
|
Token serverToken = postSnapShot.getValue(Token.class);
|
|
|
|
//Create raw payload to send
|
|
Notification notification = new Notification("Foodify", "Bạn có 1 đơn hàng mới " + order_number);
|
|
Sender content = new Sender(serverToken.getToken(), notification);
|
|
|
|
mService.sendNotification(content)
|
|
.enqueue(new Callback<MyResponse>() {
|
|
@Override
|
|
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
|
|
|
|
//Only run when get result
|
|
if (response.code() == 200) {
|
|
if (response.body().success == 1) {
|
|
new Database(getBaseContext()).cleanCart();
|
|
Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
|
finish();
|
|
} else {
|
|
Toast.makeText(Cart.this, "Hệ thống bị lỗi. Vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Call<MyResponse> call, Throwable t) {
|
|
Log.e("ERROR", t.getMessage());
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onCancelled(DatabaseError databaseError) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
private void loadListFood() {
|
|
cart = new Database(this).getCarts();
|
|
adapter = new CartAdapter(cart, this);
|
|
adapter.notifyDataSetChanged();
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
public boolean onContextItemSelected(@NonNull MenuItem item) {
|
|
if (item.getTitle().equals(Common.DELETE))
|
|
deleteCart(item.getOrder());
|
|
return true;
|
|
}
|
|
|
|
private void deleteCart(int position) {
|
|
//Remove item at List<Order> by position
|
|
cart.remove(position);
|
|
//Delete all old data from SQLite
|
|
new Database(this).cleanCart();
|
|
//Update new data from List<Order> to SQLite
|
|
for (Order item : cart)
|
|
new Database(this).addToCart(item);
|
|
//Refresh
|
|
loadListFood();
|
|
}
|
|
} |