From c0917b251dc5036a4c266b4977026e8871e52d9b Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 May 2024 18:44:01 +0200 Subject: [PATCH] Move code to filter settings file contents to `SettingsImporter` --- .../fsck/k9/preferences/SettingsFileParser.kt | 29 ++---------------- .../fsck/k9/preferences/SettingsImporter.kt | 30 +++++++++++-------- .../k9/preferences/SettingsFileParserTest.kt | 9 ++---- 3 files changed, 23 insertions(+), 45 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileParser.kt b/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileParser.kt index c0bc29fc1..46bcb2f47 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileParser.kt +++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileParser.kt @@ -14,34 +14,9 @@ import timber.log.Timber */ internal class SettingsFileParser { @Throws(SettingsImportExportException::class) - fun parseSettings( - inputStream: InputStream, - globalSettings: Boolean, - accountUuids: List?, - overview: Boolean, - ): Imported { + fun parseSettings(inputStream: InputStream): Imported { try { - val settings = XmlSettingsParser(inputStream).parse() - - // TODO: Move this filtering code out of SettingsFileParser - val filteredGlobalSettings = if (overview) { - settings.globalSettings?.let { ImportedSettings() } - } else if (globalSettings) { - settings.globalSettings - } else { - null - } - - val filteredAccounts = if (overview || accountUuids == null) { - settings.accounts - } else { - settings.accounts?.filterKeys { it in accountUuids } - } - - return settings.copy( - globalSettings = filteredGlobalSettings, - accounts = filteredAccounts, - ) + return XmlSettingsParser(inputStream).parse() } catch (e: XmlPullParserException) { throw SettingsImportExportException("Error parsing settings XML", e) } catch (e: SettingsParserException) { diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt b/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt index 71a3cf1b9..092ca90cf 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt +++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.kt @@ -51,13 +51,7 @@ class SettingsImporter internal constructor( @Throws(SettingsImportExportException::class) fun getImportStreamContents(inputStream: InputStream): ImportContents { try { - // Parse the import stream but don't save individual settings (overview=true) - val imported = settingsFileParser.parseSettings( - inputStream = inputStream, - globalSettings = false, - accountUuids = null, - overview = true, - ) + val imported = settingsFileParser.parseSettings(inputStream) // If the stream contains global settings the "globalSettings" member will not be null val globalSettings = (imported.globalSettings != null) @@ -101,11 +95,23 @@ class SettingsImporter internal constructor( val importedAccounts = mutableListOf() val erroneousAccounts = mutableListOf() - val imported = settingsFileParser.parseSettings( - inputStream = inputStream, - globalSettings = globalSettings, - accountUuids = accountUuids, - overview = false, + val settings = settingsFileParser.parseSettings(inputStream) + + val filteredGlobalSettings = if (globalSettings) { + settings.globalSettings + } else { + null + } + + val filteredAccounts = if (accountUuids == null) { + settings.accounts + } else { + settings.accounts?.filterKeys { it in accountUuids } + } + + val imported = settings.copy( + globalSettings = filteredGlobalSettings, + accounts = filteredAccounts, ) val storage = preferences.storage diff --git a/app/core/src/test/java/com/fsck/k9/preferences/SettingsFileParserTest.kt b/app/core/src/test/java/com/fsck/k9/preferences/SettingsFileParserTest.kt index be9466ec7..98e67a8fe 100644 --- a/app/core/src/test/java/com/fsck/k9/preferences/SettingsFileParserTest.kt +++ b/app/core/src/test/java/com/fsck/k9/preferences/SettingsFileParserTest.kt @@ -30,9 +30,8 @@ class SettingsFileParserTest : RobolectricTest() { """.trimIndent().byteInputStream() - val accountUuids = listOf("1") - val results = parser.parseSettings(inputStream, true, accountUuids, true) + val results = parser.parseSettings(inputStream) assertThat(results.accounts).isNotNull().all { hasSize(1) @@ -61,9 +60,8 @@ class SettingsFileParserTest : RobolectricTest() { """.trimIndent().byteInputStream() - val accountUuids = listOf("1") - val results = parser.parseSettings(inputStream, true, accountUuids, true) + val results = parser.parseSettings(inputStream) assertThat(results.accounts).isNotNull().all { hasSize(1) @@ -91,9 +89,8 @@ class SettingsFileParserTest : RobolectricTest() { """.trimIndent().byteInputStream() - val accountUuids = listOf(accountUuid) - val results = parser.parseSettings(inputStream, true, accountUuids, false) + val results = parser.parseSettings(inputStream) assertThat(results.accounts) .isNotNull()