Search in sources :

Example 1 with Product

use of com.example.asus.onlinecanteen.model.Product in project OnlineCanteen by josephgunawan97.

the class DeleteProductAdapter method onBindViewHolder.

/**
 * Bind the view with data at the specified position
 * @param holder ViewHolder which should be updated
 * @param position position of items in the adapter
 */
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // Get Transaction Item
    Product product = productList.get(position);
    // Set Information on View
    holder.userNameTextView.setText(product.getName());
    holder.priceTextView.setText("Rp." + String.valueOf(product.getPrice()));
    if (holder.checkBox.isChecked())
        product.setChecked(true);
    else
        product.setChecked(false);
    if (product.getImageUrl() != null) {
        Glide.with(holder.imageView.getContext()).load(product.getImageUrl()).into(holder.imageView);
    }
}
Also used : Product(com.example.asus.onlinecanteen.model.Product)

Example 2 with Product

use of com.example.asus.onlinecanteen.model.Product in project OnlineCanteen by josephgunawan97.

the class MainActivity method attachDatabaseReadListener.

private void attachDatabaseReadListener() {
    if (productEventListener == null) {
        productEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Product product = dataSnapshot.getValue(Product.class);
                menuListAdapter.add(product);
            }

            @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) {
            }
        };
        databaseProducts.addChildEventListener(productEventListener);
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Product(com.example.asus.onlinecanteen.model.Product) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 3 with Product

use of com.example.asus.onlinecanteen.model.Product in project OnlineCanteen by josephgunawan97.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Initialize Navigation View
    DrawerLayout drawerLayout = findViewById(R.id.main_drawer_layout);
    ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_open, R.string.navigation_close);
    drawerLayout.addDrawerListener(drawerToggle);
    drawerToggle.syncState();
    NavigationView navigationView = findViewById(R.id.main_navigation_view);
    navigationView.setNavigationItemSelectedListener(new MainNavigationListener());
    // Get User
    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getCurrentUser();
    // Initialize References
    databaseUsers = FirebaseDatabase.getInstance().getReference("users");
    databaseProducts = FirebaseDatabase.getInstance().getReference("products");
    databaseStore = FirebaseDatabase.getInstance().getReference("store");
    databaseWallet = FirebaseDatabase.getInstance().getReference("wallet");
    // Product List
    ArrayList<Product> productArrayList = new ArrayList<>();
    menuListAdapter = new MenuListAdapter(this, productArrayList);
    // Initialize ListView
    productListView = findViewById(R.id.list);
    productListView.setAdapter(menuListAdapter);
    // Cart Button and Click Handler
    placeOrder = findViewById(R.id.OrderButton);
    placeOrder.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cart = menuListAdapter.getList();
            // Check empty cart
            boolean emptyCart = true;
            for (Cart c : cart) {
                if (c.getQuantity() != 0) {
                    emptyCart = false;
                    break;
                }
            }
            if (emptyCart == true) {
                // Alert dialog if there are no items in cart
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("You don't have any items in your cart!").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();
            } else {
                // Remove item if qty is 0
                ArrayList<Cart> toRemove = new ArrayList<>();
                for (Cart c : cart) {
                    if (c.getQuantity() == 0) {
                        toRemove.add(c);
                    }
                }
                cart.removeAll(toRemove);
                // Go to cart
                Intent intent = new Intent(MainActivity.this, CartActivity.class);
                intent.putExtra("Cart", cart);
                intent.putExtra("Seller", menuListAdapter.getSeller());
                startActivity(intent);
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) NavigationView(android.support.design.widget.NavigationView) DialogInterface(android.content.DialogInterface) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ArrayList(java.util.ArrayList) Product(com.example.asus.onlinecanteen.model.Product) Intent(android.content.Intent) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) DrawerLayout(android.support.v4.widget.DrawerLayout) MenuListAdapter(com.example.asus.onlinecanteen.adapter.MenuListAdapter) Cart(com.example.asus.onlinecanteen.model.Cart)

Example 4 with Product

use of com.example.asus.onlinecanteen.model.Product in project OnlineCanteen by josephgunawan97.

the class MerchantOrderDetailActivity method decreaseStock.

private void decreaseStock(final HashMap<String, HashMap<String, Integer>> details) {
    final ArrayList<Object> keys = new ArrayList<>(Arrays.asList(details.keySet().toArray()));
    final HashMap<String, Product> products = new HashMap<>();
    Query productQuery = ProductsUtil.query("tokoId", transaction.getSid());
    productQuery.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Product product = snapshot.getValue(Product.class);
                for (Object keyObject : keys) {
                    String key = (String) keyObject;
                    if (product.getName().equals(key)) {
                        snapshot.child("stock").getRef().setValue(product.getStock() - details.get(key).get("quantity"));
                        keys.remove(key);
                    }
                }
            }
            if (keys.size() == 0) {
                Toast.makeText(MerchantOrderDetailActivity.this, "Order Accepted", Toast.LENGTH_SHORT).show();
                findViewById(R.id.progress_bar_layout).setVisibility(View.GONE);
                finish();
            } else {
                Toast.makeText(MerchantOrderDetailActivity.this, "Not all stock updated", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Query(com.google.firebase.database.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(com.example.asus.onlinecanteen.model.Product) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 5 with Product

use of com.example.asus.onlinecanteen.model.Product in project OnlineCanteen by josephgunawan97.

the class EditProductActivity method getProduct.

public void getProduct() {
    if (productEventListener == null) {
        productEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Product product = dataSnapshot.getValue(Product.class);
                if (merchant.getUid().equals(product.getTokoId())) {
                    productArrayList.add(product.getName().toString());
                }
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, productArrayList);
                spinner.setAdapter(dataAdapter);
            }

            @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) {
            }
        };
        databaseProducts.addChildEventListener(productEventListener);
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Product(com.example.asus.onlinecanteen.model.Product) DataSnapshot(com.google.firebase.database.DataSnapshot) ArrayAdapter(android.widget.ArrayAdapter) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

Product (com.example.asus.onlinecanteen.model.Product)19 DataSnapshot (com.google.firebase.database.DataSnapshot)12 DatabaseError (com.google.firebase.database.DatabaseError)12 ValueEventListener (com.google.firebase.database.ValueEventListener)7 ChildEventListener (com.google.firebase.database.ChildEventListener)6 DatabaseReference (com.google.firebase.database.DatabaseReference)4 Query (com.google.firebase.database.Query)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Activity (android.app.Activity)2 View (android.view.View)2 ListView (android.widget.ListView)2 Cart (com.example.asus.onlinecanteen.model.Cart)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)2 StorageReference (com.google.firebase.storage.StorageReference)2 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1