Skip to content

Commit

Permalink
Merge pull request #627 from shortspider/PercentOfPrecipitation
Browse files Browse the repository at this point in the history
Add support for percent of precipitation
  • Loading branch information
robinpaulson authored Dec 10, 2021
2 parents d1a31a2 + dea69c6 commit affd74e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ private void updateTodayWeatherUI() {

// Rain
double rain = Double.parseDouble(todayWeather.getRain());
String rainString = UnitConvertor.getRainString(rain, sp);
String rainString = UnitConvertor.getRainString(rain, todayWeather.getChanceOfPrecipitation(), sp);

// Wind
double wind;
Expand Down Expand Up @@ -598,6 +598,7 @@ public ParseResult parseLongTermJson(String result) {
}
}
weather.setRain(rain);
weather.setChanceOfPrecipitation(listItem.optDouble("pop", 0));

final String idString = listItem.optJSONArray("weather").getJSONObject(0).getString("id");
weather.setId(idString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onBindViewHolder(@NonNull WeatherViewHolder customViewHolder, int i)

// Rain
double rain = Double.parseDouble(weatherItem.getRain());
String rainString = UnitConvertor.getRainString(rain, sp);
String rainString = UnitConvertor.getRainString(rain, weatherItem.getChanceOfPrecipitation(), sp);

// Wind
double wind;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/cz/martykan/forecastie/models/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Weather {
private double lat;
private double lon;
private double uvIndex;
private double chanceOfPrecipitation;

public enum WindDirection {
// don't change order
Expand Down Expand Up @@ -287,4 +288,12 @@ public void setLastUpdated(String lastUpdated) {
public String getLastUpdated() {
return lastUpdated;
}

public double getChanceOfPrecipitation() {
return chanceOfPrecipitation;
}

public void setChanceOfPrecipitation(double chanceOfPrecipitation) {
this.chanceOfPrecipitation = chanceOfPrecipitation;
}
}
34 changes: 19 additions & 15 deletions app/src/main/java/cz/martykan/forecastie/utils/UnitConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,29 @@ public static float convertRain(float rain, SharedPreferences sp) {
}
}

public static String getRainString(double rain, SharedPreferences sp) {
public static String getRainString(double rain, double chanceOfPrecipitation, SharedPreferences sp) {
StringBuilder sb = new StringBuilder();
if (rain > 0) {
if (sp.getString("lengthUnit", "mm").equals("mm")) {
if (rain < 0.1) {
return " (<0.1 mm)";
} else {
return String.format(Locale.ENGLISH, " (%.1f %s)", rain, sp.getString("lengthUnit", "mm"));
}
sb.append(" (");
String lengthUnit = sp.getString("lengthUnit", "mm");
boolean isMetric = lengthUnit.equals("mm");

if (rain < 0.1) {
sb.append(isMetric ? "<0.1" : "<0.01");
} else if (isMetric) {
sb.append(String.format(Locale.ENGLISH, "%.1f %s", rain, lengthUnit));
} else {
rain = rain / 25.4;
if (rain < 0.01) {
return " (<0.01 in)";
} else {
return String.format(Locale.ENGLISH, " (%.2f %s)", rain, sp.getString("lengthUnit", "mm"));
}
sb.append(String.format(Locale.ENGLISH, "%.2f %s", rain, lengthUnit));
}
} else {
return "";

if (chanceOfPrecipitation > 0) {
sb.append(", ").append(chanceOfPrecipitation * 100).append("%");
}

sb.append(")");
}

return sb.toString();
}

public static float convertPressure(float pressure, SharedPreferences sp) {
Expand Down

0 comments on commit affd74e

Please sign in to comment.