Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory Design added on Category items , ViewAllActivity, Query #5

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/appInsightsSettings.xml

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

8 changes: 8 additions & 0 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 @@ -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<ViewAllModel> viewAllModelList;

Toolbar toolbar;

ProgressBar progressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -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<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> 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<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> 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<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> 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<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> 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();
}
});
}

}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;

public class ViewAllAdapters extends RecyclerView.Adapter<ViewAllAdapters.ViewHolder> {

Context context;
List<ViewAllModel> viewAllModelList;

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/projecto/queries/HealthQuery.java
Original file line number Diff line number Diff line change
@@ -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<QuerySnapshot> fetchProducts() {
return firestore.collection("Allproducts").whereEqualTo("type", "health").get();
}
}
Original file line number Diff line number Diff line change
@@ -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<QuerySnapshot> fetchProducts() {
return firestore.collection("Allproducts").whereEqualTo("type", "medication").get();
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/projecto/queries/PersonalQuery.java
Original file line number Diff line number Diff line change
@@ -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<QuerySnapshot> fetchProducts() {
return firestore.collection("Allproducts").whereEqualTo("type", "personal").get();
}
}
Original file line number Diff line number Diff line change
@@ -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<QuerySnapshot> fetchProducts();
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {

}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading