From febe7a5fdb1d93ada692ee962b965965361436fc Mon Sep 17 00:00:00 2001 From: sh123 Date: Mon, 5 Sep 2022 13:17:32 +0300 Subject: [PATCH] Refactor track into separate class --- .../radio/codec2talkie/maps/MapActivity.java | 56 +++---------- .../com/radio/codec2talkie/maps/MapTrack.java | 80 +++++++++++++++++++ 2 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapTrack.java diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapActivity.java b/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapActivity.java index 237f51d..b2c91fd 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapActivity.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapActivity.java @@ -5,7 +5,6 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; -import android.graphics.DashPathEffect; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; @@ -19,7 +18,6 @@ import android.view.MenuItem; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; -import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModelProvider; import androidx.preference.PreferenceManager; @@ -27,7 +25,6 @@ import com.radio.codec2talkie.R; import com.radio.codec2talkie.protocol.aprs.tools.AprsSymbolTable; import com.radio.codec2talkie.protocol.position.Position; import com.radio.codec2talkie.settings.PreferenceKeys; -import com.radio.codec2talkie.storage.position.PositionItem; import com.radio.codec2talkie.storage.position.PositionItemViewModel; import com.radio.codec2talkie.storage.station.StationItem; import com.radio.codec2talkie.storage.station.StationItemViewModel; @@ -42,7 +39,6 @@ import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.Marker; import org.osmdroid.views.overlay.Polygon; -import org.osmdroid.views.overlay.Polyline; import org.osmdroid.views.overlay.compass.CompassOverlay; import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider; import org.osmdroid.views.overlay.infowindow.MarkerInfoWindow; @@ -51,8 +47,6 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import java.util.Locale; public class MapActivity extends AppCompatActivity { @@ -73,11 +67,8 @@ public class MapActivity extends AppCompatActivity { private final HashMap _objectOverlayItems = new HashMap<>(); private final HashMap _objectOverlayRangeCircles = new HashMap<>(); - // track - private LiveData> _activeTrackLiveData; - private final HashSet _activeTrackTimestamps = new HashSet<>(); - private final List _activeTrackPoints = new ArrayList<>(); - private final Polyline _activeTrackLine = new Polyline(); + // selected track + private MapTrack _activeTrack; @Override protected void onCreate(Bundle savedInstanceState) { @@ -105,6 +96,9 @@ public class MapActivity extends AppCompatActivity { _mapController = _map.getController(); _mapController.zoomTo(5.0); + // track + _activeTrack = new MapTrack(_map, this); + // compass InternalCompassOrientationProvider compassOrientationProvider = new InternalCompassOrientationProvider(context) { @Override @@ -150,14 +144,6 @@ public class MapActivity extends AppCompatActivity { } } }); - - // add track - Paint p = _activeTrackLine.getOutlinePaint(); - p.setStrokeWidth(8); - p.setColor(Color.RED); - p.setStyle(Paint.Style.STROKE); - p.setPathEffect(new DashPathEffect(new float[] {10f, 10f}, 0f)); - _map.getOverlayManager().add(_activeTrackLine); } @Override @@ -199,21 +185,6 @@ public class MapActivity extends AppCompatActivity { polygon.setPoints(circlePoints); } - private void addTrack(List positions) { - boolean shouldSet = false; - for (PositionItem trackPoint : positions) { - if (!_activeTrackTimestamps.contains(trackPoint.getTimestampEpoch())) { - Log.i(TAG, "addPoint " + trackPoint.getTimestampEpoch() + " " + trackPoint.getLatitude() + " " + trackPoint.getLongitude()); - GeoPoint point = new GeoPoint(trackPoint.getLatitude(), trackPoint.getLongitude()); - _activeTrackPoints.add(point); - _activeTrackTimestamps.add(trackPoint.getTimestampEpoch()); - shouldSet = true; - } - } - if (shouldSet) - _activeTrackLine.setPoints(_activeTrackPoints); - } - private boolean addStationPositionIcon(StationItem group) { String callsign = group.getSrcCallsign(); Marker marker = null; @@ -300,19 +271,10 @@ public class MapActivity extends AppCompatActivity { marker.setId(callsign); marker.setIcon(drawableText); marker.setImage(drawableInfoIcon); - marker.setOnMarkerClickListener((monitoredMarker, mapView) -> { - GeoPoint markerPoint = monitoredMarker.getPosition(); - _infoWindow.open(monitoredMarker, new GeoPoint(markerPoint.getLatitude(), markerPoint.getLongitude()), 0, -2*height); - if (_activeTrackLiveData != null) - _activeTrackLiveData.removeObservers(this); - _map.getOverlays().remove(_activeTrackLine); - _activeTrackPoints.clear(); - _activeTrackTimestamps.clear(); - _activeTrackLine.setPoints(_activeTrackPoints); - _map.getOverlays().add(_activeTrackLine); - // FIXME, room livedata sends all list if one item changed event with distinctUntilChanged - _activeTrackLiveData = _positionItemViewModel.getPositionItems(monitoredMarker.getId()); - _activeTrackLiveData.observe(this, this::addTrack); + marker.setOnMarkerClickListener((monitoredStationMarker, mapView) -> { + GeoPoint markerPoint = monitoredStationMarker.getPosition(); + _infoWindow.open(monitoredStationMarker, new GeoPoint(markerPoint.getLatitude(), markerPoint.getLongitude()), 0, -2*height); + _activeTrack.drawForStationMarker(monitoredStationMarker); return false; }); _map.getOverlays().add(marker); diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapTrack.java b/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapTrack.java new file mode 100644 index 0000000..ddf9494 --- /dev/null +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/maps/MapTrack.java @@ -0,0 +1,80 @@ +package com.radio.codec2talkie.maps; + +import android.graphics.Color; +import android.graphics.DashPathEffect; +import android.graphics.Paint; +import android.util.Log; + +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModelProvider; +import androidx.lifecycle.ViewModelStoreOwner; + +import com.radio.codec2talkie.storage.position.PositionItem; +import com.radio.codec2talkie.storage.position.PositionItemViewModel; + +import org.osmdroid.util.GeoPoint; +import org.osmdroid.views.MapView; +import org.osmdroid.views.overlay.Marker; +import org.osmdroid.views.overlay.Polyline; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +public class MapTrack { + private static final String TAG = MapTrack.class.getSimpleName(); + + private final PositionItemViewModel _positionItemViewModel; + private final ViewModelStoreOwner _owner; + private final MapView _mapView; + + private LiveData> _activeTrackLiveData; + private final HashSet _activeTrackTimestamps = new HashSet<>(); + private final List _activeTrackPoints = new ArrayList<>(); + private final Polyline _activeTrackLine = new Polyline(); + + public MapTrack(MapView mapView, ViewModelStoreOwner owner) { + _owner = owner; + _mapView = mapView; + _positionItemViewModel = new ViewModelProvider(_owner).get(PositionItemViewModel.class); + + // initialize track + Paint p = _activeTrackLine.getOutlinePaint(); + p.setStrokeWidth(8); + p.setColor(Color.RED); + p.setStyle(Paint.Style.STROKE); + p.setPathEffect(new DashPathEffect(new float[] {10f, 10f}, 0f)); + _mapView.getOverlayManager().add(_activeTrackLine); + } + + public void drawForStationMarker(Marker marker) { + GeoPoint markerPoint = marker.getPosition(); + if (_activeTrackLiveData != null) + _activeTrackLiveData.removeObservers((LifecycleOwner) _owner); + _mapView.getOverlays().remove(_activeTrackLine); + _activeTrackPoints.clear(); + _activeTrackTimestamps.clear(); + _activeTrackLine.setPoints(_activeTrackPoints); + _mapView.getOverlays().add(_activeTrackLine); + // FIXME, room livedata sends all list if one item changed event with distinctUntilChanged + _activeTrackLiveData = _positionItemViewModel.getPositionItems(marker.getId()); + _activeTrackLiveData.observe((LifecycleOwner) _owner, this::addTrack); + } + + private void addTrack(List positions) { + boolean shouldSet = false; + for (PositionItem trackPoint : positions) { + if (!_activeTrackTimestamps.contains(trackPoint.getTimestampEpoch())) { + Log.i(TAG, "addPoint " + trackPoint.getTimestampEpoch() + " " + trackPoint.getLatitude() + " " + trackPoint.getLongitude()); + GeoPoint point = new GeoPoint(trackPoint.getLatitude(), trackPoint.getLongitude()); + _activeTrackPoints.add(point); + _activeTrackTimestamps.add(trackPoint.getTimestampEpoch()); + shouldSet = true; + } + } + if (shouldSet) + _activeTrackLine.setPoints(_activeTrackPoints); + } +}