package cc.pogoda.mobile.pogodacc.activity.trend.wind; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import org.threeten.bp.LocalDateTime; import org.threeten.bp.ZoneOffset; import org.threeten.bp.format.DateTimeFormatter; import org.threeten.bp.format.FormatStyle; import cc.pogoda.mobile.pogodacc.config.AppConfiguration; import cc.pogoda.mobile.pogodacc.dao.TrendDao; import cc.pogoda.mobile.pogodacc.type.web.Trend; public class WindTrendViewModel extends ViewModel { public void setStation(String station) { this.station = station; } private String station = ""; private TrendDao trendDao; private MutableLiveData displayedStationName; private MutableLiveData lastMeasuremenetTime; private MutableLiveData currentMeanValue; private MutableLiveData twoHoursMeanValue; private MutableLiveData fourHoursMeanValue; private MutableLiveData sixHoursMeanValue; private MutableLiveData eightHoursMeanValue; private MutableLiveData currentGustValue; private MutableLiveData twoHoursGustValue; private MutableLiveData fourHoursGustValue; private MutableLiveData sixHoursGustValue; private MutableLiveData eightHoursGustValue; public MutableLiveData getDisplayedStationName() { return displayedStationName; } public MutableLiveData getLastMeasuremenetTime() { return lastMeasuremenetTime; } public MutableLiveData getCurrentMeanValue() { return currentMeanValue; } public MutableLiveData getTwoHoursMeanValue() { return twoHoursMeanValue; } public MutableLiveData getFourHoursMeanValue() { return fourHoursMeanValue; } public MutableLiveData getSixHoursMeanValue() { return sixHoursMeanValue; } public MutableLiveData getEightHoursMeanValue() { return eightHoursMeanValue; } public MutableLiveData getCurrentGustValue() { return currentGustValue; } public MutableLiveData getTwoHoursGustValue() { return twoHoursGustValue; } public MutableLiveData getFourHoursGustValue() { return fourHoursGustValue; } public MutableLiveData getSixHoursGustValue() { return sixHoursGustValue; } public MutableLiveData getEightHoursGustValue() { return eightHoursGustValue; } public WindTrendViewModel() { trendDao = new TrendDao(); displayedStationName = new MutableLiveData(); lastMeasuremenetTime = new MutableLiveData(); currentMeanValue = new MutableLiveData(); twoHoursMeanValue = new MutableLiveData(); fourHoursMeanValue = new MutableLiveData(); sixHoursMeanValue = new MutableLiveData(); eightHoursMeanValue = new MutableLiveData(); currentGustValue = new MutableLiveData(); twoHoursGustValue = new MutableLiveData(); fourHoursGustValue = new MutableLiveData(); sixHoursGustValue = new MutableLiveData(); eightHoursGustValue = new MutableLiveData(); } public boolean updateData() { Trend trend = trendDao.getStationTrend(station); if (trend != null) { // format the time and date according to current locale String dt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT).format(LocalDateTime.ofEpochSecond(trend.last_timestamp, 0, ZoneOffset.UTC).atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneOffset.systemDefault())); lastMeasuremenetTime.postValue(dt); displayedStationName.postValue(trend.displayed_name); if (!trend.current_wind_qf.equals("NOT_AVALIABLE") && !trend.current_wind_qf.equals("NO_DATA")) { if (AppConfiguration.replaceMsWithKnots) { // if knots currentMeanValue.postValue(String.format("%.0f kts", trend.average_wind_speed_trend.current_value)); twoHoursMeanValue.postValue(String.format("%.0f kts", trend.average_wind_speed_trend.two_hours_value)); fourHoursMeanValue.postValue(String.format("%.0f kts", trend.average_wind_speed_trend.four_hours_value)); sixHoursMeanValue.postValue(String.format("%.0f kts", trend.average_wind_speed_trend.six_hours_value)); eightHoursMeanValue.postValue(String.format("%.0f kts", trend.average_wind_speed_trend.eight_hours_value)); currentGustValue.postValue(String.format("%.0f kts", trend.maximum_wind_speed_trend.current_value)); twoHoursGustValue.postValue(String.format("%.0f kts", trend.maximum_wind_speed_trend.two_hours_value)); fourHoursGustValue.postValue(String.format("%.0f kts", trend.maximum_wind_speed_trend.four_hours_value)); sixHoursGustValue.postValue(String.format("%.0f kts", trend.maximum_wind_speed_trend.six_hours_value)); eightHoursGustValue.postValue(String.format("%.0f kts", trend.maximum_wind_speed_trend.eight_hours_value)); } else { // if meters per second currentMeanValue.postValue(String.format("%.1f m/s", trend.average_wind_speed_trend.current_value)); twoHoursMeanValue.postValue(String.format("%.1f m/s", trend.average_wind_speed_trend.two_hours_value)); fourHoursMeanValue.postValue(String.format("%.1f m/s", trend.average_wind_speed_trend.four_hours_value)); sixHoursMeanValue.postValue(String.format("%.1f m/s", trend.average_wind_speed_trend.six_hours_value)); eightHoursMeanValue.postValue(String.format("%.1f m/s", trend.average_wind_speed_trend.eight_hours_value)); currentGustValue.postValue(String.format("%.1f m/s", trend.maximum_wind_speed_trend.current_value)); twoHoursGustValue.postValue(String.format("%.1f m/s", trend.maximum_wind_speed_trend.two_hours_value)); fourHoursGustValue.postValue(String.format("%.1f m/s", trend.maximum_wind_speed_trend.four_hours_value)); sixHoursGustValue.postValue(String.format("%.1f m/s", trend.maximum_wind_speed_trend.six_hours_value)); eightHoursGustValue.postValue(String.format("%.1f m/s", trend.maximum_wind_speed_trend.eight_hours_value)); } } else { currentMeanValue.postValue("--"); twoHoursMeanValue.postValue("--"); fourHoursMeanValue.postValue("--"); sixHoursMeanValue.postValue("--"); eightHoursMeanValue.postValue("--"); currentGustValue.postValue("--"); twoHoursGustValue.postValue("--"); fourHoursGustValue.postValue("--"); sixHoursGustValue.postValue("--"); eightHoursGustValue.postValue("--"); } return true; } else { currentMeanValue.postValue("--"); twoHoursMeanValue.postValue("--"); fourHoursMeanValue.postValue("--"); sixHoursMeanValue.postValue("--"); eightHoursMeanValue.postValue("--"); currentGustValue.postValue("--"); twoHoursGustValue.postValue("--"); fourHoursGustValue.postValue("--"); sixHoursGustValue.postValue("--"); eightHoursGustValue.postValue("--"); return false; } } }