Skip to content

Commit 8d11521

Browse files
authored
Reduce API of JoinedRoom, caller must use the Timeline API from liveTimeline instead. (#4731)
This removes lots of boilerplate code.
1 parent 3cf8237 commit 8d11521

File tree

20 files changed

+257
-650
lines changed

20 files changed

+257
-650
lines changed

features/location/impl/src/main/kotlin/io/element/android/features/location/impl/send/SendLocationPresenter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SendLocationPresenter @Inject constructor(
101101
when (mode) {
102102
SendLocationState.Mode.PinLocation -> {
103103
val geoUri = event.cameraPosition.toGeoUri()
104-
room.sendLocation(
104+
room.liveTimeline.sendLocation(
105105
body = generateBody(geoUri),
106106
geoUri = geoUri,
107107
description = null,
@@ -119,7 +119,7 @@ class SendLocationPresenter @Inject constructor(
119119
}
120120
SendLocationState.Mode.SenderLocation -> {
121121
val geoUri = event.toGeoUri()
122-
room.sendLocation(
122+
room.liveTimeline.sendLocation(
123123
body = generateBody(geoUri),
124124
geoUri = geoUri,
125125
description = null,

features/location/impl/src/test/kotlin/io/element/android/features/location/impl/send/SendLocationPresenterTest.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.element.android.libraries.matrix.api.timeline.item.event.toEventOrTran
2626
import io.element.android.libraries.matrix.test.AN_EVENT_ID
2727
import io.element.android.libraries.matrix.test.core.aBuildMeta
2828
import io.element.android.libraries.matrix.test.room.FakeJoinedRoom
29+
import io.element.android.libraries.matrix.test.timeline.FakeTimeline
2930
import io.element.android.libraries.textcomposer.model.MessageComposerMode
3031
import io.element.android.services.analytics.test.FakeAnalyticsService
3132
import io.element.android.tests.testutils.WarmUpRule
@@ -266,7 +267,9 @@ class SendLocationPresenterTest {
266267
Result.success(Unit)
267268
}
268269
val joinedRoom = FakeJoinedRoom(
269-
sendLocationResult = sendLocationResult,
270+
liveTimeline = FakeTimeline().apply {
271+
sendLocationLambda = sendLocationResult
272+
},
270273
)
271274
val sendLocationPresenter = createSendLocationPresenter(joinedRoom)
272275
fakePermissionsPresenter.givenState(
@@ -327,7 +330,9 @@ class SendLocationPresenterTest {
327330
Result.success(Unit)
328331
}
329332
val joinedRoom = FakeJoinedRoom(
330-
sendLocationResult = sendLocationResult,
333+
liveTimeline = FakeTimeline().apply {
334+
sendLocationLambda = sendLocationResult
335+
},
331336
)
332337
val sendLocationPresenter = createSendLocationPresenter(joinedRoom)
333338
fakePermissionsPresenter.givenState(
@@ -388,7 +393,9 @@ class SendLocationPresenterTest {
388393
Result.success(Unit)
389394
}
390395
val joinedRoom = FakeJoinedRoom(
391-
sendLocationResult = sendLocationResult,
396+
liveTimeline = FakeTimeline().apply {
397+
sendLocationLambda = sendLocationResult
398+
},
392399
)
393400
val sendLocationPresenter = createSendLocationPresenter(joinedRoom)
394401
fakePermissionsPresenter.givenState(

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ class MessageComposerPresenter @AssistedInject constructor(
424424
resetComposer(markdownTextEditorState, richTextEditorState, fromEdit = capturedMode is MessageComposerMode.Edit)
425425
when (capturedMode) {
426426
is MessageComposerMode.Attachment,
427-
is MessageComposerMode.Normal -> room.sendMessage(
427+
is MessageComposerMode.Normal -> room.liveTimeline.sendMessage(
428428
body = message.markdown,
429429
htmlBody = message.html,
430430
intentionalMentions = message.intentionalMentions

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/MessagesPresenterTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Please see LICENSE files in the repository root for full details.
66
*/
77

8+
@file:OptIn(ExperimentalCoroutinesApi::class)
9+
810
package io.element.android.features.messages.impl
911

1012
import androidx.lifecycle.Lifecycle
@@ -96,6 +98,7 @@ import kotlinx.collections.immutable.persistentListOf
9698
import kotlinx.coroutines.ExperimentalCoroutinesApi
9799
import kotlinx.coroutines.delay
98100
import kotlinx.coroutines.test.TestScope
101+
import kotlinx.coroutines.test.advanceUntilIdle
99102
import kotlinx.coroutines.test.runCurrent
100103
import kotlinx.coroutines.test.runTest
101104
import org.junit.Rule
@@ -173,12 +176,14 @@ class MessagesPresenterTest {
173176
skipItems(1)
174177
val initialState = awaitItem()
175178
initialState.eventSink(MessagesEvents.ToggleReaction("👍", AN_EVENT_ID.toEventOrTransactionId()))
179+
advanceUntilIdle()
176180
assert(toggleReactionSuccess)
177181
.isCalledOnce()
178182
.with(value("👍"), value(AN_EVENT_ID.toEventOrTransactionId()))
179183
// No crashes when sending a reaction failed
180-
timeline.apply { toggleReactionLambda = toggleReactionFailure }
184+
timeline.toggleReactionLambda = toggleReactionFailure
181185
initialState.eventSink(MessagesEvents.ToggleReaction("👍", AN_EVENT_ID.toEventOrTransactionId()))
186+
advanceUntilIdle()
182187
assert(toggleReactionFailure)
183188
.isCalledOnce()
184189
.with(value("👍"), value(AN_EVENT_ID.toEventOrTransactionId()))
@@ -209,6 +214,7 @@ class MessagesPresenterTest {
209214
val initialState = awaitItem()
210215
initialState.eventSink(MessagesEvents.ToggleReaction("👍", AN_EVENT_ID.toEventOrTransactionId()))
211216
initialState.eventSink(MessagesEvents.ToggleReaction("👍", AN_EVENT_ID.toEventOrTransactionId()))
217+
advanceUntilIdle()
212218
assert(toggleReactionSuccess)
213219
.isCalledExactly(2)
214220
.withSequence(

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/attachments/AttachmentsPreviewPresenterTest.kt

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import io.element.android.libraries.matrix.test.A_CAPTION
3535
import io.element.android.libraries.matrix.test.media.FakeMediaUploadHandler
3636
import io.element.android.libraries.matrix.test.permalink.FakePermalinkBuilder
3737
import io.element.android.libraries.matrix.test.room.FakeJoinedRoom
38+
import io.element.android.libraries.matrix.test.timeline.FakeTimeline
3839
import io.element.android.libraries.mediaupload.api.MediaPreProcessor
3940
import io.element.android.libraries.mediaupload.api.MediaSender
4041
import io.element.android.libraries.mediaupload.api.MediaUploadInfo
@@ -108,15 +109,18 @@ class AttachmentsPreviewPresenterTest {
108109
fun `present - send media success scenario`() = runTest {
109110
val sendFileResult =
110111
lambdaRecorder<File, FileInfo, String?, String?, ProgressCallback?, ReplyParameters?, Result<FakeMediaUploadHandler>> { _, _, _, _, _, _ ->
111-
Result.success(FakeMediaUploadHandler())
112-
}
112+
Result.success(FakeMediaUploadHandler())
113+
}
113114
val room = FakeJoinedRoom(
114-
progressCallbackValues = listOf(
115-
Pair(0, 10),
116-
Pair(5, 10),
117-
Pair(10, 10)
118-
),
119-
sendFileResult = sendFileResult,
115+
liveTimeline = FakeTimeline(
116+
progressCallbackValues = listOf(
117+
Pair(0, 10),
118+
Pair(5, 10),
119+
Pair(10, 10)
120+
),
121+
).apply {
122+
sendFileLambda = sendFileResult
123+
},
120124
)
121125
val onDoneListener = lambdaRecorder<Unit> { }
122126
val presenter = createAttachmentsPreviewPresenter(
@@ -146,10 +150,12 @@ class AttachmentsPreviewPresenterTest {
146150
fun `present - send media after pre-processing success scenario`() = runTest {
147151
val sendFileResult =
148152
lambdaRecorder<File, FileInfo, String?, String?, ProgressCallback?, ReplyParameters?, Result<FakeMediaUploadHandler>> { _, _, _, _, _, _ ->
149-
Result.success(FakeMediaUploadHandler())
150-
}
153+
Result.success(FakeMediaUploadHandler())
154+
}
151155
val room = FakeJoinedRoom(
152-
sendFileResult = sendFileResult,
156+
liveTimeline = FakeTimeline().apply {
157+
sendFileLambda = sendFileResult
158+
},
153159
)
154160
val onDoneListener = lambdaRecorder<Unit> { }
155161
val processLatch = CompletableDeferred<Unit>()
@@ -182,10 +188,12 @@ class AttachmentsPreviewPresenterTest {
182188
fun `present - send media before pre-processing success scenario`() = runTest {
183189
val sendFileResult =
184190
lambdaRecorder<File, FileInfo, String?, String?, ProgressCallback?, ReplyParameters?, Result<FakeMediaUploadHandler>> { _, _, _, _, _, _ ->
185-
Result.success(FakeMediaUploadHandler())
186-
}
191+
Result.success(FakeMediaUploadHandler())
192+
}
187193
val room = FakeJoinedRoom(
188-
sendFileResult = sendFileResult,
194+
liveTimeline = FakeTimeline().apply {
195+
sendFileLambda = sendFileResult
196+
},
189197
)
190198
val onDoneListener = lambdaRecorder<Unit> { }
191199
val processLatch = CompletableDeferred<Unit>()
@@ -298,7 +306,9 @@ class AttachmentsPreviewPresenterTest {
298306
givenImageResult()
299307
}
300308
val room = FakeJoinedRoom(
301-
sendImageResult = sendImageResult,
309+
liveTimeline = FakeTimeline().apply {
310+
sendImageLambda = sendImageResult
311+
},
302312
)
303313
val onDoneListener = lambdaRecorder<Unit> { }
304314
val presenter = createAttachmentsPreviewPresenter(
@@ -340,7 +350,9 @@ class AttachmentsPreviewPresenterTest {
340350
givenVideoResult()
341351
}
342352
val room = FakeJoinedRoom(
343-
sendVideoResult = sendVideoResult,
353+
liveTimeline = FakeTimeline().apply {
354+
sendVideoLambda = sendVideoResult
355+
},
344356
)
345357
val onDoneListener = lambdaRecorder<Unit> { }
346358
val presenter = createAttachmentsPreviewPresenter(
@@ -382,7 +394,9 @@ class AttachmentsPreviewPresenterTest {
382394
givenAudioResult()
383395
}
384396
val room = FakeJoinedRoom(
385-
sendAudioResult = sendAudioResult,
397+
liveTimeline = FakeTimeline().apply {
398+
sendAudioLambda = sendAudioResult
399+
},
386400
)
387401
val onDoneListener = lambdaRecorder<Unit> { }
388402
val presenter = createAttachmentsPreviewPresenter(
@@ -416,10 +430,12 @@ class AttachmentsPreviewPresenterTest {
416430
val failure = MediaPreProcessor.Failure(null)
417431
val sendFileResult =
418432
lambdaRecorder<File, FileInfo, String?, String?, ProgressCallback?, ReplyParameters?, Result<FakeMediaUploadHandler>> { _, _, _, _, _, _ ->
419-
Result.failure(failure)
420-
}
433+
Result.failure(failure)
434+
}
421435
val room = FakeJoinedRoom(
422-
sendFileResult = sendFileResult,
436+
liveTimeline = FakeTimeline().apply {
437+
sendFileLambda = sendFileResult
438+
},
423439
)
424440
val presenter = createAttachmentsPreviewPresenter(room = room, mediaUploadOnSendQueueEnabled = false)
425441
moleculeFlow(RecompositionMode.Immediate) {
@@ -445,11 +461,13 @@ class AttachmentsPreviewPresenterTest {
445461
val failure = MediaPreProcessor.Failure(null)
446462
val sendFileResult =
447463
lambdaRecorder<File, FileInfo, String?, String?, ProgressCallback?, ReplyParameters?, Result<FakeMediaUploadHandler>> { _, _, _, _, _, _ ->
448-
Result.failure(failure)
449-
}
464+
Result.failure(failure)
465+
}
450466
val onDoneListenerResult = lambdaRecorder<Unit> {}
451467
val room = FakeJoinedRoom(
452-
sendFileResult = sendFileResult,
468+
liveTimeline = FakeTimeline().apply {
469+
sendFileLambda = sendFileResult
470+
},
453471
)
454472
val presenter = createAttachmentsPreviewPresenter(room = room, mediaUploadOnSendQueueEnabled = true, onDoneListener = onDoneListenerResult)
455473
moleculeFlow(RecompositionMode.Immediate) {

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenterTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ class MessageComposerPresenterTest {
384384
val presenter = createPresenter(
385385
coroutineScope = this,
386386
room = FakeJoinedRoom(
387-
sendMessageResult = { _, _, _ -> Result.success(Unit) },
387+
liveTimeline = FakeTimeline().apply {
388+
sendMessageLambda = { _, _, _ -> Result.success(Unit) }
389+
},
388390
typingNoticeResult = { Result.success(Unit) }
389391
),
390392
)
@@ -418,7 +420,9 @@ class MessageComposerPresenterTest {
418420
coroutineScope = this,
419421
isRichTextEditorEnabled = false,
420422
room = FakeJoinedRoom(
421-
sendMessageResult = { _, _, _ -> Result.success(Unit) },
423+
liveTimeline = FakeTimeline().apply {
424+
sendMessageLambda = { _, _, _ -> Result.success(Unit) }
425+
},
422426
typingNoticeResult = { Result.success(Unit) }
423427
),
424428
)
@@ -1118,16 +1122,16 @@ class MessageComposerPresenterTest {
11181122
val editMessageLambda = lambdaRecorder { _: EventOrTransactionId, _: String, _: String?, _: List<IntentionalMention> ->
11191123
Result.success(Unit)
11201124
}
1125+
val sendMessageResult = lambdaRecorder { _: String, _: String?, _: List<IntentionalMention> ->
1126+
Result.success(Unit)
1127+
}
11211128
val timeline = FakeTimeline().apply {
11221129
this.replyMessageLambda = replyMessageLambda
11231130
this.editMessageLambda = editMessageLambda
1124-
}
1125-
val sendMessageResult = lambdaRecorder { _: String, _: String?, _: List<IntentionalMention> ->
1126-
Result.success(Unit)
1131+
sendMessageLambda = sendMessageResult
11271132
}
11281133
val room = FakeJoinedRoom(
11291134
liveTimeline = timeline,
1130-
sendMessageResult = sendMessageResult,
11311135
typingNoticeResult = { Result.success(Unit) }
11321136
)
11331137
val presenter = createPresenter(room = room, coroutineScope = this)

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/voicemessages/composer/VoiceMessageComposerPresenterTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.element.android.libraries.matrix.api.media.AudioInfo
2424
import io.element.android.libraries.matrix.api.room.message.ReplyParameters
2525
import io.element.android.libraries.matrix.test.media.FakeMediaUploadHandler
2626
import io.element.android.libraries.matrix.test.room.FakeJoinedRoom
27+
import io.element.android.libraries.matrix.test.timeline.FakeTimeline
2728
import io.element.android.libraries.mediaplayer.test.FakeMediaPlayer
2829
import io.element.android.libraries.mediaupload.api.MediaSender
2930
import io.element.android.libraries.mediaupload.test.FakeMediaPreProcessor
@@ -45,6 +46,7 @@ import kotlinx.collections.immutable.toImmutableList
4546
import kotlinx.collections.immutable.toPersistentList
4647
import kotlinx.coroutines.ExperimentalCoroutinesApi
4748
import kotlinx.coroutines.test.TestScope
49+
import kotlinx.coroutines.test.advanceUntilIdle
4850
import kotlinx.coroutines.test.runTest
4951
import org.junit.Rule
5052
import org.junit.Test
@@ -65,7 +67,9 @@ class VoiceMessageComposerPresenterTest {
6567
Result.success(FakeMediaUploadHandler())
6668
}
6769
private val joinedRoom = FakeJoinedRoom(
68-
sendVoiceMessageResult = sendVoiceMessageResult
70+
liveTimeline = FakeTimeline().apply {
71+
sendVoiceMessageLambda = sendVoiceMessageResult
72+
},
6973
)
7074
private val mediaPreProcessor = FakeMediaPreProcessor().apply { givenAudioResult() }
7175
private val mediaSender = MediaSender(mediaPreProcessor, joinedRoom, InMemorySessionPreferencesStore())
@@ -295,7 +299,6 @@ class VoiceMessageComposerPresenterTest {
295299
awaitItem().eventSink(VoiceMessageComposerEvents.RecorderEvent(VoiceMessageRecorderEvent.Stop))
296300
awaitItem().eventSink(VoiceMessageComposerEvents.SendVoiceMessage)
297301
assertThat(awaitItem().voiceMessageState).isEqualTo(aPreviewState().toSendingState())
298-
299302
val finalState = awaitItem()
300303
assertThat(finalState.voiceMessageState).isEqualTo(VoiceMessageState.Idle)
301304
sendVoiceMessageResult.assertions().isCalledOnce()
@@ -317,7 +320,7 @@ class VoiceMessageComposerPresenterTest {
317320
awaitItem().eventSink(VoiceMessageComposerEvents.RecorderEvent(VoiceMessageRecorderEvent.Stop))
318321
awaitItem().eventSink(VoiceMessageComposerEvents.SendVoiceMessage)
319322
skipItems(1) // Sending state
320-
323+
advanceUntilIdle()
321324
// Now reply with a voice message
322325
messageComposerContext.composerMode = aReplyMode()
323326
awaitItem().eventSink(VoiceMessageComposerEvents.RecorderEvent(VoiceMessageRecorderEvent.Start))
@@ -653,7 +656,7 @@ class VoiceMessageComposerPresenterTest {
653656
permissionsPresenter: PermissionsPresenter = createFakePermissionsPresenter(),
654657
): VoiceMessageComposerPresenter {
655658
return VoiceMessageComposerPresenter(
656-
this,
659+
backgroundScope,
657660
voiceRecorder,
658661
analyticsService,
659662
mediaSender,

features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/actions/DefaultEndPollAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DefaultEndPollAction @Inject constructor(
2222
private val analyticsService: AnalyticsService,
2323
) : EndPollAction {
2424
override suspend fun execute(pollStartId: EventId): Result<Unit> {
25-
return room.endPoll(
25+
return room.liveTimeline.endPoll(
2626
pollStartId = pollStartId,
2727
text = "The poll with event id: $pollStartId has ended."
2828
).onSuccess {

features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/actions/DefaultSendPollResponseAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DefaultSendPollResponseAction @Inject constructor(
2222
private val analyticsService: AnalyticsService,
2323
) : SendPollResponseAction {
2424
override suspend fun execute(pollStartId: EventId, answerId: String): Result<Unit> {
25-
return room.sendPollResponse(
25+
return room.liveTimeline.sendPollResponse(
2626
pollStartId = pollStartId,
2727
answers = listOf(answerId),
2828
).onSuccess {

features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/data/PollRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PollRepository @Inject constructor(
4141
pollKind: PollKind,
4242
maxSelections: Int,
4343
): Result<Unit> = when (existingPollId) {
44-
null -> room.createPoll(
44+
null -> room.liveTimeline.createPoll(
4545
question = question,
4646
answers = answers,
4747
maxSelections = maxSelections,

0 commit comments

Comments
 (0)