diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt index aab31b3d1..fca624404 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt @@ -162,15 +162,29 @@ abstract class BinaryPool { /** * Different from doForEach, provide an ordered index to each binary */ - fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit, - conditionToAdd: (binary: BinaryFile) -> Boolean) { + fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary) -> Unit, + conditionToAdd: (binary: BinaryFile) -> Boolean) { + orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary -> + action.invoke(keyBinary) + } + } + + fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary) -> Unit) { + doForEachBinaryWithoutDuplication(action, { true }) + } + + /** + * Different from doForEach, provide an ordered index to each binary + */ + fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit, + conditionToAdd: (binary: BinaryFile) -> Boolean) { orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary -> action.invoke(index, keyBinary.binary) } } - fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit) { - doForEachOrderedBinary(action, { true }) + fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit) { + doForEachOrderedBinaryWithoutDuplication(action, { true }) } fun isEmpty(): Boolean { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt index 7f28085b8..31df7f664 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt @@ -91,6 +91,14 @@ class IconsManager { return list } + fun doForEachCustomIconWithoutBinaryDuplication(action: (IconImageCustom) -> Unit) { + customCache.doForEachBinaryWithoutDuplication { keyBinary -> + keyBinary.keys.forEach { key -> + action.invoke(IconImageCustom(key, keyBinary.binary)) + } + } + } + /** * Clear the cache of icons */ diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt index ba4d8ea5a..8fcd36fed 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt @@ -138,7 +138,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, val binaryCipherKey = database.loadedCipherKey ?: throw IOException("Unable to retrieve cipher key to write binaries") - database.binaryPool.doForEachOrderedBinary { _, binary -> + database.binaryPool.doForEachOrderedBinaryWithoutDuplication { _, binary -> // Force decompression to add binary in header binary.decompress(binaryCipherKey) // Write type binary @@ -497,7 +497,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, xml.startTag(null, DatabaseKDBXXML.ElemBinaries) // Use indexes because necessarily (binary header ref is the order) - mDatabaseKDBX.binaryPool.doForEachOrderedBinary { index, binary -> + mDatabaseKDBX.binaryPool.doForEachOrderedBinaryWithoutDuplication { index, binary -> xml.startTag(null, DatabaseKDBXXML.ElemBinary) xml.attribute(null, DatabaseKDBXXML.AttrId, index.toString()) if (binary.length > 0) { @@ -701,7 +701,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, xml.startTag(null, DatabaseKDBXXML.ElemCustomIcons) - mDatabaseKDBX.iconsManager.getCustomIconList().forEach { iconCustom -> + mDatabaseKDBX.iconsManager.doForEachCustomIconWithoutBinaryDuplication { iconCustom -> xml.startTag(null, DatabaseKDBXXML.ElemCustomIconItem) writeUuid(DatabaseKDBXXML.ElemCustomIconItemID, iconCustom.uuid)