-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
466 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package delivery | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import element.Element | ||
import icons.iconTypeface | ||
import org.btcmap.databinding.ItemDeliveryBinding | ||
|
||
class DeliveryAdapter( | ||
private val onItemClick: (Item) -> Unit, | ||
) : ListAdapter<DeliveryAdapter.Item, DeliveryAdapter.ItemViewHolder>(DiffCallback()) { | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): ItemViewHolder { | ||
val binding = ItemDeliveryBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false, | ||
) | ||
|
||
binding.icon.typeface = parent.context.iconTypeface() | ||
|
||
return ItemViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | ||
holder.bind(getItem(position), onItemClick) | ||
} | ||
|
||
data class Item( | ||
val element: Element, | ||
val icon: String, | ||
val name: String, | ||
val distanceToUser: String, | ||
) | ||
|
||
class ItemViewHolder( | ||
private val binding: ItemDeliveryBinding, | ||
) : RecyclerView.ViewHolder( | ||
binding.root, | ||
) { | ||
|
||
fun bind(item: Item, onItemClick: (Item) -> Unit) { | ||
binding.apply { | ||
icon.text = item.icon | ||
name.text = item.name | ||
distance.visibility = | ||
if (item.distanceToUser.isNotEmpty()) View.VISIBLE else View.GONE | ||
distance.text = item.distanceToUser | ||
root.setOnClickListener { onItemClick(item) } | ||
} | ||
} | ||
} | ||
|
||
class DiffCallback : DiffUtil.ItemCallback<Item>() { | ||
|
||
override fun areItemsTheSame( | ||
oldItem: Item, | ||
newItem: Item, | ||
): Boolean { | ||
return newItem.element.id == oldItem.element.id | ||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: Item, | ||
newItem: Item, | ||
): Boolean { | ||
return true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package delivery | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.core.view.updateLayoutParams | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import org.btcmap.databinding.FragmentDeliveryBinding | ||
import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
import search.SearchResultModel | ||
|
||
class DeliveryFragment : Fragment() { | ||
|
||
private val model: DeliveryModel by viewModel() | ||
|
||
private val resultModel: SearchResultModel by activityViewModel() | ||
|
||
private var _binding: FragmentDeliveryBinding? = null | ||
private val binding get() = _binding!! | ||
|
||
private val adapter = DeliveryAdapter { item -> | ||
resultModel.element.update { item.element } | ||
findNavController().popBackStack() | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
_binding = FragmentDeliveryBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
ViewCompat.setOnApplyWindowInsetsListener(binding.toolbar) { toolbar, windowInsets -> | ||
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars()) | ||
toolbar.updateLayoutParams<ConstraintLayout.LayoutParams> { | ||
topMargin = insets.top | ||
} | ||
val navBarsInsets = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()) | ||
binding.list.setPadding(0, 0, 0, navBarsInsets.bottom) | ||
WindowInsetsCompat.CONSUMED | ||
} | ||
|
||
binding.toolbar.setNavigationOnClickListener { | ||
findNavController().popBackStack() | ||
} | ||
|
||
binding.list.layoutManager = LinearLayoutManager(requireContext()) | ||
binding.list.adapter = adapter | ||
binding.list.setHasFixedSize(true) | ||
|
||
viewLifecycleOwner.lifecycleScope.launch { | ||
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { | ||
model.items.collect { adapter.submitList(it) } | ||
} | ||
} | ||
|
||
model.setArgs(requireArgs()) | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
private fun requireArgs(): DeliveryModel.Args { | ||
return DeliveryModel.Args( | ||
userLat = requireArguments().getFloat("userLat").toDouble(), | ||
userLon = requireArguments().getFloat("userLon").toDouble(), | ||
searchAreaId = requireArguments().getLong("searchAreaId"), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package delivery | ||
|
||
import android.app.Application | ||
import android.location.Location | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import element.Element | ||
import element.ElementsRepo | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import org.btcmap.R | ||
import org.osmdroid.util.GeoPoint | ||
import java.text.NumberFormat | ||
|
||
class DeliveryModel( | ||
private val app: Application, | ||
private val elementsRepo: ElementsRepo, | ||
) : ViewModel() { | ||
|
||
companion object { | ||
private val DISTANCE_FORMAT = NumberFormat.getNumberInstance().apply { | ||
maximumFractionDigits = 1 | ||
} | ||
} | ||
|
||
private val args = MutableStateFlow<Args?>(null) | ||
|
||
private val _items = MutableStateFlow<List<DeliveryAdapter.Item>>(emptyList()) | ||
val items = _items.asStateFlow() | ||
|
||
init { | ||
viewModelScope.launch { | ||
args.collectLatest { args -> | ||
if (args == null) { | ||
return@collectLatest | ||
} | ||
|
||
val unsortedElements = elementsRepo.selectByOsmTagValue( | ||
"delivery", | ||
"yes", | ||
) + elementsRepo.selectByOsmTagValue( | ||
"delivery", | ||
"only", | ||
) | ||
|
||
val sortedElements = unsortedElements.sortedBy { | ||
getDistanceInMeters( | ||
startLatitude = args.userLat, | ||
startLongitude = args.userLon, | ||
endLatitude = it.lat, | ||
endLongitude = it.lon, | ||
) | ||
} | ||
|
||
_items.update { | ||
sortedElements.map { | ||
it.toAdapterItem( | ||
GeoPoint( | ||
args.userLat, | ||
args.userLon, | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun setArgs(args: Args) { | ||
this.args.update { args } | ||
} | ||
|
||
private fun Element.toAdapterItem(userLocation: GeoPoint?): DeliveryAdapter.Item { | ||
val distanceStringBuilder = StringBuilder() | ||
|
||
if (userLocation != null) { | ||
val elementLocation = GeoPoint(lat, lon) | ||
val distanceKm = userLocation.distanceToAsDouble(elementLocation) / 1000 | ||
|
||
distanceStringBuilder.apply { | ||
append(DISTANCE_FORMAT.format(distanceKm)) | ||
append(" ") | ||
append(app.resources.getString(R.string.kilometers_short)) | ||
} | ||
} | ||
|
||
return DeliveryAdapter.Item( | ||
element = this, | ||
icon = tags.optString("icon:android").ifBlank { "question_mark" }, | ||
name = overpassData.getJSONObject("tags").optString("name").ifBlank { "Unnamed" }, | ||
distanceToUser = distanceStringBuilder.toString(), | ||
) | ||
} | ||
|
||
private fun getDistanceInMeters( | ||
startLatitude: Double, | ||
startLongitude: Double, | ||
endLatitude: Double, | ||
endLongitude: Double, | ||
): Double { | ||
val distance = FloatArray(1) | ||
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, distance) | ||
return distance[0].toDouble() | ||
} | ||
|
||
data class Args( | ||
val userLat: Double, | ||
val userLon: Double, | ||
val searchAreaId: Long, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.