@@ -8,6 +8,7 @@ import android.content.res.Configuration
88import android.graphics.Color
99import android.location.Location
1010import android.os.Bundle
11+ import android.util.Log
1112import android.view.LayoutInflater
1213import android.view.View
1314import android.view.ViewGroup
@@ -42,6 +43,7 @@ import org.btcmap.db.table.event.EventQueries
4243import org.btcmap.db.table.place.Place
4344import org.btcmap.db.table.place.PlaceQueries
4445import org.btcmap.place.PlaceFragment
46+ import org.btcmap.api.AreasApi
4547import org.btcmap.http.httpClient
4648import kotlinx.coroutines.delay
4749import kotlinx.coroutines.flow.launchIn
@@ -226,6 +228,10 @@ class MapFragment : Fragment() {
226228 binding.map.getMapAsync {
227229 it.addOnCameraIdleListener {
228230 prefs.mapViewport = it.projection.visibleRegion.latLngBounds
231+ val center = it.projection.visibleRegion.latLngBounds.center
232+ viewLifecycleOwner.lifecycleScope.launch {
233+ loadAreas(center.latitude, center.longitude)
234+ }
229235 }
230236
231237 it.setStyle(Style .Builder ().fromUri(prefs.mapStyle.uri(requireContext())))
@@ -397,6 +403,19 @@ class MapFragment : Fragment() {
397403 }
398404 }.launchIn(viewLifecycleOwner.lifecycleScope)
399405
406+ areasAdapter = AreasAdapter { area ->
407+ val intent = Intent (Intent .ACTION_VIEW , area.websiteUrl.toUri())
408+ startActivity(intent)
409+ }
410+
411+ binding.areas.layoutManager =
412+ LinearLayoutManager (
413+ requireContext(),
414+ LinearLayoutManager .VERTICAL ,
415+ true ,
416+ )
417+ binding.areas.adapter = areasAdapter
418+
400419 binding.searchView.editText.doAfterTextChanged { searchString ->
401420 binding.map.getMapAsync { map ->
402421 viewLifecycleOwner.lifecycleScope.launch {
@@ -493,7 +512,7 @@ class MapFragment : Fragment() {
493512 return @addOnMapClickListener true
494513 }
495514 } catch (e: Exception ) {
496- // Ignore
515+ Log .e( null , null , e)
497516 }
498517
499518 false
@@ -505,9 +524,6 @@ class MapFragment : Fragment() {
505524 override fun onStateChanged (bottomSheet : View , newState : Int ) {
506525 if (newState == BottomSheetBehavior .STATE_HIDDEN ) {
507526 viewLifecycleOwner.lifecycleScope.launch { selectPlace(null ) }
508- binding.fab.isVisible = true
509- } else {
510- binding.fab.isVisible = false
511527 }
512528 }
513529
@@ -1173,6 +1189,8 @@ class MapFragment : Fragment() {
11731189 private val _searchResults = MutableStateFlow <List <SearchAdapterItem >>(emptyList())
11741190 val searchResults = _searchResults .asStateFlow()
11751191
1192+ private lateinit var areasAdapter: AreasAdapter
1193+
11761194 suspend fun search (referenceLocation : LatLng , searchString : String ) {
11771195 if (searchString.length < MIN_QUERY_LENGTH ) {
11781196 _searchResults .update { emptyList() }
@@ -1222,8 +1240,19 @@ class MapFragment : Fragment() {
12221240 return distance[0 ].toDouble()
12231241 }
12241242
1243+ private suspend fun loadAreas (lat : Double , lon : Double ) {
1244+ try {
1245+ val areas = withContext(Dispatchers .IO ) {
1246+ AreasApi .getAreas(lat, lon)
1247+ }
1248+ areasAdapter.submitList(areas)
1249+ } catch (_: Throwable ) {
1250+ areasAdapter.submitList(emptyList())
1251+ }
1252+ }
1253+
12251254 private fun initInsets (binding : MapFragmentBinding ) {
1226- ViewCompat .setOnApplyWindowInsetsListener(binding.fab ) { v, windowInsets ->
1255+ ViewCompat .setOnApplyWindowInsetsListener(binding.fabContainer ) { v, windowInsets ->
12271256 val insets = windowInsets.getInsets(WindowInsetsCompat .Type .systemBars())
12281257
12291258 v.updateLayoutParams<ViewGroup .MarginLayoutParams > {
0 commit comments