diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/LinkedOrganizationsRepository.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/LinkedOrganizationsRepository.kt index b8ce2fe90d..c46773d47d 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/LinkedOrganizationsRepository.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/LinkedOrganizationsRepository.kt @@ -13,7 +13,7 @@ import javax.inject.Singleton class LinkedOrganizationsRepository @Inject constructor() { private var challenge: Challenge? = null private var onChallengeUpdatedCallback: ((Challenge) -> Unit)? = null - private var linkedLaos: MutableMap> = mutableMapOf() + private var linkedLaos: MutableMap>> = mutableMapOf() private var onLinkedLaosUpdatedCallback: ((MutableMap>) -> Unit)? = null private var newTokensNotifyFunction: ((String, String, Array) -> Unit)? = null var otherLaoId: String? = null @@ -33,15 +33,22 @@ class LinkedOrganizationsRepository @Inject constructor() { return challenge } - fun addLinkedLao(lao_id: String, tokens: Array) { - linkedLaos[lao_id] = tokens - onLinkedLaosUpdatedCallback?.invoke(linkedLaos) + fun addLinkedLao(laoId: String, otherLaoId: String, tokens: Array) { + if (!linkedLaos.containsKey(laoId)) { + linkedLaos[laoId] = mutableMapOf() + } + linkedLaos[laoId]!![otherLaoId] = tokens + onLinkedLaosUpdatedCallback?.invoke(linkedLaos[laoId]!!) } - fun updateAndNotifyLinkedLao(lao_id: String, tokens: Array, rollCallId: String) { - linkedLaos[lao_id] = tokens - newTokensNotifyFunction?.invoke(lao_id, rollCallId, tokens) - onLinkedLaosUpdatedCallback?.invoke(linkedLaos) + fun updateAndNotifyLinkedLao( + laoId: String, + otherLaoId: String, + tokens: Array, + rollCallId: String + ) { + addLinkedLao(laoId, otherLaoId, tokens) + newTokensNotifyFunction?.invoke(laoId, rollCallId, tokens) } fun setOnLinkedLaosUpdatedCallback(callback: (MutableMap>) -> Unit) { @@ -52,8 +59,12 @@ class LinkedOrganizationsRepository @Inject constructor() { newTokensNotifyFunction = function } - fun getLinkedLaos(): MutableMap> { - return linkedLaos + fun getLinkedLaos(laoId: String): MutableMap> { + return if (linkedLaos.containsKey(laoId)) { + linkedLaos[laoId]!! + } else { + mutableMapOf() + } } fun flush() { diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/federation/LinkedOrganizationsViewModel.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/federation/LinkedOrganizationsViewModel.kt index 78b71003f6..70054ed050 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/federation/LinkedOrganizationsViewModel.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/federation/LinkedOrganizationsViewModel.kt @@ -169,7 +169,7 @@ constructor( } fun getLinkedLaosMap(): Map> { - return linkedOrgRepo.getLinkedLaos() + return linkedOrgRepo.getLinkedLaos(laoId) } fun isRepositoryValid(): Boolean { diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/utility/handler/data/LinkedOrganizationsHandler.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/utility/handler/data/LinkedOrganizationsHandler.kt index e3fc6f8a9c..863412df95 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/utility/handler/data/LinkedOrganizationsHandler.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/utility/handler/data/LinkedOrganizationsHandler.kt @@ -43,12 +43,13 @@ constructor( if (result.isSuccess()) { if (result.challenge.data == linkedOrgRepo.getChallenge() && linkedOrgRepo.otherLaoId != null) { - linkedOrgRepo.addLinkedLao(linkedOrgRepo.otherLaoId!!, arrayOf()) + val laoId = context.channel.extractLaoId() + linkedOrgRepo.addLinkedLao(laoId, linkedOrgRepo.otherLaoId!!, arrayOf()) laoRepo.addDisposable( context.messageSender .subscribe(Channel.getLaoChannel(linkedOrgRepo.otherLaoId!!)) .subscribe( - { putRemoteLaoTokensInRepository() }, + { putRemoteLaoTokensInRepository(laoId) }, { error: Throwable -> Timber.tag(TAG).e(error, "subscription error") })) } else { Timber.tag(TAG).d("Invalid FederationResult success") @@ -67,7 +68,8 @@ constructor( @Throws(UnknownLaoException::class) fun handleTokensExchange(context: HandlerContext, tokenExchange: TokensExchange) { // Adds the tokens in the repository - linkedOrgRepo.addLinkedLao(tokenExchange.laoId, tokenExchange.tokens) + linkedOrgRepo.addLinkedLao( + context.channel.extractLaoId(), tokenExchange.laoId, tokenExchange.tokens) // Subscribes to social of the linked organization automatically // Note that for now the participants of an LAO automatically subscribe to social of the other @@ -84,12 +86,12 @@ constructor( } } - private fun putRemoteLaoTokensInRepository() { + private fun putRemoteLaoTokensInRepository(myLaoId: String) { try { val rollCall = rollCallRepo.getLastClosedRollCall(linkedOrgRepo.otherLaoId!!) val attendees = rollCall.attendees.map { e -> e.encoded }.toTypedArray() linkedOrgRepo.updateAndNotifyLinkedLao( - linkedOrgRepo.otherLaoId!!, attendees, rollCall.persistentId) + myLaoId, linkedOrgRepo.otherLaoId!!, attendees, rollCall.persistentId) } catch (e: NoRollCallException) { Timber.tag(TAG).d("No RollCall was found on the linked LAO") }