Improved contents update in file transfer chat message
This commit is contained in:
parent
df7f689501
commit
9a4600196a
3 changed files with 66 additions and 59 deletions
|
@ -36,8 +36,8 @@ import org.linphone.utils.FileUtils
|
||||||
import org.linphone.utils.ImageUtils
|
import org.linphone.utils.ImageUtils
|
||||||
|
|
||||||
class ChatMessageContentData(
|
class ChatMessageContentData(
|
||||||
val content: Content,
|
|
||||||
private val chatMessage: ChatMessage,
|
private val chatMessage: ChatMessage,
|
||||||
|
private val contentIndex: Int,
|
||||||
private val listener: OnContentClickedListener?
|
private val listener: OnContentClickedListener?
|
||||||
) {
|
) {
|
||||||
val isImage = MutableLiveData<Boolean>()
|
val isImage = MutableLiveData<Boolean>()
|
||||||
|
@ -68,6 +68,9 @@ class ChatMessageContentData(
|
||||||
return count == 1
|
return count == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isFileEncrypted: Boolean = false
|
||||||
|
private lateinit var content: Content
|
||||||
|
|
||||||
private val chatMessageListener: ChatMessageListenerStub = object : ChatMessageListenerStub() {
|
private val chatMessageListener: ChatMessageListenerStub = object : ChatMessageListenerStub() {
|
||||||
override fun onFileTransferProgressIndication(
|
override fun onFileTransferProgressIndication(
|
||||||
message: ChatMessage,
|
message: ChatMessage,
|
||||||
|
@ -75,31 +78,67 @@ class ChatMessageContentData(
|
||||||
offset: Int,
|
offset: Int,
|
||||||
total: Int
|
total: Int
|
||||||
) {
|
) {
|
||||||
if (message == chatMessage) {
|
if (c.filePath == content.filePath) {
|
||||||
if (c.filePath == content.filePath) {
|
val percent = offset * 100 / total
|
||||||
val percent = offset * 100 / total
|
Log.d("[Content] Download progress is: $offset / $total ($percent%)")
|
||||||
Log.d("[Content] Download progress is: $offset / $total ($percent%)")
|
|
||||||
downloadProgressInt.postValue(percent)
|
downloadProgressInt.value = percent
|
||||||
downloadProgressString.postValue("$percent%")
|
downloadProgressString.value = "$percent%"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMsgStateChanged(message: ChatMessage, state: ChatMessage.State) {
|
override fun onMsgStateChanged(message: ChatMessage, state: ChatMessage.State) {
|
||||||
downloadEnabled.postValue(chatMessage.state != ChatMessage.State.FileTransferInProgress)
|
downloadEnabled.value = state != ChatMessage.State.FileTransferInProgress
|
||||||
if (message == chatMessage) {
|
|
||||||
if (state == ChatMessage.State.FileTransferDone || state == ChatMessage.State.FileTransferError) {
|
if (state == ChatMessage.State.FileTransferDone || state == ChatMessage.State.FileTransferError) {
|
||||||
downloadProgressInt.value = 0
|
updateContent()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val isEncrypted = content.isFileEncrypted
|
|
||||||
|
|
||||||
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
updateContent()
|
||||||
|
chatMessage.addListener(chatMessageListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
scope.cancel()
|
||||||
|
|
||||||
|
val path = filePath.value.orEmpty()
|
||||||
|
if (path.isNotEmpty() && isFileEncrypted) {
|
||||||
|
Log.i("[Content] Deleting file used for preview: $path")
|
||||||
|
FileUtils.deleteFile(path)
|
||||||
|
filePath.value = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
chatMessage.removeListener(chatMessageListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun download() {
|
||||||
|
val filePath = content.filePath
|
||||||
|
if (content.isFileTransfer && (filePath == null || filePath.isEmpty())) {
|
||||||
|
val contentName = content.name
|
||||||
|
if (contentName != null) {
|
||||||
|
val file = FileUtils.getFileStoragePath(contentName)
|
||||||
|
content.filePath = file.path
|
||||||
|
downloadEnabled.value = false
|
||||||
|
|
||||||
|
Log.i("[Content] Started downloading $contentName into ${content.filePath}")
|
||||||
|
chatMessage.downloadContent(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun openFile() {
|
||||||
|
listener?.onContentClicked(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateContent() {
|
||||||
|
content = chatMessage.contents[contentIndex]
|
||||||
|
isFileEncrypted = content.isFileEncrypted
|
||||||
|
|
||||||
filePath.value = ""
|
filePath.value = ""
|
||||||
fileName.value = if (content.name.isNullOrEmpty() && !content.filePath.isNullOrEmpty()) {
|
fileName.value = if (content.name.isNullOrEmpty() && !content.filePath.isNullOrEmpty()) {
|
||||||
FileUtils.getNameFromFilePath(content.filePath!!)
|
FileUtils.getNameFromFilePath(content.filePath!!)
|
||||||
|
@ -151,39 +190,6 @@ class ChatMessageContentData(
|
||||||
downloadEnabled.value = !chatMessage.isFileTransferInProgress
|
downloadEnabled.value = !chatMessage.isFileTransferInProgress
|
||||||
downloadProgressInt.value = 0
|
downloadProgressInt.value = 0
|
||||||
downloadProgressString.value = "0%"
|
downloadProgressString.value = "0%"
|
||||||
chatMessage.addListener(chatMessageListener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun destroy() {
|
|
||||||
scope.cancel()
|
|
||||||
|
|
||||||
val path = filePath.value.orEmpty()
|
|
||||||
if (path.isNotEmpty() && isEncrypted) {
|
|
||||||
Log.i("[Content] Deleting file used for preview: $path")
|
|
||||||
FileUtils.deleteFile(path)
|
|
||||||
filePath.value = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
chatMessage.removeListener(chatMessageListener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun download() {
|
|
||||||
val filePath = content.filePath
|
|
||||||
if (content.isFileTransfer && (filePath == null || filePath.isEmpty())) {
|
|
||||||
val contentName = content.name
|
|
||||||
if (contentName != null) {
|
|
||||||
val file = FileUtils.getFileStoragePath(contentName)
|
|
||||||
content.filePath = file.path
|
|
||||||
downloadEnabled.value = false
|
|
||||||
|
|
||||||
Log.i("[Content] Started downloading $contentName into ${content.filePath}")
|
|
||||||
chatMessage.downloadContent(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun openFile() {
|
|
||||||
listener?.onContentClicked(content)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,12 +67,11 @@ class ChatMessageData(
|
||||||
time.value = TimestampUtils.toString(chatMessage.time)
|
time.value = TimestampUtils.toString(chatMessage.time)
|
||||||
updateChatMessageState(state)
|
updateChatMessageState(state)
|
||||||
|
|
||||||
// TODO FIXME : find a way to refresh outgoing message downloaded
|
if (state == ChatMessage.State.FileTransferDone) {
|
||||||
if (state == ChatMessage.State.FileTransferDone && !message.isOutgoing) {
|
|
||||||
Log.i("[Chat Message] File transfer done")
|
Log.i("[Chat Message] File transfer done")
|
||||||
updateContentsList()
|
if (!message.isOutgoing) {
|
||||||
|
coreContext.exportFilesInMessageToMediaStore(message)
|
||||||
coreContext.exportFilesInMessageToMediaStore(message)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,11 +149,13 @@ class ChatMessageData(
|
||||||
|
|
||||||
private fun updateContentsList() {
|
private fun updateContentsList() {
|
||||||
contents.value.orEmpty().forEach(ChatMessageContentData::destroy)
|
contents.value.orEmpty().forEach(ChatMessageContentData::destroy)
|
||||||
|
|
||||||
val list = arrayListOf<ChatMessageContentData>()
|
val list = arrayListOf<ChatMessageContentData>()
|
||||||
for (content in chatMessage.contents) {
|
|
||||||
|
val contentsList = chatMessage.contents
|
||||||
|
for (index in 0 until contentsList.size) {
|
||||||
|
val content = contentsList[index]
|
||||||
if (content.isFileTransfer || content.isFile) {
|
if (content.isFileTransfer || content.isFile) {
|
||||||
list.add(ChatMessageContentData(content, chatMessage, contentListener))
|
list.add(ChatMessageContentData(chatMessage, index, contentListener))
|
||||||
} else if (content.isText) {
|
} else if (content.isText) {
|
||||||
val spannable = Spannable.Factory.getInstance().newSpannable(content.utf8Text)
|
val spannable = Spannable.Factory.getInstance().newSpannable(content.utf8Text)
|
||||||
LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS)
|
LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS)
|
||||||
|
|
|
@ -56,7 +56,7 @@ class ContactsListViewModel : ViewModel() {
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getContactsList(): ArrayList<Contact> {
|
private fun getSelectedContactsList(): ArrayList<Contact> {
|
||||||
return if (sipContactsSelected.value == true) coreContext.contactsManager.sipContacts else coreContext.contactsManager.contacts
|
return if (sipContactsSelected.value == true) coreContext.contactsManager.sipContacts else coreContext.contactsManager.contacts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,11 +65,11 @@ class ContactsListViewModel : ViewModel() {
|
||||||
|
|
||||||
val filterValue = filter.value.orEmpty()
|
val filterValue = filter.value.orEmpty()
|
||||||
list = if (filterValue.isNotEmpty()) {
|
list = if (filterValue.isNotEmpty()) {
|
||||||
getContactsList().filter { contact ->
|
getSelectedContactsList().filter { contact ->
|
||||||
contact.fullName?.contains(filterValue, true) ?: false
|
contact.fullName?.contains(filterValue, true) ?: false
|
||||||
} as ArrayList<Contact>
|
} as ArrayList<Contact>
|
||||||
} else {
|
} else {
|
||||||
getContactsList()
|
getSelectedContactsList()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent blinking items when list hasn't changed
|
// Prevent blinking items when list hasn't changed
|
||||||
|
|
Loading…
Reference in a new issue