Added download progress bar + prevent downloading more than one content/message simultaneously

This commit is contained in:
Sylvain Berfini 2021-01-28 16:12:05 +01:00
parent 4c05ba2145
commit b3ebaae360
2 changed files with 39 additions and 8 deletions

View file

@ -27,6 +27,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.linphone.core.ChatMessage
import org.linphone.core.ChatMessageListenerStub
import org.linphone.core.Content
import org.linphone.core.tools.Log
import org.linphone.utils.AppUtils
@ -51,6 +52,8 @@ class ChatMessageContentViewModel(
val downloadEnabled = MutableLiveData<Boolean>()
val downloadProgress = MutableLiveData<Int>()
val isAlone: Boolean
get() {
var count = 0
@ -62,6 +65,25 @@ class ChatMessageContentViewModel(
return count == 1
}
private val chatMessageListener: ChatMessageListenerStub = object : ChatMessageListenerStub() {
override fun onFileTransferProgressIndication(
message: ChatMessage,
c: Content,
offset: Int,
total: Int
) {
val percent = offset * 100 / total
if (!c.filePath.isNullOrEmpty() && !content.filePath.isNullOrEmpty() && c.filePath == content.filePath) {
Log.d("[Content] Download progress is: $offset / $total ($percent%)")
downloadProgress.postValue(percent)
}
}
override fun onMsgStateChanged(message: ChatMessage, state: ChatMessage.State) {
downloadEnabled.postValue(chatMessage.state != ChatMessage.State.FileTransferInProgress)
}
}
init {
fileName.value = if (content.name.isNullOrEmpty() && !content.filePath.isNullOrEmpty()) {
FileUtils.getNameFromFilePath(content.filePath!!)
@ -94,18 +116,15 @@ class ChatMessageContentViewModel(
isAudio.value = false
}
} else {
if (chatMessage.isFileTransferInProgress) {
Log.i("[Content] Found content currently being downloaded: ${content.name}")
downloadEnabled.value = false
} else {
Log.i("[Content] Found downloadable content: ${content.name}")
downloadEnabled.value = true
}
downloadable.value = true
isImage.value = false
isVideo.value = false
isAudio.value = false
}
downloadEnabled.value = !chatMessage.isFileTransferInProgress
downloadProgress.value = 0
chatMessage.addListener(chatMessageListener)
}
fun download() {

View file

@ -86,6 +86,7 @@
android:orientation="horizontal"
android:gravity="center"
android:background="@drawable/resizable_assistant_button"
android:layout_margin="5dp"
android:visibility="@{data.downloadable ? View.VISIBLE : View.GONE}"
android:onClick="@{() -> data.download()}"
android:onLongClick="@{longClickListener}"
@ -119,7 +120,18 @@
android:layout_height="wrap_content"
android:textColor="@drawable/assistant_button_text_color"
android:layout_marginTop="5dp"
android:ellipsize="end" />
android:ellipsize="end"
android:visibility="@{data.downloadEnabled ? View.VISIBLE : View.GONE}" />
<ProgressBar
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="101"
android:layout_marginTop="5dp"
android:visibility="@{data.downloadProgress > 0 ? View.VISIBLE : View.GONE, default=gone}"
android:progress="@{data.downloadProgress, default=50}"/>
</LinearLayout>