@@ -40,19 +40,23 @@ import androidx.lifecycle.Lifecycle
4040import androidx.lifecycle.lifecycleScope
4141import androidx.lifecycle.repeatOnLifecycle
4242import androidx.recyclerview.widget.LinearLayoutManager
43+ import androidx.sqlite.SQLiteConnection
44+ import api.Api
4345import app.dpToPx
4446import area.AreaResultModel
4547import area.bounds
4648import com.google.android.material.bottomsheet.BottomSheetBehavior
4749import element.ElementFragment
4850import element.ElementsCluster
4951import element.ElementsRepo
52+ import kotlinx.coroutines.Dispatchers
5053import kotlinx.coroutines.delay
5154import kotlinx.coroutines.flow.collectLatest
5255import kotlinx.coroutines.flow.launchIn
5356import kotlinx.coroutines.flow.onEach
5457import kotlinx.coroutines.flow.update
5558import kotlinx.coroutines.launch
59+ import kotlinx.coroutines.withContext
5660import okhttp3.HttpUrl.Companion.toHttpUrl
5761import org.btcmap.R
5862import org.btcmap.databinding.FragmentMapBinding
@@ -521,15 +525,17 @@ class MapFragment : Fragment() {
521525
522526 if (elementsRepo.selectCount() == 0L && elementsRepo.hasBundledElements()) {
523527 elementsRepo.fetchBundledElements()
524-
525- binding.map.getMapAsync { map ->
526- model.loadItems(
527- bounds = map.projection.visibleRegion.latLngBounds,
528- zoom = map.cameraPosition.zoom,
529- filter = MapModel .Filter .Merchants ,
530- )
531- }
528+ refreshData()
532529 }
530+
531+ elementsRepo.sync()
532+ refreshData()
533+
534+ val api by inject<Api >()
535+ val conn by inject<SQLiteConnection >()
536+ val events = api.getEvents()
537+ withContext(Dispatchers .IO ) { event.deleteAllAndInsertBatch(events, conn) }
538+ refreshData()
533539 }
534540 }
535541
@@ -667,20 +673,7 @@ class MapFragment : Fragment() {
667673 prefs.mapViewport =
668674 map.projection.visibleRegion.latLngBounds
669675
670- val filter =
671- if (binding.filterMerchantsActive.isVisible) {
672- MapModel .Filter .Merchants
673- } else if (binding.filterEventsActive.isVisible) {
674- MapModel .Filter .Events
675- } else {
676- MapModel .Filter .Exchanges
677- }
678-
679- model.loadItems(
680- bounds = map.projection.visibleRegion.latLngBounds,
681- zoom = map.cameraPosition.zoom,
682- filter = filter,
683- )
676+ refreshData()
684677
685678 searchModel.setLocation(
686679 LatLng (
@@ -690,6 +683,27 @@ class MapFragment : Fragment() {
690683 }
691684 }
692685
686+ private fun refreshData () {
687+ binding.map.getMapAsync { map ->
688+ viewLifecycleOwner.lifecycleScope.launch {
689+ val filter =
690+ if (binding.filterMerchantsActive.isVisible) {
691+ MapModel .Filter .Merchants
692+ } else if (binding.filterEventsActive.isVisible) {
693+ MapModel .Filter .Events
694+ } else {
695+ MapModel .Filter .Exchanges
696+ }
697+
698+ model.loadItems(
699+ bounds = map.projection.visibleRegion.latLngBounds,
700+ zoom = map.cameraPosition.zoom,
701+ filter = filter,
702+ )
703+ }
704+ }
705+ }
706+
693707 private fun BottomSheetBehavior <* >.addSlideCallback () {
694708 addBottomSheetCallback(object : BottomSheetBehavior .BottomSheetCallback () {
695709 override fun onStateChanged (bottomSheet : View , newState : Int ) {
0 commit comments