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) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(sortedFavourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(sortedFavourites.getList(), this, callReason.getReason());
|
||||||
|
|
||||||
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
recyclerViewFavourites.setAdapter(adapter);
|
recyclerViewFavourites.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +73,8 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
||||||
|
|
||||||
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
recyclerViewFavourites.setAdapter(adapter);
|
recyclerViewFavourites.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,10 +109,19 @@ public class FavouritesActivity extends AppCompatActivity {
|
||||||
if (recyclerViewFavourites != null) {
|
if (recyclerViewFavourites != null) {
|
||||||
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
adapter = new WeatherStationRecyclerViewAdapter(favourites.getList(), this, callReason.getReason());
|
||||||
|
|
||||||
|
adapter.createAndStartUpdater();
|
||||||
|
|
||||||
recyclerViewFavourites.setAdapter(adapter);
|
recyclerViewFavourites.setAdapter(adapter);
|
||||||
|
|
||||||
recyclerViewFavourites.setLayoutManager(new LinearLayoutManager(this));
|
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;
|
package cc.pogoda.mobile.pogodacc.adapter;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
@ -15,6 +16,7 @@ import java.util.List;
|
||||||
|
|
||||||
import cc.pogoda.mobile.pogodacc.R;
|
import cc.pogoda.mobile.pogodacc.R;
|
||||||
import cc.pogoda.mobile.pogodacc.activity.handler.AllStationsActRecyclerViewButtonClickEvent;
|
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.activity.view.AllStationsActRecyclerViewHolder;
|
||||||
import cc.pogoda.mobile.pogodacc.dao.SummaryDao;
|
import cc.pogoda.mobile.pogodacc.dao.SummaryDao;
|
||||||
import cc.pogoda.mobile.pogodacc.type.ParceableFavsCallReason;
|
import cc.pogoda.mobile.pogodacc.type.ParceableFavsCallReason;
|
||||||
|
@ -32,11 +34,41 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
||||||
|
|
||||||
SummaryDao summaryDao;
|
SummaryDao summaryDao;
|
||||||
|
|
||||||
|
FavouritesStationDetailsUpdater favsUpdater = null;
|
||||||
|
|
||||||
|
Handler handler = null;
|
||||||
|
|
||||||
public WeatherStationRecyclerViewAdapter(List<WeatherStation> stations, AppCompatActivity parentActivity, ParceableFavsCallReason.Reason callReason) {
|
public WeatherStationRecyclerViewAdapter(List<WeatherStation> stations, AppCompatActivity parentActivity, ParceableFavsCallReason.Reason callReason) {
|
||||||
this.stations = stations;
|
this.stations = stations;
|
||||||
this.activity = parentActivity;
|
this.activity = parentActivity;
|
||||||
this.reason = callReason;
|
this.reason = callReason;
|
||||||
this.summaryDao = new SummaryDao();
|
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
|
@NonNull
|
||||||
|
@ -64,10 +96,16 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull AllStationsActRecyclerViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull AllStationsActRecyclerViewHolder holder, int position) {
|
||||||
|
// this TextView shows the station name
|
||||||
TextView textView = holder.textView;
|
TextView textView = holder.textView;
|
||||||
|
|
||||||
|
// this TextView shows station data if this is favourites list
|
||||||
TextView textViewData = holder.textViewData;
|
TextView textViewData = holder.textViewData;
|
||||||
|
|
||||||
|
// button to go to the StationDetailsActivity
|
||||||
Button button = holder.button;
|
Button button = holder.button;
|
||||||
|
|
||||||
|
// get the station object from a list of either all stations or favourites
|
||||||
WeatherStation station = stations.get(position);
|
WeatherStation station = stations.get(position);
|
||||||
|
|
||||||
if (station != null) {
|
if (station != null) {
|
||||||
|
@ -77,31 +115,36 @@ public class WeatherStationRecyclerViewAdapter extends RecyclerView.Adapter<AllS
|
||||||
button.setOnClickListener(new AllStationsActRecyclerViewButtonClickEvent(station, activity, reason));
|
button.setOnClickListener(new AllStationsActRecyclerViewButtonClickEvent(station, activity, reason));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textViewData != null) {
|
// this if distinguish between All Stations and Favorites view
|
||||||
Summary summary = summaryDao.getStationSummary(station.getSystemName());
|
if (textViewData != null && favsUpdater != null) {
|
||||||
|
|
||||||
if (summary != null) {
|
favsUpdater.addNewStation(station.getSystemName(), textViewData);
|
||||||
String str;
|
// // in Favorites view there is 'textViewData' which displays measurements in each entry
|
||||||
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)) {
|
// Summary summary = summaryDao.getStationSummary(station.getSystemName());
|
||||||
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);
|
//
|
||||||
}
|
// if (summary != null) {
|
||||||
else {
|
// String str;
|
||||||
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);
|
// 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 {
|
// }
|
||||||
if (summary.humidity_qf_native.equals(QualityFactor.FULL) || summary.humidity_qf_native.equals(QualityFactor.DEGRADED)) {
|
// else {
|
||||||
str = String.format("%d°C %d%%", Math.round(summary.avg_temperature), summary.humidity);
|
// 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 {
|
// }
|
||||||
str = String.format("%d°C", Math.round(summary.avg_temperature));
|
// 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 {
|
||||||
textViewData.setText(str);
|
// str = String.format("%d°C", Math.round(summary.avg_temperature));
|
||||||
}
|
//
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// textViewData.setText(str);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue