- Moves migrateSettings to be a public function in LocalPreferences.

- Removes redundant settings migration call during individual account migration.
- Modifies the main migration logic in `Amber.kt` to:
    - Iterate through legacy accounts and attempt to migrate each.
    - Introduce a `FailedMigrationException` to halt the process if any account migration fails, preventing the deletion of old settings data in such cases.
    - Ensure a current account is set if one exists after migration.
    - Propagate `FailedMigrationException` in coroutine exception handlers.
This commit is contained in:
greenart7c3
2025-09-12 10:06:10 -03:00
parent c051244c26
commit a1462191ea
2 changed files with 24 additions and 9 deletions
@@ -67,6 +67,7 @@ class Amber : Application(), LifecycleObserver {
val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
Log.e("AmberCoroutine", "Caught exception: ${throwable.message}", throwable)
if (throwable is FailedMigrationException) throw throwable
}
val applicationIOScope = CoroutineScope(Dispatchers.IO + SupervisorJob() + exceptionHandler)
@@ -215,16 +216,31 @@ class Amber : Application(), LifecycleObserver {
LocalPreferences.allSavedAccounts(this@Amber).forEach {
if (LocalPreferences.didMigrateFromLegacyStorage(this@Amber, it.npub)) {
LocalPreferences.deleteLegacyUserPreferenceFile(this@Amber, it.npub)
if (LocalPreferences.existsLegacySettings(this@Amber)) LocalPreferences.deleteSettingsPreferenceFile(this@Amber)
}
}
if (LocalPreferences.existsLegacySettings(this@Amber)) {
LocalPreferences.allLegacySavedAccounts(this@Amber).forEach {
LocalPreferences.migrateFromSharedPrefs(this@Amber, it.npub)
LocalPreferences.loadFromEncryptedStorage(this@Amber, it.npub)
var error = false
for (accountInfo in LocalPreferences.allLegacySavedAccounts(this@Amber)) {
try {
LocalPreferences.migrateFromSharedPrefs(this@Amber, accountInfo.npub)
LocalPreferences.loadFromEncryptedStorage(this@Amber, accountInfo.npub)
} catch (e: Exception) {
error = true
Log.e(TAG, "Failed to migrate settings for account ${accountInfo.npub}", e)
}
}
if (!error) {
LocalPreferences.migrateSettings(this@Amber)
LocalPreferences.deleteSettingsPreferenceFile(this@Amber)
} else {
throw FailedMigrationException("Failed to migrate settings for all accounts")
}
}
LocalPreferences.migrateTorSettings(this@Amber)
val currentAccount = LocalPreferences.currentAccount(this@Amber)
if (currentAccount.isNullOrBlank() && LocalPreferences.allSavedAccounts(this@Amber).isNotEmpty()) {
LocalPreferences.switchToAccount(this@Amber, LocalPreferences.allSavedAccounts(this@Amber).first().npub)
}
settings = LocalPreferences.loadSettingsFromEncryptedStorage()
LocalPreferences.reloadApp()
fixRejectedPermissions()
@@ -264,6 +280,7 @@ class Amber : Application(), LifecycleObserver {
Log.e(TAG, "Failed to run migrations", e)
isStartingApp.value = false
if (e is CancellationException) throw e
if (e is FailedMigrationException) throw e
}
}
}
@@ -466,3 +483,5 @@ class Amber : Application(), LifecycleObserver {
}
}
}
class FailedMigrationException(msg: String) : Exception(msg)
@@ -546,10 +546,6 @@ object LocalPreferences {
}
migrateUserSharedPrefs(context, npub)
deleteLegacyUserPreferenceFile(context, npub)
if (existsLegacySettings(context)) {
migrateSettings(context)
deleteSettingsPreferenceFile(context)
}
}
}
@@ -578,7 +574,7 @@ object LocalPreferences {
}
@Suppress("DEPRECATION")
private fun migrateSettings(context: Context) {
fun migrateSettings(context: Context) {
val settingsPrefs = sharedPrefs(context)
val legacySettingsPrefs = encryptedPreferences(context)
for ((key, value) in legacySettingsPrefs.all) {