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 {
|
internal class SettingsFileParser {
|
||||||
@Throws(SettingsImportExportException::class)
|
@Throws(SettingsImportExportException::class)
|
||||||
fun parseSettings(
|
fun parseSettings(inputStream: InputStream): Imported {
|
||||||
inputStream: InputStream,
|
|
||||||
globalSettings: Boolean,
|
|
||||||
accountUuids: List<String>?,
|
|
||||||
overview: Boolean,
|
|
||||||
): Imported {
|
|
||||||
try {
|
try {
|
||||||
val settings = XmlSettingsParser(inputStream).parse()
|
return 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,
|
|
||||||
)
|
|
||||||
} catch (e: XmlPullParserException) {
|
} catch (e: XmlPullParserException) {
|
||||||
throw SettingsImportExportException("Error parsing settings XML", e)
|
throw SettingsImportExportException("Error parsing settings XML", e)
|
||||||
} catch (e: SettingsParserException) {
|
} catch (e: SettingsParserException) {
|
||||||
|
|
|
@ -51,13 +51,7 @@ class SettingsImporter internal constructor(
|
||||||
@Throws(SettingsImportExportException::class)
|
@Throws(SettingsImportExportException::class)
|
||||||
fun getImportStreamContents(inputStream: InputStream): ImportContents {
|
fun getImportStreamContents(inputStream: InputStream): ImportContents {
|
||||||
try {
|
try {
|
||||||
// Parse the import stream but don't save individual settings (overview=true)
|
val imported = settingsFileParser.parseSettings(inputStream)
|
||||||
val imported = settingsFileParser.parseSettings(
|
|
||||||
inputStream = inputStream,
|
|
||||||
globalSettings = false,
|
|
||||||
accountUuids = null,
|
|
||||||
overview = true,
|
|
||||||
)
|
|
||||||
|
|
||||||
// If the stream contains global settings the "globalSettings" member will not be null
|
// If the stream contains global settings the "globalSettings" member will not be null
|
||||||
val globalSettings = (imported.globalSettings != null)
|
val globalSettings = (imported.globalSettings != null)
|
||||||
|
@ -101,11 +95,23 @@ class SettingsImporter internal constructor(
|
||||||
val importedAccounts = mutableListOf<AccountDescriptionPair>()
|
val importedAccounts = mutableListOf<AccountDescriptionPair>()
|
||||||
val erroneousAccounts = mutableListOf<AccountDescription>()
|
val erroneousAccounts = mutableListOf<AccountDescription>()
|
||||||
|
|
||||||
val imported = settingsFileParser.parseSettings(
|
val settings = settingsFileParser.parseSettings(inputStream)
|
||||||
inputStream = inputStream,
|
|
||||||
globalSettings = globalSettings,
|
val filteredGlobalSettings = if (globalSettings) {
|
||||||
accountUuids = accountUuids,
|
settings.globalSettings
|
||||||
overview = false,
|
} 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
|
val storage = preferences.storage
|
||||||
|
|
|
@ -30,9 +30,8 @@ class SettingsFileParserTest : RobolectricTest() {
|
||||||
</accounts>
|
</accounts>
|
||||||
</k9settings>
|
</k9settings>
|
||||||
""".trimIndent().byteInputStream()
|
""".trimIndent().byteInputStream()
|
||||||
val accountUuids = listOf("1")
|
|
||||||
|
|
||||||
val results = parser.parseSettings(inputStream, true, accountUuids, true)
|
val results = parser.parseSettings(inputStream)
|
||||||
|
|
||||||
assertThat(results.accounts).isNotNull().all {
|
assertThat(results.accounts).isNotNull().all {
|
||||||
hasSize(1)
|
hasSize(1)
|
||||||
|
@ -61,9 +60,8 @@ class SettingsFileParserTest : RobolectricTest() {
|
||||||
</accounts>
|
</accounts>
|
||||||
</k9settings>
|
</k9settings>
|
||||||
""".trimIndent().byteInputStream()
|
""".trimIndent().byteInputStream()
|
||||||
val accountUuids = listOf("1")
|
|
||||||
|
|
||||||
val results = parser.parseSettings(inputStream, true, accountUuids, true)
|
val results = parser.parseSettings(inputStream)
|
||||||
|
|
||||||
assertThat(results.accounts).isNotNull().all {
|
assertThat(results.accounts).isNotNull().all {
|
||||||
hasSize(1)
|
hasSize(1)
|
||||||
|
@ -91,9 +89,8 @@ class SettingsFileParserTest : RobolectricTest() {
|
||||||
</accounts>
|
</accounts>
|
||||||
</k9settings>
|
</k9settings>
|
||||||
""".trimIndent().byteInputStream()
|
""".trimIndent().byteInputStream()
|
||||||
val accountUuids = listOf(accountUuid)
|
|
||||||
|
|
||||||
val results = parser.parseSettings(inputStream, true, accountUuids, false)
|
val results = parser.parseSettings(inputStream)
|
||||||
|
|
||||||
assertThat(results.accounts)
|
assertThat(results.accounts)
|
||||||
.isNotNull()
|
.isNotNull()
|
||||||
|
|
Loading…
Reference in a new issue