Skip to content

Commit d1f6db4

Browse files
committed
using dedicated ProcessedEvent data class instead of type alias for passing around the process notificatiable events
- also includes @JvmName on all conflicting extensions for consistency
1 parent d3234b3 commit d1f6db4

File tree

8 files changed

+80
-66
lines changed

8 files changed

+80
-66
lines changed

vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,32 @@
1717
package im.vector.app.features.notifications
1818

1919
import im.vector.app.features.invite.AutoAcceptInvites
20-
import im.vector.app.features.notifications.ProcessedType.KEEP
21-
import im.vector.app.features.notifications.ProcessedType.REMOVE
20+
import im.vector.app.features.notifications.ProcessedEvent.Type.KEEP
21+
import im.vector.app.features.notifications.ProcessedEvent.Type.REMOVE
2222
import javax.inject.Inject
2323

24+
private typealias ProcessedEvents = List<ProcessedEvent<NotifiableEvent>>
25+
2426
class NotifiableEventProcessor @Inject constructor(
2527
private val outdatedDetector: OutdatedEventDetector,
2628
private val autoAcceptInvites: AutoAcceptInvites
2729
) {
2830

29-
fun process(eventList: List<NotifiableEvent>, currentRoomId: String?, renderedEventsList: List<ProcessedEvent>): List<ProcessedEvent> {
31+
fun process(eventList: List<NotifiableEvent>, currentRoomId: String?, renderedEventsList: ProcessedEvents): ProcessedEvents {
3032
val processedEventList = eventList.map {
31-
when (it) {
33+
val type = when (it) {
3234
is InviteNotifiableEvent -> if (autoAcceptInvites.hideInvites) REMOVE else KEEP
3335
is NotifiableMessageEvent -> if (shouldIgnoreMessageEventInRoom(currentRoomId, it.roomId) || outdatedDetector.isMessageOutdated(it)) {
3436
REMOVE
3537
} else KEEP
3638
is SimpleNotifiableEvent -> KEEP
37-
} to it
39+
}
40+
ProcessedEvent(type, it)
3841
}
3942

4043
val removedEventsDiff = renderedEventsList.filter { renderedEvent ->
41-
eventList.none { it.eventId == renderedEvent.second.eventId }
42-
}.map { REMOVE to it.second }
44+
eventList.none { it.eventId == renderedEvent.event.eventId }
45+
}.map { ProcessedEvent(REMOVE, it.event) }
4346

4447
return removedEventsDiff + processedEventList
4548
}

vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
7070
* we keep track of them in order to know which events have been removed from the eventList
7171
* allowing us to cancel any notifications previous displayed by now removed events
7272
*/
73-
private var renderedEventsList = emptyList<Pair<ProcessedType, NotifiableEvent>>()
73+
private var renderedEventsList = emptyList<ProcessedEvent<NotifiableEvent>>()
7474
private val avatarSize = context.resources.getDimensionPixelSize(R.dimen.profile_avatar_size)
7575
private var currentRoomId: String? = null
7676

vector/src/main/java/im/vector/app/features/notifications/NotificationFactory.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,38 @@ import android.app.Notification
2020
import androidx.core.content.pm.ShortcutInfoCompat
2121
import javax.inject.Inject
2222

23-
private typealias ProcessedMessageEvent = Pair<ProcessedType, NotifiableMessageEvent>
23+
private typealias ProcessedMessageEvents = List<ProcessedEvent<NotifiableMessageEvent>>
2424

2525
class NotificationFactory @Inject constructor(
2626
private val notificationUtils: NotificationUtils,
2727
private val roomGroupMessageCreator: RoomGroupMessageCreator,
2828
private val summaryGroupMessageCreator: SummaryGroupMessageCreator
2929
) {
3030

31-
fun Map<String, List<ProcessedMessageEvent>>.toNotifications(myUserDisplayName: String, myUserAvatarUrl: String?): List<RoomNotification> {
31+
fun Map<String, ProcessedMessageEvents>.toNotifications(myUserDisplayName: String, myUserAvatarUrl: String?): List<RoomNotification> {
3232
return map { (roomId, events) ->
3333
when {
3434
events.hasNoEventsToDisplay() -> RoomNotification.Removed(roomId)
3535
else -> {
36-
val messageEvents = events.filter { it.first == ProcessedType.KEEP }.map { it.second }
36+
val messageEvents = events.onlyKeptEvents()
3737
roomGroupMessageCreator.createRoomMessage(messageEvents, roomId, myUserDisplayName, myUserAvatarUrl)
3838
}
3939
}
4040
}
4141
}
4242

43-
private fun List<Pair<ProcessedType, NotifiableMessageEvent>>.hasNoEventsToDisplay() = isEmpty() || all {
44-
it.first == ProcessedType.REMOVE || it.second.canNotBeDisplayed()
43+
private fun ProcessedMessageEvents.hasNoEventsToDisplay() = isEmpty() || all {
44+
it.type == ProcessedEvent.Type.REMOVE || it.event.canNotBeDisplayed()
4545
}
4646

4747
private fun NotifiableMessageEvent.canNotBeDisplayed() = isRedacted
4848

49-
fun List<Pair<ProcessedType, InviteNotifiableEvent>>.toNotifications(myUserId: String): List<OneShotNotification> {
49+
@JvmName("toNotificationsInviteNotifiableEvent")
50+
fun List<ProcessedEvent<InviteNotifiableEvent>>.toNotifications(myUserId: String): List<OneShotNotification> {
5051
return map { (processed, event) ->
5152
when (processed) {
52-
ProcessedType.REMOVE -> OneShotNotification.Removed(key = event.roomId)
53-
ProcessedType.KEEP -> OneShotNotification.Append(
53+
ProcessedEvent.Type.REMOVE -> OneShotNotification.Removed(key = event.roomId)
54+
ProcessedEvent.Type.KEEP -> OneShotNotification.Append(
5455
notificationUtils.buildRoomInvitationNotification(event, myUserId),
5556
OneShotNotification.Append.Meta(
5657
key = event.roomId,
@@ -64,11 +65,11 @@ class NotificationFactory @Inject constructor(
6465
}
6566

6667
@JvmName("toNotificationsSimpleNotifiableEvent")
67-
fun List<Pair<ProcessedType, SimpleNotifiableEvent>>.toNotifications(myUserId: String): List<OneShotNotification> {
68+
fun List<ProcessedEvent<SimpleNotifiableEvent>>.toNotifications(myUserId: String): List<OneShotNotification> {
6869
return map { (processed, event) ->
6970
when (processed) {
70-
ProcessedType.REMOVE -> OneShotNotification.Removed(key = event.eventId)
71-
ProcessedType.KEEP -> OneShotNotification.Append(
71+
ProcessedEvent.Type.REMOVE -> OneShotNotification.Removed(key = event.eventId)
72+
ProcessedEvent.Type.KEEP -> OneShotNotification.Append(
7273
notificationUtils.buildSimpleEventNotification(event, myUserId),
7374
OneShotNotification.Append.Meta(
7475
key = event.eventId,

vector/src/main/java/im/vector/app/features/notifications/NotificationRenderer.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class NotificationRenderer @Inject constructor(private val notificationDisplayer
3434
myUserDisplayName: String,
3535
myUserAvatarUrl: String?,
3636
useCompleteNotificationFormat: Boolean,
37-
eventsToProcess: List<ProcessedEvent>) {
37+
eventsToProcess: List<ProcessedEvent<NotifiableEvent>>) {
3838
val (roomEvents, simpleEvents, invitationEvents) = eventsToProcess.groupByType()
3939
with(notificationFactory) {
4040
val roomNotifications = roomEvents.toNotifications(myUserDisplayName, myUserAvatarUrl)
@@ -108,28 +108,28 @@ class NotificationRenderer @Inject constructor(private val notificationDisplayer
108108
}
109109
}
110110

111-
private fun List<ProcessedEvent>.groupByType(): GroupedNotificationEvents {
112-
val roomIdToEventMap: MutableMap<String, MutableList<Pair<ProcessedType, NotifiableMessageEvent>>> = LinkedHashMap()
113-
val simpleEvents: MutableList<Pair<ProcessedType, SimpleNotifiableEvent>> = ArrayList()
114-
val invitationEvents: MutableList<Pair<ProcessedType, InviteNotifiableEvent>> = ArrayList()
111+
private fun List<ProcessedEvent<NotifiableEvent>>.groupByType(): GroupedNotificationEvents {
112+
val roomIdToEventMap: MutableMap<String, MutableList<ProcessedEvent<NotifiableMessageEvent>>> = LinkedHashMap()
113+
val simpleEvents: MutableList<ProcessedEvent<SimpleNotifiableEvent>> = ArrayList()
114+
val invitationEvents: MutableList<ProcessedEvent<InviteNotifiableEvent>> = ArrayList()
115115
forEach {
116-
when (val event = it.second) {
117-
is InviteNotifiableEvent -> invitationEvents.add(it.asPair())
116+
when (val event = it.event) {
117+
is InviteNotifiableEvent -> invitationEvents.add(it.castedToEventType())
118118
is NotifiableMessageEvent -> {
119119
val roomEvents = roomIdToEventMap.getOrPut(event.roomId) { ArrayList() }
120-
roomEvents.add(it.asPair())
120+
roomEvents.add(it.castedToEventType())
121121
}
122-
is SimpleNotifiableEvent -> simpleEvents.add(it.asPair())
122+
is SimpleNotifiableEvent -> simpleEvents.add(it.castedToEventType())
123123
}
124124
}
125125
return GroupedNotificationEvents(roomIdToEventMap, simpleEvents, invitationEvents)
126126
}
127127

128128
@Suppress("UNCHECKED_CAST")
129-
private fun <T : NotifiableEvent> Pair<ProcessedType, *>.asPair(): Pair<ProcessedType, T> = this as Pair<ProcessedType, T>
129+
private fun <T : NotifiableEvent> ProcessedEvent<NotifiableEvent>.castedToEventType(): ProcessedEvent<T> = this as ProcessedEvent<T>
130130

131131
data class GroupedNotificationEvents(
132-
val roomEvents: Map<String, List<Pair<ProcessedType, NotifiableMessageEvent>>>,
133-
val simpleEvents: List<Pair<ProcessedType, SimpleNotifiableEvent>>,
134-
val invitationEvents: List<Pair<ProcessedType, InviteNotifiableEvent>>
132+
val roomEvents: Map<String, List<ProcessedEvent<NotifiableMessageEvent>>>,
133+
val simpleEvents: List<ProcessedEvent<SimpleNotifiableEvent>>,
134+
val invitationEvents: List<ProcessedEvent<InviteNotifiableEvent>>
135135
)

vector/src/main/java/im/vector/app/features/notifications/ProcessedEvent.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package im.vector.app.features.notifications
1818

19-
typealias ProcessedEvent = Pair<ProcessedType, NotifiableEvent>
19+
data class ProcessedEvent<T>(
20+
val type: Type,
21+
val event: T
22+
) {
2023

21-
enum class ProcessedType {
22-
KEEP,
23-
REMOVE
24+
enum class Type {
25+
KEEP,
26+
REMOVE
27+
}
2428
}
2529

26-
fun List<ProcessedEvent>.onlyKeptEvents() = filter { it.first == ProcessedType.KEEP }.map { it.second }
30+
fun <T> List<ProcessedEvent<T>>.onlyKeptEvents() = filter { it.type == ProcessedEvent.Type.KEEP }.map { it.event }

vector/src/test/java/im/vector/app/features/notifications/NotifiableEventProcessorTest.kt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package im.vector.app.features.notifications
1818

19+
import im.vector.app.features.notifications.ProcessedEvent.Type
1920
import im.vector.app.test.fakes.FakeAutoAcceptInvites
2021
import im.vector.app.test.fakes.FakeOutdatedEventDetector
2122
import org.amshove.kluent.shouldBeEqualTo
@@ -39,9 +40,9 @@ class NotifiableEventProcessorTest {
3940

4041
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
4142

42-
result shouldBeEqualTo listOf(
43-
ProcessedType.KEEP to events[0],
44-
ProcessedType.KEEP to events[1]
43+
result shouldBeEqualTo listOfProcessedEvents(
44+
Type.KEEP to events[0],
45+
Type.KEEP to events[1]
4546
)
4647
}
4748

@@ -55,9 +56,9 @@ class NotifiableEventProcessorTest {
5556

5657
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
5758

58-
result shouldBeEqualTo listOf(
59-
ProcessedType.REMOVE to events[0],
60-
ProcessedType.REMOVE to events[1]
59+
result shouldBeEqualTo listOfProcessedEvents(
60+
Type.REMOVE to events[0],
61+
Type.REMOVE to events[1]
6162
)
6263
}
6364

@@ -71,9 +72,9 @@ class NotifiableEventProcessorTest {
7172

7273
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
7374

74-
result shouldBeEqualTo listOf(
75-
ProcessedType.KEEP to events[0],
76-
ProcessedType.KEEP to events[1]
75+
result shouldBeEqualTo listOfProcessedEvents(
76+
Type.KEEP to events[0],
77+
Type.KEEP to events[1]
7778
)
7879
}
7980

@@ -84,8 +85,8 @@ class NotifiableEventProcessorTest {
8485

8586
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
8687

87-
result shouldBeEqualTo listOf(
88-
ProcessedType.REMOVE to events[0],
88+
result shouldBeEqualTo listOfProcessedEvents(
89+
Type.REMOVE to events[0],
8990
)
9091
}
9192

@@ -96,8 +97,8 @@ class NotifiableEventProcessorTest {
9697

9798
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
9899

99-
result shouldBeEqualTo listOf(
100-
ProcessedType.KEEP to events[0],
100+
result shouldBeEqualTo listOfProcessedEvents(
101+
Type.KEEP to events[0],
101102
)
102103
}
103104

@@ -107,26 +108,30 @@ class NotifiableEventProcessorTest {
107108

108109
val result = eventProcessor.process(events, currentRoomId = "room-1", renderedEventsList = emptyList())
109110

110-
result shouldBeEqualTo listOf(
111-
ProcessedType.REMOVE to events[0],
111+
result shouldBeEqualTo listOfProcessedEvents(
112+
Type.REMOVE to events[0],
112113
)
113114
}
114115

115116
@Test
116117
fun `given events are different to rendered events when processing then removes difference`() {
117118
val events = listOf(aSimpleNotifiableEvent(eventId = "event-1"))
118-
val renderedEvents = listOf(
119-
ProcessedType.KEEP to events[0],
120-
ProcessedType.KEEP to anInviteNotifiableEvent(roomId = "event-2")
119+
val renderedEvents = listOf<ProcessedEvent<NotifiableEvent>>(
120+
ProcessedEvent(Type.KEEP, events[0]),
121+
ProcessedEvent(Type.KEEP, anInviteNotifiableEvent(roomId = "event-2"))
121122
)
122123

123124
val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = renderedEvents)
124125

125-
result shouldBeEqualTo listOf(
126-
ProcessedType.REMOVE to renderedEvents[1].second,
127-
ProcessedType.KEEP to renderedEvents[0].second
126+
result shouldBeEqualTo listOfProcessedEvents(
127+
Type.REMOVE to renderedEvents[1].event,
128+
Type.KEEP to renderedEvents[0].event
128129
)
129130
}
131+
132+
private fun listOfProcessedEvents(vararg event: Pair<Type, NotifiableEvent>) = event.map {
133+
ProcessedEvent(it.first, it.second)
134+
}
130135
}
131136

132137
fun aSimpleNotifiableEvent(eventId: String) = SimpleNotifiableEvent(

vector/src/test/java/im/vector/app/features/notifications/NotificationFactoryTest.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package im.vector.app.features.notifications
1818

19+
import im.vector.app.features.notifications.ProcessedEvent.Type
1920
import im.vector.app.test.fakes.FakeNotificationUtils
2021
import im.vector.app.test.fakes.FakeRoomGroupMessageCreator
2122
import im.vector.app.test.fakes.FakeSummaryGroupMessageCreator
@@ -46,7 +47,7 @@ class NotificationFactoryTest {
4647
@Test
4748
fun `given a room invitation when mapping to notification then is Append`() = testWith(notificationFactory) {
4849
val expectedNotification = notificationUtils.givenBuildRoomInvitationNotificationFor(AN_INVITATION_EVENT, MY_USER_ID)
49-
val roomInvitation = listOf(ProcessedType.KEEP to AN_INVITATION_EVENT)
50+
val roomInvitation = listOf(ProcessedEvent(Type.KEEP, AN_INVITATION_EVENT))
5051

5152
val result = roomInvitation.toNotifications(MY_USER_ID)
5253

@@ -63,7 +64,7 @@ class NotificationFactoryTest {
6364

6465
@Test
6566
fun `given a missing event in room invitation when mapping to notification then is Removed`() = testWith(notificationFactory) {
66-
val missingEventRoomInvitation = listOf(ProcessedType.REMOVE to AN_INVITATION_EVENT)
67+
val missingEventRoomInvitation = listOf(ProcessedEvent(Type.REMOVE, AN_INVITATION_EVENT))
6768

6869
val result = missingEventRoomInvitation.toNotifications(MY_USER_ID)
6970

@@ -75,7 +76,7 @@ class NotificationFactoryTest {
7576
@Test
7677
fun `given a simple event when mapping to notification then is Append`() = testWith(notificationFactory) {
7778
val expectedNotification = notificationUtils.givenBuildSimpleInvitationNotificationFor(A_SIMPLE_EVENT, MY_USER_ID)
78-
val roomInvitation = listOf(ProcessedType.KEEP to A_SIMPLE_EVENT)
79+
val roomInvitation = listOf(ProcessedEvent(Type.KEEP, A_SIMPLE_EVENT))
7980

8081
val result = roomInvitation.toNotifications(MY_USER_ID)
8182

@@ -92,7 +93,7 @@ class NotificationFactoryTest {
9293

9394
@Test
9495
fun `given a missing simple event when mapping to notification then is Removed`() = testWith(notificationFactory) {
95-
val missingEventRoomInvitation = listOf(ProcessedType.REMOVE to A_SIMPLE_EVENT)
96+
val missingEventRoomInvitation = listOf(ProcessedEvent(Type.REMOVE, A_SIMPLE_EVENT))
9697

9798
val result = missingEventRoomInvitation.toNotifications(MY_USER_ID)
9899

@@ -105,7 +106,7 @@ class NotificationFactoryTest {
105106
fun `given room with message when mapping to notification then delegates to room group message creator`() = testWith(notificationFactory) {
106107
val events = listOf(A_MESSAGE_EVENT)
107108
val expectedNotification = roomGroupMessageCreator.givenCreatesRoomMessageFor(events, A_ROOM_ID, MY_USER_ID, MY_AVATAR_URL)
108-
val roomWithMessage = mapOf(A_ROOM_ID to listOf(ProcessedType.KEEP to A_MESSAGE_EVENT))
109+
val roomWithMessage = mapOf(A_ROOM_ID to listOf(ProcessedEvent(Type.KEEP, A_MESSAGE_EVENT)))
109110

110111
val result = roomWithMessage.toNotifications(MY_USER_ID, MY_AVATAR_URL)
111112

@@ -114,7 +115,7 @@ class NotificationFactoryTest {
114115

115116
@Test
116117
fun `given a room with no events to display when mapping to notification then is Empty`() = testWith(notificationFactory) {
117-
val events = listOf(ProcessedType.REMOVE to A_MESSAGE_EVENT)
118+
val events = listOf(ProcessedEvent(Type.REMOVE, A_MESSAGE_EVENT))
118119
val emptyRoom = mapOf(A_ROOM_ID to events)
119120

120121
val result = emptyRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL)
@@ -126,7 +127,7 @@ class NotificationFactoryTest {
126127

127128
@Test
128129
fun `given a room with only redacted events when mapping to notification then is Empty`() = testWith(notificationFactory) {
129-
val redactedRoom = mapOf(A_ROOM_ID to listOf(ProcessedType.KEEP to A_MESSAGE_EVENT.copy(isRedacted = true)))
130+
val redactedRoom = mapOf(A_ROOM_ID to listOf(ProcessedEvent(Type.KEEP, A_MESSAGE_EVENT.copy(isRedacted = true))))
130131

131132
val result = redactedRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL)
132133

vector/src/test/java/im/vector/app/features/notifications/NotificationRendererTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private const val AN_EVENT_ID = "event-id"
3030
private const val A_ROOM_ID = "room-id"
3131
private const val USE_COMPLETE_NOTIFICATION_FORMAT = true
3232

33-
private val AN_EVENT_LIST = listOf<Pair<ProcessedType, NotifiableEvent>>()
33+
private val AN_EVENT_LIST = listOf<ProcessedEvent<NotifiableEvent>>()
3434
private val A_PROCESSED_EVENTS = GroupedNotificationEvents(emptyMap(), emptyList(), emptyList())
3535
private val A_SUMMARY_NOTIFICATION = SummaryNotification.Update(mockk())
3636
private val A_REMOVE_SUMMARY_NOTIFICATION = SummaryNotification.Removed

0 commit comments

Comments
 (0)