Merge pull request #4050 from element-hq/feature/bma/normalizedFilter
Make the room filter use normalized strings.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
package io.element.android.libraries.core.extensions
|
||||
|
||||
import java.text.Normalizer
|
||||
import java.util.Locale
|
||||
|
||||
fun Boolean.toOnOff() = if (this) "ON" else "OFF"
|
||||
@@ -83,3 +84,8 @@ fun String.safeCapitalize(): String {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun String.withoutAccents(): String {
|
||||
return Normalizer.normalize(this, Normalizer.Form.NFD)
|
||||
.replace("\\p{Mn}+".toRegex(), "")
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
package io.element.android.libraries.matrix.api.roomlist
|
||||
|
||||
import io.element.android.libraries.core.extensions.withoutAccents
|
||||
|
||||
sealed interface RoomListFilter {
|
||||
companion object {
|
||||
/**
|
||||
@@ -73,5 +75,7 @@ sealed interface RoomListFilter {
|
||||
*/
|
||||
data class NormalizedMatchRoomName(
|
||||
val pattern: String
|
||||
) : RoomListFilter
|
||||
) : RoomListFilter {
|
||||
val normalizedPattern: String = pattern.withoutAccents()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package io.element.android.libraries.matrix.impl.roomlist
|
||||
|
||||
import io.element.android.libraries.core.extensions.withoutAccents
|
||||
import io.element.android.libraries.matrix.api.room.CurrentUserMembership
|
||||
import io.element.android.libraries.matrix.api.room.isDm
|
||||
import io.element.android.libraries.matrix.api.roomlist.RoomListFilter
|
||||
@@ -30,7 +31,7 @@ val RoomListFilter.predicate
|
||||
!roomSummary.isInvited() && (roomSummary.info.numUnreadNotifications > 0 || roomSummary.info.isMarkedUnread)
|
||||
}
|
||||
is RoomListFilter.NormalizedMatchRoomName -> { roomSummary: RoomSummary ->
|
||||
roomSummary.info.name.orEmpty().contains(pattern, ignoreCase = true)
|
||||
roomSummary.info.name?.withoutAccents().orEmpty().contains(normalizedPattern, ignoreCase = true)
|
||||
}
|
||||
RoomListFilter.Invite -> { roomSummary: RoomSummary ->
|
||||
roomSummary.isInvited()
|
||||
|
||||
@@ -34,6 +34,9 @@ class RoomListFilterTest {
|
||||
private val roomToSearch = aRoomSummary(
|
||||
name = "Room to search"
|
||||
)
|
||||
private val roomWithAccent = aRoomSummary(
|
||||
name = "Frédéric"
|
||||
)
|
||||
private val invitedRoom = aRoomSummary(
|
||||
currentUserMembership = CurrentUserMembership.INVITED
|
||||
)
|
||||
@@ -45,6 +48,7 @@ class RoomListFilterTest {
|
||||
markedAsUnreadRoom,
|
||||
unreadNotificationRoom,
|
||||
roomToSearch,
|
||||
roomWithAccent,
|
||||
invitedRoom
|
||||
)
|
||||
|
||||
@@ -69,7 +73,14 @@ class RoomListFilterTest {
|
||||
@Test
|
||||
fun `Room list filter group`() = runTest {
|
||||
val filter = RoomListFilter.Category.Group
|
||||
assertThat(roomSummaries.filter(filter)).containsExactly(regularRoom, favoriteRoom, markedAsUnreadRoom, unreadNotificationRoom, roomToSearch)
|
||||
assertThat(roomSummaries.filter(filter)).containsExactly(
|
||||
regularRoom,
|
||||
favoriteRoom,
|
||||
markedAsUnreadRoom,
|
||||
unreadNotificationRoom,
|
||||
roomToSearch,
|
||||
roomWithAccent,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,6 +107,18 @@ class RoomListFilterTest {
|
||||
assertThat(roomSummaries.filter(filter)).containsExactly(roomToSearch)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Room list filter normalized match room name with accent`() = runTest {
|
||||
val filter = RoomListFilter.NormalizedMatchRoomName("Fred")
|
||||
assertThat(roomSummaries.filter(filter)).containsExactly(roomWithAccent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Room list filter normalized match room name with accent when searching with accent`() = runTest {
|
||||
val filter = RoomListFilter.NormalizedMatchRoomName("Fréd")
|
||||
assertThat(roomSummaries.filter(filter)).containsExactly(roomWithAccent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Room list filter all with one match`() = runTest {
|
||||
val filter = RoomListFilter.all(
|
||||
|
||||
Reference in New Issue
Block a user