2021-12-10 10:21:06 +00:00
package cc.pogoda.mobile.meteosystem.activity.updater ;
2021-11-25 20:13:27 +00:00
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 ;
2021-12-10 10:21:06 +00:00
import cc.pogoda.mobile.meteosystem.dao.AvailableParametersDao ;
import cc.pogoda.mobile.meteosystem.dao.SummaryDao ;
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
* which is updated by { @link FavouritesStationSummaryUpdater }
2021-11-25 20:13:27 +00:00
* /
public class FavouritesStationDetailsUpdater implements Runnable {
/ * *
* 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-01-06 11:36:18 +00:00
private AvailableParametersDao availableParametersDao = null ;
2021-11-25 20:13:27 +00:00
2021-12-08 12:36:01 +00:00
/ * *
2022-01-06 11:36:18 +00:00
* This map comes from ' Main ' class and it is shared with @link { { @link FavouritesStationSummaryUpdater } }
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-01-06 11:36:18 +00:00
public FavouritesStationDetailsUpdater ( Handler _handler , HashMap < String , Summary > _station_system_name_to_summary ) {
2021-11-25 20:13:27 +00:00
handler = _handler ;
stationsToUpdate = new HashMap < > ( ) ;
2021-12-08 12:36:01 +00:00
availableParametersDao = new AvailableParametersDao ( ) ;
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-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 ) ;
Logger . debug ( "[FavouritesStationDetailsUpdater][run][stationSystemName = " + stationSystemName + "][summary.last_timestamp = " + summary . last_timestamp + "]" ) ;
2021-11-25 20:13:27 +00:00
2021-12-08 12:36:01 +00:00
// query for available parameters
AvailableParametersWeb params = availableParametersDao . getAvaliableParamsByStationName ( stationSystemName ) ;
2021-11-25 20:13:27 +00:00
// if data has been collected
if ( summary ! = null ) {
String str ;
// check if this station transmits wind information
2021-12-08 12:36:01 +00:00
if ( params . hasWind ) {
2021-11-25 20:13:27 +00:00
// check if station transmits humidity
2021-12-08 12:36:01 +00:00
if ( params . hasHumidity ) {
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 {
2021-12-08 12:36:01 +00:00
if ( params . hasHumidity ) {
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
if ( ( params . hasHumidity & & summary . humidity_qf_native . equals ( QualityFactor . NOT_AVALIABLE ) ) | |
( summary . temperature_qf_native . equals ( QualityFactor . NOT_AVALIABLE ) ) | |
( params . hasWind & & 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
}
}
2022-01-06 11:36:18 +00:00
handler . postDelayed ( this , 60000 ) ;
2021-11-25 20:13:27 +00:00
}
}
}