forked from shihaam/thijooree
41 lines
1.9 KiB
Kotlin
41 lines
1.9 KiB
Kotlin
package sh.sar.basedbank.util
|
|
|
|
import sh.sar.basedbank.BasedBankApp
|
|
import sh.sar.basedbank.api.bml.BmlContactsClient
|
|
import sh.sar.basedbank.api.mib.MibContactsClient
|
|
|
|
/**
|
|
* Behaviour dispatcher for contact operations.
|
|
* Routes add/delete to the correct bank API based on TransferNetwork.
|
|
* UI code never inspects the network or bank type directly.
|
|
*/
|
|
object ContactManager {
|
|
|
|
/** Deletes [contact] via the appropriate bank API. Returns true on success. */
|
|
suspend fun delete(contact: ContactDisplay, app: BasedBankApp): Boolean = when (contact.network) {
|
|
TransferNetwork.BML -> deleteBml(contact, app)
|
|
TransferNetwork.FAHIPAY -> false // Fahipay contacts are read-only
|
|
TransferNetwork.MIB, TransferNetwork.LOCAL, TransferNetwork.SWIFT -> deleteMib(contact, app)
|
|
}
|
|
|
|
private fun deleteBml(contact: ContactDisplay, app: BasedBankApp): Boolean {
|
|
val sess = app.bmlSessions[contact.profileId] ?: app.anyBmlSession() ?: return false
|
|
val contactId = contact.id.removePrefix("bml_")
|
|
return try { BmlContactsClient().deleteContact(sess, contactId) } catch (_: Exception) { false }
|
|
}
|
|
|
|
private fun deleteMib(contact: ContactDisplay, app: BasedBankApp): Boolean {
|
|
val sess = app.anyMibSession() ?: return false
|
|
return try {
|
|
if (contact.profileId.isNotBlank()) {
|
|
val (loginId, profile) = app.mibProfilesMap.entries
|
|
.firstNotNullOfOrNull { (id, profiles) ->
|
|
profiles.firstOrNull { it.profileId == contact.profileId }?.let { id to it }
|
|
} ?: (null to null)
|
|
if (profile != null && loginId != null) app.mibFlowFor(loginId).switchProfile(sess, profile)
|
|
}
|
|
MibContactsClient().deleteContact(sess, contact.id)
|
|
} catch (_: Exception) { false }
|
|
}
|
|
}
|