Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import wangdaye.com.geometricweather.R;
import wangdaye.com.geometricweather.common.basic.models.weather.AirQuality;
import wangdaye.com.geometricweather.common.basic.models.weather.UV;
import wangdaye.com.geometricweather.common.basic.models.weather.Wind;

public class CommonConverter {
Expand Down Expand Up @@ -122,4 +123,27 @@ public static boolean isDaylight(Date sunrise, Date sunset, Date current) {

return sunriseTime < currentTime && currentTime < sunsetTime;
}

public static UV getCurrentUV(int dayMaxUV, Date currentDate, Date sunriseDate, Date sunsetDate) {
if (currentDate == null || sunriseDate == null || sunsetDate == null || sunriseDate.after(sunsetDate))
return new UV(null, null, null);

// You can visualize formula here: https://www.desmos.com/calculator/lna7dco4zi
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
float currentTime = calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE) / 60f; // Approximating to the minute is enough

calendar.setTime(sunriseDate);
float sunRiseTime = calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE) / 60f; // b in desmos graph

calendar.setTime(sunsetDate);
float sunSetTime = calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE) / 60f; // c in desmos graph

float sunlightDuration = sunSetTime - sunRiseTime; // d in desmos graph

float sunRiseOffset = (float) -Math.PI * sunRiseTime / sunlightDuration; // o in desmos graph
double currentUV = dayMaxUV * Math.sin(Math.PI / sunlightDuration * currentTime + sunRiseOffset); // dayMaxUV = a in desmos graph

return new UV(Math.toIntExact(Math.round(currentUV)), null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static WeatherService.WeatherResultWrapper convert(Context context,
currentResult.observation.wind.speed * 3.6f,
CommonConverter.getWindLevel(context, currentResult.observation.wind.speed * 3.6f)
),
new UV(null, null, null),
getCurrentUV(new Date(), forecastResult.dailyForecasts),
getAirQuality(new Date(), aqiAtmoAuraResult),
null,
null,
Expand Down Expand Up @@ -461,6 +461,17 @@ private static Float getSnowCumul(MfForecastResult.Forecast.Snow snow) {
return null;
}

private static UV getCurrentUV(Date currentDate, List<MfForecastResult.DailyForecast> dailyForecasts) {
if (dailyForecasts == null || dailyForecasts.isEmpty())
return new UV(null, null, null);

MfForecastResult.DailyForecast todayForecast = dailyForecasts.get(0);
if (todayForecast == null || todayForecast.sun.rise == null || todayForecast.sun.set == null)
return new UV(null, null, null);

return CommonConverter.getCurrentUV(todayForecast.uv, currentDate, new Date(todayForecast.sun.rise * 1000), new Date(todayForecast.sun.set * 1000));
}

private static Precipitation getHourlyPrecipitation(MfForecastResult.Forecast hourlyForecast) {
Float rainCumul = getRainCumul(hourlyForecast.rain);
Float snowCumul = getSnowCumul(hourlyForecast.snow);
Expand Down