call in attachments

This commit is contained in:
2021-10-11 00:06:36 +03:00
parent 147e6c5a33
commit ef7d1a6031
17 changed files with 251 additions and 21 deletions
@@ -142,9 +142,7 @@ object VkUtils {
}
BaseVkAttachmentItem.AttachmentType.VOICE -> {
val voiceMessage = baseAttachment.voiceMessage ?: continue
attachments += VkVoiceMessage(
link = voiceMessage.link_mp3
)
attachments += voiceMessage.asVkVoiceMessage()
}
BaseVkAttachmentItem.AttachmentType.STICKER -> {
val sticker = baseAttachment.sticker ?: continue
@@ -180,9 +178,7 @@ object VkUtils {
}
BaseVkAttachmentItem.AttachmentType.CALL -> {
val call = baseAttachment.call ?: continue
attachments += VkCall(
initiatorId = call.initiator_id
)
attachments += call.asVkCall()
}
BaseVkAttachmentItem.AttachmentType.GROUP_CALL_IN_PROGRESS -> {
val groupCall = baseAttachment.groupCall ?: continue
@@ -5,7 +5,12 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class VkCall(
val initiatorId: Int
val initiatorId: Int,
val receiverId: Int,
val state: String,
val time: Int,
val duration: Int,
val isVideo: Boolean
) : VkAttachment() {
@IgnoredOnParcel
@@ -5,9 +5,18 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class VkVoiceMessage(
val link: String
val id: Int,
val ownerId: Int,
val duration: Int,
val waveform: List<Int>,
val linkOgg: String,
val linkMp3: String,
val accessKey: String,
val transcriptState: String,
val transcript: String
) : VkAttachment() {
@IgnoredOnParcel
val className: String = this::class.java.name
}
@@ -1,6 +1,7 @@
package com.meloda.fast.api.model.base.attachments
import android.os.Parcelable
import com.meloda.fast.api.model.attachments.VkCall
import kotlinx.parcelize.Parcelize
@Parcelize
@@ -11,4 +12,15 @@ data class BaseVkCall(
val time: Int,
val duration: Int,
val video: Boolean
) : Parcelable
) : Parcelable {
fun asVkCall() = VkCall(
initiatorId = initiator_id,
receiverId = receiver_id,
state = state,
time = time,
duration = duration,
isVideo = video
)
}
@@ -1,6 +1,7 @@
package com.meloda.fast.api.model.base.attachments
import android.os.Parcelable
import com.meloda.fast.api.model.attachments.VkVoiceMessage
import kotlinx.parcelize.Parcelize
@Parcelize
@@ -14,4 +15,18 @@ data class BaseVkVoiceMessage(
val access_key: String,
val transcript_state: String,
val transcript: String
) : Parcelable
) : Parcelable {
fun asVkVoiceMessage() = VkVoiceMessage(
id = id,
ownerId = owner_id,
duration = duration,
waveform = waveform,
linkOgg = link_ogg,
linkMp3 = link_mp3,
accessKey = access_key,
transcriptState = transcript_state,
transcript = transcript
)
}
@@ -15,9 +15,11 @@ import androidx.core.content.ContextCompat
import androidx.core.view.isNotEmpty
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import androidx.core.view.updatePadding
import coil.load
import com.google.android.material.imageview.ShapeableImageView
import com.meloda.fast.R
import com.meloda.fast.api.UserConfig
import com.meloda.fast.api.VkUtils
import com.meloda.fast.api.model.VkGroup
import com.meloda.fast.api.model.VkMessage
@@ -34,6 +36,7 @@ import kotlin.math.roundToInt
class AttachmentInflater constructor(
private val context: Context,
private val container: LinearLayoutCompat,
private val textContainer: LinearLayoutCompat,
private val message: VkMessage,
private val profiles: Map<Int, VkUser>,
private val groups: Map<Int, VkGroup>
@@ -57,6 +60,7 @@ class AttachmentInflater constructor(
attachments = message.attachments!!
container.removeAllViews()
textContainer.removeAllViews()
if (attachments.size == 1) {
when (val attachment = attachments[0]) {
@@ -90,7 +94,8 @@ class AttachmentInflater constructor(
is VkAudio -> audio(attachment)
is VkFile -> file(attachment)
is VkLink -> link(attachment)
is VkStory -> story(attachment)
is VkVoiceMessage -> voice(attachment)
is VkCall -> call(attachment)
else -> Log.e(
"Attachment inflater",
@@ -312,8 +317,57 @@ class AttachmentInflater constructor(
).format(wall.date * 1000L)
}
private fun story(story: VkStory) {
private fun voice(voiceMessage: VkVoiceMessage) {
val binding = ItemMessageAttachmentVoiceBinding.inflate(inflater, textContainer, true)
if (message.isOut)
binding.root.updatePadding(
bottom = AndroidUtils.px(5).roundToInt(),
left = AndroidUtils.px(6).roundToInt()
)
val waveform = IntArray(voiceMessage.waveform.size)
voiceMessage.waveform.forEachIndexed { index, i -> waveform[index] = i }
binding.waveform.sample = waveform
binding.waveform.maxProgress = 100f
binding.waveform.progress = 100f
binding.duration.text = SimpleDateFormat(
"mm:ss",
Locale.getDefault()
).format(voiceMessage.duration * 1000L)
}
private fun call(call: VkCall) {
val binding = ItemMessageAttachmentCallBinding.inflate(inflater, textContainer, true)
if (message.isOut)
binding.root.updatePadding(
bottom = AndroidUtils.px(5).roundToInt(),
left = AndroidUtils.px(6).roundToInt()
)
val callType =
context.getString(
if (call.initiatorId == UserConfig.userId) R.string.message_call_type_outgoing
else R.string.message_call_type_incoming
)
binding.type.text = callType
var callState =
context.getString(
if (call.state == "reached") R.string.message_call_state_ended
else if (call.state == "canceled_by_initiator") {
if (call.initiatorId == UserConfig.userId) R.string.message_call_state_cancelled
else R.string.message_call_state_missed
} else R.string.message_call_unknown
)
if (callState == context.getString(R.string.message_call_unknown)) callState = call.state
binding.state.text = callState
}
}
@@ -136,6 +136,8 @@ class MessagesHistoryAdapter constructor(
text = binding.text,
spacer = binding.spacer,
unread = binding.unread,
textContainer = binding.textContainer,
attachmentContainer = binding.attachmentContainer,
attachmentSpacer = binding.attachmentSpacer,
@@ -172,6 +174,8 @@ class MessagesHistoryAdapter constructor(
text = binding.text,
spacer = binding.spacer,
unread = binding.unread,
textContainer = binding.textContainer,
attachmentContainer = binding.attachmentContainer,
attachmentSpacer = binding.attachmentSpacer,
@@ -42,6 +42,7 @@ class MessagesPreparator constructor(
private val spacer: Space? = null,
private val unread: ImageView? = null,
private val time: TextView? = null,
private val textContainer: LinearLayoutCompat? = null,
private val attachmentContainer: LinearLayoutCompat? = null,
private val attachmentSpacer: Space? = null,
@@ -158,7 +159,7 @@ class MessagesPreparator constructor(
}
private fun prepareAttachments() {
if (attachmentContainer != null) {
if (attachmentContainer != null && textContainer != null) {
if (message.attachments.isNullOrEmpty()) {
attachmentContainer.isVisible = false
attachmentContainer.removeAllViews()
@@ -168,6 +169,7 @@ class MessagesPreparator constructor(
AttachmentInflater(
context = context,
container = attachmentContainer,
textContainer = textContainer,
message = message,
groups = groups,
profiles = profiles