Skip to content

Commit 059183f

Browse files
committed
Move extension function inside AttachRoomOperation, remove leftover reversed() call
1 parent 46def9b commit 059183f

File tree

2 files changed

+13
-26
lines changed
  • appnav/src/main/kotlin/io/element/android/appnav
  • libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/collections

2 files changed

+13
-26
lines changed

appnav/src/main/kotlin/io/element/android/appnav/LoggedInFlowNode.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import io.element.android.features.share.api.ShareEntryPoint
6666
import io.element.android.features.startchat.api.StartChatEntryPoint
6767
import io.element.android.features.userprofile.api.UserProfileEntryPoint
6868
import io.element.android.features.verifysession.api.IncomingVerificationEntryPoint
69-
import io.element.android.libraries.androidutils.collections.takeExceptLast
7069
import io.element.android.libraries.architecture.BackstackView
7170
import io.element.android.libraries.architecture.BaseFlowNode
7271
import 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

libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/collections/ListExt.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)