Skip to content

Commit

Permalink
Merge pull request #11 from Mofazzal874/Apan
Browse files Browse the repository at this point in the history
Observer Design patter implemented
  • Loading branch information
Apn7 authored May 24, 2024
2 parents ff4d364 + 310c746 commit 257e131
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 63 deletions.
3 changes: 0 additions & 3 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ private void addP() {

// 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);
product.setProductDetails(productName, productGenericName, Double.parseDouble(productPrice), productDescription, imageUrl, productType, productDiscount);

// Assuming 'Allproducts' is the Firestore collection
db.collection("Allproducts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -19,6 +16,7 @@
import com.bumptech.glide.Glide;
import com.example.projecto.R;
import com.example.projecto.models.ViewAllModel;
import com.example.projecto.observer.Observer;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
Expand All @@ -29,15 +27,13 @@
import java.util.Calendar;
import java.util.HashMap;

public class DetailActivity extends AppCompatActivity {
public class DetailActivity extends AppCompatActivity implements Observer {

ImageView detailedImg;
TextView name, price, gname,description;

TextView name, price, gname, description;
Button addToCart;
ImageView addItem, removeItem;
Toolbar toolbar;

TextView quantity;

int totalQuantity = 1;
Expand All @@ -55,7 +51,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);


toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand All @@ -72,9 +67,10 @@ public void onClick(View v) {

final Object object = getIntent().getSerializableExtra("detail");

if (object instanceof ViewAllModel){
if (object instanceof ViewAllModel) {
viewAllModel = (ViewAllModel) object;
}

detailedImg = findViewById(R.id.detail_img);
addItem = findViewById(R.id.add_item);
removeItem = findViewById(R.id.remove_item);
Expand All @@ -83,39 +79,33 @@ public void onClick(View v) {
name = findViewById(R.id.detail_name);
gname = findViewById(R.id.detail_gname);
description = findViewById(R.id.detail_des);

quantity = findViewById(R.id.quantity);
addToCart = findViewById(R.id.add_to_cart);

firestore = FirebaseFirestore.getInstance();
auth = FirebaseAuth.getInstance();

if (viewAllModel != null){
if (viewAllModel != null) {
viewAllModel.registerObserver(this);
progressBar.setVisibility(View.GONE);
Glide.with(getApplicationContext()).load(viewAllModel.getImg_url()).into(detailedImg);
name.setText(viewAllModel.getName());
gname.setText(viewAllModel.getGname());
description.setText(viewAllModel.getDescription());
price.setText("Unit Price "+viewAllModel.getPrice()+" Tk");

totalPrice = (double) (viewAllModel.getPrice() * totalQuantity);

update();
}

addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (totalQuantity < 10){
if (totalQuantity < 10) {
totalQuantity++;
quantity.setText(String.valueOf(totalQuantity));
totalPrice = (double) (viewAllModel.getPrice() * totalQuantity);
}
}
});

removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (totalQuantity > 0){
if (totalQuantity > 0) {
totalQuantity--;
quantity.setText(String.valueOf(totalQuantity));
totalPrice = (double) (viewAllModel.getPrice() * totalQuantity);
Expand All @@ -133,12 +123,30 @@ public void onClick(View v) {
addedToCart();
}
});
}

@Override
public void update() {
if (viewAllModel != null) {
Glide.with(getApplicationContext()).load(viewAllModel.getImg_url()).into(detailedImg);
name.setText(viewAllModel.getName());
gname.setText(viewAllModel.getGname());
description.setText(viewAllModel.getDescription());
price.setText("Unit Price " + viewAllModel.getPrice() + " Tk");
totalPrice = (double) (viewAllModel.getPrice() * totalQuantity);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (viewAllModel != null) {
viewAllModel.removeObserver(this);
}
}

private void addedToCart() {
String saveCurrentDate,saveCurrentTime;
String saveCurrentDate, saveCurrentTime;
Calendar calForDate = Calendar.getInstance();

SimpleDateFormat currentDate = new SimpleDateFormat("MM dd, yyyy");
Expand All @@ -147,14 +155,14 @@ private void addedToCart() {
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calForDate.getTime());

final HashMap<String,Object> cartMap = new HashMap<>();
final HashMap<String, Object> cartMap = new HashMap<>();

cartMap.put("productName",viewAllModel.getName());
cartMap.put("productPrice",price.getText().toString());
cartMap.put("currentDate",saveCurrentDate);
cartMap.put("currentTime",saveCurrentTime);
cartMap.put("totalQuantity",quantity.getText().toString());
cartMap.put("totalPrice",totalPrice);
cartMap.put("productName", viewAllModel.getName());
cartMap.put("productPrice", price.getText().toString());
cartMap.put("currentDate", saveCurrentDate);
cartMap.put("currentTime", saveCurrentTime);
cartMap.put("totalQuantity", quantity.getText().toString());
cartMap.put("totalPrice", totalPrice);

firestore.collection("CurrentUser").document(auth.getCurrentUser().getUid())
.collection("AddToCart").add(cartMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
Expand All @@ -168,6 +176,5 @@ public void onComplete(@NonNull Task<DocumentReference> task) {
finish();
}
});

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,8 @@ private void modifyProduct(String productId) {

// 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);
modifiedProduct.setProductDetails(modifiedName, modifiedGenericName, Double.parseDouble(modifiedPrice), modifiedDescription, imageUrl, modifiedType, modifiedDiscount);


// Update the document in Firestore with the modified object
db.collection("Allproducts").document(productId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.projecto.R;
import com.example.projecto.adapters.ViewAllAdapters;
import com.example.projecto.models.ViewAllModel;
import com.example.projecto.observer.Observer;
import com.example.projecto.queries.ProductQuery;
import com.example.projecto.queries.ProductQueryFactory;
import com.google.android.gms.tasks.OnCompleteListener;
Expand All @@ -25,7 +26,7 @@
import java.util.ArrayList;
import java.util.List;

public class ViewAllActivity extends AppCompatActivity {
public class ViewAllActivity extends AppCompatActivity implements Observer {

FirebaseFirestore firestore;
RecyclerView recyclerView;
Expand Down Expand Up @@ -64,6 +65,9 @@ public void onClick(View v) {
viewAllAdapters = new ViewAllAdapters(this, viewAllModelList);
recyclerView.setAdapter(viewAllAdapters);

// Register this activity as an observer
ViewAllModel.getInstance().registerObserver(this);

if (type != null) {
fetchProducts(type);
}
Expand All @@ -88,4 +92,17 @@ public void onComplete(@NonNull Task<QuerySnapshot> task) {
}
});
}

@Override
public void update() {
// Update RecyclerView data
viewAllAdapters.notifyDataSetChanged();
}

@Override
protected void onDestroy() {
super.onDestroy();
// Remove this activity as an observer
ViewAllModel.getInstance().removeObserver(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onBindViewHolder(@NonNull ViewAllAdapters.ViewHolder holder, int pos
@Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("detail", viewAllModelList.get(holder.getAdapterPosition()));
intent.putExtra("detail", (Serializable)viewAllModelList.get(holder.getAdapterPosition()));
context.startActivity(intent);
}
});
Expand Down
63 changes: 50 additions & 13 deletions app/src/main/java/com/example/projecto/models/ViewAllModel.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
package com.example.projecto.models;

import com.example.projecto.observer.Observer;
import com.example.projecto.observer.Subject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ViewAllModel implements Serializable {
public class ViewAllModel implements Subject, Serializable{
private static ViewAllModel instance;

private String name;
private String gname;
private List<Observer> observers = new ArrayList<>();

// Existing fields
private String name, gname, description, img_url, type, discount;
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;
}

// Method to reset the singleton instance for testing
public static void resetInstance() {
instance = null;
@Override
public void registerObserver(Observer observer) {
if (!observers.contains(observer)) {
observers.add(observer);
}
}

@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}

@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}

// Call notifyObservers whenever a product is updated, added, or removed
public void setProductDetails(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;
notifyObservers();
}

// Method to reset the singleton instance for testing
public static void resetInstance() { instance = null; }


// Getters and Setters
// Existing getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
notifyObservers();
}

public String getGname() {
Expand All @@ -45,6 +76,7 @@ public String getGname() {

public void setGname(String gname) {
this.gname = gname;
notifyObservers();
}

public double getPrice() {
Expand All @@ -53,6 +85,7 @@ public double getPrice() {

public void setPrice(double price) {
this.price = price;
notifyObservers();
}

public String getDescription() {
Expand All @@ -61,6 +94,7 @@ public String getDescription() {

public void setDescription(String description) {
this.description = description;
notifyObservers();
}

public String getImg_url() {
Expand All @@ -69,6 +103,7 @@ public String getImg_url() {

public void setImg_url(String img_url) {
this.img_url = img_url;
notifyObservers();
}

public String getType() {
Expand All @@ -77,6 +112,7 @@ public String getType() {

public void setType(String type) {
this.type = type;
notifyObservers();
}

public String getDiscount() {
Expand All @@ -85,5 +121,6 @@ public String getDiscount() {

public void setDiscount(String discount) {
this.discount = discount;
notifyObservers();
}
}
Loading

0 comments on commit 257e131

Please sign in to comment.