Skip to content

Commit

Permalink
Add sync indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Mar 4, 2023
1 parent 186ef63 commit 669a3e0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/src/main/kotlin/app/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app

import android.util.Log
import api.Api
import api.ApiImpl
import area.AreaQueries
Expand Down Expand Up @@ -40,10 +39,10 @@ val appModule = module {
single {
OkHttpClient.Builder()
.addInterceptor(BrotliInterceptor)
.addInterceptor {
Log.d("okhttp", it.request().url.toString())
it.proceed(it.request())
}
// .addInterceptor {
// Log.d("okhttp", it.request().url.toString())
// it.proceed(it.request())
// }
.build()
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/kotlin/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import area.AreaResultModel
import area.polygons
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.search.SearchView
import com.google.android.material.snackbar.Snackbar
import element.ElementFragment
import element.ElementsCluster
import filter.FilterResultModel
Expand Down Expand Up @@ -417,6 +418,18 @@ class MapFragment : Fragment() {
val text = it.toString()
searchModel.setSearchString(text)
}

viewLifecycleOwner.lifecycleScope.launch {
val snackBar = Snackbar.make(view, R.string.syncing_data, Snackbar.LENGTH_INDEFINITE)

model.syncActive.collect {
if (it) {
snackBar.show()
} else {
snackBar.dismiss()
}
}
}
}

override fun onResume() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/map/MapModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class MapModel(
private val _visibleElements = MutableStateFlow<List<ElementsCluster>>(emptyList())
val visibleElements = _visibleElements.asStateFlow()

val syncActive = sync.active

init {
combine(
mapViewport,
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import event.EventsRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import user.UsersRepo
import java.time.ZoneOffset
Expand All @@ -23,7 +26,11 @@ class Sync(
private val eventsRepo: EventsRepo,
) {

private val _active = MutableStateFlow(false)
val active = _active.asStateFlow()

suspend fun sync() {
_active.update { true }
Log.d(TAG, "Sync was requested")

val lastSyncDateTime = confRepo.conf.value.lastSyncDate
Expand All @@ -33,6 +40,7 @@ class Sync(

if (lastSyncDateTime != null && lastSyncDateTime.isAfter(minSyncIntervalExpiryDate)) {
Log.d(TAG, "Cache is up to date, skipping sync")
_active.update { false }
return
}

Expand Down Expand Up @@ -107,6 +115,8 @@ class Sync(
Log.d(TAG, "Finished sync in ${System.currentTimeMillis() - startTime} ms")

confRepo.update { it.copy(lastSyncDate = ZonedDateTime.now(ZoneOffset.UTC)) }

_active.update { false }
}

companion object {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<string name="atm">ATM</string>
<string name="checkmark_icon">Checkmark icon</string>
<string name="filter">Filter</string>
<string name="syncing_data">Syncing data</string>
<plurals name="d_changes">
<item quantity="zero">%d changes</item>
<item quantity="one">%d change</item>
Expand Down

0 comments on commit 669a3e0

Please sign in to comment.