Skip to content

Commit

Permalink
Merge pull request #87 from kike-canaries/av/fix_map_init
Browse files Browse the repository at this point in the history
Av/fix map init
  • Loading branch information
hpsaturn authored Mar 31, 2021
2 parents c1eb9fa + 9936e8e commit c2ba687
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class RecordTrackService extends Service {
private RecordTrackManager recordTrackManager;

private final int RETRY_POLICY = 5;
private final int MAX_POINTS_SAVING = 1500;
private final int MAX_POINTS_SAVING = 3000;

private int retry_connect = 0;
private int retry_notify_setup = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class ChartFragment extends Fragment {
RelativeLayout rl_separator;

private long referenceTimestamp;
private boolean loadingData = true;
private boolean loadingData = true; // it's true for block data from real time chart

private static final String KEY_RECORD_ID = "key_record_id";
private String recordId;
Expand Down Expand Up @@ -250,11 +250,15 @@ private void calculateReferenceTime(){
* @param data
*/
private void addData(ArrayList<SensorData> data){
if(data==null)return;
if(data==null){
chart.setNoDataText("No data.");
loadingData = false;
return;
}
else if (!data.isEmpty()) {
Iterator<SensorData> it = data.iterator();
int count = 0;
while (it.hasNext() && count++ <3000) {
while (it.hasNext() && count++ <=5000) {
SensorData d = it.next();
long time = d.timestamp - referenceTimestamp;
addValue(time,d);
Expand Down Expand Up @@ -305,6 +309,8 @@ public void addData(SensorData data) {
addValue(time,data);
refreshDataSets();
}
else
Logger.v(TAG,"addData skip, in loading data.");
}

private void addMapSegment(ChartVar var, SensorData data) {
Expand Down
52 changes: 27 additions & 25 deletions app/src/main/java/hpsaturn/pollutionreporter/view/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().runOnUiThread(() -> setupMap(view));
setupMap(view);
requireActivity().runOnUiThread(this::loadLastTracks);
}

private void setupMap(View view) {
Expand All @@ -78,46 +79,47 @@ private void setupMap(View view) {
mapView.setUseDataConnection(true); //keeps the mapView from loading online tiles using network connection.
mapView.setEnabled(true);
(mapView.getTileProvider().getTileCache()).getProtectedTileComputers().clear();
loadLastTracks();
}

private void loadLastTracks() {
Query query = getMain().getDatabase().child(Config.FB_TRACKS_INFO).orderByKey().limitToLast(500);
Query query = getMain().getDatabase().child(Config.FB_TRACKS_INFO).orderByKey().limitToLast(150);
query.get().addOnCompleteListener(task -> {

if (task.isSuccessful()) {
Iterable<DataSnapshot> data = task.getResult().getChildren();
for (DataSnapshot datum : data) {
SensorTrackInfo track = datum.getValue(SensorTrackInfo.class);
if (track.getSize() > 0 && track.getSize() < 3000) addMarker(track);
if (track.getSize() > 0 && track.getSize() <= 5000) addMarker(track);
}
Logger.i(TAG,"markers count:"+mapView.getOverlays().size());
Logger.v(TAG,"markers count:"+mapView.getOverlays().size());
}
});

}

public void addMarker(SensorTrackInfo trackInfo) {
Marker pointMarker = new Marker(mapView);
pointMarker.setOnMarkerClickListener((marker, mapView) -> {
Logger.d(TAG, "OnMarkerClickListener => " + trackInfo.getName());
getMain().showTrackInfoFragment(trackInfo.getName());
return true;
});

SensorData lastSensorData = trackInfo.getLastSensorData();

Drawable icon;
if(lastSensorData!=null && lastSensorData.P25 > 20)
icon = ResourcesCompat.getDrawable(getResources(), R.drawable.map_mark_red, null);
else
icon = ResourcesCompat.getDrawable(getResources(), R.drawable.map_mark_yellow, null);

pointMarker.setPosition(new GeoPoint(trackInfo.getLastLat(), trackInfo.getLastLon()));
pointMarker.setIcon(icon);

mapView.getOverlays().add(pointMarker);
mapView.getController().setCenter(new GeoPoint(trackInfo.getLastLat(),trackInfo.getLastLon()));
if(mapView!=null){
Marker pointMarker = new Marker(mapView);
pointMarker.setOnMarkerClickListener((marker, mapView) -> {
Logger.d(TAG, "OnMarkerClickListener => " + trackInfo.getName());
getMain().showTrackInfoFragment(trackInfo.getName());
return true;
});

SensorData lastSensorData = trackInfo.getLastSensorData();

Drawable icon;
if(lastSensorData!=null && lastSensorData.P25 > 20)
icon = ResourcesCompat.getDrawable(getResources(), R.drawable.map_mark_red, null);
else
icon = ResourcesCompat.getDrawable(getResources(), R.drawable.map_mark_yellow, null);

pointMarker.setPosition(new GeoPoint(trackInfo.getLastLat(), trackInfo.getLastLon()));
pointMarker.setIcon(icon);

mapView.getOverlays().add(pointMarker);
mapView.getController().setCenter(new GeoPoint(trackInfo.getLastLat(),trackInfo.getLastLon()));
}
}

private void loadAqicnData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
super.onViewCreated(view, savedInstanceState);

// Set up FirebaseRecyclerAdapter with the Query
Query postsQuery = getMain().getDatabase().child(Config.FB_TRACKS_INFO).orderByKey().limitToLast(50);
Query postsQuery = getMain().getDatabase().child(Config.FB_TRACKS_INFO).orderByKey().limitToLast(100);
Logger.d(TAG,"[FB][POSTS] Query: "+postsQuery.toString());
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<SensorTrackInfo>()
.setQuery(postsQuery, SensorTrackInfo.class)
Expand Down Expand Up @@ -97,9 +97,24 @@ protected void onBindViewHolder(@NonNull PostsViewHolder viewHolder, int positio
mRecordsList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mAdapter.startListening();
mUpdateTimeTask.run(); // TODO: fucking workaround, firebase recycler wasn't update in fist time

}

private Handler mHandler = new Handler();
private UpdateTimeTask mUpdateTimeTask = new UpdateTimeTask();

class UpdateTimeTask extends TimerTask {
private int counter = 0;
public void run() {
Logger.i(TAG,"[FB][POST] UpdateTimeTask, force refresh data..");
refresh();
int retries = 2;
if(counter++> retries)this.cancel();
else mHandler.postDelayed(this,3000);
}
}

private void updateUI() {
if(mAdapter!=null&&mAdapter.getItemCount()>0) {
mEmptyMessage.setVisibility(View.GONE);
Expand All @@ -123,9 +138,20 @@ public void onDestroy() {
if (mAdapter != null) {
mAdapter.stopListening();
}
try {
mHandler.removeCallbacks(mUpdateTimeTask);
} catch (Exception e) {
e.printStackTrace();
}
super.onDestroy();
}

@Override
public void onResume() {
super.onResume();
updateUI();
}

private MainActivity getMain() {
return ((MainActivity) getActivity());
}
Expand Down

0 comments on commit c2ba687

Please sign in to comment.