Skip to content

Commit

Permalink
* Fixed bug when min is greater than max for difficulty and terrain f…
Browse files Browse the repository at this point in the history
…ilter

* Fixed wrong solving of isValid method in Geocaching API for difficulty and terrain filter
* Fixed rounding bug in formating coordinates
  • Loading branch information
arcao committed Feb 15, 2012
1 parent 5a659b5 commit da3ba1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
20 changes: 12 additions & 8 deletions src/com/arcao/geocaching4locus/PreferenceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ public boolean onPreferenceClick(Preference preference) {
difficultyMinPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
float min = Float.parseFloat(difficultyMinPreference.getValue());
float min = Float.parseFloat((String)newValue);
float max = Float.parseFloat(difficultyMaxPreference.getValue());

if (min > max) {
difficultyMaxPreference.setValue(difficultyMinPreference.getValue());
difficultyMaxPreference.setValue((String)newValue);
difficultyMaxPreference.setSummary(prepareRatingSummary((String)newValue));
}
return true;
}
Expand All @@ -206,10 +207,11 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
float min = Float.parseFloat(difficultyMinPreference.getValue());
float max = Float.parseFloat(difficultyMaxPreference.getValue());
float max = Float.parseFloat((String)newValue);

if (min > max) {
difficultyMinPreference.setValue(difficultyMaxPreference.getValue());
difficultyMinPreference.setValue((String)newValue);
difficultyMinPreference.setSummary(prepareRatingSummary((String)newValue));
}
return true;
}
Expand All @@ -222,11 +224,12 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
terrainMinPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
float min = Float.parseFloat(terrainMinPreference.getValue());
float min = Float.parseFloat((String)newValue);
float max = Float.parseFloat(terrainMaxPreference.getValue());

if (min > max) {
terrainMaxPreference.setValue(terrainMinPreference.getValue());
terrainMaxPreference.setValue((String)newValue);
terrainMaxPreference.setSummary(prepareRatingSummary((String)newValue));
}
return true;
}
Expand All @@ -237,10 +240,11 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
float min = Float.parseFloat(terrainMinPreference.getValue());
float max = Float.parseFloat(terrainMaxPreference.getValue());
float max = Float.parseFloat((String)newValue);

if (min > max) {
terrainMinPreference.setValue(terrainMaxPreference.getValue());
terrainMinPreference.setValue((String)newValue);
terrainMinPreference.setSummary(prepareRatingSummary((String)newValue));
}
return true;
}
Expand Down
23 changes: 15 additions & 8 deletions src/com/arcao/geocaching4locus/util/Coordinates.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ public static String convertDoubleToDeg(double source, boolean isLon, int precis
sb.append(' ');

int deg = (int) source;

// FIX for rounding errors
double min = roundDouble(((source - deg) * 60D), precision);
if (min == 60D) {
deg++;
min = 0D;
}

sb.append(deg);
sb.append("\u00B0 ");

double min = (source - deg) * 60D;

sb.append(round(min, precision));

return sb.toString();
Expand Down Expand Up @@ -85,6 +90,12 @@ private static int getDoubleNumberEnd(String source, int start) {
}
return source.length();
}

public static double roundDouble(double source, int decimalPlaces) {
double multipicationFactor = Math.pow(10, decimalPlaces);
double sourceMultipled = source * multipicationFactor;
return Math.round(sourceMultipled) / multipicationFactor;
}

public static String round(double source, int decimals) {
if (decimals < 0)
Expand All @@ -94,11 +105,7 @@ public static String round(double source, int decimals) {
return Long.toString((long) source);
}

int decimalLimiter = 1;
for (byte i = 0; i < decimals; i++)
decimalLimiter *= 10;

double rounded = ((double) Math.round(source * decimalLimiter)) / decimalLimiter;
double rounded = roundDouble(source, decimals);

String val = Double.toString(rounded);
int dot = val.indexOf('.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public float getMax() {

@Override
public boolean isValid() {
return min != 1 && max != 5;
return min != 1 || max != 5;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public float getMax() {

@Override
public boolean isValid() {
return min != 1 && max != 5;
return min != 1 || max != 5;
}

@Override
Expand Down

0 comments on commit da3ba1b

Please sign in to comment.