From 2979d8b62ac47cc034028e34286dd7805930866b Mon Sep 17 00:00:00 2001 From: Mateusz Idziejczak Date: Mon, 23 Aug 2021 18:16:41 +0200 Subject: [PATCH] Show information when the recipient has read the message (#1430) --- .../data/repositories/MessageRepository.kt | 5 +- .../message/preview/MessagePreviewAdapter.kt | 52 ++++++++++++++++--- .../main/res/layout/item_message_preview.xml | 13 ++++- app/src/main/res/values/strings.xml | 7 ++- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/MessageRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/MessageRepository.kt index 2034000e..2d70e26e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/MessageRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/MessageRepository.kt @@ -20,14 +20,12 @@ import io.github.wulkanowy.data.pojos.MessageDraftJsonAdapter import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Folder import io.github.wulkanowy.sdk.pojo.SentMessage -import io.github.wulkanowy.ui.modules.message.send.RecipientChipItem import io.github.wulkanowy.utils.AutoRefreshHelper import io.github.wulkanowy.utils.getRefreshKey import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map import kotlinx.coroutines.sync.Mutex import timber.log.Timber import java.time.LocalDateTime.now @@ -79,8 +77,9 @@ class MessageRepository @Inject constructor( }, saveFetchResult = { old, (downloadedMessage, attachments) -> checkNotNull(old, { "Fetched message no longer exist!" }) - messagesDb.updateAll(listOf(old.message.copy(unread = !markAsRead).apply { + messagesDb.updateAll(listOf(old.message.apply { id = old.message.id + unread = !markAsRead content = content.ifBlank { downloadedMessage } })) messageAttachmentDao.insertAttachments(attachments) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewAdapter.kt index 206a7460..421453c9 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewAdapter.kt @@ -33,7 +33,8 @@ class MessagePreviewAdapter @Inject constructor() : private var attachments: List = emptyList() - override fun getItemCount() = if (messageWithAttachment == null) 0 else attachments.size + 1 + if (attachments.isNotEmpty()) 1 else 0 + override fun getItemCount() = + if (messageWithAttachment == null) 0 else attachments.size + 1 + if (attachments.isNotEmpty()) 1 else 0 override fun getItemViewType(position: Int) = when (position) { 0 -> ViewType.MESSAGE.id @@ -45,25 +46,60 @@ class MessagePreviewAdapter @Inject constructor() : val inflater = LayoutInflater.from(parent.context) return when (viewType) { - ViewType.MESSAGE.id -> MessageViewHolder(ItemMessagePreviewBinding.inflate(inflater, parent, false)) - ViewType.DIVIDER.id -> DividerViewHolder(ItemMessageDividerBinding.inflate(inflater, parent, false)) - ViewType.ATTACHMENT.id -> AttachmentViewHolder(ItemMessageAttachmentBinding.inflate(inflater, parent, false)) + ViewType.MESSAGE.id -> MessageViewHolder( + ItemMessagePreviewBinding.inflate(inflater, parent, false) + ) + ViewType.DIVIDER.id -> DividerViewHolder( + ItemMessageDividerBinding.inflate(inflater, parent, false) + ) + ViewType.ATTACHMENT.id -> AttachmentViewHolder( + ItemMessageAttachmentBinding.inflate(inflater, parent, false) + ) else -> throw IllegalStateException() } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder) { - is MessageViewHolder -> bindMessage(holder, requireNotNull(messageWithAttachment).message) - is AttachmentViewHolder -> bindAttachment(holder, requireNotNull(messageWithAttachment).attachments[position - 2]) + is MessageViewHolder -> bindMessage( + holder, + requireNotNull(messageWithAttachment).message + ) + is AttachmentViewHolder -> bindAttachment( + holder, + requireNotNull(messageWithAttachment).attachments[position - 2] + ) } } @SuppressLint("SetTextI18n") private fun bindMessage(holder: MessageViewHolder, message: Message) { + val context = holder.binding.root.context + val recipientCount = message.unreadBy + message.readBy + + val readText = when { + recipientCount > 1 -> { + context.resources.getQuantityString( + R.plurals.message_read_by, + message.readBy, + message.readBy, + recipientCount + ) + } + message.readBy == 1 -> { + context.getString(R.string.message_read, context.getString(R.string.all_yes)) + } + else -> context.getString(R.string.message_read, context.getString(R.string.all_no)) + } + with(holder.binding) { - messagePreviewSubject.text = message.subject.ifBlank { root.context.getString(R.string.message_no_subject) } - messagePreviewDate.text = root.context.getString(R.string.message_date, message.date.toFormattedString("yyyy-MM-dd HH:mm:ss")) + messagePreviewSubject.text = + message.subject.ifBlank { root.context.getString(R.string.message_no_subject) } + messagePreviewDate.text = root.context.getString( + R.string.message_date, + message.date.toFormattedString("yyyy-MM-dd HH:mm:ss") + ) + messagePreviewRead.text = readText messagePreviewContent.text = message.content messagePreviewFromSender.text = message.sender messagePreviewToRecipient.text = message.recipient diff --git a/app/src/main/res/layout/item_message_preview.xml b/app/src/main/res/layout/item_message_preview.xml index 83a4406b..c5dc2fa9 100644 --- a/app/src/main/res/layout/item_message_preview.xml +++ b/app/src/main/res/layout/item_message_preview.xml @@ -78,6 +78,17 @@ app:layout_constraintTop_toBottomOf="@id/messagePreviewToLabel" tools:text="@tools:sample/date/ddmmyy" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8b3f3b8e..b655769d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,7 +101,7 @@ Predicted grade Calculated average Final average - From %d of %d subjects + From %1$d of %2$d subjects Summary Class Mark as read @@ -237,6 +237,11 @@ The message content must be at least 3 characters Only unread Only with attachments + Read: %s + + Read by: %1$d of %2$d people + Read by: %1$d of %2$d people + %d message %d messages