@@ -66,7 +66,6 @@ import io.element.android.features.share.api.ShareEntryPoint
6666import io.element.android.features.startchat.api.StartChatEntryPoint
6767import io.element.android.features.userprofile.api.UserProfileEntryPoint
6868import io.element.android.features.verifysession.api.IncomingVerificationEntryPoint
69- import io.element.android.libraries.androidutils.collections.takeExceptLast
7069import io.element.android.libraries.architecture.BackstackView
7170import io.element.android.libraries.architecture.BaseFlowNode
7271import io.element.android.libraries.architecture.callback
@@ -694,6 +693,16 @@ private class AttachRoomOperation(
694693 val roomTarget : LoggedInFlowNode .NavTarget .Room ,
695694 val clearBackstack : Boolean ,
696695) : BackStackOperation<LoggedInFlowNode.NavTarget> {
696+
697+ /* *
698+ * Returns a list containing last [count] elements that match [predicate] while preserving other elements.
699+ */
700+ private fun <T > List<T>.keepingLast (count : Int , predicate : (T ) -> Boolean ): List <T > {
701+ val matchingIndices = indices.filter { predicate(this [it]) }
702+ val indicesToRemove = matchingIndices.dropLast(count).toSet()
703+ return filterIndexed { index, _ -> index !in indicesToRemove }
704+ }
705+
697706 override fun isApplicable (elements : NavElements <LoggedInFlowNode .NavTarget , BackStack .State >) = true
698707
699708 override fun invoke (elements : BackStackElements <LoggedInFlowNode .NavTarget >): BackStackElements <LoggedInFlowNode .NavTarget > {
@@ -721,14 +730,9 @@ private class AttachRoomOperation(
721730 val roomElementCount = elements.count { it.key.navTarget is LoggedInFlowNode .NavTarget .Room }
722731
723732 Timber .d(" Current room nodes: $roomElementCount /$MAX_ROOM_NODE_COUNT " )
724- val currentElements = if (roomElementCount + 1 > MAX_ROOM_NODE_COUNT ) {
725- elements.takeExceptLast(MAX_ROOM_NODE_COUNT ) { element ->
726- element.key.navTarget is LoggedInFlowNode .NavTarget .Room
727- }
728- // Then reverse the order again after removing the extra room nodes
729- .asReversed()
730- } else {
731- elements
733+ // Crate a new list keeping all the elements, but for Room ones just keep the last MAX_ROOM_NODE_COUNT
734+ val currentElements = elements.keepingLast(MAX_ROOM_NODE_COUNT ) { element ->
735+ element.key.navTarget is LoggedInFlowNode .NavTarget .Room
732736 }
733737
734738 // If the room already existed, remove it from the stack and add a new node at the end
0 commit comments