From ef70e9a69776900ed105fec3584275cd5cd032e3 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 16 Sep 2020 15:15:25 +0200 Subject: [PATCH] Fixed using already mapped TAN procedures and media instead of creating a new one each time leading to duplicates in db --- .../persistence/RoomBankingPersistence.kt | 20 +++++++++++++++---- .../dankito/banking/persistence/model/Bank.kt | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt index 0bbfa2ae..87acc0b8 100644 --- a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt +++ b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt @@ -2,6 +2,7 @@ package net.dankito.banking.persistence import android.content.Context import androidx.room.Room +import net.dankito.banking.persistence.dao.BaseDao import net.dankito.banking.persistence.dao.saveOrUpdate import net.dankito.banking.persistence.model.* import net.dankito.banking.search.IRemitteeSearcher @@ -36,12 +37,22 @@ open class RoomBankingPersistence(applicationContext: Context) : IBankingPersist // TODO: in this way removed TAN procedures won't be deleted from DB and therefore still be visible to user val tanProcedures = bank.supportedTanProcedures.filterIsInstance() - tanProcedures.forEach { it.bankId = bank.id } - db.tanProcedureDao().saveOrUpdate(tanProcedures) + tanProcedures.forEach { tanProcedure -> + if (tanProcedure.bankId == BaseDao.ObjectNotInsertedId) { + tanProcedure.bankId = bank.id + db.tanProcedureDao().insert(tanProcedure) + } + else { + db.tanProcedureDao().update(tanProcedure) + } + } // TODO: in this way removed TAN procedures won't be deleted from DB and therefore still be visible to user - val tanMedia = bank.tanMedia.map { map(bank, it) } + val tanMedia = bank.tanMedia.map { tanMedium -> + bank.tanMediumEntities.firstOrNull { it.id == tanMedium.technicalId } ?: map(bank, tanMedium) + } db.tanMediumDao().saveOrUpdate(tanMedia) + bank.tanMediumEntities = tanMedia } } @@ -85,7 +96,8 @@ open class RoomBankingPersistence(applicationContext: Context) : IBankingPersist bank.supportedTanProcedures = tanProcedures.filter { it.bankId == bank.id } bank.selectedTanProcedure = bank.supportedTanProcedures.firstOrNull { it.technicalId == bank.selectedTanProcedureId } - bank.tanMedia = tanMedia.filter { it.bankId == bank.id }.map { map(it) } + bank.tanMediumEntities = tanMedia.filter { it.bankId == bank.id } + bank.tanMedia = bank.tanMediumEntities.map { map(it) } } return banks diff --git a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/model/Bank.kt b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/model/Bank.kt index 471020a0..4aabb281 100644 --- a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/model/Bank.kt +++ b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/model/Bank.kt @@ -47,6 +47,9 @@ open class Bank( open var selectedTanProcedureId: String? = null + @Ignore + open var tanMediumEntities = listOf() + override fun toString(): String { return stringRepresentation