Skip to content

Commit

Permalink
Add sunshine_hours to weekforecast open-meteo/open-meteo#467
Browse files Browse the repository at this point in the history
Remove unused constructors and other unused stuff
  • Loading branch information
woheller69 committed Nov 14, 2023
1 parent 2aa4113 commit 44d6438
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this,R.string.error_no_gps,Toast.LENGTH_LONG).show();
} else {
if (db.getAllCitiesToWatch().isEmpty()) {
CityToWatch newCity = new CityToWatch(db.getMaxRank() + 1, "--", -1, -1, 0, 0, "--°/--°");
CityToWatch newCity = new CityToWatch(db.getMaxRank() + 1, -1, -1, 0, 0, "--°/--°");
cityId = (int) db.addCityToWatch(newCity);
initResources();
noCityText.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ private CityToWatch convertCityToWatched(City selectedCity) {

return new CityToWatch(
database.getMaxRank() + 1,
selectedCity.getCountryCode(),
-1,
selectedCity.getCityId(), selectedCity.getLongitude(),selectedCity.getLatitude(),
selectedCity.getCityName()
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/org/woheller69/weather/database/City.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public class City {
public City() {
}

public City(int cityId, String cityName, String countryCode, float lon, float lat) {
this.cityId = cityId;
this.cityName = cityName;
this.countryCode = countryCode;
this.lon = lon;
this.lat = lat;
}

public int getCityId() {
return cityId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CityToWatch {
public CityToWatch() {
}

public CityToWatch(int rank, String countryCode, int id, int cityId, float lon, float lat, String cityName) {
public CityToWatch(int rank, int id, int cityId, float lon, float lat, String cityName) {
this.rank = rank;
this.lon = lon;
this.lat = lat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ public CurrentWeatherData() {
this.city_id = Integer.MIN_VALUE;
}

public CurrentWeatherData(int id, int city_id, long timestamp, int weatherID, float temperatureCurrent, float temperatureMin, float temperatureMax, float humidity, float pressure, float windSpeed, float windDirection, float cloudiness, long timeSunrise, long timeSunset, int timeZoneSeconds) {
this.id = id;
this.city_id = city_id;
this.timestamp = timestamp;
this.weatherID = weatherID;
this.temperatureCurrent = temperatureCurrent;
this.humidity = humidity;
this.pressure = pressure;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.cloudiness = cloudiness;
this.timeSunrise = timeSunrise;
this.timeSunset = timeSunset;
this.timeZoneSeconds = timeZoneSeconds;
}

public int getId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ public class HourlyForecast {
public HourlyForecast() {
}

public HourlyForecast(int id, int city_id, long timestamp, long forecastFor, int weatherID, float temperature, float humidity,
float pressure, float windSpeed, float windDirection, float precipitation) {
this.id = id;
this.city_id = city_id;
this.timestamp = timestamp;
this.forecastFor = forecastFor;
this.weatherID = weatherID;
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
}

public float getWindDirection() {
return windDirection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ public class QuarterHourlyForecast {
public QuarterHourlyForecast() {
}

public QuarterHourlyForecast(int id, int city_id, long timestamp, long forecastFor, int weatherID, float temperature, float windSpeed, float windDirection, float precipitation) {
this.id = id;
this.city_id = city_id;
this.timestamp = timestamp;
this.forecastFor = forecastFor;
this.weatherID = weatherID;
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
}

public float getWindDirection() {
return windDirection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class SQLiteHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final int DATABASE_VERSION = 3;
private Context context;

private List<City> allCities = new ArrayList<>();
Expand Down Expand Up @@ -86,6 +86,7 @@ public class SQLiteHelper extends SQLiteOpenHelper {
private static final String WEEKFORECAST_COLUMN_UV_INDEX = "uv_index";
private static final String WEEKFORECAST_COLUMN_TIME_SUNRISE = "time_sunrise";
private static final String WEEKFORECAST_COLUMN_TIME_SUNSET = "time_sunset";
private static final String WEEKFORECAST_COLUMN_SUNSHINE_HOURS = "sunshine_hours";


//Names of columns in TABLE_CURRENT_WEATHER
Expand Down Expand Up @@ -168,7 +169,8 @@ public class SQLiteHelper extends SQLiteOpenHelper {
WEEKFORECAST_COLUMN_WIND_DIRECTION + " REAL," +
WEEKFORECAST_COLUMN_UV_INDEX + " REAL," +
WEEKFORECAST_COLUMN_TIME_SUNRISE + " LONG NOT NULL," +
WEEKFORECAST_COLUMN_TIME_SUNSET + " LONG NOT NULL)";
WEEKFORECAST_COLUMN_TIME_SUNSET + " LONG NOT NULL," +
WEEKFORECAST_COLUMN_SUNSHINE_HOURS + " REAL)";

private static final String CREATE_TABLE_CITIES_TO_WATCH = "CREATE TABLE " + TABLE_CITIES_TO_WATCH +
"(" +
Expand Down Expand Up @@ -207,6 +209,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 1:
db.execSQL(CREATE_TABLE_QUARTERHOURLYFORECASTS);
// we want both updates, so no break statement here...
case 2:
db.execSQL("ALTER TABLE "+TABLE_WEEKFORECAST+" ADD COLUMN "+ WEEKFORECAST_COLUMN_SUNSHINE_HOURS +" REAL DEFAULT 0");
}
}

Expand Down Expand Up @@ -532,6 +536,7 @@ public synchronized void addWeekForecasts(List<WeekForecast> weekForecasts) {
values.put(WEEKFORECAST_COLUMN_UV_INDEX, weekForecast.getUv_index());
values.put(WEEKFORECAST_COLUMN_TIME_SUNRISE, weekForecast.getTimeSunrise());
values.put(WEEKFORECAST_COLUMN_TIME_SUNSET, weekForecast.getTimeSunset());
values.put(WEEKFORECAST_COLUMN_SUNSHINE_HOURS, weekForecast.getSunshineHours());
database.insert(TABLE_WEEKFORECAST, null, values);
}
database.close();
Expand Down Expand Up @@ -566,7 +571,8 @@ public synchronized List<WeekForecast> getWeekForecastsByCityId(int cityId) {
WEEKFORECAST_COLUMN_WIND_DIRECTION,
WEEKFORECAST_COLUMN_UV_INDEX,
WEEKFORECAST_COLUMN_TIME_SUNRISE,
WEEKFORECAST_COLUMN_TIME_SUNSET}
WEEKFORECAST_COLUMN_TIME_SUNSET,
WEEKFORECAST_COLUMN_SUNSHINE_HOURS}
, WEEKFORECAST_CITY_ID + "=?",
new String[]{String.valueOf(cityId)}, null, null, null, null);

Expand All @@ -592,6 +598,7 @@ public synchronized List<WeekForecast> getWeekForecastsByCityId(int cityId) {
weekForecast.setUv_index(Float.parseFloat(cursor.getString(13)));
weekForecast.setTimeSunrise(Long.parseLong(cursor.getString(14)));
weekForecast.setTimeSunset(Long.parseLong(cursor.getString(15)));
weekForecast.setSunshineHours(Float.parseFloat(cursor.getString(16)));
list.add(weekForecast);
} while (cursor.moveToNext());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,11 @@ public class WeekForecast {
private float uv_index;
private long timeSunrise;
private long timeSunset;
private float sunshineHours;

public WeekForecast() {
}

public WeekForecast(int id, int city_id, long timestamp, long forecastFor, int weatherID, float temperature, float temperature_min, float temperature_max, float humidity, float pressure, float precipitation, float wind_speed, float wind_direction, float uv_index) {
this.id = id;
this.city_id = city_id;
this.timestamp = timestamp;
this.forecastFor = forecastFor;
this.weatherID = weatherID;
this.temperature = temperature;
this.temperature_min = temperature_min;
this.temperature_max = temperature_max;
this.humidity = humidity;
this.pressure = pressure;
this.precipitation=precipitation;
this.wind_speed=wind_speed;
this.wind_direction=wind_direction;
this.uv_index=uv_index;
}


/**
* @return Returns the ID of the record (which uniquely identifies the record).
*/
Expand Down Expand Up @@ -203,4 +186,7 @@ public void setTimeSunset(long timeSunset) {
this.timeSunset = timeSunset;
}

public float getSunshineHours() { return sunshineHours; }

public void setSunshineHours(float sunshineHours) { this.sunshineHours = sunshineHours; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public void onBindViewHolder(WeekForecastViewHolder holder, int position) {
holder.wind_speed.setText(StringFormatUtils.formatWindSpeed(context, weekForecast.getWind_speed()));
holder.wind_speed.setBackground(StringFormatUtils.colorWindSpeed(context, weekForecast.getWind_speed()));

holder.sunshine_hours.setText("\ud83d\udd06\u200a"+Math.round(weekForecast.getSunshineHours())+"\u200a"+context.getString(R.string.units_hours));

Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT"));
c.setTimeInMillis(weekForecast.getLocalForecastTime(context));
Expand Down Expand Up @@ -141,6 +143,7 @@ class WeekForecastViewHolder extends RecyclerView.ViewHolder {
TextView wind_speed;
TextView precipitation;
TextView uv_index;
TextView sunshine_hours;
ImageView windicon;

WeekForecastViewHolder(View itemView) {
Expand All @@ -156,6 +159,7 @@ class WeekForecastViewHolder extends RecyclerView.ViewHolder {
precipitation = itemView.findViewById(R.id.week_forecast_precipitation);
uv_index = itemView.findViewById(R.id.week_forecast_uv_index);
windicon = itemView.findViewById(R.id.week_forecast_wind_icon);
sunshine_hours = itemView.findViewById(R.id.week_forecast_sunshine_hours);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,4 @@ public interface IDataExtractor {

List<QuarterHourlyForecast> extractQuarterHourlyForecast(String data);

/**
* @param data0, data1, data2, data3, data4 contain the information to retrieve the rain for a minute within the next 60min.
* @return Returns a string with a rain drop in case of rain or a - in case of no rain
*/
String extractRain60min(String data0,String data1, String data2, String data3, String data4);

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public List<WeekForecast> extractWeekForecast(String data) {
JSONArray snowfallArray = jsonData.has("snowfall_sum") ? jsonData.getJSONArray("snowfall_sum") : null;
JSONArray showersArray = jsonData.has("showers_sum") ? jsonData.getJSONArray("showers_sum") : null;
JSONArray rainArray = jsonData.has("rain_sum") ? jsonData.getJSONArray("rain_sum") : null;
JSONArray sunshineDurationArray = jsonData.has("sunshine_duration") ? jsonData.getJSONArray("sunshine_duration") : null;

IApiToDatabaseConversion conversion = new OMToDatabaseConversion();
for (int i = 0; i < timeArray.length(); i++) {
Expand All @@ -87,6 +88,7 @@ public List<WeekForecast> extractWeekForecast(String data) {
if (tempMinArray != null && !tempMinArray.isNull(i)) weekForecast.setMinTemperature((float) tempMinArray.getDouble(i));
if (sunriseArray != null && !sunriseArray.isNull(i)) weekForecast.setTimeSunrise(sunriseArray.getLong(i));
if (sunsetArray != null && !sunsetArray.isNull(i)) weekForecast.setTimeSunset(sunsetArray.getLong(i));
if (sunshineDurationArray != null && !sunshineDurationArray.isNull(i)) weekForecast.setSunshineHours((float) (sunshineDurationArray.getDouble(i)/3600));
if (uvIndexArray != null && !uvIndexArray.isNull(i)) {
weekForecast.setUv_index((float) uvIndexArray.getDouble(i));
} else weekForecast.setUv_index(-1);
Expand Down Expand Up @@ -211,47 +213,4 @@ public List<QuarterHourlyForecast> extractQuarterHourlyForecast(String data) {
return null;
}

/**
* @see IDataExtractor#extractRain60min(String, String, String, String, String)
*/
@Override
public String extractRain60min(String data0,String data1, String data2, String data3, String data4) {
try {

String rain = "";
JSONObject jsonData0 = new JSONObject(data0);
JSONObject jsonData1 = new JSONObject(data1);
JSONObject jsonData2 = new JSONObject(data2);
JSONObject jsonData3 = new JSONObject(data3);
JSONObject jsonData4 = new JSONObject(data4);
double rain5min=jsonData0.getDouble("precipitation")+jsonData1.getDouble("precipitation")+jsonData2.getDouble("precipitation")+jsonData3.getDouble("precipitation")+jsonData4.getDouble("precipitation");
if (rain5min==0){
rain ="\u25a1";
} else if (rain5min<2.5){ // very light rain equals <0.5mm/h (2.5 = 5 x 0.5)
rain ="\u25a4";
}else if (rain5min<12.5){ //light rain equals <2.5mm/h (12.5 = 5 x 2.5)
rain ="\u25a6";
} else{
rain ="\u25a0";
}

return rain;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}


/**
* @param data The data that contains the information to retrieve the ID of the city.
* If data for a single city were requested, the response string can be
* passed as an argument.
* If data for multiple cities were requested, make sure to pass only one item
* of the response list at a time!
* @return Returns the ID of the city or Integer#MIN_VALUE in case the data is not well-formed
* and the information could not be extracted.
*/


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected String getUrlForQueryingOMweatherAPI(Context context, float lat, float

if (sharedPreferences.getBoolean("pref_snow", false)){
return String.format(
"%sforecast?latitude=%s&longitude=%s&forecast_days=%s&hourly=%s,relativehumidity_2m,rain,showers,snowfall,weathercode,pressure_msl,windspeed_10m,winddirection_10m&minutely_15=%s,rain,showers,snowfall,weathercode,windspeed_10m&daily=weathercode,%s,%s,sunrise,sunset,uv_index_max,rain_sum,showers_sum,snowfall_sum,windspeed_10m_max&current_weather=true&windspeed_unit=ms&timeformat=unixtime&timezone=auto",
"%sforecast?latitude=%s&longitude=%s&forecast_days=%s&hourly=%s,relativehumidity_2m,rain,showers,snowfall,weathercode,pressure_msl,windspeed_10m,winddirection_10m&minutely_15=%s,rain,showers,snowfall,weathercode,windspeed_10m&daily=weathercode,%s,%s,sunrise,sunset,uv_index_max,rain_sum,showers_sum,snowfall_sum,windspeed_10m_max,sunshine_duration&current_weather=true&windspeed_unit=ms&timeformat=unixtime&timezone=auto",
BuildConfig.BASE_URL,
lat,
lon,
Expand All @@ -37,7 +37,7 @@ protected String getUrlForQueryingOMweatherAPI(Context context, float lat, float
);
} else {
return String.format(
"%sforecast?latitude=%s&longitude=%s&forecast_days=%s&hourly=%s,relativehumidity_2m,precipitation,weathercode,pressure_msl,windspeed_10m,winddirection_10m&minutely_15=%s,precipitation,weathercode,windspeed_10m&daily=weathercode,%s,%s,sunrise,sunset,uv_index_max,precipitation_sum,windspeed_10m_max&current_weather=true&windspeed_unit=ms&timeformat=unixtime&timezone=auto",
"%sforecast?latitude=%s&longitude=%s&forecast_days=%s&hourly=%s,relativehumidity_2m,precipitation,weathercode,pressure_msl,windspeed_10m,winddirection_10m&minutely_15=%s,precipitation,weathercode,windspeed_10m&daily=weathercode,%s,%s,sunrise,sunset,uv_index_max,precipitation_sum,windspeed_10m_max,sunshine_duration&current_weather=true&windspeed_unit=ms&timeformat=unixtime&timezone=auto",
BuildConfig.BASE_URL,
lat,
lon,
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/list_item_week_forecast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@
android:textColor="@color/colorPrimaryDark"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/week_forecast_sunshine_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="\ud83d\udd06\u200a2\u200ah"
android:textColor="@color/colorPrimaryDark"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<string name="summary_precipitation">Brug snemængde i stedet for den tilsvarende mængde vand.</string>
<string name="settings_forecast_days">Antal prognosedage</string>
<string name="settings_apparent_temperature">Vis følt temperatur</string>
<string name="units_hours">h</string>
<!-- Generated by Automatic String Resource Translation -->
<!-- https://asrt.gluege.boerde.de -->
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@
<string name="summary_precipitation">Schneemenge anstelle der entsprechenden Wassermenge verwenden.</string>
<string name="settings_forecast_days">Anzahl der prognostizierten Tage</string>
<string name="settings_apparent_temperature">Gefühlte Temperatur anzeigen</string>
<string name="units_hours">h</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@
<string name="summary_precipitation">Use la cantidad de nieve en lugar de la cantidad equivalente de agua.</string>
<string name="settings_forecast_days">Número de días de pronóstico</string>
<string name="settings_apparent_temperature">Visualización de la temperatura del fieltro</string>
<string name="units_hours">h</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@
<string name="summary_precipitation">Utilisez la quantité de neige au lieu de la quantité équivalente d\'eau.</string>
<string name="settings_forecast_days">Nombre de jours de prévision</string>
<string name="settings_apparent_temperature">Affiche la température ressentie</string>
<string name="units_hours">h</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@
<string name="summary_precipitation">Usa la quantità di neve invece della quantità equivalente di acqua.</string>
<string name="settings_forecast_days">Numero di giorni di previsione</string>
<string name="settings_apparent_temperature">Mostra la temperatura percepita</string>
<string name="units_hours">h</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<string name="summary_precipitation">Gebruik de hoeveelheid sneeuw in plaats van de equivalente hoeveelheid water.</string>
<string name="settings_forecast_days">Aantal voorspellingsdagen</string>
<string name="settings_apparent_temperature">Toon vilttemperatuur</string>
<string name="units_hours">h</string>

<!-- Generated by Automatic String Resource Translation -->
<!-- https://asrt.gluege.boerde.de -->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@
<string name="summary_precipitation">Użyj ilości śniegu zamiast równoważnej ilości wody.</string>
<string name="settings_forecast_days">Liczba dni prognozy</string>
<string name="settings_apparent_temperature">Pokaż odczuwalną temperaturę</string>
<string name="units_hours">h</string>
</resources>

1 change: 1 addition & 0 deletions app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@
<string name="summary_precipitation">Use a quantidade de neve em vez da quantidade equivalente de água.</string>
<string name="settings_forecast_days">Número de dias de previsão</string>
<string name="settings_apparent_temperature">Mostrar temperatura do feltro</string>
<string name="units_hours">h</string>
</resources>
Loading

0 comments on commit 44d6438

Please sign in to comment.