Skip to content

Commit

Permalink
Allow changing backup location when flash drive isn't plugged in
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Dec 12, 2024
1 parent c6fa60c commit b6d519b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.backend.BackendManager
import com.stevesoltys.seedvault.permitDiskReads
import com.stevesoltys.seedvault.restore.RestoreActivity
import com.stevesoltys.seedvault.settings.BackupPermission.BackupAllowed
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager
import com.stevesoltys.seedvault.ui.toRelativeTime
import org.calyxos.seedvault.core.backends.BackendProperties
Expand Down Expand Up @@ -83,8 +84,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
trySetBackupEnabled(false)
dialog.dismiss()
}
.setNegativeButton(R.string.settings_backup_apk_dialog_cancel) { dialog,
_ -> dialog.dismiss()
.setNegativeButton(R.string.settings_backup_apk_dialog_cancel) { d, _ ->
d.dismiss()
}
.show()
return@OnPreferenceChangeListener false
Expand Down Expand Up @@ -143,12 +144,16 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

viewModel.backupPossible.observe(viewLifecycleOwner) { possible ->
toolbar.menu.findItem(R.id.action_backup)?.isEnabled = possible
toolbar.menu.findItem(R.id.action_restore)?.isEnabled = possible
backupLocation.isEnabled = possible
backupAppCheck.isEnabled = possible
backupFileCheck.isEnabled = possible
viewModel.backupPossible.observe(viewLifecycleOwner) { permission ->
val allowed = permission == BackupAllowed
toolbar.menu.findItem(R.id.action_backup)?.isEnabled = allowed
toolbar.menu.findItem(R.id.action_restore)?.isEnabled = allowed
// backup location can be changed when backup isn't allowed,
// because flash-drive isn't plugged in
backupLocation.isEnabled = allowed ||
(permission as? BackupPermission.BackupRestricted)?.unavailableUsb == true
backupAppCheck.isEnabled = allowed
backupFileCheck.isEnabled = allowed
}

viewModel.lastBackupTime.observe(viewLifecycleOwner) { time ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.stevesoltys.seedvault.backend.BackendManager
import com.stevesoltys.seedvault.crypto.KeyManager
import com.stevesoltys.seedvault.permitDiskReads
import com.stevesoltys.seedvault.repo.Checker
import com.stevesoltys.seedvault.settings.BackupPermission.BackupAllowed
import com.stevesoltys.seedvault.settings.BackupPermission.BackupRestricted
import com.stevesoltys.seedvault.storage.StorageBackupJobService
import com.stevesoltys.seedvault.ui.LiveEvent
import com.stevesoltys.seedvault.ui.MutableLiveEvent
Expand All @@ -64,6 +66,11 @@ import java.util.concurrent.TimeUnit.HOURS
private const val TAG = "SettingsViewModel"
private const val USER_FULL_DATA_BACKUP_AWARE = "user_full_data_backup_aware"

sealed class BackupPermission {
object BackupAllowed : BackupPermission()
class BackupRestricted(val unavailableUsb: Boolean = false) : BackupPermission()
}

internal class SettingsViewModel(
app: Application,
settingsManager: SettingsManager,
Expand All @@ -86,8 +93,8 @@ internal class SettingsViewModel(

private val isBackupRunning: StateFlow<Boolean>
private val isCheckOrPruneRunning: StateFlow<Boolean>
private val mBackupPossible = MutableLiveData(false)
val backupPossible: LiveData<Boolean> = mBackupPossible
private val mBackupPossible = MutableLiveData<BackupPermission>(BackupRestricted())
val backupPossible: LiveData<BackupPermission> = mBackupPossible

private val mBackupSize = MutableLiveData<Long>()
val backupSize: LiveData<Long> = mBackupSize
Expand Down Expand Up @@ -183,12 +190,24 @@ internal class SettingsViewModel(
onStoragePropertiesChanged()
}

private fun onBackupRunningStateChanged() {
private suspend fun onBackupRunningStateChanged() = withContext(Dispatchers.IO) {
val backupAllowed = !isBackupRunning.value && !isCheckOrPruneRunning.value
if (backupAllowed) viewModelScope.launch(Dispatchers.IO) {
val canDo = !backendManager.isOnUnavailableUsb()
mBackupPossible.postValue(canDo)
} else mBackupPossible.postValue(false)
if (backupAllowed) {
if (backendManager.isOnUnavailableUsb()) {
updateBackupPossible(BackupRestricted(unavailableUsb = true))
} else {
updateBackupPossible(BackupAllowed)
}
} else updateBackupPossible(BackupRestricted())
}

/**
* Updates [mBackupPossible] on the UiThread to avoid race conditions.
*/
private suspend fun updateBackupPossible(newValue: BackupPermission) {
withContext(Dispatchers.Main) {
mBackupPossible.value = newValue
}
}

private fun onStoragePropertiesChanged() {
Expand Down Expand Up @@ -221,7 +240,7 @@ internal class SettingsViewModel(
networkCallback.registered = true
}
// update whether we can do backups right now or not
onBackupRunningStateChanged()
viewModelScope.launch { onBackupRunningStateChanged() }
}

override fun onCleared() {
Expand Down

0 comments on commit b6d519b

Please sign in to comment.