Skip to content

Commit

Permalink
Introduce db cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vshcherb committed Feb 21, 2025
1 parent a4f1b59 commit b9861a3
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.io.Serializable;


public class NearbyPlacePoint implements Serializable, LocationPoint {
public class ExploreTopPlacePoint implements Serializable, LocationPoint {

private static final long serialVersionUID = 829654300829771466L;

Expand All @@ -35,7 +35,7 @@ public class NearbyPlacePoint implements Serializable, LocationPoint {
@Nullable
private Bitmap imageBitmap;

public NearbyPlacePoint(OsmandApiFeatureData featureData) {
public ExploreTopPlacePoint(OsmandApiFeatureData featureData) {
this.id = featureData.properties.osmid;
WikiImage wikiIMage = WikiCoreHelper.getImageData(featureData.properties.photoTitle);
this.iconUrl = wikiIMage == null ? "" : wikiIMage.getImageIconUrl();
Expand Down Expand Up @@ -144,7 +144,7 @@ public boolean equals(Object o) {
return false;
}

NearbyPlacePoint point = (NearbyPlacePoint) o;
ExploreTopPlacePoint point = (ExploreTopPlacePoint) o;
if (!Algorithms.stringsEqual(photoTitle, point.photoTitle)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import net.osmand.PlatformUtil
import net.osmand.data.NearbyPlacePoint
import net.osmand.data.ExploreTopPlacePoint
import net.osmand.data.QuadRect
import net.osmand.plus.R
import net.osmand.plus.activities.MapActivity
Expand Down Expand Up @@ -84,10 +84,7 @@ class ExplorePlacesFragment : BaseOsmAndFragment(), NearbyPlacesAdapter.NearbyIt
.setImageDrawable(uiUtilities.getIcon(R.drawable.ic_action_marker_dark, nightMode))

view.findViewById<View>(R.id.show_on_map).setOnClickListener {
// TODO make dynamic load
app.osmandMap.mapLayers.nearbyPlacesLayer.setCustomMapObjects(
app.explorePlacesProvider.getDataCollection(app.osmandMap.mapView.currentRotatedTileBox.latLonBounds)
)
app.osmandMap.mapLayers.explorePlacesLayer.enableLayer(true)
hide()
}
}
Expand Down Expand Up @@ -152,7 +149,7 @@ class ExplorePlacesFragment : BaseOsmAndFragment(), NearbyPlacesAdapter.NearbyIt

}

override fun onNearbyItemClicked(item: NearbyPlacePoint) {
override fun onNearbyItemClicked(item: ExploreTopPlacePoint) {
mapActivity?.let {
app.explorePlacesProvider.showPointInContextMenu(it, item)
hide()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.data.NearbyPlacePoint;
import net.osmand.data.ExploreTopPlacePoint;
import net.osmand.data.QuadRect;
import net.osmand.plus.activities.MapActivity;

Expand All @@ -12,11 +12,11 @@

public interface ExplorePlacesProvider {

@NotNull List<NearbyPlacePoint> getDataCollection(QuadRect mapRect);
@NotNull List<ExploreTopPlacePoint> getDataCollection(QuadRect mapRect);

@NotNull List<NearbyPlacePoint> getDataCollection(QuadRect mapRect, int limit);
@NotNull List<ExploreTopPlacePoint> getDataCollection(QuadRect mapRect, int limit);

void showPointInContextMenu(@NotNull MapActivity it, @NotNull NearbyPlacePoint item);
void showPointInContextMenu(@NotNull MapActivity it, @NotNull ExploreTopPlacePoint item);

void addListener(ExplorePlacesListener listener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.osmand.binary.ObfConstants;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.data.NearbyPlacePoint;
import net.osmand.data.ExploreTopPlacePoint;
import net.osmand.data.QuadRect;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
Expand All @@ -31,16 +31,18 @@
// TODO errors shouldn'go with empty response "" into cache!
// TODO remove checks poi type subtype null
// TODO display all data downloaded even if maps are not loaded
// TODO: why recreate provider when new points are loaded? that causes blinking
// Extra: display new categories from web
public class ExplorePlacesProviderJava implements ExplorePlacesProvider {

private OsmandApplication app;
private static final int DEFAULT_LIMIT_POINTS = 200;
private static final int NEARBY_MIN_RADIUS = 50;

private final int MAX_LEVEL_ZOOM_CACHE = 13;
private static final int MAX_TILES_PER_QUAD_RECT = 12;
private static final double LOAD_ALL_TINY_RECT = 0.5;

private OsmandApplication app;
private volatile int startedTasks = 0;
private volatile int finishedTasks = 0;

Expand Down Expand Up @@ -90,11 +92,11 @@ private String getLang() {
return preferredLang;
}

public List<NearbyPlacePoint> getDataCollection(QuadRect rect) {
return getDataCollection(rect, 1000);
public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect) {
return getDataCollection(rect, DEFAULT_LIMIT_POINTS);
}

public List<NearbyPlacePoint> getDataCollection(QuadRect rect, int limit) {
public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect, int limit) {
if (rect == null) {
return Collections.emptyList();
}
Expand All @@ -121,7 +123,7 @@ public List<NearbyPlacePoint> getDataCollection(QuadRect rect, int limit) {

// Fetch data for all tiles within the bounds
PlacesDatabaseHelper dbHelper = new PlacesDatabaseHelper(app);
List<NearbyPlacePoint> filteredPoints = new ArrayList<>();
List<ExploreTopPlacePoint> filteredPoints = new ArrayList<>();
Set<Long> uniqueIds = new HashSet<>(); // Use a Set to track unique IDs
final String queryLang = getLang();

Expand All @@ -136,7 +138,7 @@ public List<NearbyPlacePoint> getDataCollection(QuadRect rect, int limit) {
|| item.properties.poitype == null || item.properties.poisubtype == null) {
continue;
}
NearbyPlacePoint point = new NearbyPlacePoint(item);
ExploreTopPlacePoint point = new ExploreTopPlacePoint(item);
double lat = point.getLatitude();
double lon = point.getLongitude();
if ((rect.contains(lon, lat, lon, lat) || loadAll) && uniqueIds.add(point.getId())) {
Expand Down Expand Up @@ -209,7 +211,7 @@ public void onFinish(@NonNull List<? extends OsmandApiFeatureData> result) {
}).execute();
}

public void showPointInContextMenu(MapActivity mapActivity, NearbyPlacePoint point) {
public void showPointInContextMenu(MapActivity mapActivity, ExploreTopPlacePoint point) {
double latitude = point.getLatitude();
double longitude = point.getLongitude();
app.getSettings().setMapLocationToShow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static MenuController getMenuController(@NonNull MapActivity mapActivity,
}
} else if (object instanceof SearchHistoryHelper.HistoryEntry) {
menuController = new HistoryMenuController(mapActivity, pointDescription, (SearchHistoryHelper.HistoryEntry) object);
} else if (object instanceof NearbyPlacePoint point) {
} else if (object instanceof ExploreTopPlacePoint point) {
Amenity amenity = mapActivity.getMyApplication().getExplorePlacesProvider().getAmenity(
new LatLon(point.getLatitude(), point.getLongitude()), point.getId());
if (amenity != null) {
Expand Down
10 changes: 5 additions & 5 deletions OsmAnd/src/net/osmand/plus/search/NearbyPlacesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import net.osmand.Location
import net.osmand.data.LatLon
import net.osmand.data.NearbyPlacePoint
import net.osmand.data.ExploreTopPlacePoint
import net.osmand.plus.OsmandApplication
import net.osmand.plus.R
import net.osmand.plus.helpers.AndroidUiHelper
Expand All @@ -22,13 +22,13 @@ import net.osmand.util.Algorithms

class NearbyPlacesAdapter(
val app: OsmandApplication,
var items: List<NearbyPlacePoint>,
var items: List<ExploreTopPlacePoint>,
private var isVertical: Boolean,
private val onItemClickListener: NearbyItemClickListener
) : RecyclerView.Adapter<NearbyPlacesAdapter.NearbyViewHolder>() {

interface NearbyItemClickListener {
fun onNearbyItemClicked(item: NearbyPlacePoint)
fun onNearbyItemClicked(item: ExploreTopPlacePoint)
}

// Initialize the UpdateLocationViewCache
Expand Down Expand Up @@ -67,7 +67,7 @@ class NearbyPlacesAdapter(
private val distanceTextView: TextView? = itemView.findViewById(R.id.distance)
private val arrowImageView: ImageView? = itemView.findViewById(R.id.direction)

fun bind(item: NearbyPlacePoint, onItemClickListener: NearbyItemClickListener, position: Int) {
fun bind(item: ExploreTopPlacePoint, onItemClickListener: NearbyItemClickListener, position: Int) {
val app = imageView.context.applicationContext as OsmandApplication
val poiTypes = app.poiTypes
val subType = poiTypes.getPoiTypeByKey(item.poisubtype)
Expand Down Expand Up @@ -133,7 +133,7 @@ class NearbyPlacesAdapter(
itemView.setOnClickListener { onItemClickListener.onNearbyItemClicked(item) }
}

private fun calculateDistance(app: OsmandApplication, item: NearbyPlacePoint): Float? {
private fun calculateDistance(app: OsmandApplication, item: ExploreTopPlacePoint): Float? {
val currentLocation = app.locationProvider?.lastKnownLocation
if (currentLocation != null) {
val results = FloatArray(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -973,7 +972,7 @@ public void onDismiss(@NonNull DialogInterface dialog) {
fragmentManager.popBackStack();
}
}
app.getOsmandMap().getMapLayers().getNearbyPlacesLayer().customObjectsDelegate.setCustomMapObjects(null);
app.getOsmandMap().getMapLayers().getExplorePlacesLayer().enableLayer(false);
super.onDismiss(dialog);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import net.osmand.data.NearbyPlacePoint;
import net.osmand.data.ExploreTopPlacePoint;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry;
Expand All @@ -29,7 +29,7 @@ public class QuickSearchHistoryListFragment extends QuickSearchListFragment impl
private boolean selectionMode;
private NearbyPlacesCard nearbyPlacesCard;

public void onNearbyItemClicked(@NonNull NearbyPlacePoint point) {
public void onNearbyItemClicked(@NonNull ExploreTopPlacePoint point) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
getMyApplication().getExplorePlacesProvider().showPointInContextMenu(mapActivity, point);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import net.osmand.data.NearbyPlacePoint;
import net.osmand.data.ExploreTopPlacePoint;
import net.osmand.data.QuadRect;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
Expand Down Expand Up @@ -97,7 +97,7 @@ private void updateExpandState() {

private NearbyPlacesAdapter getNearbyAdapter() {
if (adapter == null) {
List<NearbyPlacePoint> nearbyData = app.getExplorePlacesProvider().getDataCollection(visiblePlacesRect);
List<ExploreTopPlacePoint> nearbyData = app.getExplorePlacesProvider().getDataCollection(visiblePlacesRect);
adapter = new NearbyPlacesAdapter(app, nearbyData, false, clickListener);
}
return adapter;
Expand Down
10 changes: 5 additions & 5 deletions OsmAnd/src/net/osmand/plus/views/MapLayers.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class MapLayers {
private PreviewRouteLineLayer previewRouteLineLayer;
private POIMapLayer poiMapLayer;
private FavouritesLayer mFavouritesLayer;
private NearbyPlacesLayer nearbyPlacesLayer;
private ExploreTopPlacesLayer explorePlacesLayer;
private TransportStopsLayer transportStopsLayer;
private PointLocationLayer locationLayer;
private RadiusRulerControlLayer radiusRulerControlLayer;
Expand Down Expand Up @@ -185,8 +185,8 @@ public void createLayers(@NonNull OsmandMapTileView mapView) {
mapInfoLayer = new MapInfoLayer(app, routeLayer);
mapView.addLayer(mapInfoLayer, 9);

nearbyPlacesLayer = new NearbyPlacesLayer(app);
mapView.addLayer(nearbyPlacesLayer, 4.1f);
explorePlacesLayer = new ExploreTopPlacesLayer(app);
mapView.addLayer(explorePlacesLayer, 4.1f);
// 11. route info layer
mapControlsLayer = new MapControlsLayer(app);
mapView.addLayer(mapControlsLayer, 11);
Expand Down Expand Up @@ -623,8 +623,8 @@ public FavouritesLayer getFavouritesLayer() {
return mFavouritesLayer;
}

public NearbyPlacesLayer getNearbyPlacesLayer() {
return nearbyPlacesLayer;
public ExploreTopPlacesLayer getExplorePlacesLayer() {
return explorePlacesLayer;
}

public MeasurementToolLayer getMeasurementToolLayer() {
Expand Down
Loading

0 comments on commit b9861a3

Please sign in to comment.