mirror of
https://github.com/element-hq/element-android
synced 2024-11-23 18:05:36 +03:00
Message type : add data classes for all the types
This commit is contained in:
parent
c7c3a57c93
commit
dbb812ad84
21 changed files with 238 additions and 27 deletions
|
@ -7,7 +7,8 @@ import im.vector.matrix.android.api.permalinks.MatrixPermalinkSpan
|
||||||
import im.vector.matrix.android.api.session.events.model.EventType
|
import im.vector.matrix.android.api.session.events.model.EventType
|
||||||
import im.vector.matrix.android.api.session.events.model.TimelineEvent
|
import im.vector.matrix.android.api.session.events.model.TimelineEvent
|
||||||
import im.vector.matrix.android.api.session.events.model.toModel
|
import im.vector.matrix.android.api.session.events.model.toModel
|
||||||
import im.vector.matrix.android.api.session.room.model.MessageContent
|
import im.vector.matrix.android.api.session.room.model.message.MessageContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageTextContent
|
||||||
import im.vector.riotredesign.core.extensions.localDateTime
|
import im.vector.riotredesign.core.extensions.localDateTime
|
||||||
|
|
||||||
class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatter) {
|
class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatter) {
|
||||||
|
@ -17,28 +18,43 @@ class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatte
|
||||||
fun create(event: TimelineEvent,
|
fun create(event: TimelineEvent,
|
||||||
nextEvent: TimelineEvent?,
|
nextEvent: TimelineEvent?,
|
||||||
callback: TimelineEventController.Callback?
|
callback: TimelineEventController.Callback?
|
||||||
): MessageItem? {
|
): MessageTextItem? {
|
||||||
|
|
||||||
val messageContent: MessageContent? = event.root.content.toModel()
|
|
||||||
val roomMember = event.roomMember
|
val roomMember = event.roomMember
|
||||||
if (messageContent == null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val nextRoomMember = nextEvent?.roomMember
|
val nextRoomMember = nextEvent?.roomMember
|
||||||
|
|
||||||
val date = event.root.localDateTime()
|
val date = event.root.localDateTime()
|
||||||
val nextDate = nextEvent?.root?.localDateTime()
|
val nextDate = nextEvent?.root?.localDateTime()
|
||||||
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
||||||
val isNextMessageReceivedMoreThanOneHourAgo = nextDate?.isBefore(date.minusMinutes(60))
|
val isNextMessageReceivedMoreThanOneHourAgo = nextDate?.isBefore(date.minusMinutes(60))
|
||||||
?: false
|
?: false
|
||||||
|
|
||||||
if (addDaySeparator
|
if (addDaySeparator
|
||||||
|| nextRoomMember != roomMember
|
|| nextRoomMember != roomMember
|
||||||
|| nextEvent?.root?.type != EventType.MESSAGE
|
|| nextEvent?.root?.type != EventType.MESSAGE
|
||||||
|| isNextMessageReceivedMoreThanOneHourAgo) {
|
|| isNextMessageReceivedMoreThanOneHourAgo) {
|
||||||
messagesDisplayedWithInformation.add(event.root.eventId)
|
messagesDisplayedWithInformation.add(event.root.eventId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val messageContent: MessageContent = event.root.content.toModel() ?: return null
|
||||||
|
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)
|
||||||
|
val time = timelineDateFormatter.formatMessageHour(date)
|
||||||
|
val avatarUrl = roomMember?.avatarUrl
|
||||||
|
val memberName = roomMember?.displayName ?: event.root.sender
|
||||||
|
|
||||||
|
return when (messageContent) {
|
||||||
|
is MessageTextContent -> buildTextMessageItem(messageContent, memberName, avatarUrl, time, showInformation, callback)
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildTextMessageItem(messageContent: MessageTextContent,
|
||||||
|
memberName: String?,
|
||||||
|
avatarUrl: String?,
|
||||||
|
time: String,
|
||||||
|
showInformation: Boolean,
|
||||||
|
callback: TimelineEventController.Callback?): MessageTextItem? {
|
||||||
|
|
||||||
val message = messageContent.body?.let {
|
val message = messageContent.body?.let {
|
||||||
val spannable = SpannableStringBuilder(it)
|
val spannable = SpannableStringBuilder(it)
|
||||||
MatrixLinkify.addLinks(spannable, object : MatrixPermalinkSpan.Callback {
|
MatrixLinkify.addLinks(spannable, object : MatrixPermalinkSpan.Callback {
|
||||||
|
@ -49,13 +65,12 @@ class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatte
|
||||||
Linkify.addLinks(spannable, Linkify.ALL)
|
Linkify.addLinks(spannable, Linkify.ALL)
|
||||||
spannable
|
spannable
|
||||||
}
|
}
|
||||||
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)
|
return MessageTextItem(
|
||||||
return MessageItem(
|
|
||||||
message = message,
|
message = message,
|
||||||
avatarUrl = roomMember?.avatarUrl,
|
avatarUrl = avatarUrl,
|
||||||
showInformation = showInformation,
|
showInformation = showInformation,
|
||||||
time = timelineDateFormatter.formatMessageHour(date),
|
time = time,
|
||||||
memberName = roomMember?.displayName ?: event.root.sender
|
memberName = memberName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import im.vector.riotredesign.R
|
||||||
import im.vector.riotredesign.core.epoxy.KotlinModel
|
import im.vector.riotredesign.core.epoxy.KotlinModel
|
||||||
import im.vector.riotredesign.features.home.AvatarRenderer
|
import im.vector.riotredesign.features.home.AvatarRenderer
|
||||||
|
|
||||||
class MessageItem(
|
class MessageTextItem(
|
||||||
val message: CharSequence? = null,
|
val message: CharSequence? = null,
|
||||||
val time: CharSequence? = null,
|
val time: CharSequence? = null,
|
||||||
val avatarUrl: String?,
|
val avatarUrl: String?,
|
|
@ -0,0 +1,11 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class AudioInfo(
|
||||||
|
@Json(name = "mimetype") val mimeType: String,
|
||||||
|
@Json(name = "size") val size: Long,
|
||||||
|
@Json(name = "duration") val duration: Int
|
||||||
|
)
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class FileInfo(
|
||||||
|
@Json(name = "mimetype") val mimeType: String,
|
||||||
|
@Json(name = "size") val size: Long,
|
||||||
|
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
|
||||||
|
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
|
||||||
|
)
|
|
@ -0,0 +1,16 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class ImageInfo(
|
||||||
|
@Json(name = "mimetype") val mimeType: String,
|
||||||
|
@Json(name = "w") val w: Int,
|
||||||
|
@Json(name = "h") val h: Int,
|
||||||
|
@Json(name = "size") val size: Long,
|
||||||
|
@Json(name = "rotation") val rotation: Int? = null,
|
||||||
|
@Json(name = "orientation") val orientation: Int? = null,
|
||||||
|
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
|
||||||
|
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
|
||||||
|
)
|
|
@ -0,0 +1,10 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class LocationInfo(
|
||||||
|
@Json(name = "thumbnail_url") val thumbnailUrl: String,
|
||||||
|
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo
|
||||||
|
)
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageAudioContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "info") val info: AudioInfo,
|
||||||
|
@Json(name = "url") val url: String? = null
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,6 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
interface MessageContent {
|
||||||
|
val type: String
|
||||||
|
val body: String
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageDefaultContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageEmoteContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "format") val format: String? = null,
|
||||||
|
@Json(name = "formatted_body") val formattedBody: String? = null
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,13 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageFileContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "filename") val filename: String,
|
||||||
|
@Json(name = "info") val info: FileInfo,
|
||||||
|
@Json(name = "url") val url: String? = null
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageImageContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "info") val info: ImageInfo,
|
||||||
|
@Json(name = "url") val url: String? = null
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageLocationContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "geo_uri") val geoUri: String,
|
||||||
|
@Json(name = "info") val info: LocationInfo
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageNoticeContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "format") val format: String? = null,
|
||||||
|
@Json(name = "formatted_body") val formattedBody: String? = null
|
||||||
|
) : MessageContent
|
|
@ -1,14 +1,12 @@
|
||||||
package im.vector.matrix.android.api.session.room.model
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
import com.squareup.moshi.Json
|
import com.squareup.moshi.Json
|
||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class MessageContent(
|
data class MessageTextContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
@Json(name = "msgtype") val type: String? = null,
|
@Json(name = "body") override val body: String,
|
||||||
@Json(name = "body") val body: String? = null,
|
|
||||||
@Json(name = "format") val format: String? = null,
|
@Json(name = "format") val format: String? = null,
|
||||||
@Json(name = "formatted_body") val formattedBody: String? = null
|
@Json(name = "formatted_body") val formattedBody: String? = null
|
||||||
|
) : MessageContent
|
||||||
)
|
|
|
@ -1,4 +1,4 @@
|
||||||
package im.vector.matrix.android.api.session.room.model
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
object MessageType {
|
object MessageType {
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class MessageVideoContent(
|
||||||
|
@Json(name = "msgtype") override val type: String,
|
||||||
|
@Json(name = "body") override val body: String,
|
||||||
|
@Json(name = "info") val info: VideoInfo,
|
||||||
|
@Json(name = "url") val url: String? = null
|
||||||
|
) : MessageContent
|
|
@ -0,0 +1,12 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class ThumbnailInfo(
|
||||||
|
@Json(name = "w") val width: Int,
|
||||||
|
@Json(name = "h") val height: Int,
|
||||||
|
@Json(name = "size") val size: Long,
|
||||||
|
@Json(name = "mimetype") val mimeType: String
|
||||||
|
)
|
|
@ -0,0 +1,15 @@
|
||||||
|
package im.vector.matrix.android.api.session.room.model.message
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class VideoInfo(
|
||||||
|
@Json(name = "mimetype") val mimeType: String,
|
||||||
|
@Json(name = "w") val w: Int,
|
||||||
|
@Json(name = "h") val h: Int,
|
||||||
|
@Json(name = "size") val size: Long,
|
||||||
|
@Json(name = "duration") val duration: Int,
|
||||||
|
@Json(name = "thumbnail_info") val thumbnailInfo: ThumbnailInfo? = null,
|
||||||
|
@Json(name = "thumbnail_url") val thumbnailUrl: String? = null
|
||||||
|
)
|
|
@ -4,8 +4,8 @@ import im.vector.matrix.android.api.auth.data.Credentials
|
||||||
import im.vector.matrix.android.api.session.events.model.Content
|
import im.vector.matrix.android.api.session.events.model.Content
|
||||||
import im.vector.matrix.android.api.session.events.model.Event
|
import im.vector.matrix.android.api.session.events.model.Event
|
||||||
import im.vector.matrix.android.api.session.events.model.EventType
|
import im.vector.matrix.android.api.session.events.model.EventType
|
||||||
import im.vector.matrix.android.api.session.room.model.MessageContent
|
import im.vector.matrix.android.api.session.room.model.message.MessageTextContent
|
||||||
import im.vector.matrix.android.api.session.room.model.MessageType
|
import im.vector.matrix.android.api.session.room.model.message.MessageType
|
||||||
import im.vector.matrix.android.internal.di.MoshiProvider
|
import im.vector.matrix.android.internal.di.MoshiProvider
|
||||||
|
|
||||||
internal class EventFactory(private val credentials: Credentials) {
|
internal class EventFactory(private val credentials: Credentials) {
|
||||||
|
@ -13,7 +13,7 @@ internal class EventFactory(private val credentials: Credentials) {
|
||||||
private val moshi = MoshiProvider.providesMoshi()
|
private val moshi = MoshiProvider.providesMoshi()
|
||||||
|
|
||||||
fun createTextEvent(roomId: String, text: String): Event {
|
fun createTextEvent(roomId: String, text: String): Event {
|
||||||
val content = MessageContent(type = MessageType.MSGTYPE_TEXT, body = text)
|
val content = MessageTextContent(type = MessageType.MSGTYPE_TEXT, body = text)
|
||||||
|
|
||||||
return Event(
|
return Event(
|
||||||
roomId = roomId,
|
roomId = roomId,
|
||||||
|
|
|
@ -1,6 +1,17 @@
|
||||||
package im.vector.matrix.android.internal.di
|
package im.vector.matrix.android.internal.di
|
||||||
|
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageAudioContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageDefaultContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageEmoteContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageFileContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageImageContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageLocationContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageNoticeContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageTextContent
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageType
|
||||||
|
import im.vector.matrix.android.api.session.room.model.message.MessageVideoContent
|
||||||
import im.vector.matrix.android.internal.network.parsing.RuntimeJsonAdapterFactory
|
import im.vector.matrix.android.internal.network.parsing.RuntimeJsonAdapterFactory
|
||||||
import im.vector.matrix.android.internal.network.parsing.UriMoshiAdapter
|
import im.vector.matrix.android.internal.network.parsing.UriMoshiAdapter
|
||||||
import im.vector.matrix.android.internal.session.sync.model.UserAccountData
|
import im.vector.matrix.android.internal.session.sync.model.UserAccountData
|
||||||
|
@ -14,6 +25,16 @@ object MoshiProvider {
|
||||||
.add(RuntimeJsonAdapterFactory.of(UserAccountData::class.java, "type", UserAccountDataFallback::class.java)
|
.add(RuntimeJsonAdapterFactory.of(UserAccountData::class.java, "type", UserAccountDataFallback::class.java)
|
||||||
.registerSubtype(UserAccountDataDirectMessages::class.java, UserAccountData.TYPE_DIRECT_MESSAGES)
|
.registerSubtype(UserAccountDataDirectMessages::class.java, UserAccountData.TYPE_DIRECT_MESSAGES)
|
||||||
)
|
)
|
||||||
|
.add(RuntimeJsonAdapterFactory.of(MessageContent::class.java, "msgtype", MessageDefaultContent::class.java)
|
||||||
|
.registerSubtype(MessageTextContent::class.java, MessageType.MSGTYPE_TEXT)
|
||||||
|
.registerSubtype(MessageNoticeContent::class.java, MessageType.MSGTYPE_NOTICE)
|
||||||
|
.registerSubtype(MessageEmoteContent::class.java, MessageType.MSGTYPE_EMOTE)
|
||||||
|
.registerSubtype(MessageAudioContent::class.java, MessageType.MSGTYPE_AUDIO)
|
||||||
|
.registerSubtype(MessageImageContent::class.java, MessageType.MSGTYPE_IMAGE)
|
||||||
|
.registerSubtype(MessageVideoContent::class.java, MessageType.MSGTYPE_VIDEO)
|
||||||
|
.registerSubtype(MessageLocationContent::class.java, MessageType.MSGTYPE_LOCATION)
|
||||||
|
.registerSubtype(MessageFileContent::class.java, MessageType.MSGTYPE_FILE)
|
||||||
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun providesMoshi(): Moshi {
|
fun providesMoshi(): Moshi {
|
||||||
|
|
Loading…
Reference in a new issue