Skip to content

Commit a7c0d7c

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

File tree

2 files changed

+12
-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

+12
-26
lines changed

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

Lines changed: 12 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,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

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)