Skip to content

Commit

Permalink
Minor improvements to logs and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Jan 22, 2025
1 parent fc868a8 commit 801dd62
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions appnav/src/main/kotlin/io/element/android/appnav/SyncObserver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ class SyncObserver @Inject constructor(

private var coroutineScope: CoroutineScope? = null

private val tag = "SyncObserver"

/**
* Observe the app state and network state to start/stop the sync service.
*
* Before observing the state, a first attempt at starting the sync service will happen if it's not already running.
*/
@OptIn(FlowPreview::class)
fun observe() {
Timber.tag(tag).d("start observing the app and network state")

if (syncService.syncState.value != SyncState.Running) {
Timber.d("SyncObserver: initial startSync")
Timber.tag(tag).d("initial startSync")
sessionCoroutineScope.launch(dispatchers.io) {
try {
initialSyncMutex.lock()
Expand All @@ -71,7 +75,7 @@ class SyncObserver @Inject constructor(
}
}

coroutineScope = CoroutineScope(sessionCoroutineScope.coroutineContext + CoroutineName("SyncObserver") + dispatchers.io)
coroutineScope = CoroutineScope(sessionCoroutineScope.coroutineContext + CoroutineName(tag) + dispatchers.io)

coroutineScope?.launch {
// Wait until the initial sync is done, either successfully or failing
Expand All @@ -88,8 +92,9 @@ class SyncObserver @Inject constructor(
val isAppActive = isInForeground || isInCall || isSyncingNotificationEvent
val isNetworkAvailable = networkState == NetworkStatus.Online

Timber.d("SyncObserver: isAppActive=$isAppActive, isNetworkAvailable=$isNetworkAvailable")
Timber.tag(tag).d("isAppActive=$isAppActive, isNetworkAvailable=$isNetworkAvailable")
if (syncState == SyncState.Running && (!isAppActive || !isNetworkAvailable)) {
// Don't stop the sync immediately, wait a bit to avoid starting/stopping the sync too often
delay(3.seconds)
SyncObserverAction.StopSync
} else if (syncState != SyncState.Running && isAppActive && isNetworkAvailable) {
Expand Down Expand Up @@ -117,6 +122,7 @@ class SyncObserver @Inject constructor(
* Stop observing the app state and network state.
*/
fun stop() {
Timber.tag(tag).d("stop observing the app and network state")
coroutineScope?.cancel()
coroutineScope = null
}
Expand Down

0 comments on commit 801dd62

Please sign in to comment.