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 Apr 11, 2024
1 parent 5a4955c commit 10ce9d5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
43 changes: 39 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 @@ -113,6 +114,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 @@ -1203,7 +1205,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 @@ -1222,6 +1244,14 @@ 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)
}
setLastSyncTimeToNow()
}
else -> {
Timber.i("autoSync: start")
sync()
Expand All @@ -1244,8 +1274,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 @@ -1325,7 +1358,9 @@ open class DeckPicker :
dialogHandler.sendMessage(OneWaySyncDialog(message).toMessage())
}
}
automaticSync()
launchCatchingTask {
automaticSync()
}
}

private fun showCollectionErrorDialog() {
Expand Down
10 changes: 7 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ fun DeckPicker.handleNewSync(
throw exc
}
withCol { notetypes._clear_cache() }
sharedPrefs().edit {
putLong("lastSyncTime", TimeManager.time.intTimeMS())
}
setLastSyncTimeToNow()
refreshState()
}
}
Expand Down Expand Up @@ -388,6 +386,12 @@ fun DeckPicker.showSyncLogMessage(@StringRes messageResource: Int, syncMessage:
}
}

fun Context.setLastSyncTimeToNow() {
sharedPrefs().edit {
putLong("lastSyncTime", TimeManager.time.intTimeMS())
}
}

fun joinSyncMessages(dialogMessage: String?, syncMessage: String?): String? {
// If both strings have text, separate them by a new line, otherwise return whichever has text
return if (!dialogMessage.isNullOrEmpty() && !syncMessage.isNullOrEmpty()) {
Expand Down

0 comments on commit 10ce9d5

Please sign in to comment.