Skip to content

Commit

Permalink
Change SettingsFile.Contents.accounts to a list
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed May 16, 2024
1 parent 7d5d607 commit a4a10ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal interface SettingsFile {
data class Contents(
val contentVersion: Int,
val globalSettings: SettingsMap?,
val accounts: Map<String, Account>,
val accounts: List<Account>,
)

data class Account(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private class XmlSettingsParser(

private fun readRoot(): Contents {
var generalSettings: SettingsMap? = null
var accounts: Map<String, Account>? = null
var accounts: List<Account>? = null

val fileFormatVersion = readFileFormatVersion()
if (fileFormatVersion != SettingsExporter.FILE_FORMAT_VERSION) {
Expand Down Expand Up @@ -144,8 +144,8 @@ private class XmlSettingsParser(
return settings.takeIf { it.isNotEmpty() }
}

private fun readAccounts(): Map<String, Account> {
val accounts = mutableMapOf<String, Account>()
private fun readAccounts(): List<Account> {
val accounts = mutableListOf<Account>()

readElement { eventType ->
if (eventType == XmlPullParser.START_TAG) {
Expand All @@ -155,10 +155,10 @@ private class XmlSettingsParser(

if (account == null) {
// Do nothing - readAccount() already logged a message
} else if (!accounts.containsKey(account.uuid)) {
accounts[account.uuid] = account
} else {
} else if (accounts.any { it.uuid == account.uuid }) {
Timber.w("Duplicate account entries with UUID %s. Ignoring!", account.uuid)
} else {
accounts.add(account)
}
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SettingsImporter internal constructor(
// If the stream contains global settings the "globalSettings" member will not be null
val globalSettings = (imported.globalSettings != null)

val accounts = imported.accounts?.values.orEmpty().map { importedAccount ->
val accounts = imported.accounts.map { importedAccount ->
AccountDescription(
name = getAccountDisplayName(importedAccount),
uuid = importedAccount.uuid,
Expand Down Expand Up @@ -103,7 +103,7 @@ class SettingsImporter internal constructor(
null
}

val filteredAccounts = settings.accounts.filterKeys { it in accountUuids }
val filteredAccounts = settings.accounts.filter { it.uuid in accountUuids }

val imported = settings.copy(
globalSettings = filteredGlobalSettings,
Expand Down Expand Up @@ -133,15 +133,15 @@ class SettingsImporter internal constructor(
}

if (accountUuids.isNotEmpty()) {
val foundAccountUuids = imported.accounts.map { it.value.uuid }.toSet()
val foundAccountUuids = imported.accounts.map { it.uuid }.toSet()
val missingAccountUuids = accountUuids.toSet() - foundAccountUuids
if (missingAccountUuids.isNotEmpty()) {
for (accountUuid in missingAccountUuids) {
Timber.w("Was asked to import account %s. But this account wasn't found.", accountUuid)
}
}

for (account in imported.accounts.values) {
for (account in imported.accounts) {
try {
var editor = preferences.createStorageEditor()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.extracting
import assertk.assertions.hasSize
import assertk.assertions.index
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.key
import assertk.assertions.prop
import com.fsck.k9.mail.AuthType
import com.fsck.k9.preferences.SettingsFile.Account
Expand Down Expand Up @@ -38,7 +38,7 @@ class SettingsFileParserTest : RobolectricTest() {

assertThat(results.accounts).isNotNull().all {
hasSize(1)
key(accountUuid).all {
index(0).all {
prop(Account::uuid).isEqualTo(accountUuid)
prop(Account::name).isEqualTo("Account")
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class SettingsFileParserTest : RobolectricTest() {

assertThat(results.accounts).isNotNull().all {
hasSize(1)
key(accountUuid).all {
index(0).all {
prop(Account::uuid).isEqualTo(accountUuid)
prop(Account::identities).isNotNull()
.extracting(Identity::email).containsExactly("[email protected]")
Expand Down Expand Up @@ -97,7 +97,7 @@ class SettingsFileParserTest : RobolectricTest() {

assertThat(results.accounts)
.isNotNull()
.key(accountUuid)
.index(0)
.prop(Account::incoming)
.isNotNull()
.prop(Server::authenticationType)
Expand Down

0 comments on commit a4a10ce

Please sign in to comment.