Improve VoiceBroadcastItemFactory

This commit is contained in:
Florian Renaud 2022-10-24 16:35:16 +02:00
parent 4defc3dded
commit a7c24b23a0
2 changed files with 27 additions and 24 deletions

View file

@ -15,14 +15,14 @@
*/
package im.vector.app.features.home.room.detail.timeline.factory
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.resources.DrawableProvider
import im.vector.app.features.displayname.getBestName
import im.vector.app.features.home.room.detail.timeline.TimelineEventController
import im.vector.app.features.home.room.detail.timeline.helper.AvatarSizeProvider
import im.vector.app.features.home.room.detail.timeline.helper.VoiceBroadcastEventsGroup
import im.vector.app.features.home.room.detail.timeline.item.AbsMessageItem
import im.vector.app.features.home.room.detail.timeline.item.AbsMessageVoiceBroadcastItem
import im.vector.app.features.home.room.detail.timeline.item.MessageVoiceBroadcastListeningItem
import im.vector.app.features.home.room.detail.timeline.item.MessageVoiceBroadcastListeningItem_
import im.vector.app.features.home.room.detail.timeline.item.MessageVoiceBroadcastRecordingItem
@ -34,7 +34,7 @@ import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.getUser
import org.matrix.android.sdk.api.session.getUserOrDefault
import org.matrix.android.sdk.api.util.toMatrixItem
import javax.inject.Inject
@ -53,31 +53,31 @@ class VoiceBroadcastItemFactory @Inject constructor(
highlight: Boolean,
callback: TimelineEventController.Callback?,
attributes: AbsMessageItem.Attributes,
): VectorEpoxyModel<out VectorEpoxyHolder>? {
): AbsMessageVoiceBroadcastItem<*>? {
// Only display item of the initial event with updated data
if (messageContent.voiceBroadcastState != VoiceBroadcastState.STARTED) return null
val eventsGroup = params.eventsGroup ?: return null
val voiceBroadcastEventsGroup = VoiceBroadcastEventsGroup(eventsGroup)
val mostRecentTimelineEvent = voiceBroadcastEventsGroup.getLastDisplayableEvent()
val mostRecentEvent = mostRecentTimelineEvent.root.asVoiceBroadcastEvent()
val mostRecentMessageContent = mostRecentEvent?.content ?: return null
val isRecording = mostRecentMessageContent.voiceBroadcastState != VoiceBroadcastState.STOPPED && mostRecentEvent.root.stateKey == session.myUserId
val recorderName = mostRecentTimelineEvent.root.stateKey?.let { session.getUser(it) }?.displayName ?: mostRecentTimelineEvent.root.stateKey
val voiceBroadcastEventsGroup = params.eventsGroup?.let { VoiceBroadcastEventsGroup(it) } ?: return null
val voiceBroadcastEvent = voiceBroadcastEventsGroup.getLastDisplayableEvent().root.asVoiceBroadcastEvent() ?: return null
val voiceBroadcastContent = voiceBroadcastEvent.content ?: return null
val voiceBroadcastId = voiceBroadcastEventsGroup.voiceBroadcastId
val isRecording = voiceBroadcastContent.voiceBroadcastState != VoiceBroadcastState.STOPPED && voiceBroadcastEvent.root.stateKey == session.myUserId
return if (isRecording) {
createRecordingItem(
params.event.roomId,
eventsGroup.groupId,
mostRecentMessageContent.voiceBroadcastState,
params,
voiceBroadcastId,
voiceBroadcastContent.voiceBroadcastState,
highlight,
callback,
attributes
)
} else {
createListeningItem(
params.event.roomId,
eventsGroup.groupId,
mostRecentMessageContent.voiceBroadcastState,
recorderName,
params,
voiceBroadcastId,
voiceBroadcastContent.voiceBroadcastState,
highlight,
callback,
attributes
@ -86,14 +86,14 @@ class VoiceBroadcastItemFactory @Inject constructor(
}
private fun createRecordingItem(
roomId: String,
params: TimelineItemFactoryParams,
voiceBroadcastId: String,
voiceBroadcastState: VoiceBroadcastState?,
highlight: Boolean,
callback: TimelineEventController.Callback?,
attributes: AbsMessageItem.Attributes,
): MessageVoiceBroadcastRecordingItem {
val roomSummary = session.getRoom(roomId)?.roomSummary()
val roomSummary = session.getRoom(params.event.roomId)?.roomSummary()
return MessageVoiceBroadcastRecordingItem_()
.id("voice_broadcast_$voiceBroadcastId")
.attributes(attributes)
@ -109,15 +109,15 @@ class VoiceBroadcastItemFactory @Inject constructor(
}
private fun createListeningItem(
roomId: String,
params: TimelineItemFactoryParams,
voiceBroadcastId: String,
voiceBroadcastState: VoiceBroadcastState?,
broadcasterName: String?,
highlight: Boolean,
callback: TimelineEventController.Callback?,
attributes: AbsMessageItem.Attributes,
): MessageVoiceBroadcastListeningItem {
val roomSummary = session.getRoom(roomId)?.roomSummary()
val roomSummary = session.getRoom(params.event.roomId)?.roomSummary()
val recorderName = params.event.root.stateKey?.let { session.getUserOrDefault(it) }?.toMatrixItem()?.getBestName()
return MessageVoiceBroadcastListeningItem_()
.id("voice_broadcast_$voiceBroadcastId")
.attributes(attributes)
@ -128,7 +128,7 @@ class VoiceBroadcastItemFactory @Inject constructor(
.voiceBroadcastPlayer(voiceBroadcastPlayer)
.voiceBroadcastId(voiceBroadcastId)
.voiceBroadcastState(voiceBroadcastState)
.broadcasterName(broadcasterName)
.broadcasterName(recorderName)
.leftGuideline(avatarSizeProvider.leftGuideline)
.callback(callback)
}

View file

@ -141,6 +141,9 @@ class CallSignalingEventsGroup(private val group: TimelineEventsGroup) {
}
class VoiceBroadcastEventsGroup(private val group: TimelineEventsGroup) {
val voiceBroadcastId = group.groupId
fun getLastDisplayableEvent(): TimelineEvent {
return group.events.find { it.root.asVoiceBroadcastEvent()?.content?.voiceBroadcastState == VoiceBroadcastState.STOPPED }
?: group.events.filter { it.root.type == VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO }.maxBy { it.root.originServerTs ?: 0L }