@@ -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,15 @@ private class AttachRoomOperation(
694693 val roomTarget : LoggedInFlowNode .NavTarget .Room ,
695694 val clearBackstack : Boolean ,
696695) : BackStackOperation<LoggedInFlowNode.NavTarget> {
696+ /* *
697+ * Returns a list containing last [count] elements that match [predicate] while preserving other elements.
698+ */
699+ private fun <T > List<T>.keepingLast (count : Int , predicate : (T ) -> Boolean ): List <T > {
700+ val matchingIndices = indices.filter { predicate(this [it]) }
701+ val indicesToRemove = matchingIndices.dropLast(count).toSet()
702+ return filterIndexed { index, _ -> index !in indicesToRemove }
703+ }
704+
697705 override fun isApplicable (elements : NavElements <LoggedInFlowNode .NavTarget , BackStack .State >) = true
698706
699707 override fun invoke (elements : BackStackElements <LoggedInFlowNode .NavTarget >): BackStackElements <LoggedInFlowNode .NavTarget > {
@@ -721,14 +729,9 @@ private class AttachRoomOperation(
721729 val roomElementCount = elements.count { it.key.navTarget is LoggedInFlowNode .NavTarget .Room }
722730
723731 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
732+ // Crate a new list keeping all the elements, but for Room ones just keep the last MAX_ROOM_NODE_COUNT
733+ val currentElements = elements.keepingLast(MAX_ROOM_NODE_COUNT ) { element ->
734+ element.key.navTarget is LoggedInFlowNode .NavTarget .Room
732735 }
733736
734737 // If the room already existed, remove it from the stack and add a new node at the end
0 commit comments