Skip to content

Commit

Permalink
feat: only auto sync if there are changes
Browse files Browse the repository at this point in the history
this avoids blocking the UI with the sync dialog if syncing isn't necessary
  • Loading branch information
BrayanDSO committed Mar 28, 2024
1 parent 0203850 commit 5a8a9c4
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import anki.collection.OpChanges
import anki.sync.SyncStatusResponse
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.google.android.material.snackbar.BaseTransientBottomBar
Expand Down Expand Up @@ -115,6 +116,7 @@ import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.ui.dialogs.storageMigrationFailedDialogIsShownOrPending
import com.ichi2.anki.utils.SECONDS_PER_DAY
import com.ichi2.anki.widgets.DeckAdapter
import com.ichi2.anki.worker.SyncMediaWorker
import com.ichi2.annotations.NeedsTest
import com.ichi2.async.*
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
Expand Down Expand Up @@ -1200,7 +1202,27 @@ open class DeckPicker :
}
}

private fun automaticSync() {
private suspend fun automaticSync() {
/**
* @return whether there are collection changes to be sync.
*
* It DOES NOT include if there are media to be synced.
*/
suspend fun areThereChangesToSync(): Boolean {
val auth = syncAuth() ?: return false
val status = withContext(Dispatchers.IO) {
CollectionManager.getBackend().syncStatus(auth)
}.required

return when (status) {
SyncStatusResponse.Required.NO_CHANGES,
SyncStatusResponse.Required.UNRECOGNIZED,
null -> false
SyncStatusResponse.Required.FULL_SYNC,
SyncStatusResponse.Required.NORMAL_SYNC -> true
}
}

fun syncIntervalPassed(): Boolean {
val lastSyncTime = sharedPrefs().getLong("lastSyncTime", 0)
val automaticSyncIntervalInMS = AUTOMATIC_SYNC_MINIMAL_INTERVAL_IN_MINUTES * 60 * 1000
Expand All @@ -1219,6 +1241,13 @@ open class DeckPicker :
!syncIntervalPassed() -> Timber.d("autoSync: sync interval not passed")
!isLoggedIn() -> Timber.d("autoSync: not logged in")
mediaMigrationIsInProgress(this) -> Timber.d("autoSync: migrating storage")
!areThereChangesToSync() -> {
Timber.d("autoSync: no collection changes to sync. Syncing media if set")
if (shouldFetchMedia(sharedPrefs())) {
val auth = syncAuth() ?: return
SyncMediaWorker.start(this, auth)
}
}
else -> {
Timber.i("autoSync: start")
sync()
Expand All @@ -1241,8 +1270,11 @@ open class DeckPicker :
false
) || backButtonPressedToExit
) {
automaticSync()
finish()
launchCatchingTask {
automaticSync()
}.invokeOnCompletion {
finish()
}
} else {
showSnackbar(R.string.back_pressed_once, Snackbar.LENGTH_SHORT)
}
Expand Down Expand Up @@ -1322,7 +1354,9 @@ open class DeckPicker :
dialogHandler.sendMessage(OneWaySyncDialog(message).toMessage())
}
}
automaticSync()
launchCatchingTask {
automaticSync()
}
}

private fun showCollectionErrorDialog() {
Expand Down

0 comments on commit 5a8a9c4

Please sign in to comment.