Skip to content

Commit

Permalink
Refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
vshcherb committed Feb 21, 2025
1 parent 002d539 commit 3eb681c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
2 changes: 2 additions & 0 deletions OsmAnd/res/layout/search_nearby_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
android:maxLines="1"
android:ellipsize="end"
tools:text="Monument" />


</LinearLayout>

</LinearLayout>
29 changes: 28 additions & 1 deletion OsmAnd/res/layout/search_nearby_item_vertical.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
android:id="@+id/item_icon"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical|top"
android:layout_gravity="center_vertical"
android:layout_marginEnd="6dp" />

<TextView
Expand All @@ -58,6 +58,33 @@
android:textColor="?android:textColorSecondary"
android:textSize="14sp"
tools:text="Monument" />

<LinearLayout
android:id="@+id/compass_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|right"
android:orientation="horizontal">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/direction"
android:layout_width="@dimen/directionIconSize"
android:layout_height="@dimen/directionIconSize"
android:layout_marginTop="1sp"
android:layout_gravity="center_vertical"/>

<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_sub_text_size"
tools:text="100500 km"
android:layout_marginStart="2dp" />

</LinearLayout>
</LinearLayout>

</LinearLayout>
Expand Down
68 changes: 59 additions & 9 deletions OsmAnd/src/net/osmand/plus/search/NearbyPlacesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
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.plus.OsmandApplication
import net.osmand.plus.R
import net.osmand.plus.helpers.AndroidUiHelper
import net.osmand.plus.render.RenderingIcons
import net.osmand.plus.utils.OsmAndFormatter
import net.osmand.plus.utils.PicassoUtils
import net.osmand.plus.utils.UiUtilities
import net.osmand.plus.utils.UpdateLocationUtils
import net.osmand.util.Algorithms
import net.osmand.wiki.WikiImage

class NearbyPlacesAdapter(
val app: OsmandApplication,
Expand All @@ -28,13 +31,17 @@ class NearbyPlacesAdapter(
fun onNearbyItemClicked(item: NearbyPlacePoint)
}

// Initialize the UpdateLocationViewCache
private val updateLocationViewCache = UpdateLocationUtils.getUpdateLocationViewCache(app)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NearbyViewHolder {
val inflater = UiUtilities.getInflater(parent.context, isNightMode())
val view = inflater.inflate(
if (isVertical) R.layout.search_nearby_item_vertical else R.layout.search_nearby_item,
parent,
false)
return NearbyViewHolder(view)
false
)
return NearbyViewHolder(view, updateLocationViewCache)
}

private fun isNightMode(): Boolean {
Expand All @@ -43,19 +50,24 @@ class NearbyPlacesAdapter(

override fun onBindViewHolder(holder: NearbyViewHolder, position: Int) {
val item = items[position]
holder.bind(item, onItemClickListener)
holder.bind(item, onItemClickListener, position)
}

override fun getItemCount(): Int = items.size

class NearbyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
class NearbyViewHolder(
itemView: View,
private val updateLocationViewCache: UpdateLocationUtils.UpdateLocationViewCache
) : RecyclerView.ViewHolder(itemView) {
private val imageView: ImageView = itemView.findViewById(R.id.item_image)
private val iconImageView: ImageView = itemView.findViewById(R.id.item_icon)
private val titleTextView: TextView = itemView.findViewById(R.id.item_title)
private val descriptionTextView: TextView? = itemView.findViewById(R.id.item_description)
private val itemTypeTextView: TextView = itemView.findViewById(R.id.item_type)
private val distanceTextView: TextView? = itemView.findViewById(R.id.distance)
private val arrowImageView: ImageView? = itemView.findViewById(R.id.direction)

fun bind(item: NearbyPlacePoint, onItemClickListener: NearbyItemClickListener) {
fun bind(item: NearbyPlacePoint, onItemClickListener: NearbyItemClickListener, position: Int) {
val app = imageView.context.applicationContext as OsmandApplication
val poiTypes = app.poiTypes
val subType = poiTypes.getPoiTypeByKey(item.poisubtype)
Expand All @@ -66,8 +78,8 @@ class NearbyPlacesAdapter(
uiUtilities.getRenderingIcon(
app,
subType.keyName,
nightMode)

nightMode
)
} else {
uiUtilities.getIcon(R.drawable.ic_action_info_dark, nightMode)
}
Expand All @@ -90,13 +102,51 @@ class NearbyPlacesAdapter(
}
})
}

// Add row number to the title
titleTextView.text = "${position + 1}. ${item.wikiTitle}"

descriptionTextView?.text = item.wikiDesc
descriptionTextView?.let {
AndroidUiHelper.updateVisibility(it, !Algorithms.isEmpty(item.wikiDesc))
}
titleTextView.text = item.wikiTitle

itemTypeTextView.text = subType.translation

// Calculate distance and show arrow
if (distanceTextView != null && arrowImageView != null) {
val distance = calculateDistance(app, item)
if (distance != null) {
distanceTextView.text = OsmAndFormatter.getFormattedDistance(distance, app)
distanceTextView.visibility = View.VISIBLE
arrowImageView.visibility = View.VISIBLE

// Update compass icon rotation
val latLon = LatLon(item.latitude, item.longitude)
UpdateLocationUtils.updateLocationView(app, updateLocationViewCache, arrowImageView, distanceTextView, latLon)
} else {
distanceTextView.visibility = View.GONE
arrowImageView.visibility = View.GONE
}
}

itemView.setOnClickListener { onItemClickListener.onNearbyItemClicked(item) }
}

private fun calculateDistance(app: OsmandApplication, item: NearbyPlacePoint): Float? {
val currentLocation = app.locationProvider?.lastKnownLocation
if (currentLocation != null) {
val results = FloatArray(1)
Location.distanceBetween(
currentLocation.latitude,
currentLocation.longitude,
item.latitude,
item.longitude,
results
)
return results[0]
}
return null
}
}
}

0 comments on commit 3eb681c

Please sign in to comment.