diff --git a/.idea/appInsightsSettings.xml b/.idea/appInsightsSettings.xml new file mode 100644 index 0000000..6bbe2ae --- /dev/null +++ b/.idea/appInsightsSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index b268ef3..16d5e86 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -4,6 +4,14 @@ diff --git a/app/src/main/java/com/example/projecto/activities/ViewAllActivity.java b/app/src/main/java/com/example/projecto/activities/ViewAllActivity.java index 5f05776..480ce97 100644 --- a/app/src/main/java/com/example/projecto/activities/ViewAllActivity.java +++ b/app/src/main/java/com/example/projecto/activities/ViewAllActivity.java @@ -14,30 +14,26 @@ import com.example.projecto.R; import com.example.projecto.adapters.ViewAllAdapters; import com.example.projecto.models.ViewAllModel; +import com.example.projecto.queries.ProductQuery; +import com.example.projecto.queries.ProductQueryFactory; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.QuerySnapshot; -import com.google.firebase.ktx.Firebase; import java.util.ArrayList; import java.util.List; public class ViewAllActivity extends AppCompatActivity { - FirebaseFirestore firestore; RecyclerView recyclerView; ViewAllAdapters viewAllAdapters; - List viewAllModelList; - Toolbar toolbar; - ProgressBar progressBar; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -68,65 +64,28 @@ public void onClick(View v) { viewAllAdapters = new ViewAllAdapters(this, viewAllModelList); recyclerView.setAdapter(viewAllAdapters); - if (type != null && type.equalsIgnoreCase("medication")){ - firestore.collection("Allproducts").whereEqualTo("type","medication").get().addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) { - ViewAllModel viewAllModel = documentSnapshot.toObject(ViewAllModel.class); - viewAllModelList.add(viewAllModel); - viewAllAdapters.notifyDataSetChanged(); - progressBar.setVisibility(View.GONE); - recyclerView.setVisibility(View.VISIBLE); - } - } - else{ - Toast.makeText(ViewAllActivity.this, ""+task.getException(), Toast.LENGTH_SHORT).show(); - } - } - }); - } - - if (type != null && type.equalsIgnoreCase("health")){ - firestore.collection("Allproducts").whereEqualTo("type","health").get().addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) { - ViewAllModel viewAllModel = documentSnapshot.toObject(ViewAllModel.class); - viewAllModelList.add(viewAllModel); - viewAllAdapters.notifyDataSetChanged(); - progressBar.setVisibility(View.GONE); - recyclerView.setVisibility(View.VISIBLE); - } - } - else{ - Toast.makeText(ViewAllActivity.this, ""+task.getException(), Toast.LENGTH_SHORT).show(); - } - } - }); + if (type != null) { + fetchProducts(type); } + } - if (type != null && type.equalsIgnoreCase("personal")){ - firestore.collection("Allproducts").whereEqualTo("type","personal").get().addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) { - ViewAllModel viewAllModel = documentSnapshot.toObject(ViewAllModel.class); - viewAllModelList.add(viewAllModel); - viewAllAdapters.notifyDataSetChanged(); - progressBar.setVisibility(View.GONE); - recyclerView.setVisibility(View.VISIBLE); - } - } - else{ - Toast.makeText(ViewAllActivity.this, ""+task.getException(), Toast.LENGTH_SHORT).show(); + private void fetchProducts(String type) { + ProductQuery productQuery = ProductQueryFactory.createQuery(type, firestore); + productQuery.fetchProducts().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) { + ViewAllModel viewAllModel = documentSnapshot.toObject(ViewAllModel.class); + viewAllModelList.add(viewAllModel); + viewAllAdapters.notifyDataSetChanged(); } + progressBar.setVisibility(View.GONE); + recyclerView.setVisibility(View.VISIBLE); + } else { + Toast.makeText(ViewAllActivity.this, "" + task.getException(), Toast.LENGTH_SHORT).show(); } - }); - } - + } + }); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/projecto/adapters/ViewAllAdapters.java b/app/src/main/java/com/example/projecto/adapters/ViewAllAdapters.java index 522e4ae..cb96732 100644 --- a/app/src/main/java/com/example/projecto/adapters/ViewAllAdapters.java +++ b/app/src/main/java/com/example/projecto/adapters/ViewAllAdapters.java @@ -21,7 +21,6 @@ import java.util.List; public class ViewAllAdapters extends RecyclerView.Adapter { - Context context; List viewAllModelList; diff --git a/app/src/main/java/com/example/projecto/queries/HealthQuery.java b/app/src/main/java/com/example/projecto/queries/HealthQuery.java new file mode 100644 index 0000000..8af75d2 --- /dev/null +++ b/app/src/main/java/com/example/projecto/queries/HealthQuery.java @@ -0,0 +1,18 @@ +package com.example.projecto.queries; + +import com.google.android.gms.tasks.Task; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.QuerySnapshot; + +public class HealthQuery implements ProductQuery { + private FirebaseFirestore firestore; + + public HealthQuery(FirebaseFirestore firestore) { + this.firestore = firestore; + } + + @Override + public Task fetchProducts() { + return firestore.collection("Allproducts").whereEqualTo("type", "health").get(); + } +} diff --git a/app/src/main/java/com/example/projecto/queries/MedicationQuery.java b/app/src/main/java/com/example/projecto/queries/MedicationQuery.java new file mode 100644 index 0000000..fee2817 --- /dev/null +++ b/app/src/main/java/com/example/projecto/queries/MedicationQuery.java @@ -0,0 +1,18 @@ +package com.example.projecto.queries; + +import com.google.android.gms.tasks.Task; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.QuerySnapshot; + +public class MedicationQuery implements ProductQuery { + private FirebaseFirestore firestore; + + public MedicationQuery(FirebaseFirestore firestore) { + this.firestore = firestore; + } + + @Override + public Task fetchProducts() { + return firestore.collection("Allproducts").whereEqualTo("type", "medication").get(); + } +} diff --git a/app/src/main/java/com/example/projecto/queries/PersonalQuery.java b/app/src/main/java/com/example/projecto/queries/PersonalQuery.java new file mode 100644 index 0000000..1531d92 --- /dev/null +++ b/app/src/main/java/com/example/projecto/queries/PersonalQuery.java @@ -0,0 +1,18 @@ +package com.example.projecto.queries; + +import com.google.android.gms.tasks.Task; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.QuerySnapshot; + +public class PersonalQuery implements ProductQuery { + private FirebaseFirestore firestore; + + public PersonalQuery(FirebaseFirestore firestore) { + this.firestore = firestore; + } + + @Override + public Task fetchProducts() { + return firestore.collection("Allproducts").whereEqualTo("type", "personal").get(); + } +} diff --git a/app/src/main/java/com/example/projecto/queries/ProductQuery.java b/app/src/main/java/com/example/projecto/queries/ProductQuery.java new file mode 100644 index 0000000..1a7fe09 --- /dev/null +++ b/app/src/main/java/com/example/projecto/queries/ProductQuery.java @@ -0,0 +1,8 @@ +package com.example.projecto.queries; + +import com.google.android.gms.tasks.Task; +import com.google.firebase.firestore.QuerySnapshot; + +public interface ProductQuery { + Task fetchProducts(); +} diff --git a/app/src/main/java/com/example/projecto/queries/ProductQueryFactory.java b/app/src/main/java/com/example/projecto/queries/ProductQueryFactory.java new file mode 100644 index 0000000..ebc7403 --- /dev/null +++ b/app/src/main/java/com/example/projecto/queries/ProductQueryFactory.java @@ -0,0 +1,18 @@ +package com.example.projecto.queries; + +import com.google.firebase.firestore.FirebaseFirestore; + +public class ProductQueryFactory { + public static ProductQuery createQuery(String type, FirebaseFirestore firestore) { + switch (type.toLowerCase()) { + case "medication": + return new MedicationQuery(firestore); + case "health": + return new HealthQuery(firestore); + case "personal": + return new PersonalQuery(firestore); + default: + throw new IllegalArgumentException("Unknown type: " + type); + } + } +} diff --git a/app/src/test/java/com/example/projecto/activities/ViewAllActivityTest.java b/app/src/test/java/com/example/projecto/activities/ViewAllActivityTest.java new file mode 100644 index 0000000..a4e2c31 --- /dev/null +++ b/app/src/test/java/com/example/projecto/activities/ViewAllActivityTest.java @@ -0,0 +1,15 @@ +package com.example.projecto.activities; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + + +import com.example.projecto.models.ViewAllModel; + + +public class ViewAllActivityTest { + +} diff --git a/build.gradle.kts b/build.gradle.kts index c3c976d..0cb3d73 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,4 +7,4 @@ buildscript { // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id("com.android.application") version "8.4.0" apply false -} \ No newline at end of file +}