1
0

Fix grade regex (#91)

This commit is contained in:
Mikołaj Pich
2018-04-27 01:36:40 +02:00
committed by Rafał Borcz
parent 7dde13585c
commit 3443b01b9a
7 changed files with 64 additions and 52 deletions

View File

@ -10,8 +10,6 @@ import org.greenrobot.greendao.annotation.Property;
import java.io.Serializable;
import io.github.wulkanowy.R;
@Entity(
nameInDb = "Grades",
active = true
@ -94,31 +92,6 @@ public class Grade implements Serializable {
this.read = read;
}
public int getValueColor() {
String replacedString = value.replaceAll("[^0-9]", "");
if (!"".equals(replacedString)) {
switch (Integer.parseInt(replacedString)) {
case 6:
return R.color.six_grade;
case 5:
return R.color.five_grade;
case 4:
return R.color.four_grade;
case 3:
return R.color.three_grade;
case 2:
return R.color.two_grade;
case 1:
return R.color.one_grade;
default:
return R.color.default_grade;
}
}
return R.color.default_grade;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -18,7 +18,7 @@ import eu.davidea.viewholders.ExpandableViewHolder;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.utils.AnimationUtils;
import io.github.wulkanowy.utils.AverageCalculator;
import io.github.wulkanowy.utils.GradeUtils;
public class GradeHeaderItem
extends AbstractExpandableHeaderItem<GradeHeaderItem.HeaderViewHolder, GradesSubItem> {
@ -168,7 +168,7 @@ public class GradeHeaderItem
}
private String getGradesAverageString(Subject item) {
float average = AverageCalculator.calculate(item.getGradeList());
float average = GradeUtils.calculate(item.getGradeList());
if (average < 0) {
return resources.getString(R.string.info_no_average);

View File

@ -16,6 +16,7 @@ import butterknife.OnClick;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.utils.CommonUtils;
import io.github.wulkanowy.utils.GradeUtils;
public class GradesDialogFragment extends DialogFragment {
@ -76,7 +77,7 @@ public class GradesDialogFragment extends DialogFragment {
subject.setText(grade.getSubject());
value.setText(grade.getValue());
value.setBackgroundResource(grade.getValueColor());
value.setBackgroundResource(GradeUtils.getValueColor(grade.getValue()));
weight.setText(grade.getWeight());
date.setText(grade.getDate());
color.setText(CommonUtils.colorHexToColorName(grade.getColor()));

View File

@ -18,6 +18,7 @@ import eu.davidea.flexibleadapter.items.AbstractSectionableItem;
import eu.davidea.viewholders.FlexibleViewHolder;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.utils.GradeUtils;
public class GradesSubItem
extends AbstractSectionableItem<GradesSubItem.SubItemViewHolder, GradeHeaderItem> {
@ -108,7 +109,7 @@ public class GradesSubItem
this.subjectAlertImage = subjectAlertImage;
value.setText(item.getValue());
value.setBackgroundResource(item.getValueColor());
value.setBackgroundResource(GradeUtils.getValueColor(item.getValue()));
date.setText(item.getDate());
description.setText(getDescriptionString());
alert.setVisibility(item.getRead() ? View.INVISIBLE : View.VISIBLE);

View File

@ -1,12 +1,18 @@
package io.github.wulkanowy.utils;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade;
public final class AverageCalculator {
public final class GradeUtils {
private AverageCalculator() {
private final static Pattern validGradePattern = Pattern.compile("^(\\++|-|--|=)?[0-6](\\++|-|--|=)?$");
private final static Pattern simpleGradeValuePattern = Pattern.compile("([0-6])");
private GradeUtils() {
throw new IllegalStateException("Utility class");
}
@ -58,4 +64,33 @@ public final class AverageCalculator {
private static int getIntegerForWeightOfGrade(String weightOfGrade) {
return Integer.valueOf(weightOfGrade.substring(0, weightOfGrade.length() - 3));
}
public static int getValueColor(String value) {
Matcher m1 = validGradePattern.matcher(value);
if (!m1.find()) {
return R.color.default_grade;
}
Matcher m2 = simpleGradeValuePattern.matcher(m1.group());
if (!m2.find()) {
return R.color.default_grade;
}
switch (Integer.parseInt(m2.group())) {
case 6:
return R.color.six_grade;
case 5:
return R.color.five_grade;
case 4:
return R.color.four_grade;
case 3:
return R.color.three_grade;
case 2:
return R.color.two_grade;
case 1:
return R.color.one_grade;
default:
return R.color.default_grade;
}
}
}