Skip to content

Commit

Permalink
NF: Remove SyncStatus
Browse files Browse the repository at this point in the history
The enum was used only in one function. Adding an intermediary step
does not add clarity to the code.

Each method were also only used once.

I think the resulting code is easier to read, thanks to less layer of abstractions.
  • Loading branch information
Arthur-Milchior committed Dec 11, 2024
1 parent 36ea9ea commit 7f3d83b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 103 deletions.
4 changes: 1 addition & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ import com.ichi2.anki.preferences.PreferencesActivity
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.receiver.SdCardReceiver
import com.ichi2.anki.servicelayer.ScopedStorageService
import com.ichi2.anki.servicelayer.checkMedia
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
Expand Down Expand Up @@ -164,7 +163,6 @@ import com.ichi2.utils.HandlerUtils
import com.ichi2.utils.ImportUtils
import com.ichi2.utils.ImportUtils.ImportResult
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.SyncStatus
import com.ichi2.utils.VersionUtils
import com.ichi2.utils.cancelable
import com.ichi2.utils.checkWebviewVersion
Expand Down Expand Up @@ -204,7 +202,7 @@ import java.io.File
* * Filtering decks (if more than 10) [toolbarSearchView]
* * Controlling syncs
* * A user may [pull down][pullToSyncWrapper] on the 'tree view' to sync
* * A [button][syncHandler.updateSyncIconFromState] which relies on [SyncStatus] to display whether a sync is needed
* * A [button][syncHandler.updateSyncIconFromState] to display whether a sync is needed
* * Blocks the UI and displays sync progress when syncing
* * Displaying 'General' AnkiDroid options: backups, import, 'check media' etc...
* * General handler for error/global dialogs (search for 'as DeckPicker')
Expand Down
49 changes: 34 additions & 15 deletions AnkiDroid/src/main/java/com/ichi2/anki/SyncHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import com.ichi2.libanki.utils.TimeManager
import com.ichi2.ui.BadgeDrawableBuilder
import com.ichi2.utils.NetworkUtils
import com.ichi2.utils.NetworkUtils.isActiveNetworkMetered
import com.ichi2.utils.SyncStatus
import com.ichi2.utils.checkBoxPrompt
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
Expand All @@ -80,6 +79,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.ankiweb.rsdroid.exceptions.BackendInterruptedException
import net.ankiweb.rsdroid.exceptions.BackendNetworkException
import net.ankiweb.rsdroid.exceptions.BackendSyncException
import timber.log.Timber

Expand All @@ -90,8 +90,6 @@ class SyncHandler(
private val activity: AnkiActivity,
private val delegate: SyncHandlerDelegate,
) : SyncErrorDialogListener {


/** Job running the sync media */
private var syncMediaProgressJob: Job? = null

Expand Down Expand Up @@ -192,11 +190,33 @@ class SyncHandler(

private suspend fun fetchSyncStatus(): SyncIconState {
val auth = syncAuth()
return when (SyncStatus.getSyncStatus(activity, auth)) {
SyncStatus.BADGE_DISABLED, SyncStatus.NO_CHANGES, SyncStatus.ERROR -> SyncIconState.Normal
SyncStatus.HAS_CHANGES -> SyncIconState.PendingChanges
SyncStatus.NO_ACCOUNT -> SyncIconState.NotLoggedIn
SyncStatus.ONE_WAY -> SyncIconState.OneWay
val isDisabled = !AnkiDroidApp.sharedPrefs().getBoolean("showSyncStatusBadge", true)
return if (isDisabled) {
SyncIconState.Normal
} else if (auth == null) {
SyncIconState.NotLoggedIn
} else {
try {
// Use CollectionManager to ensure that this doesn't block 'deck count' tasks
// throws if a .colpkg import or similar occurs just before this call
val output = withContext(Dispatchers.IO) { CollectionManager.getBackend().syncStatus(auth) }
if (output.hasNewEndpoint()) {
activity.sharedPrefs().edit {
putString(SyncPreferences.CURRENT_SYNC_URI, output.newEndpoint)
}
}
when (output.required) {
SyncStatusResponse.Required.NO_CHANGES -> SyncIconState.Normal
SyncStatusResponse.Required.NORMAL_SYNC -> SyncIconState.PendingChanges
SyncStatusResponse.Required.FULL_SYNC -> SyncIconState.OneWay
SyncStatusResponse.Required.UNRECOGNIZED, null -> TODO("unexpected required response")
}
} catch (_: BackendNetworkException) {
SyncIconState.PendingChanges
} catch (e: Exception) {
Timber.d(e, "error obtaining sync status: collection likely closed")
SyncIconState.Normal
}
}
}

Expand Down Expand Up @@ -228,7 +248,6 @@ class SyncHandler(
}
}


suspend fun automaticSync(runInBackground: Boolean = false) {
/**
* @return whether there are collection changes to be sync.
Expand All @@ -246,10 +265,10 @@ class SyncHandler(
SyncStatusResponse.Required.NO_CHANGES,
SyncStatusResponse.Required.UNRECOGNIZED,
null,
-> false
-> false
SyncStatusResponse.Required.FULL_SYNC,
SyncStatusResponse.Required.NORMAL_SYNC,
-> true
-> true
}
}

Expand All @@ -263,7 +282,7 @@ class SyncHandler(

val isBlockedByMeteredConnection =
!activity.sharedPrefs().getBoolean(activity.getString(R.string.metered_sync_key), false) &&
isActiveNetworkMetered()
isActiveNetworkMetered()

when {
!isAutoSyncEnabled -> Timber.d("autoSync: not enabled")
Expand Down Expand Up @@ -339,7 +358,7 @@ class SyncHandler(
}

/** In the conflict case, we need to store the USN received from the initial sync, and reuse
it after the user has decided. */
it after the user has decided. */
var mediaUsnOnConflict: Int? = null

/**
Expand Down Expand Up @@ -511,7 +530,7 @@ class SyncHandler(
SyncCollectionResponse.ChangesRequired.NORMAL_SYNC,
SyncCollectionResponse.ChangesRequired.UNRECOGNIZED,
null,
-> {
-> {
TODO("should never happen")
}
}
Expand Down Expand Up @@ -638,7 +657,7 @@ class SyncHandler(
val onlyIfUnmetered = activity.getString(R.string.sync_media_only_unmetered_value)
val shouldFetchMedia = preferences.getString(activity.getString(R.string.sync_fetch_media_key), always)
return shouldFetchMedia == always ||
(shouldFetchMedia == onlyIfUnmetered && !isActiveNetworkMetered())
(shouldFetchMedia == onlyIfUnmetered && !isActiveNetworkMetered())
}

/**
Expand Down
85 changes: 0 additions & 85 deletions AnkiDroid/src/main/java/com/ichi2/utils/SyncStatus.kt

This file was deleted.

0 comments on commit 7f3d83b

Please sign in to comment.