MeteoSystem/app/src/main/java/cc/pogoda/mobile/meteosystem/activity/updater/FavouritesStationDetailsOnL...

155 wiersze
6.2 KiB
Java
Czysty Zwykły widok Historia

2021-12-10 10:21:06 +00:00
package cc.pogoda.mobile.meteosystem.activity.updater;
2021-11-25 20:13:27 +00:00
import static cc.pogoda.mobile.meteosystem.config.ConstAppConfiguration.DETAILS_ON_FAVS_LIST_DEFAULT_UPDATE;
import static cc.pogoda.mobile.meteosystem.config.ConstAppConfiguration.DETAILS_ON_FAVS_LIST_REUPDATE;
import android.annotation.SuppressLint;
import android.graphics.Color;
2021-11-25 20:13:27 +00:00
import android.os.Handler;
import android.widget.TextView;
import org.tinylog.Logger;
2021-11-25 20:13:27 +00:00
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import cc.pogoda.mobile.meteosystem.activity.updater.thread.FavouritesStationSummaryUpdaterThread;
2021-12-10 10:21:06 +00:00
import cc.pogoda.mobile.meteosystem.dao.AvailableParametersDao;
import cc.pogoda.mobile.meteosystem.type.AvailableParameters;
2021-12-10 10:21:06 +00:00
import cc.pogoda.mobile.meteosystem.type.web.AvailableParametersWeb;
import cc.pogoda.mobile.meteosystem.type.web.QualityFactor;
import cc.pogoda.mobile.meteosystem.type.web.Summary;
2021-11-25 20:13:27 +00:00
/**
* This class is used to update entries (TextView) on Favourites list using HashMap
* which is updated by {@link FavouritesStationSummaryUpdaterThread}
2021-11-25 20:13:27 +00:00
*/
public class FavouritesStationDetailsOnListUpdater implements Runnable {
2021-11-25 20:13:27 +00:00
/**
* Handler is used by Android to put a Runnable into MessageQueue handler by the Looper. This
* runnable can be scheduled to be serviced at certain point of time
*/
private Handler handler;
/**
* A collection which holds
*/
private HashMap<String, TextView> stationsToUpdate;
/**
*
2021-11-25 20:13:27 +00:00
*/
//private AvailableParametersDao availableParametersDao = null;
private HashMap<String, AvailableParameters> availParams;
2021-11-25 20:13:27 +00:00
/**
* This map comes from 'Main' class and it is shared with @link{{@link FavouritesStationSummaryUpdaterThread}}
*/
HashMap<String, Summary> stationNameSummary = null;
2021-11-25 20:13:27 +00:00
/**
* Not sure if this is really required but just to be sure that updater won't be started
* after the activity had been torn down.
*/
private boolean enabled;
public FavouritesStationDetailsOnListUpdater(Handler _handler, HashMap<String, Summary> _station_system_name_to_summary, HashMap<String, AvailableParameters> _avail_params) {
2021-11-25 20:13:27 +00:00
handler = _handler;
stationsToUpdate = new HashMap<>();
availParams = _avail_params;
stationNameSummary = _station_system_name_to_summary;
2021-11-25 20:13:27 +00:00
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void addNewStation(String _station_system_name, TextView _tv) {
stationsToUpdate.put(_station_system_name, _tv);
}
@SuppressLint("ResourceAsColor")
2021-11-25 20:13:27 +00:00
@Override
public void run() {
int nextExecutionDelay = DETAILS_ON_FAVS_LIST_DEFAULT_UPDATE;
if (stationNameSummary != null && enabled && stationsToUpdate != null && stationsToUpdate.size() > 0) {
2021-11-25 20:13:27 +00:00
// get a set of all elements stored in the map
Set<Map.Entry<String, TextView>> entries = stationsToUpdate.entrySet();
// create something iterable from the set. the set itself doesn't guarantee the same order than
// objects were put in, but in this case it isn't a problem.
Vector<Map.Entry<String, TextView>> vectorOfEntries = new Vector<>(entries);
for (Map.Entry<String, TextView> e : vectorOfEntries) {
// extract data from pair
String stationSystemName = e.getKey();
TextView toUpdate = e.getValue();
// query web service for station data
Summary summary = stationNameSummary.get(stationSystemName);
// query for available parameters
AvailableParameters params = availParams.get(stationSystemName);
2021-11-25 20:13:27 +00:00
// if data has been collected
if (summary != null && params != null) {
Logger.debug("[stationSystemName = " + stationSystemName +"][summary.last_timestamp = " + summary.last_timestamp +"]");
2021-11-25 20:13:27 +00:00
String str;
// check if this station transmits wind information
if (params.windSpeed) {
2021-11-25 20:13:27 +00:00
// check if station transmits humidity
if (params.humidity) {
str = String.format("%s %d%% %s %s max %s", summary.getTemperatureStr(false, true), summary.humidity, summary.getWindDirStr(), summary.getWindspeedStr(false), summary.getWindgustsStr(false));
2021-11-25 20:13:27 +00:00
}
else {
str = String.format("%s %s %s max %s", summary.getTemperatureStr(false, true), summary.getWindDirStr(), summary.getWindspeedStr(false), summary.getWindgustsStr(false));
2021-11-25 20:13:27 +00:00
}
}
else {
if (params.humidity) {
str = String.format("%s %d%%", summary.getTemperatureStr(false, true), summary.humidity);
2021-11-25 20:13:27 +00:00
}
else {
str = String.format("%s", summary.getTemperatureStr(false, true));
2021-11-25 20:13:27 +00:00
}
}
// update text view on the favourites list
toUpdate.setText(str);
if ( (params.humidity && summary.humidity_qf_native.equals(QualityFactor.NOT_AVALIABLE)) ||
(summary.temperature_qf_native.equals(QualityFactor.NOT_AVALIABLE)) ||
(params.windSpeed && summary.wind_qf_native.equals(QualityFactor.NOT_AVALIABLE)))
{
toUpdate.setTextColor(Color.RED);
}
else {
toUpdate.setTextColor(androidx.activity.R.color.secondary_text_default_material_light);
}
2021-11-25 20:13:27 +00:00
}
else {
Logger.error("[stationSystemName = " + stationSystemName + "][summary object is null!! Maybe the API responds exceptionally slow?]");
nextExecutionDelay = DETAILS_ON_FAVS_LIST_REUPDATE;
}
2021-11-25 20:13:27 +00:00
}
handler.postDelayed(this, nextExecutionDelay);
2021-11-25 20:13:27 +00:00
}
}
}