Move code to filter settings file contents to SettingsImporter
This commit is contained in:
parent
61af35b9a0
commit
c0917b251d
3 changed files with 23 additions and 45 deletions
|
@ -14,34 +14,9 @@ import timber.log.Timber
|
|||
*/
|
||||
internal class SettingsFileParser {
|
||||
@Throws(SettingsImportExportException::class)
|
||||
fun parseSettings(
|
||||
inputStream: InputStream,
|
||||
globalSettings: Boolean,
|
||||
accountUuids: List<String>?,
|
||||
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) {
|
||||
|
|
|
@ -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<AccountDescriptionPair>()
|
||||
val erroneousAccounts = mutableListOf<AccountDescription>()
|
||||
|
||||
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
|
||||
|
|
|
@ -30,9 +30,8 @@ class SettingsFileParserTest : RobolectricTest() {
|
|||
</accounts>
|
||||
</k9settings>
|
||||
""".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() {
|
|||
</accounts>
|
||||
</k9settings>
|
||||
""".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() {
|
|||
</accounts>
|
||||
</k9settings>
|
||||
""".trimIndent().byteInputStream()
|
||||
val accountUuids = listOf(accountUuid)
|
||||
|
||||
val results = parser.parseSettings(inputStream, true, accountUuids, false)
|
||||
val results = parser.parseSettings(inputStream)
|
||||
|
||||
assertThat(results.accounts)
|
||||
.isNotNull()
|
||||
|
|
Loading…
Reference in a new issue