From ea28fc783cf2c24e25606f5ba13dff120d1101ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Fri, 1 Mar 2024 21:14:43 +0100 Subject: [PATCH] Add message from trash restoring (#2438) --- .../wulkanowy/data/enums/MessageFolder.kt | 7 ++- .../data/repositories/MessageRepository.kt | 50 ++++++++++----- .../message/preview/MessagePreviewFragment.kt | 27 ++++---- .../preview/MessagePreviewPresenter.kt | 61 +++++++++++++++---- .../message/preview/MessagePreviewView.kt | 8 +-- .../message/send/SendMessagePresenter.kt | 2 +- .../modules/message/tab/MessageTabFragment.kt | 16 +++-- .../message/tab/MessageTabPresenter.kt | 23 ++++++- .../ic_menu_message_delete_forever.xml | 9 +++ .../res/drawable/ic_menu_message_restore.xml | 9 +++ .../res/menu/action_menu_message_preview.xml | 14 +++++ .../res/menu/context_menu_message_tab.xml | 14 +++++ app/src/main/res/values/strings.xml | 3 + 13 files changed, 192 insertions(+), 51 deletions(-) create mode 100644 app/src/main/res/drawable/ic_menu_message_delete_forever.xml create mode 100644 app/src/main/res/drawable/ic_menu_message_restore.xml diff --git a/app/src/main/java/io/github/wulkanowy/data/enums/MessageFolder.kt b/app/src/main/java/io/github/wulkanowy/data/enums/MessageFolder.kt index 899ba908..7cb4202a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/enums/MessageFolder.kt +++ b/app/src/main/java/io/github/wulkanowy/data/enums/MessageFolder.kt @@ -3,5 +3,10 @@ package io.github.wulkanowy.data.enums enum class MessageFolder(val id: Int = 1) { RECEIVED(1), SENT(2), - TRASHED(3) + TRASHED(3), + ; + + companion object { + fun byId(id: Int) = entries.first { it.id == id } + } } 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 6d591c5b..96f04870 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 @@ -18,6 +18,7 @@ import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.enums.MessageFolder import io.github.wulkanowy.data.enums.MessageFolder.RECEIVED +import io.github.wulkanowy.data.enums.MessageFolder.SENT import io.github.wulkanowy.data.enums.MessageFolder.TRASHED import io.github.wulkanowy.data.mappers.mapFromEntities import io.github.wulkanowy.data.mappers.mapToEntities @@ -25,6 +26,7 @@ import io.github.wulkanowy.data.networkBoundResource import io.github.wulkanowy.data.onResourceError import io.github.wulkanowy.data.onResourceSuccess import io.github.wulkanowy.data.pojos.MessageDraft +import io.github.wulkanowy.data.toFirstResult import io.github.wulkanowy.data.waitForResult import io.github.wulkanowy.domain.messages.GetMailboxByStudentUseCase import io.github.wulkanowy.sdk.Sdk @@ -34,7 +36,6 @@ import io.github.wulkanowy.utils.getRefreshKey import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.uniqueSubtract import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first import kotlinx.coroutines.sync.Mutex import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json @@ -155,17 +156,30 @@ class MessageRepository @Inject constructor( subject: String, content: String, recipients: List, - mailboxId: String, + mailbox: Mailbox, ) { sdk.init(student).sendMessage( subject = subject, content = content, recipients = recipients.mapFromEntities(), - mailboxId = mailboxId, + mailboxId = mailbox.globalKey, ) + refreshFolders(student, mailbox, listOf(SENT)) } - suspend fun deleteMessages(student: Student, mailbox: Mailbox?, messages: List) { + suspend fun restoreMessages(student: Student, mailbox: Mailbox?, messages: List) { + sdk.init(student).restoreMessages( + messages = messages.map { it.messageGlobalKey }, + ) + + refreshFolders(student, mailbox) + } + + suspend fun deleteMessage(student: Student, message: Message) { + deleteMessages(student, listOf(message)) + } + + suspend fun deleteMessages(student: Student, messages: List) { val firstMessage = messages.first() sdk.init(student).deleteMessages( messages = messages.map { it.messageGlobalKey }, @@ -184,18 +198,24 @@ class MessageRepository @Inject constructor( } messagesDb.updateAll(deletedMessages) - } else messagesDb.deleteAll(messages) - - getMessages( - student = student, - mailbox = mailbox, - folder = TRASHED, - forceRefresh = true, - ).first() + } else { + messagesDb.deleteAll(messages) + } } - suspend fun deleteMessage(student: Student, mailbox: Mailbox?, message: Message) { - deleteMessages(student, mailbox, listOf(message)) + private suspend fun refreshFolders( + student: Student, + mailbox: Mailbox?, + folders: List = MessageFolder.entries + ) { + folders.forEach { + getMessages( + student = student, + mailbox = mailbox, + folder = it, + forceRefresh = true, + ).toFirstResult() + } } suspend fun getMailboxes(student: Student, forceRefresh: Boolean) = networkBoundResource( @@ -240,7 +260,7 @@ class MessageRepository @Inject constructor( value?.let { json.encodeToString(it) } ) - suspend fun isMuted(author: String): Boolean { + private suspend fun isMuted(author: String): Boolean { return mutedMessageSendersDao.checkMute(author) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt index 3b33bb51..75778bac 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt @@ -44,8 +44,12 @@ class MessagePreviewFragment : private var menuForwardButton: MenuItem? = null + private var menuRestoreButton: MenuItem? = null + private var menuDeleteButton: MenuItem? = null + private var menuDeleteForeverButton: MenuItem? = null + private var menuShareButton: MenuItem? = null private var menuPrintButton: MenuItem? = null @@ -64,6 +68,9 @@ class MessagePreviewFragment : override val unmuteMessageSuccessString: String get() = getString(R.string.message_unmute_success) + override val restoreMessageSuccessString: String + get() = getString(R.string.message_restore_success) + override val messageNoSubjectString: String get() = getString(R.string.message_no_subject) @@ -111,7 +118,9 @@ class MessagePreviewFragment : inflater.inflate(R.menu.action_menu_message_preview, menu) menuReplyButton = menu.findItem(R.id.messagePreviewMenuReply) menuForwardButton = menu.findItem(R.id.messagePreviewMenuForward) + menuRestoreButton = menu.findItem(R.id.messagePreviewMenuRestore) menuDeleteButton = menu.findItem(R.id.messagePreviewMenuDelete) + menuDeleteForeverButton = menu.findItem(R.id.messagePreviewMenuDeleteForever) menuShareButton = menu.findItem(R.id.messagePreviewMenuShare) menuPrintButton = menu.findItem(R.id.messagePreviewMenuPrint) menuMuteButton = menu.findItem(R.id.messagePreviewMenuMute) @@ -124,7 +133,9 @@ class MessagePreviewFragment : return when (item.itemId) { R.id.messagePreviewMenuReply -> presenter.onReply() R.id.messagePreviewMenuForward -> presenter.onForward() + R.id.messagePreviewMenuRestore -> presenter.onMessageRestore() R.id.messagePreviewMenuDelete -> presenter.onMessageDelete() + R.id.messagePreviewMenuDeleteForever -> presenter.onMessageDelete() R.id.messagePreviewMenuShare -> presenter.onShare() R.id.messagePreviewMenuPrint -> presenter.onPrint() R.id.messagePreviewMenuMute -> presenter.onMute() @@ -152,23 +163,17 @@ class MessagePreviewFragment : binding.messagePreviewRecycler.visibility = if (show) VISIBLE else GONE } - override fun showOptions(show: Boolean, isReplayable: Boolean) { - menuReplyButton?.isVisible = isReplayable + override fun showOptions(show: Boolean, isReplayable: Boolean, isRestorable: Boolean) { + menuReplyButton?.isVisible = show && isReplayable menuForwardButton?.isVisible = show - menuDeleteButton?.isVisible = show + menuRestoreButton?.isVisible = show && isRestorable + menuDeleteButton?.isVisible = show && !isRestorable + menuDeleteForeverButton?.isVisible = show && isRestorable menuShareButton?.isVisible = show menuPrintButton?.isVisible = show menuMuteButton?.isVisible = show && isReplayable } - override fun setDeletedOptionsLabels() { - menuDeleteButton?.setTitle(R.string.message_delete_forever) - } - - override fun setNotDeletedOptionsLabels() { - menuDeleteButton?.setTitle(R.string.message_move_to_trash) - } - override fun showErrorView(show: Boolean) { binding.messagePreviewError.visibility = if (show) VISIBLE else GONE } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index 2eff245f..9bb0d32a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -14,9 +14,11 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.AnalyticsHelper import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject +import kotlin.time.Duration.Companion.seconds class MessagePreviewPresenter @Inject constructor( errorHandler: ErrorHandler, @@ -74,6 +76,7 @@ class MessagePreviewPresenter @Inject constructor( } } } else { + delay(1.seconds) view?.run { showMessage(messageNotExists) popView() @@ -172,13 +175,51 @@ class MessagePreviewPresenter @Inject constructor( return true } + private fun restoreMessage() { + val message = messageWithAttachments?.message ?: return + + view?.run { + showContent(false) + showProgress(true) + showOptions( + show = false, + isReplayable = false, + isRestorable = false, + ) + showErrorView(false) + } + Timber.i("Restore message ${message.messageGlobalKey}") + presenterScope.launch { + runCatching { + val student = studentRepository.getCurrentStudent(decryptPass = true) + val mailbox = messageRepository.getMailboxByStudent(student) + messageRepository.restoreMessages(student, mailbox, listOfNotNull(message)) + } + .onFailure { + retryCallback = { onMessageRestore() } + errorHandler.dispatch(it) + } + .onSuccess { + view?.run { + showMessage(restoreMessageSuccessString) + popView() + } + } + view?.showProgress(false) + } + } + private fun deleteMessage() { messageWithAttachments?.message ?: return view?.run { showContent(false) showProgress(true) - showOptions(show = false, isReplayable = false) + showOptions( + show = false, + isReplayable = false, + isRestorable = false, + ) showErrorView(false) } @@ -187,8 +228,7 @@ class MessagePreviewPresenter @Inject constructor( presenterScope.launch { runCatching { val student = studentRepository.getCurrentStudent(decryptPass = true) - val mailbox = messageRepository.getMailboxByStudent(student) - messageRepository.deleteMessage(student, mailbox, messageWithAttachments?.message!!) + messageRepository.deleteMessage(student, messageWithAttachments?.message!!) }.onFailure { retryCallback = { onMessageDelete() } errorHandler.dispatch(it) @@ -213,6 +253,11 @@ class MessagePreviewPresenter @Inject constructor( } } + fun onMessageRestore(): Boolean { + restoreMessage() + return true + } + fun onMessageDelete(): Boolean { deleteMessage() return true @@ -222,15 +267,9 @@ class MessagePreviewPresenter @Inject constructor( view?.apply { showOptions( show = messageWithAttachments?.message != null, - isReplayable = messageWithAttachments?.message?.folderId != MessageFolder.SENT.id, + isReplayable = messageWithAttachments?.message?.folderId == MessageFolder.RECEIVED.id, + isRestorable = messageWithAttachments?.message?.folderId == MessageFolder.TRASHED.id, ) - messageWithAttachments?.message?.let { - when (it.folderId == MessageFolder.TRASHED.id) { - true -> setDeletedOptionsLabels() - false -> setNotDeletedOptionsLabels() - } - } - } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt index cbe1c3cb..ee0b6ce0 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt @@ -13,6 +13,8 @@ interface MessagePreviewView : BaseView { val unmuteMessageSuccessString: String + val restoreMessageSuccessString: String + val messageNoSubjectString: String val printHTML: String @@ -35,11 +37,7 @@ interface MessagePreviewView : BaseView { fun setErrorRetryCallback(callback: () -> Unit) - fun showOptions(show: Boolean, isReplayable: Boolean) - - fun setDeletedOptionsLabels() - - fun setNotDeletedOptionsLabels() + fun showOptions(show: Boolean, isReplayable: Boolean, isRestorable: Boolean) fun openMessageReply(message: Message?) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt index e776e994..6155baea 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt @@ -203,7 +203,7 @@ class SendMessagePresenter @Inject constructor( subject = subject, content = content, recipients = recipients, - mailboxId = mailbox.globalKey, + mailbox = mailbox, ) }.logResourceStatus("sending message").onEach { when (it) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt index 4364e868..12f9d323 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt @@ -5,7 +5,9 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View -import android.view.View.* +import android.view.View.GONE +import android.view.View.INVISIBLE +import android.view.View.VISIBLE import android.widget.CompoundButton import androidx.annotation.StringRes import androidx.appcompat.view.ActionMode @@ -64,10 +66,12 @@ class MessageTabFragment : BaseFragment(R.layout.frag } override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { - if (presenter.folder == MessageFolder.TRASHED) { - val menuItem = menu.findItem(R.id.messageTabContextMenuDelete) - menuItem.setTitle(R.string.message_delete_forever) - } + val isTrashFolder = presenter.folder == MessageFolder.TRASHED + + menu.findItem(R.id.messageTabContextMenuDelete).setVisible(!isTrashFolder) + menu.findItem(R.id.messageTabContextMenuDeleteForever).setVisible(isTrashFolder) + menu.findItem(R.id.messageTabContextMenuRestore).setVisible(isTrashFolder) + return presenter.onPrepareActionMode() } @@ -79,6 +83,8 @@ class MessageTabFragment : BaseFragment(R.layout.frag override fun onActionItemClicked(mode: ActionMode, menu: MenuItem): Boolean { when (menu.itemId) { R.id.messageTabContextMenuDelete -> presenter.onActionModeSelectDelete() + R.id.messageTabContextMenuRestore -> presenter.onActionModeSelectRestore() + R.id.messageTabContextMenuDeleteForever -> presenter.onActionModeSelectDelete() R.id.messageTabContextMenuSelectAll -> presenter.onActionModeSelectCheckAll() } return true diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index f8283721..cda0b32b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -121,8 +121,27 @@ class MessageTabPresenter @Inject constructor( return true } + fun onActionModeSelectRestore() { + Timber.i("Restore ${messagesToDelete.size} messages") + val messageList = messagesToDelete.toList() + + presenterScope.launch { + view?.run { + showProgress(true) + showContent(false) + showActionMode(false) + } + runCatching { + val student = studentRepository.getCurrentStudent(true) + messageRepository.restoreMessages(student, selectedMailbox, messageList) + } + .onFailure(errorHandler::dispatch) + .onSuccess { view?.showMessage(R.string.message_messages_restored) } + } + } + fun onActionModeSelectDelete() { - Timber.i("Delete ${messagesToDelete.size} messages)") + Timber.i("Delete ${messagesToDelete.size} messages") val messageList = messagesToDelete.toList() presenterScope.launch { @@ -134,7 +153,7 @@ class MessageTabPresenter @Inject constructor( runCatching { val student = studentRepository.getCurrentStudent(true) - messageRepository.deleteMessages(student, selectedMailbox, messageList) + messageRepository.deleteMessages(student, messageList) } .onFailure(errorHandler::dispatch) .onSuccess { view?.showMessage(R.string.message_messages_deleted) } diff --git a/app/src/main/res/drawable/ic_menu_message_delete_forever.xml b/app/src/main/res/drawable/ic_menu_message_delete_forever.xml new file mode 100644 index 00000000..a7b5ac53 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_message_delete_forever.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu_message_restore.xml b/app/src/main/res/drawable/ic_menu_message_restore.xml new file mode 100644 index 00000000..5c8544f2 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_message_restore.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/action_menu_message_preview.xml b/app/src/main/res/menu/action_menu_message_preview.xml index 22478867..04af8671 100644 --- a/app/src/main/res/menu/action_menu_message_preview.xml +++ b/app/src/main/res/menu/action_menu_message_preview.xml @@ -29,6 +29,13 @@ android:title="@string/message_forward" app:iconTint="@color/material_on_surface_emphasis_medium" app:showAsAction="ifRoom" /> + + + + Forward Select all Unselect all + Restore from trash Move to trash Delete permanently + Message restored successfully Message deleted successfully student parent @@ -364,6 +366,7 @@ %1$d selected Messages deleted + Messages restored Choose mailbox Incognito mode is on Thanks to incognito mode sender is not notified when you read the message