Skip to content

Commit

Permalink
refactor/made ConstructionFormData.java into a public class
Browse files Browse the repository at this point in the history
refactor/answer switches (in List<String> answers) give "true"/"false" in firebase
refactor/clean up all comments and unused code
  • Loading branch information
IbrahimSaeedPurdue authored and IbrahimSaeedPurdue committed Mar 4, 2021
1 parent 9c2ca34 commit 5af5a1c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Object> data;
// String contact
// String date
// String inspector
// String location

// (they are lists because firebase doesn't serialize arrays)
// List<String> answers
// List<String> imgLink

public ConstructionFormData(String contact, String date, String inspector,
String location, List<String> answers, List<String> 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<String, Object> getMap() {
return data;
}

@Override
public String toString() {
return "ConstructionFormData{" +
"data=" + data +
'}';
}

}
120 changes: 36 additions & 84 deletions app/src/main/java/com/sf/stormwaterutilityandroid/InspectionForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DocumentReference>() {
@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);
Expand Down Expand Up @@ -162,14 +177,16 @@ 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();
String date = dateInspectedDatePicker.getValue();

ArrayList<String> answersList = new ArrayList<>(20);

String projectRepPresent = getSwitchString(projectRepPresentSwitch);
String projectRepPresent = getSwitchBool(projectRepPresentSwitch);
answersList.add(projectRepPresent);

String projectManager = projectManagerTextField.getValue();
Expand All @@ -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);
Expand All @@ -191,10 +208,10 @@ public void onClick(View view) {
List<String> 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);
Expand All @@ -214,89 +231,24 @@ public void onClick(View view) {
List<String> enforcementAction = enforcementActionPickerSingle.getOptionsSelected();
answersList.addAll(enforcementAction);

String imgLink = new String(evidenceTextField.getValue());

List<String> 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<DocumentReference>() {
@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<String, Object> data = new HashMap<>();

public ConstructionFormData(String contact, String date, String inspector,
String location, List<String> 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<String, Object> 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" +
// '}';
// }
}
}

0 comments on commit 5af5a1c

Please sign in to comment.