Search in sources :

Example 1 with CartActivityAdapter

use of com.example.asus.onlinecanteen.adapter.CartActivityAdapter in project OnlineCanteen by josephgunawan97.

the class CartActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    cart = (ArrayList<Cart>) getIntent().getSerializableExtra("Cart");
    cartActivityAdapter = new CartActivityAdapter(this, cart);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    walletUtil = new WalletUtil();
    notificationRef = FirebaseDatabase.getInstance().getReference().child("notifications");
    // Calculating the grand total
    for (Cart c : cart) {
        total += c.getTotalPrice(c);
    }
    // Initialize ListView
    cartList = findViewById(R.id.cartList);
    grandTotal = findViewById(R.id.grandTotal);
    deliveryFee = findViewById(R.id.deliveryFee);
    orderButton = findViewById(R.id.OrderButton);
    locationEditText = findViewById(R.id.userLocation);
    // Set views
    grandTotal.setText("TOTAL: Rp " + total);
    deliveryFee.setText("Delivery Fee : Rp ");
    cartList.setAdapter(cartActivityAdapter);
    orderButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!locationEditText.getText().toString().isEmpty()) {
                // Make new transaction
                walletRef = FirebaseDatabase.getInstance().getReference().child("wallet");
                walletRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            String valueString = dataSnapshot.getValue().toString();
                            int walletCash = Integer.parseInt(valueString);
                            // if wallet money >= total
                            if (walletCash >= total) {
                                ArrayList<PurchasedItem> items = new ArrayList<>();
                                for (Cart c : cart) {
                                    PurchasedItem item = new PurchasedItem(c.getProductName(), c.getProductPrice(), c.getQuantity());
                                    items.add(item);
                                }
                                Intent intent = getIntent();
                                Transaction transaction = new Transaction(intent.getStringExtra("Seller"), FirebaseAuth.getInstance().getUid(), items, locationEditText.getText().toString());
                                TransactionUtil.insert(transaction);
                                Toast.makeText(getApplicationContext(), "Transcation done", Toast.LENGTH_SHORT).show();
                                setResult(RESULT_OK);
                                // Make notification to the seller
                                HashMap<String, String> notificationData = new HashMap<>();
                                notificationData.put("from", transaction.getUid());
                                notificationData.put("type", "new order");
                                notificationRef.child(transaction.getSid()).push().setValue(notificationData);
                                // Go back to main menu
                                finish();
                            } else {
                                Toast.makeText(getApplicationContext(), "Not enough wallet cash, please top-up at our counter", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            } else {
                // Alert dialog if the location is not filled in
                AlertDialog.Builder builder = new AlertDialog.Builder(CartActivity.this);
                builder.setMessage("Please enter your location!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.setTitle("Error");
                alert.show();
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) CartActivityAdapter(com.example.asus.onlinecanteen.adapter.CartActivityAdapter) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) ValueEventListener(com.google.firebase.database.ValueEventListener) WalletUtil(com.example.asus.onlinecanteen.utils.WalletUtil) PurchasedItem(com.example.asus.onlinecanteen.model.PurchasedItem) Cart(com.example.asus.onlinecanteen.model.Cart)

Aggregations

DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 CartActivityAdapter (com.example.asus.onlinecanteen.adapter.CartActivityAdapter)1 Cart (com.example.asus.onlinecanteen.model.Cart)1 PurchasedItem (com.example.asus.onlinecanteen.model.PurchasedItem)1 Transaction (com.example.asus.onlinecanteen.model.Transaction)1 WalletUtil (com.example.asus.onlinecanteen.utils.WalletUtil)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1