kopia lustrzana https://github.com/SP8EBC/MeteoSystem
updating favourites list in background
rodzic
22d340efbd
commit
bbd4657946
|
@ -61,6 +61,8 @@ public class FavouritesActivity extends AppCompatActivity {
|
|||
if (recyclerViewFavourites != null) {
|
||||
adapter = new WeatherStationRecyclerViewAdapter(sortedFavourites.getList(), this, callReason.getReason());
|
||||
|
||||
adapter.createAndStartUpdater();
|
||||
|
||||
recyclerViewFavourites.setAdapter(adapter);
|
||||
}
|
||||
|
||||
|
@ -71,6 +73,8 @@ public class FavouritesActivity extends AppCompatActivity {
|
|||
if (recyclerViewFavourites != null) {
|
||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
||||
|
||||
adapter.createAndStartUpdater();
|
||||
|
||||
recyclerViewFavourites.setAdapter(adapter);
|
||||
}
|
||||
|
||||
|
@ -105,10 +109,19 @@ public class FavouritesActivity extends AppCompatActivity {
|
|||
if (recyclerViewFavourites != null) {
|
||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
||||
|
||||
adapter.createAndStartUpdater();
|
||||
|
||||
recyclerViewFavourites.setAdapter(adapter);
|
||||
|
||||
recyclerViewFavourites.setLayoutManager(new LinearLayoutManager(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
adapter.stopUpdater();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
package cc.pogoda.mobile.pogodacc.activity.updater;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
|
||||
import cc.pogoda.mobile.pogodacc.dao.SummaryDao;
|
||||
import cc.pogoda.mobile.pogodacc.type.web.QualityFactor;
|
||||
import cc.pogoda.mobile.pogodacc.type.web.Summary;
|
||||
|
||||
/**
|
||||
* This class is used to update entries on Favourites list
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Used to get data from web service
|
||||
*/
|
||||
private SummaryDao dao = null;
|
||||
|
||||
/**
|
||||
* 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 FavouritesStationDetailsUpdater(Handler _handler) {
|
||||
handler = _handler;
|
||||
dao = new SummaryDao();
|
||||
stationsToUpdate = new HashMap<>();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (enabled && stationsToUpdate != null && stationsToUpdate.size() > 0) {
|
||||
|
||||
// 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 = dao.getStationSummary(stationSystemName);
|
||||
|
||||
// if data has been collected
|
||||
if (summary != null) {
|
||||
String str;
|
||||
|
||||
// check if this station transmits wind information
|
||||
if (summary.wind_qf_native.equals(QualityFactor.FULL) || summary.wind_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
|
||||
// check if station transmits humidity
|
||||
if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
str = String.format("%d°C %d%% %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.humidity, summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
}
|
||||
else {
|
||||
str = String.format("%d°C %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
str = String.format("%d°C %d%%", Math.round(summary.avg_temperature), summary.humidity);
|
||||
}
|
||||
else {
|
||||
str = String.format("%d°C", Math.round(summary.avg_temperature));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// update text view on the favourites list
|
||||
toUpdate.setText(str);
|
||||
}
|
||||
}
|
||||
|
||||
handler.postDelayed(this, 30000);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package cc.pogoda.mobile.pogodacc.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -15,6 +16,7 @@ import java.util.List;
|
|||
|
||||
import cc.pogoda.mobile.pogodacc.R;
|
||||
import cc.pogoda.mobile.pogodacc.activity.handler.AllStationsActRecyclerViewButtonClickEvent;
|
||||
import cc.pogoda.mobile.pogodacc.activity.updater.FavouritesStationDetailsUpdater;
|
||||
import cc.pogoda.mobile.pogodacc.activity.view.AllStationsActRecyclerViewHolder;
|
||||
import cc.pogoda.mobile.pogodacc.dao.SummaryDao;
|
||||
import cc.pogoda.mobile.pogodacc.type.ParceableFavsCallReason;
|
||||
|
@ -32,11 +34,41 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
|||
|
||||
SummaryDao summaryDao;
|
||||
|
||||
FavouritesStationDetailsUpdater favsUpdater = null;
|
||||
|
||||
Handler handler = null;
|
||||
|
||||
public WeatherStationRecyclerViewAdapter(List<WeatherStation> stations, AppCompatActivity parentActivity, ParceableFavsCallReason.Reason callReason) {
|
||||
this.stations = stations;
|
||||
this.activity = parentActivity;
|
||||
this.reason = callReason;
|
||||
this.summaryDao = new SummaryDao();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void createAndStartUpdater() {
|
||||
|
||||
if (reason.equals(ParceableFavsCallReason.Reason.FAVOURITES)) {
|
||||
|
||||
// check if there is previous instance of updater
|
||||
if (favsUpdater != null && favsUpdater.isEnabled()) {
|
||||
stopUpdater();
|
||||
}
|
||||
|
||||
handler = new Handler();
|
||||
favsUpdater = new FavouritesStationDetailsUpdater(handler);
|
||||
|
||||
handler.postDelayed(favsUpdater, 3000);
|
||||
favsUpdater.setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopUpdater() {
|
||||
if (reason.equals(ParceableFavsCallReason.Reason.FAVOURITES)) {
|
||||
handler.removeCallbacks(favsUpdater);
|
||||
favsUpdater.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -64,10 +96,16 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
|||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull AllStationsActRecyclerViewHolder holder, int position) {
|
||||
// this TextView shows the station name
|
||||
TextView textView = holder.textView;
|
||||
|
||||
// this TextView shows station data if this is favourites list
|
||||
TextView textViewData = holder.textViewData;
|
||||
|
||||
// button to go to the StationDetailsActivity
|
||||
Button button = holder.button;
|
||||
|
||||
// get the station object from a list of either all stations or favourites
|
||||
WeatherStation station = stations.get(position);
|
||||
|
||||
if (station != null) {
|
||||
|
@ -77,31 +115,36 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
|||
button.setOnClickListener(new AllStationsActRecyclerViewButtonClickEvent(station, activity, reason));
|
||||
}
|
||||
|
||||
if (textViewData != null) {
|
||||
Summary summary = summaryDao.getStationSummary(station.getSystemName());
|
||||
// this if distinguish between All Stations and Favorites view
|
||||
if (textViewData != null && favsUpdater != null) {
|
||||
|
||||
if (summary != null) {
|
||||
String str;
|
||||
if (summary.wind_qf_native.equals(QualityFactor.FULL) || summary.wind_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
str = String.format("%d°C %d%% %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.humidity, summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
}
|
||||
else {
|
||||
str = String.format("%d°C %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
str = String.format("%d°C %d%%", Math.round(summary.avg_temperature), summary.humidity);
|
||||
}
|
||||
else {
|
||||
str = String.format("%d°C", Math.round(summary.avg_temperature));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
textViewData.setText(str);
|
||||
}
|
||||
favsUpdater.addNewStation(station.getSystemName(), textViewData);
|
||||
// // in Favorites view there is 'textViewData' which displays measurements in each entry
|
||||
//
|
||||
// Summary summary = summaryDao.getStationSummary(station.getSystemName());
|
||||
//
|
||||
// if (summary != null) {
|
||||
// String str;
|
||||
// if (summary.wind_qf_native.equals(QualityFactor.FULL) || summary.wind_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
// if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
// str = String.format("%d°C %d%% %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.humidity, summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
// }
|
||||
// else {
|
||||
// str = String.format("%d°C %s %3.1f m/s max %3.1f m/s", Math.round(summary.avg_temperature), summary.getWindDirStr(), summary.average_speed, summary.gusts);
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
||||
// str = String.format("%d°C %d%%", Math.round(summary.avg_temperature), summary.humidity);
|
||||
// }
|
||||
// else {
|
||||
// str = String.format("%d°C", Math.round(summary.avg_temperature));
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// textViewData.setText(str);
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue