diff --git a/app/src/main/java/com/example/projecto/activities/AddProducts.java b/app/src/main/java/com/example/projecto/activities/AddProducts.java index 54e1ce0..09ef605 100644 --- a/app/src/main/java/com/example/projecto/activities/AddProducts.java +++ b/app/src/main/java/com/example/projecto/activities/AddProducts.java @@ -70,34 +70,39 @@ private void openImagePicker() { } private void addP() { - String productName = name.getText().toString().trim(); - String productDescription = description.getText().toString().trim(); - String productGenericName = gname.getText().toString().trim(); - String productPrice = price.getText().toString().trim(); - String productType = type.getText().toString().trim(); - String productDiscount = discount.getText().toString().trim(); - - if (productName.isEmpty() || productDescription.isEmpty() || productGenericName.isEmpty() - || productPrice.isEmpty() || productType.isEmpty() || productDiscount.isEmpty()) { - Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show(); - return; - } + String productName = name.getText().toString().trim(); + String productDescription = description.getText().toString().trim(); + String productGenericName = gname.getText().toString().trim(); + String productPrice = price.getText().toString().trim(); + String productType = type.getText().toString().trim(); + String productDiscount = discount.getText().toString().trim(); + + if (productName.isEmpty() || productDescription.isEmpty() || productGenericName.isEmpty() + || productPrice.isEmpty() || productType.isEmpty() || productDiscount.isEmpty()) { + Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show(); + return; + } - // Create an instance of your ViewAllModel - ViewAllModel product = new ViewAllModel( - productName, productGenericName, Double.parseDouble(productPrice), - productDescription, imageUrl, productType, productDiscount); - - // Assuming 'Allproducts' is the Firestore collection - db.collection("Allproducts") - .add(product) - .addOnSuccessListener(documentReference -> { - Toast.makeText(AddProducts.this, "Product added successfully", Toast.LENGTH_SHORT).show(); - finish(); // Finish the activity after adding the product - }) - .addOnFailureListener(e -> { - Toast.makeText(AddProducts.this, "Failed to add product", Toast.LENGTH_SHORT).show(); - }); + // Use the singleton instance of ViewAllModel + ViewAllModel product = ViewAllModel.getInstance(); + product.setName(productName); + product.setGname(productGenericName); + product.setPrice(Double.parseDouble(productPrice)); + product.setDescription(productDescription); + product.setImg_url(imageUrl); + product.setType(productType); + product.setDiscount(productDiscount); + + // Assuming 'Allproducts' is the Firestore collection + db.collection("Allproducts") + .add(product) + .addOnSuccessListener(documentReference -> { + Toast.makeText(AddProducts.this, "Product added successfully", Toast.LENGTH_SHORT).show(); + finish(); // Finish the activity after adding the product + }) + .addOnFailureListener(e -> { + Toast.makeText(AddProducts.this, "Failed to add product", Toast.LENGTH_SHORT).show(); + }); } @Override diff --git a/app/src/main/java/com/example/projecto/activities/ModifyProducts.java b/app/src/main/java/com/example/projecto/activities/ModifyProducts.java index 2913a23..7c2afb1 100644 --- a/app/src/main/java/com/example/projecto/activities/ModifyProducts.java +++ b/app/src/main/java/com/example/projecto/activities/ModifyProducts.java @@ -109,10 +109,15 @@ private void modifyProduct(String productId) { String modifiedDiscount = etDiscount.getText().toString().trim(); String modifiedType = etType.getText().toString().trim(); - // Create a ViewAllModel object with the modified data - ViewAllModel modifiedProduct = new ViewAllModel( - modifiedName, modifiedGenericName, Double.parseDouble(modifiedPrice), - modifiedDescription, imageUrl, modifiedType, modifiedDiscount); + // Use the singleton instance of ViewAllModel + ViewAllModel modifiedProduct = ViewAllModel.getInstance(); + modifiedProduct.setName(modifiedName); + modifiedProduct.setGname(modifiedGenericName); + modifiedProduct.setPrice(Double.parseDouble(modifiedPrice)); + modifiedProduct.setDescription(modifiedDescription); + modifiedProduct.setImg_url(imageUrl); + modifiedProduct.setType(modifiedType); + modifiedProduct.setDiscount(modifiedDiscount); // Update the document in Firestore with the modified object db.collection("Allproducts").document(productId) diff --git a/app/src/main/java/com/example/projecto/models/ViewAllModel.java b/app/src/main/java/com/example/projecto/models/ViewAllModel.java index 375231f..e97f3d5 100644 --- a/app/src/main/java/com/example/projecto/models/ViewAllModel.java +++ b/app/src/main/java/com/example/projecto/models/ViewAllModel.java @@ -3,26 +3,28 @@ import java.io.Serializable; public class ViewAllModel implements Serializable { - String name; - String gname; - double price; - String description; - String img_url; - String type; - String discount; - - public ViewAllModel(){} - - public ViewAllModel(String name, String gname, double price, String description, String img_url, String type, String discount) { - this.name = name; - this.gname = gname; - this.price = price; - this.description = description; - this.img_url = img_url; - this.type = type; - this.discount = discount; + private static ViewAllModel instance; + + private String name; + private String gname; + private double price; + private String description; + private String img_url; + private String type; + private String discount; + + // Private constructor to prevent instantiation + private ViewAllModel() {} + + // Static method to get the singleton instance + public static synchronized ViewAllModel getInstance() { + if (instance == null) { + instance = new ViewAllModel(); + } + return instance; } + // Getters and Setters public String getName() { return name; }