From ef7d1a603106da6b890ab14285ccd268bcfd8166 Mon Sep 17 00:00:00 2001 From: Danil Nikolaev Date: Mon, 11 Oct 2021 00:06:36 +0300 Subject: [PATCH] call in attachments --- app/build.gradle.kts | 4 +- app/src/main/AndroidManifest.xml | 1 + .../kotlin/com/meloda/fast/api/VkUtils.kt | 8 +-- .../fast/api/model/attachments/VkCall.kt | 7 ++- .../api/model/attachments/VkVoiceMessage.kt | 11 +++- .../api/model/base/attachments/BaseVkCall.kt | 14 ++++- .../base/attachments/BaseVkVoiceMessage.kt | 17 +++++- .../screens/messages/AttachmentInflater.kt | 58 +++++++++++++++++- .../messages/MessagesHistoryAdapter.kt | 4 ++ .../screens/messages/MessagesPreparator.kt | 4 +- .../layout/item_message_attachment_call.xml | 59 +++++++++++++++++++ .../layout/item_message_attachment_voice.xml | 56 ++++++++++++++++++ app/src/main/res/layout/item_message_in.xml | 2 +- app/src/main/res/layout/item_message_out.xml | 19 ++++-- app/src/main/res/values-v31/colors.xml | 1 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 6 ++ 17 files changed, 251 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/layout/item_message_attachment_call.xml create mode 100644 app/src/main/res/layout/item_message_attachment_voice.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 899d0828..0117e214 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -82,6 +82,8 @@ dependencies { coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") + implementation("com.github.massoudss:waveformSeekBar:3.1.0") + implementation("androidx.core:core-splashscreen:1.0.0-alpha02") implementation("androidx.work:work-runtime-ktx:2.6.0") @@ -100,7 +102,7 @@ dependencies { implementation("androidx.fragment:fragment-ktx:1.3.6") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2") implementation("androidx.room:room-ktx:2.3.0") implementation("androidx.room:room-runtime:2.3.0") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1486ffe8..4e07d606 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ { 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 diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkCall.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkCall.kt index 43c54a00..c55143da 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkCall.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkCall.kt @@ -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 diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkVoiceMessage.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkVoiceMessage.kt index 5ead3805..6fcce196 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkVoiceMessage.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VkVoiceMessage.kt @@ -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, + val linkOgg: String, + val linkMp3: String, + val accessKey: String, + val transcriptState: String, + val transcript: String ) : VkAttachment() { @IgnoredOnParcel val className: String = this::class.java.name + } \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkCall.kt b/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkCall.kt index 9911e79a..2bbde082 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkCall.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkCall.kt @@ -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 \ No newline at end of file +) : Parcelable { + + fun asVkCall() = VkCall( + initiatorId = initiator_id, + receiverId = receiver_id, + state = state, + time = time, + duration = duration, + isVideo = video + ) + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkVoiceMessage.kt b/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkVoiceMessage.kt index 88b37355..4445ffe0 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkVoiceMessage.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/model/base/attachments/BaseVkVoiceMessage.kt @@ -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 \ No newline at end of file +) : 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 + ) + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/screens/messages/AttachmentInflater.kt b/app/src/main/kotlin/com/meloda/fast/screens/messages/AttachmentInflater.kt index 5e231f39..902748af 100644 --- a/app/src/main/kotlin/com/meloda/fast/screens/messages/AttachmentInflater.kt +++ b/app/src/main/kotlin/com/meloda/fast/screens/messages/AttachmentInflater.kt @@ -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, private val groups: Map @@ -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 } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesHistoryAdapter.kt b/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesHistoryAdapter.kt index 29c0b9ce..426b8a24 100644 --- a/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesHistoryAdapter.kt +++ b/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesHistoryAdapter.kt @@ -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, diff --git a/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesPreparator.kt b/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesPreparator.kt index c46d9b9e..3cf1de56 100644 --- a/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesPreparator.kt +++ b/app/src/main/kotlin/com/meloda/fast/screens/messages/MessagesPreparator.kt @@ -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 diff --git a/app/src/main/res/layout/item_message_attachment_call.xml b/app/src/main/res/layout/item_message_attachment_call.xml new file mode 100644 index 00000000..2556aec9 --- /dev/null +++ b/app/src/main/res/layout/item_message_attachment_call.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_message_attachment_voice.xml b/app/src/main/res/layout/item_message_attachment_voice.xml new file mode 100644 index 00000000..1a5452bd --- /dev/null +++ b/app/src/main/res/layout/item_message_attachment_voice.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_message_in.xml b/app/src/main/res/layout/item_message_in.xml index a142f1e8..83f5bc99 100644 --- a/app/src/main/res/layout/item_message_in.xml +++ b/app/src/main/res/layout/item_message_in.xml @@ -56,6 +56,7 @@ tools:ignore="UselessParent"> @@ -71,7 +72,6 @@ tools:text="This" /> - - + android:orientation="vertical"> + + + @android:color/system_accent2_100 @android:color/system_accent2_200 + @android:color/system_accent2_300 @android:color/system_accent2_700 @android:color/system_accent2_1000 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 6ed665cd..5a658924 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -39,6 +39,7 @@ #DCE1F7 #C0C6DA + #A4ABBF #414757 #F8D6FC diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 989b5d0a..64973862 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -140,4 +140,10 @@ Pin the conversation? Pin Unpin + Outgoing call + Incoming call + Ended + Cancelled + Missed + Unknown