2021-12-10 10:21:06 +00:00
package cc.pogoda.mobile.meteosystem.activity.updater ;
2021-11-25 20:13:27 +00:00
2022-05-11 15:04:21 +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 ;
2021-12-08 12:36:01 +00:00
import android.annotation.SuppressLint ;
import android.graphics.Color ;
2021-11-25 20:13:27 +00:00
import android.os.Handler ;
import android.widget.TextView ;
2022-01-06 11:36:18 +00:00
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 ;
2022-05-11 15:04:21 +00:00
import cc.pogoda.mobile.meteosystem.activity.updater.thread.FavouritesStationSummaryUpdaterThread ;
2021-12-10 10:21:06 +00:00
import cc.pogoda.mobile.meteosystem.dao.AvailableParametersDao ;
2022-05-11 15:04:21 +00:00
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
/ * *
2022-01-06 11:36:18 +00:00
* This class is used to update entries ( TextView ) on Favourites list using HashMap
2022-05-11 15:04:21 +00:00
* which is updated by { @link FavouritesStationSummaryUpdaterThread }
2021-11-25 20:13:27 +00:00
* /
2022-01-06 18:52:59 +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 ;
/ * *
2022-01-06 11:36:18 +00:00
*
2021-11-25 20:13:27 +00:00
* /
2022-05-11 15:04:21 +00:00
//private AvailableParametersDao availableParametersDao = null;
private HashMap < String , AvailableParameters > availParams ;
2021-11-25 20:13:27 +00:00
2021-12-08 12:36:01 +00:00
/ * *
2022-05-11 15:04:21 +00:00
* This map comes from ' Main ' class and it is shared with @link { { @link FavouritesStationSummaryUpdaterThread } }
2021-12-08 12:36:01 +00:00
* /
2022-01-06 11:36:18 +00:00
HashMap < String , Summary > stationNameSummary = null ;
2021-12-08 12:36:01 +00:00
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 ;
2022-05-11 15:04:21 +00:00
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 < > ( ) ;
2022-05-11 15:04:21 +00:00
availParams = _avail_params ;
2022-01-06 11:36:18 +00:00
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 ) ;
}
2021-12-08 12:36:01 +00:00
@SuppressLint ( "ResourceAsColor" )
2021-11-25 20:13:27 +00:00
@Override
public void run ( ) {
2022-05-11 15:04:21 +00:00
int nextExecutionDelay = DETAILS_ON_FAVS_LIST_DEFAULT_UPDATE ;
2022-05-03 17:56:00 +00:00
2022-01-06 11:36:18 +00:00
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
2022-01-06 11:36:18 +00:00
Summary summary = stationNameSummary . get ( stationSystemName ) ;
2021-12-08 12:36:01 +00:00
// query for available parameters
2022-05-11 15:04:21 +00:00
AvailableParameters params = availParams . get ( stationSystemName ) ;
2021-12-08 12:36:01 +00:00
2021-11-25 20:13:27 +00:00
// if data has been collected
2022-05-03 15:38:36 +00:00
if ( summary ! = null & & params ! = null ) {
2022-05-11 15:04:21 +00:00
Logger . debug ( "[stationSystemName = " + stationSystemName + "][summary.last_timestamp = " + summary . last_timestamp + "]" ) ;
2022-05-03 15:38:36 +00:00
2021-11-25 20:13:27 +00:00
String str ;
// check if this station transmits wind information
2022-05-11 15:04:21 +00:00
if ( params . windSpeed ) {
2021-11-25 20:13:27 +00:00
// check if station transmits humidity
2022-05-11 15:04:21 +00:00
if ( params . humidity ) {
2021-11-25 23:27:13 +00:00
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 {
2021-11-25 23:27:13 +00:00
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 {
2022-05-11 15:04:21 +00:00
if ( params . humidity ) {
2021-11-25 23:27:13 +00:00
str = String . format ( "%s %d%%" , summary . getTemperatureStr ( false , true ) , summary . humidity ) ;
2021-11-25 20:13:27 +00:00
}
else {
2021-11-25 23:27:13 +00:00
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 ) ;
2021-12-08 12:36:01 +00:00
2022-05-11 15:04:21 +00:00
if ( ( params . humidity & & summary . humidity_qf_native . equals ( QualityFactor . NOT_AVALIABLE ) ) | |
2021-12-08 12:36:01 +00:00
( summary . temperature_qf_native . equals ( QualityFactor . NOT_AVALIABLE ) ) | |
2022-05-11 15:04:21 +00:00
( params . windSpeed & & summary . wind_qf_native . equals ( QualityFactor . NOT_AVALIABLE ) ) )
2021-12-08 12:36:01 +00:00
{
toUpdate . setTextColor ( Color . RED ) ;
}
else {
toUpdate . setTextColor ( androidx . activity . R . color . secondary_text_default_material_light ) ;
}
2021-11-25 20:13:27 +00:00
}
2022-05-03 15:38:36 +00:00
else {
2022-05-11 15:04:21 +00:00
Logger . error ( "[stationSystemName = " + stationSystemName + "][summary object is null!! Maybe the API responds exceptionally slow?]" ) ;
nextExecutionDelay = DETAILS_ON_FAVS_LIST_REUPDATE ;
2022-05-03 15:38:36 +00:00
}
2021-11-25 20:13:27 +00:00
}
2022-05-03 17:56:00 +00:00
handler . postDelayed ( this , nextExecutionDelay ) ;
2021-11-25 20:13:27 +00:00
}
}
}