Skip to content

Commit 83d66ca

Browse files
committed
Refactor sync
1 parent 4373a6e commit 83d66ca

2 files changed

Lines changed: 37 additions & 23 deletions

File tree

app/src/main/kotlin/map/MapFragment.kt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ import androidx.lifecycle.Lifecycle
4040
import androidx.lifecycle.lifecycleScope
4141
import androidx.lifecycle.repeatOnLifecycle
4242
import androidx.recyclerview.widget.LinearLayoutManager
43+
import androidx.sqlite.SQLiteConnection
44+
import api.Api
4345
import app.dpToPx
4446
import area.AreaResultModel
4547
import area.bounds
4648
import com.google.android.material.bottomsheet.BottomSheetBehavior
4749
import element.ElementFragment
4850
import element.ElementsCluster
4951
import element.ElementsRepo
52+
import kotlinx.coroutines.Dispatchers
5053
import kotlinx.coroutines.delay
5154
import kotlinx.coroutines.flow.collectLatest
5255
import kotlinx.coroutines.flow.launchIn
5356
import kotlinx.coroutines.flow.onEach
5457
import kotlinx.coroutines.flow.update
5558
import kotlinx.coroutines.launch
59+
import kotlinx.coroutines.withContext
5660
import okhttp3.HttpUrl.Companion.toHttpUrl
5761
import org.btcmap.R
5862
import 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) {

app/src/main/kotlin/map/MapModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MapModel(
3737
fun loadItems(bounds: LatLngBounds, zoom: Double, filter: Filter) {
3838
prevLoadItems?.cancel()
3939

40-
viewModelScope.launch {
40+
prevLoadItems = viewModelScope.launch {
4141
withContext(Dispatchers.IO) {
4242
when (filter) {
4343
Filter.Merchants -> {

0 commit comments

Comments
 (0)