Skip to content

Commit

Permalink
singleTon on ViewAllModel is completed..Testing is not done yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Apn7 committed May 18, 2024
1 parent 06fd461 commit 9c4b708
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
59 changes: 32 additions & 27 deletions app/src/main/java/com/example/projecto/activities/AddProducts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 20 additions & 18 deletions app/src/main/java/com/example/projecto/models/ViewAllModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9c4b708

Please sign in to comment.