diff --git a/app/src/main/java/com/sf/stormwaterutilityandroid/ConstructionFormData.java b/app/src/main/java/com/sf/stormwaterutilityandroid/ConstructionFormData.java new file mode 100644 index 0000000..a66ec46 --- /dev/null +++ b/app/src/main/java/com/sf/stormwaterutilityandroid/ConstructionFormData.java @@ -0,0 +1,40 @@ +package com.sf.stormwaterutilityandroid; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ConstructionFormData { + private Map data; + // String contact + // String date + // String inspector + // String location + + // (they are lists because firebase doesn't serialize arrays) + // List answers + // List imgLink + + public ConstructionFormData(String contact, String date, String inspector, + String location, List answers, List imgLink) { + data = new HashMap<>(); + data.put("contact", contact); + data.put("date", date); + data.put("inspector", inspector); + data.put("location", location); + data.put("answers", answers); + data.put("imgLink", imgLink); + } + + public Map getMap() { + return data; + } + + @Override + public String toString() { + return "ConstructionFormData{" + + "data=" + data + + '}'; + } + +} diff --git a/app/src/main/java/com/sf/stormwaterutilityandroid/InspectionForm.java b/app/src/main/java/com/sf/stormwaterutilityandroid/InspectionForm.java index a9f8580..f8a16f1 100644 --- a/app/src/main/java/com/sf/stormwaterutilityandroid/InspectionForm.java +++ b/app/src/main/java/com/sf/stormwaterutilityandroid/InspectionForm.java @@ -15,9 +15,7 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import me.riddhimanadib.formmaster.FormBuilder; import me.riddhimanadib.formmaster.model.BaseFormElement; @@ -45,6 +43,23 @@ protected void onCreate(Bundle savedInstanceState) { setupForm(); } + private void uploadToFirestore(ConstructionFormData constructionFormData) { + FirebaseFirestore db = FirebaseFirestore.getInstance(); + db.collection("ConstructionSiteForm") + .add(constructionFormData.getMap()) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(DocumentReference documentReference) { + System.out.println("SUCCESSFUL - FIRESTORE"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + System.out.println("FAILED - FIRESTORE: " + e); + } + }); + } private void setupForm() { formBuilder = new FormBuilder(this, recyclerView); @@ -162,6 +177,8 @@ private void setupForm() { submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + + // data retrieval from forms String contact = projectTextField.getValue(); String location = addressTextField.getValue(); String inspector = inspectorTextField.getValue(); @@ -169,7 +186,7 @@ public void onClick(View view) { ArrayList answersList = new ArrayList<>(20); - String projectRepPresent = getSwitchString(projectRepPresentSwitch); + String projectRepPresent = getSwitchBool(projectRepPresentSwitch); answersList.add(projectRepPresent); String projectManager = projectManagerTextField.getValue(); @@ -179,10 +196,10 @@ public void onClick(View view) { answersList.add(siteManager); answersList.add(projectOwner); - String correctiveAction1 = getSwitchString(correctiveAction1Switch); - String correctiveAction2 = getSwitchString(correctiveAction2Switch); - String correctiveAction3 = getSwitchString(correctiveAction3Switch); - String correctiveAction4 = getSwitchString(correctiveAction4Switch); + String correctiveAction1 = getSwitchBool(correctiveAction1Switch); + String correctiveAction2 = getSwitchBool(correctiveAction2Switch); + String correctiveAction3 = getSwitchBool(correctiveAction3Switch); + String correctiveAction4 = getSwitchBool(correctiveAction4Switch); answersList.add(correctiveAction1); answersList.add(correctiveAction2); answersList.add(correctiveAction3); @@ -191,10 +208,10 @@ public void onClick(View view) { List correctiveAction5 = correctiveAction5Multi.getOptionsSelected(); answersList.addAll(correctiveAction5); - String correctiveAction6 = getSwitchString(correctiveAction6Switch); - String correctiveAction7 = getSwitchString(correctiveAction6Switch); - String correctiveAction8 = getSwitchString(correctiveAction8Switch); - String correctiveAction9 = getSwitchString(correctiveAction9Switch); + String correctiveAction6 = getSwitchBool(correctiveAction6Switch); + String correctiveAction7 = getSwitchBool(correctiveAction6Switch); + String correctiveAction8 = getSwitchBool(correctiveAction8Switch); + String correctiveAction9 = getSwitchBool(correctiveAction9Switch); answersList.add(correctiveAction6); answersList.add(correctiveAction7); answersList.add(correctiveAction8); @@ -214,89 +231,24 @@ public void onClick(View view) { List enforcementAction = enforcementActionPickerSingle.getOptionsSelected(); answersList.addAll(enforcementAction); - String imgLink = new String(evidenceTextField.getValue()); - + List imgLink = new ArrayList<>(); + imgLink.add(evidenceTextField.getValue()); String[] answers = answersList.toArray(new String[0]); ConstructionFormData constructionFormData = new ConstructionFormData(contact, date, inspector, location, answersList, imgLink); - FirebaseFirestore db = FirebaseFirestore.getInstance(); - db.collection("ConstructionSiteForm") - .add(constructionFormData.getMap()) - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(DocumentReference documentReference) { - System.out.println("ibra SUCCESSFUL - FIRESTORE"); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception e) { - System.out.println("ibra SUCCESSFUL - ERROR/FAILED: " + e); - } - }); - - // System.out.println(constructionFormData); + + // submit data to firestore + uploadToFirestore(constructionFormData); } }); } - private String getSwitchString(FormElementSwitch switchElement) { - System.out.println(switchElement.getValue()); - if (switchElement.getValue().equals("Yes")) return switchElement.getTitle(); - return ""; + private String getSwitchBool(FormElementSwitch switchElement) { + if (switchElement.getValue().equals("Yes")) return "true"; + return "false"; } - private class ConstructionFormData { -// String contact = ""; -// String date = ""; -// String inspector = ""; -// String location = ""; -// String[] answers; - // SortTimeStamp set in constructor -// Date SortTimeStamp; -// -// String[] imgLink; - - Map data = new HashMap<>(); - - public ConstructionFormData(String contact, String date, String inspector, - String location, List answers, String imgLink) { - data.put("contact", contact); - data.put("date", date); - data.put("inspector", inspector); - data.put("location", location); - data.put("answers", answers); - data.put("imgLink", imgLink); - -// this.contact = contact; -// this.date = date; -// this.inspector = inspector; -// this.location = location; -// this.answers = answers; -// -// this.SortTimeStamp = new Date(System.currentTimeMillis()); -// -// this.answers = answers; - } - - public Map getMap() { - return data; - } - -// @Override -// public String toString() { -// return "ConstructionFormData{" + -// "contact=" + contact + '\n' + -// ", date=" + date + '\n' + -// ", inspector=" + inspector + '\n' + -// ", location=" + location + '\n' + -// ", answers=" + Arrays.toString(answers) + "\n" + -// ", SortTimeStamp=" + SortTimeStamp + "\n" + -// ", imgLink=" + Arrays.toString(imgLink) + "\n" + -// '}'; -// } - } }