Skip to content

Commit

Permalink
Use backend to update sync status; trigger media sync on auto full sync
Browse files Browse the repository at this point in the history
+ Update sync status after normal media sync completes

Closes ankidroid#11902
  • Loading branch information
dae committed Aug 6, 2022
1 parent 48bff41 commit 33b12d0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
14 changes: 9 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,20 @@ open class DeckPicker :

@VisibleForTesting
protected open suspend fun displaySyncBadge(menu: Menu) {
val syncStatus = withOpenColOrNull { SyncStatus.getSyncStatus(this) }
val auth = syncAuth()
val syncStatus = withOpenColOrNull {
SyncStatus.getSyncStatus(this, auth)
}
if (syncStatus == null) {
return
}
val syncMenu = menu.findItem(R.id.action_sync)
when (syncStatus) {
SyncStatus.BADGE_DISABLED, SyncStatus.NO_CHANGES, SyncStatus.INCONCLUSIVE -> {
BadgeDrawableBuilder.removeBadge(syncMenu)
syncMenu.setTitle(R.string.button_sync)
syncMenu?.let {
BadgeDrawableBuilder.removeBadge(it)
it.setTitle(R.string.button_sync)
}
}
SyncStatus.HAS_CHANGES -> {
// Light orange icon
Expand Down Expand Up @@ -1519,14 +1524,13 @@ open class DeckPicker :
override fun sync(conflict: ConflictResolution?) {
val preferences = AnkiDroidApp.getSharedPrefs(baseContext)
val hkey = preferences.getString("hkey", "")
val hostNum = HostNumFactory.getInstance(baseContext).getHostNum()
if (hkey!!.isEmpty()) {
Timber.w("User not logged in")
mPullToSyncWrapper.isRefreshing = false
showSyncErrorDialog(SyncErrorDialog.DIALOG_USER_NOT_LOGGED_IN_SYNC)
} else {
if (!BackendFactory.defaultLegacySchema) {
handleNewSync(hkey, hostNum ?: 0, conflict)
handleNewSync(conflict)
} else {
Connection.sync(
mSyncListener,
Expand Down
26 changes: 18 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.exceptions.BackendSyncException
import timber.log.Timber

fun DeckPicker.syncAuth(): SyncAuth? {
val preferences = AnkiDroidApp.getSharedPrefs(this)
val hkey = preferences.getString("hkey", null)
val hostNum = HostNumFactory.getInstance(baseContext).getHostNum()
return hkey?.let {
syncAuth {
this.hkey = hkey
this.hostNumber = hostNum ?: 0
}
}
}

fun DeckPicker.handleNewSync(
hkey: String,
hostNum: Int,
conflict: Connection.ConflictResolution?
) {
val auth = syncAuth {
this.hkey = hkey
this.hostNumber = hostNum
}
val auth = this.syncAuth() ?: return
val deckPicker = this
launchCatchingTask {
try {
Expand Down Expand Up @@ -121,19 +128,18 @@ private suspend fun handleNormalSync(
SyncCollectionResponse.ChangesRequired.NO_CHANGES -> {
// a successful sync returns this value
deckPicker.showSyncLogMessage(R.string.sync_database_acknowledge, output.serverMessage)
deckPicker.refreshState()
// kick off media sync - future implementations may want to run this in the
// background instead
handleMediaSync(deckPicker, auth)
}

SyncCollectionResponse.ChangesRequired.FULL_DOWNLOAD -> {
handleDownload(deckPicker, auth)
handleMediaSync(deckPicker, auth)
}

SyncCollectionResponse.ChangesRequired.FULL_UPLOAD -> {
handleUpload(deckPicker, auth)
handleMediaSync(deckPicker, auth)
}

SyncCollectionResponse.ChangesRequired.FULL_SYNC -> {
Expand Down Expand Up @@ -178,6 +184,8 @@ private suspend fun handleDownload(
reopen(afterFullSync = true)
}
}
deckPicker.refreshState()
handleMediaSync(deckPicker, auth)
}

Timber.i("Full Download Completed")
Expand All @@ -200,6 +208,8 @@ private suspend fun handleUpload(
reopen(afterFullSync = true)
}
}
deckPicker.refreshState()
handleMediaSync(deckPicker, auth)
}
Timber.i("Full Upload Completed")
deckPicker.showSyncLogMessage(R.string.sync_log_uploading_message, "")
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sync/BackendSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ichi2.libanki.sync

import anki.sync.SyncAuth
import anki.sync.SyncCollectionResponse
import anki.sync.SyncStatusResponse
import com.ichi2.libanki.CollectionV16

fun CollectionV16.syncLogin(username: String, password: String): SyncAuth {
Expand All @@ -39,3 +40,7 @@ fun CollectionV16.fullDownload(auth: SyncAuth) {
fun CollectionV16.syncMedia(auth: SyncAuth) {
return backend.syncMedia(input = auth)
}

fun CollectionV16.syncStatus(auth: SyncAuth): SyncStatusResponse {
return backend.syncStatus(input = auth)
}
34 changes: 17 additions & 17 deletions AnkiDroid/src/main/java/com/ichi2/utils/SyncStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package com.ichi2.utils

import anki.sync.SyncAuth
import anki.sync.SyncStatusResponse
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.libanki.Collection
import timber.log.Timber
import java.util.function.Supplier
import net.ankiweb.rsdroid.BackendFactory

enum class SyncStatus {
INCONCLUSIVE, NO_ACCOUNT, NO_CHANGES, HAS_CHANGES, FULL_SYNC, BADGE_DISABLED;
Expand All @@ -29,26 +30,16 @@ enum class SyncStatus {
private var sMarkedInMemory = false

@JvmStatic
fun getSyncStatus(getCol: Supplier<Collection>): SyncStatus {
return try {
val col = getCol.get()
// may fail when the collection is closed for a full sync,
// as col.db is null
getSyncStatus(col)
} catch (e: Exception) {
Timber.w(e)
return INCONCLUSIVE
}
}

@JvmStatic
fun getSyncStatus(col: Collection): SyncStatus {
fun getSyncStatus(col: Collection, auth: SyncAuth?): SyncStatus {
if (isDisabled) {
return BADGE_DISABLED
}
if (!isLoggedIn) {
if (auth == null) {
return NO_ACCOUNT
}
if (!BackendFactory.defaultLegacySchema) {
return syncStatusFromRequired(col.newBackend.backend.syncStatus(auth).required)
}
if (col.schemaChanged()) {
return FULL_SYNC
}
Expand All @@ -59,6 +50,15 @@ enum class SyncStatus {
}
}

private fun syncStatusFromRequired(required: SyncStatusResponse.Required?): SyncStatus {
return when (required) {
SyncStatusResponse.Required.NO_CHANGES -> NO_CHANGES
SyncStatusResponse.Required.NORMAL_SYNC -> HAS_CHANGES
SyncStatusResponse.Required.FULL_SYNC -> FULL_SYNC
SyncStatusResponse.Required.UNRECOGNIZED, null -> TODO("unexpected required response")
}
}

private val isDisabled: Boolean
get() {
val preferences = AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance())
Expand Down

0 comments on commit 33b12d0

Please sign in to comment.