Show information when the recipient has read the message (#1430)

This commit is contained in:
Mateusz Idziejczak 2021-08-23 18:16:41 +02:00 committed by GitHub
parent aba2068a84
commit 2979d8b62a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 13 deletions

View File

@ -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)

View File

@ -33,7 +33,8 @@ class MessagePreviewAdapter @Inject constructor() :
private var attachments: List<MessageAttachment> = 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

View File

@ -78,6 +78,17 @@
app:layout_constraintTop_toBottomOf="@id/messagePreviewToLabel"
tools:text="@tools:sample/date/ddmmyy" />
<TextView
android:id="@+id/messagePreviewRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textColor="?android:textColorSecondary"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/messagePreviewDate"
tools:text="Read: Yes" />
<TextView
android:id="@+id/messagePreviewContent"
android:layout_width="0dp"
@ -88,6 +99,6 @@
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/messagePreviewDate"
app:layout_constraintTop_toBottomOf="@+id/messagePreviewRead"
tools:text="@tools:sample/lorem/random" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -101,7 +101,7 @@
<string name="grade_summary_predicted_grade">Predicted grade</string>
<string name="grade_summary_calculated_average">Calculated average</string>
<string name="grade_summary_final_average">Final average</string>
<string name="grade_summary_from_subjects">From %d of %d subjects</string>
<string name="grade_summary_from_subjects">From %1$d of %2$d subjects</string>
<string name="grade_menu_summary">Summary</string>
<string name="grade_menu_statistics">Class</string>
<string name="grade_menu_read">Mark as read</string>
@ -237,6 +237,11 @@
<string name="message_content_min_length">The message content must be at least 3 characters</string>
<string name="message_chip_only_unread">Only unread</string>
<string name="message_chip_only_with_attachments">Only with attachments</string>
<string name="message_read">Read: %s</string>
<plurals name="message_read_by">
<item quantity="one">Read by: %1$d of %2$d people</item>
<item quantity="other">Read by: %1$d of %2$d people</item>
</plurals>
<plurals name="message_number_item">
<item quantity="one">%d message</item>
<item quantity="other">%d messages</item>