Browse Source
The flow is somewhat misleading so its logic has been merged into `InReplyToDetails.metadata()`.pull/1904/head
Marco Romano
10 months ago
committed by
GitHub
5 changed files with 445 additions and 103 deletions
@ -0,0 +1,108 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2023 New Vector Ltd |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package io.element.android.features.messages.impl.timeline.model |
||||||
|
|
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.runtime.Immutable |
||||||
|
import androidx.compose.ui.res.stringResource |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.AudioMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.FileMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.ImageMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.LocationMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.MessageContent |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.PollContent |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.VideoMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.VoiceMessageType |
||||||
|
import io.element.android.libraries.matrix.ui.components.AttachmentThumbnailInfo |
||||||
|
import io.element.android.libraries.matrix.ui.components.AttachmentThumbnailType |
||||||
|
import io.element.android.libraries.ui.strings.CommonStrings |
||||||
|
|
||||||
|
@Immutable |
||||||
|
internal sealed interface InReplyToMetadata { |
||||||
|
|
||||||
|
val text: String? |
||||||
|
|
||||||
|
data class Thumbnail( |
||||||
|
val attachmentThumbnailInfo: AttachmentThumbnailInfo |
||||||
|
) : InReplyToMetadata { |
||||||
|
override val text: String? = attachmentThumbnailInfo.textContent |
||||||
|
} |
||||||
|
|
||||||
|
data class Text( |
||||||
|
override val text: String |
||||||
|
) : InReplyToMetadata |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Computes metadata for the in reply to message. |
||||||
|
* |
||||||
|
* Metadata can be either a thumbnail with a text OR just a text. |
||||||
|
*/ |
||||||
|
@Composable |
||||||
|
internal fun InReplyToDetails.metadata(): InReplyToMetadata? = when (eventContent) { |
||||||
|
is MessageContent -> when (val type = eventContent.type) { |
||||||
|
is ImageMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = type.info?.thumbnailSource ?: type.source, |
||||||
|
textContent = eventContent.body, |
||||||
|
type = AttachmentThumbnailType.Image, |
||||||
|
blurHash = type.info?.blurhash, |
||||||
|
) |
||||||
|
) |
||||||
|
is VideoMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = type.info?.thumbnailSource, |
||||||
|
textContent = eventContent.body, |
||||||
|
type = AttachmentThumbnailType.Video, |
||||||
|
blurHash = type.info?.blurhash, |
||||||
|
) |
||||||
|
) |
||||||
|
is FileMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = type.info?.thumbnailSource, |
||||||
|
textContent = eventContent.body, |
||||||
|
type = AttachmentThumbnailType.File, |
||||||
|
) |
||||||
|
) |
||||||
|
is LocationMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
textContent = stringResource(CommonStrings.common_shared_location), |
||||||
|
type = AttachmentThumbnailType.Location, |
||||||
|
) |
||||||
|
) |
||||||
|
is AudioMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
textContent = eventContent.body, |
||||||
|
type = AttachmentThumbnailType.Audio, |
||||||
|
) |
||||||
|
) |
||||||
|
is VoiceMessageType -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
textContent = stringResource(CommonStrings.common_voice_message), |
||||||
|
type = AttachmentThumbnailType.Voice, |
||||||
|
) |
||||||
|
) |
||||||
|
else -> InReplyToMetadata.Text(textContent ?: eventContent.body) |
||||||
|
} |
||||||
|
is PollContent -> InReplyToMetadata.Thumbnail( |
||||||
|
AttachmentThumbnailInfo( |
||||||
|
textContent = eventContent.question, |
||||||
|
type = AttachmentThumbnailType.Poll, |
||||||
|
) |
||||||
|
) |
||||||
|
else -> null |
||||||
|
} |
@ -1,28 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2023 New Vector Ltd |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
|
|
||||||
package io.element.android.features.messages.impl.timeline.model |
|
||||||
|
|
||||||
import androidx.compose.ui.tooling.preview.PreviewParameterProvider |
|
||||||
|
|
||||||
internal class TimelineItemGroupPositionProvider : PreviewParameterProvider<TimelineItemGroupPosition> { |
|
||||||
override val values = sequenceOf( |
|
||||||
TimelineItemGroupPosition.First, |
|
||||||
TimelineItemGroupPosition.Middle, |
|
||||||
TimelineItemGroupPosition.Last, |
|
||||||
TimelineItemGroupPosition.None, |
|
||||||
) |
|
||||||
} |
|
@ -0,0 +1,321 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2023 New Vector Ltd |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package io.element.android.features.messages.impl.timeline.model |
||||||
|
|
||||||
|
import android.content.res.Configuration |
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.runtime.CompositionLocalProvider |
||||||
|
import androidx.compose.ui.platform.LocalConfiguration |
||||||
|
import androidx.compose.ui.platform.LocalContext |
||||||
|
import androidx.test.core.app.ApplicationProvider |
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4 |
||||||
|
import app.cash.molecule.RecompositionMode |
||||||
|
import app.cash.molecule.moleculeFlow |
||||||
|
import app.cash.turbine.test |
||||||
|
import com.google.common.truth.Truth |
||||||
|
import io.element.android.libraries.matrix.api.core.EventId |
||||||
|
import io.element.android.libraries.matrix.api.core.UserId |
||||||
|
import io.element.android.libraries.matrix.api.media.AudioInfo |
||||||
|
import io.element.android.libraries.matrix.api.media.FileInfo |
||||||
|
import io.element.android.libraries.matrix.api.media.VideoInfo |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.AudioMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.EventContent |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.FileMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.ImageMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.LocationMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.RedactedContent |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.VideoMessageType |
||||||
|
import io.element.android.libraries.matrix.api.timeline.item.event.VoiceMessageType |
||||||
|
import io.element.android.libraries.matrix.test.AN_EVENT_ID |
||||||
|
import io.element.android.libraries.matrix.test.A_USER_ID |
||||||
|
import io.element.android.libraries.matrix.test.media.aMediaSource |
||||||
|
import io.element.android.libraries.matrix.test.room.aMessageContent |
||||||
|
import io.element.android.libraries.matrix.test.room.aPollContent |
||||||
|
import io.element.android.libraries.matrix.ui.components.AttachmentThumbnailInfo |
||||||
|
import io.element.android.libraries.matrix.ui.components.AttachmentThumbnailType |
||||||
|
import kotlinx.coroutines.test.runTest |
||||||
|
import org.junit.Test |
||||||
|
import org.junit.runner.RunWith |
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class) |
||||||
|
class InReplyToMetadataKtTest { |
||||||
|
@Test |
||||||
|
fun `any message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails(eventContent = aMessageContent()).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo(InReplyToMetadata.Text("textContent")) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `an image message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = ImageMessageType( |
||||||
|
body = "body", |
||||||
|
source = aMediaSource(), |
||||||
|
info = null, |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = aMediaSource(), |
||||||
|
textContent = "body", |
||||||
|
type = AttachmentThumbnailType.Image, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a video message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = VideoMessageType( |
||||||
|
body = "body", |
||||||
|
source = aMediaSource(), |
||||||
|
info = VideoInfo( |
||||||
|
duration = null, |
||||||
|
height = null, |
||||||
|
width = null, |
||||||
|
mimetype = null, |
||||||
|
size = null, |
||||||
|
thumbnailInfo = null, |
||||||
|
thumbnailSource = aMediaSource(), |
||||||
|
blurhash = null |
||||||
|
), |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = aMediaSource(), |
||||||
|
textContent = "body", |
||||||
|
type = AttachmentThumbnailType.Video, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a file message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = FileMessageType( |
||||||
|
body = "body", |
||||||
|
source = aMediaSource(), |
||||||
|
info = FileInfo( |
||||||
|
mimetype = null, |
||||||
|
size = null, |
||||||
|
thumbnailInfo = null, |
||||||
|
thumbnailSource = aMediaSource(), |
||||||
|
), |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = aMediaSource(), |
||||||
|
textContent = "body", |
||||||
|
type = AttachmentThumbnailType.File, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a audio message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = AudioMessageType( |
||||||
|
body = "body", |
||||||
|
source = aMediaSource(), |
||||||
|
info = AudioInfo( |
||||||
|
duration = null, |
||||||
|
size = null, |
||||||
|
mimetype = null |
||||||
|
), |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
textContent = "body", |
||||||
|
type = AttachmentThumbnailType.Audio, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a location message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
testEnv { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = LocationMessageType( |
||||||
|
body = "body", |
||||||
|
geoUri = "geo:3.0,4.0;u=5.0", |
||||||
|
description = null, |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
} |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = null, |
||||||
|
textContent = "Shared location", |
||||||
|
type = AttachmentThumbnailType.Location, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a voice message content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
testEnv { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aMessageContent( |
||||||
|
messageType = VoiceMessageType( |
||||||
|
body = "body", |
||||||
|
source = aMediaSource(), |
||||||
|
info = null, |
||||||
|
details = null, |
||||||
|
) |
||||||
|
) |
||||||
|
).metadata() |
||||||
|
} |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = null, |
||||||
|
textContent = "Voice message", |
||||||
|
type = AttachmentThumbnailType.Voice, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `a poll content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = aPollContent() |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo( |
||||||
|
InReplyToMetadata.Thumbnail( |
||||||
|
attachmentThumbnailInfo = AttachmentThumbnailInfo( |
||||||
|
thumbnailSource = null, |
||||||
|
textContent = "Do you like polls?", |
||||||
|
type = AttachmentThumbnailType.Poll, |
||||||
|
blurHash = null, |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `any other content`() = runTest { |
||||||
|
moleculeFlow(RecompositionMode.Immediate) { |
||||||
|
anInReplyToDetails( |
||||||
|
eventContent = RedactedContent |
||||||
|
).metadata() |
||||||
|
}.test { |
||||||
|
awaitItem().let { |
||||||
|
Truth.assertThat(it).isEqualTo(null) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fun anInReplyToDetails( |
||||||
|
eventId: EventId = AN_EVENT_ID, |
||||||
|
senderId: UserId = A_USER_ID, |
||||||
|
senderDisplayName: String? = "senderDisplayName", |
||||||
|
senderAvatarUrl: String? = "senderAvatarUrl", |
||||||
|
eventContent: EventContent? = aMessageContent(), |
||||||
|
textContent: String? = "textContent", |
||||||
|
) = InReplyToDetails( |
||||||
|
eventId = eventId, |
||||||
|
senderId = senderId, |
||||||
|
senderDisplayName = senderDisplayName, |
||||||
|
senderAvatarUrl = senderAvatarUrl, |
||||||
|
eventContent = eventContent, |
||||||
|
textContent = textContent, |
||||||
|
) |
||||||
|
|
||||||
|
@Composable |
||||||
|
private fun testEnv(content: @Composable () -> Any?): Any? { |
||||||
|
var result: Any? = null |
||||||
|
CompositionLocalProvider( |
||||||
|
LocalConfiguration provides Configuration(), |
||||||
|
LocalContext provides ApplicationProvider.getApplicationContext(), |
||||||
|
) { |
||||||
|
content().apply { |
||||||
|
result = this |
||||||
|
} |
||||||
|
} |
||||||
|
return result |
||||||
|
} |
Loading…
Reference in new issue