Skip to content

Commit

Permalink
Add No Categories, and make some parsings more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
theKidOfArcrania committed Sep 14, 2016
1 parent 5c3b530 commit ce6233b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
13 changes: 11 additions & 2 deletions app/src/main/java/app/sunstreak/yourpisd/ClassSwipeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void onClick(View arg0) {
builder.setTitle(view.getName());

StringBuilder msg = new StringBuilder();
msg.append(view.getCategory().getType())
msg.append(view.getCategory() == null ? "No category" : view.getCategory().getType())
.append("\nDue Date: " + DateHelper.daysRelative(view.getDueDate()))
.append("\nWeight: x" + String.format("%.1f", view.getWeight()));
try {
Expand Down Expand Up @@ -415,8 +415,17 @@ public void onClick(DialogInterface dialog, int id) {

LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

boolean hasNoCategoryGrades = false;
for (Assignment grade : mTermReport.getAssignments())
if (grade.getCategory().getType().equals(GradeCategory.NO_CATEGORY))
{
hasNoCategoryGrades = true;
break;
}

for (GradeCategory category : mTermReport.getCategories()) {
if (category.getType().equals(GradeCategory.NO_CATEGORY) && !hasNoCategoryGrades)
continue;
LinearLayout card = new LinearLayout(getActivity());
card.setOrientation(LinearLayout.VERTICAL);
card.setBackgroundResource(R.drawable.card_custom);
Expand All @@ -427,7 +436,7 @@ public void onClick(DialogInterface dialog, int id) {
// for every grade in this term [any category]
for (Assignment grade : mTermReport.getAssignments()) {
// only if this grade is in the category which we're looking for
if (grade.getCategory().equals(category)) {
if (category.equals(grade.getCategory())) {
LinearLayout innerLayout = (LinearLayout) inflater.inflate(R.layout.class_swipe_grade_view, card, false);

TextView descriptionView = (TextView) innerLayout.findViewById(R.id.description);
Expand Down
47 changes: 23 additions & 24 deletions app/src/main/java/app/sunstreak/yourpisd/net/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ public static void parseTermReport(String html, TermReport report) {
Element main = doc.getElementById("Main").getElementById("Content").getElementById("ContentMain");

//refill grade categories and assignments
report.getCategories().clear();
List<GradeCategory> categories = report.getCategories();
categories.clear();
report.getAssignments().clear();

Element categoryTable = main.getElementById("Categories");
if (categoryTable != null && categoryTable.children().size() > 0) {
//For each <TR> element
for (Element category : categoryTable.children().get(0).children()) {
report.getCategories().add(new GradeCategory(category.getElementsByClass("description").get(0).html().split("\n")[0].trim(), Integer.parseInt(category.getElementsByClass("percent").get(0).html().replaceAll("[^0-9]", "")) * 0.01));
report.getCategories().get(report.getCategories().size() - 1).setGrade(Integer.parseInt(category.getElementsByClass("letter").get(1).child(0).child(0).child(0).html().replace("%", "")));
categories.add(new GradeCategory(category.getElementsByClass("description").get(0).html().split("\n")[0].trim(), Integer.parseInt(category.getElementsByClass("percent").get(0).html().replaceAll("[^0-9]", "")) * 0.01));
categories.get(categories.size() - 1).setGrade(Integer.parseInt(category.getElementsByClass("letter").get(1).child(0).child(0).child(0).html().replace("%", "")));
//Log.d("testTag", report.getCategories().get(report.getCategories().size() - 1).getGrade() + " " + report.getCategories().get(report.getCategories().size() - 1).getWeight() + " " + report.getCategories().get(report.getCategories().size() - 1).getType());
}

}

GradeCategory noCategory = new GradeCategory(GradeCategory.NO_CATEGORY, 0);
categories.add(noCategory); //Put no-category grades at the end (if there are any).

Element assignments = main.getElementById("Assignments");
if (assignments != null && assignments.children().size() > 0)
{
Expand All @@ -65,33 +68,29 @@ public static void parseTermReport(String html, TermReport report) {
{
String name = org.jsoup.parser.Parser.unescapeEntities(assignment.getElementsByClass("title").get(0).html().replaceAll("&amp;", "&"), true);

GradeCategory category = report.getCategories().get(0);
String temp = "";
try
Elements divCategory = assignment.getElementsByClass("category");
GradeCategory category = noCategory;
if (!divCategory.isEmpty())
{
temp = assignment.getElementsByClass("category").get(0).html();
} catch (Exception e)
{
e.printStackTrace();
}
for (GradeCategory cc : report.getCategories())
{
if (cc.getType().equals(temp ))
String catName = divCategory.get(0).html();
for (GradeCategory cc : categories)
{
category = cc;
break;
if (cc.getType().equals(catName))
{
category = cc;
break;
}
}
}

final int year = 2016; //TODO: year of fall semester
final String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
try
{
month = Arrays.asList(months).indexOf(assignment.getElementsByClass("m").get(0).html()) + 1;
day = Integer.parseInt(assignment.getElementsByClass("m").get(0).parent().html().replaceAll("[^0-9]", ""));
} catch (Exception e)

Elements spanDate = assignment.getElementsByClass("m");
if (!spanDate.isEmpty())
{
//e.printStackTrace();
month = Arrays.asList(months).indexOf(spanDate.get(0).html()) + 1;
day = Integer.parseInt(spanDate.get(0).parent().html().replaceAll("[^0-9]", ""));
}
DateTime date = new DateTime(year, month, day, 0, 0);
if (date.getMonthOfYear() < 7) //spring semester
Expand All @@ -106,7 +105,7 @@ public static void parseTermReport(String html, TermReport report) {
else
newAssignment.setWeight(Double.parseDouble(weight.get(0).html().replaceAll("[^0-9]", "")));

temp = assignment.getElementsByClass("points").get(0).html();
String temp = assignment.getElementsByClass("points").get(0).html();
if (temp.isEmpty())
newAssignment.setGrade(-1); //TODO: Grade doesn't exist
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Represents a grade category
*/
public class GradeCategory {
public static String NO_CATEGORY = "No category";

private final String type;
private final double weight;
private int grade = -1;
Expand Down

0 comments on commit ce6233b

Please sign in to comment.