Skip to content

Commit

Permalink
Handle a situation when a thread not found.| #74
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 25, 2024
1 parent 5c39c1b commit 2a88f6e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package com.flowcrypt.email.jetpack.viewmodel

import android.app.Application
import android.content.Context
import androidx.lifecycle.viewModelScope
import com.flowcrypt.email.R
import com.flowcrypt.email.api.email.FoldersManager
import com.flowcrypt.email.api.email.JavaEmailConstants
import com.flowcrypt.email.api.email.MsgsCacheManager
Expand All @@ -31,6 +33,7 @@ import com.flowcrypt.email.ui.adapter.MessagesInThreadListAdapter
import com.flowcrypt.email.ui.adapter.MessagesInThreadListAdapter.Message
import com.flowcrypt.email.util.RecipientLookUpManager
import com.flowcrypt.email.util.coroutines.runners.ControlledRunner
import com.flowcrypt.email.util.exception.ThreadNotFoundException
import jakarta.mail.Message.RecipientType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -388,6 +391,12 @@ class ThreadDetailsViewModel(
) ?: error("Thread not found")

val threadInfo = thread.toThreadInfo(getApplication(), activeAccount)

if (!threadInfo.labels.contains(localFolder.fullName)) {
val context: Context = getApplication()
throw ThreadNotFoundException(context.getString(R.string.thread_was_deleted_or_moved))
}

//keep thread info updated in the local cache
roomDatabase.msgDao().updateSuspend(
MessageEntity.genMessageEntities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ import com.flowcrypt.email.ui.adapter.GmailApiLabelsListAdapter
import com.flowcrypt.email.ui.adapter.MessagesInThreadListAdapter
import com.flowcrypt.email.ui.adapter.recyclerview.itemdecoration.SkipFirstAndLastDividerItemDecoration
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.exception.ThreadNotFoundException
import com.google.api.client.googleapis.json.GoogleJsonResponseException
import org.apache.commons.io.FilenameUtils
import java.net.HttpURLConnection

/**
* @author Denys Bondarenko
Expand Down Expand Up @@ -471,6 +474,32 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), Prog
}

Result.Status.EXCEPTION -> {
val actionsForMissedThreadSituation = {
toast(getString(R.string.thread_was_deleted_or_moved))
navController?.navigateUp()
threadDetailsViewModel.deleteThread()
}

when (it.exception) {
is GoogleJsonResponseException -> if (it.exception.details.code == HttpURLConnection.HTTP_NOT_FOUND
&& it.exception.details.errors.any { errorInfo ->
errorInfo.reason.equals("notFound", true)
}
) {
if (isActive) {
actionsForMissedThreadSituation.invoke()
return@collect
}
}

is ThreadNotFoundException -> {
if (isActive) {
actionsForMissedThreadSituation.invoke()
return@collect
}
}
}

if (it.data?.silentUpdate == true) {
toast(it.exceptionMsg)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: denbond7
*/

package com.flowcrypt.email.util.exception

/**
* @author Denys Bondarenko
*/
class ThreadNotFoundException(message: String) : FlowCryptException(message)
1 change: 1 addition & 0 deletions FlowCrypt/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@
<string name="thread_was_deleted">Переписка удалена</string>
<string name="permissions_granted_and_now_you_can_download_attachments">Разрешения предоставлены и теперь Вы можете загружать вложения.</string>
<string name="delete_draft">Удалить черновик?</string>
<string name="thread_was_deleted_or_moved">Переписка удалена или перемещена</string>
<plurals name="drafts_count">
<item quantity="one" tools:ignore="ImpliedQuantity">Черновик</item>
<item quantity="few">Черновики(%1$d)</item>
Expand Down
1 change: 1 addition & 0 deletions FlowCrypt/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@
<string name="thread_was_deleted">Бесіду видалено</string>
<string name="permissions_granted_and_now_you_can_download_attachments">Дозволи надано, і тепер ви можете завантажити вкладені файли</string>
<string name="delete_draft">Видалити чернетку?</string>
<string name="thread_was_deleted_or_moved">Бесіду видалено або переміщено</string>
<plurals name="drafts_count">
<item quantity="one" tools:ignore="ImpliedQuantity">Чорновик</item>
<item quantity="few">Чорновики(%1$d)</item>
Expand Down
1 change: 1 addition & 0 deletions FlowCrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@
<string name="thread_was_deleted">The thread was deleted</string>
<string name="permissions_granted_and_now_you_can_download_attachments">The permissions have been granted, and now you can download attachments</string>
<string name="delete_draft">Delete draft?</string>
<string name="thread_was_deleted_or_moved">The thread was deleted or moved</string>
<plurals name="drafts_count">
<item quantity="one">Draft</item>
<item quantity="other">Drafts(%1$d)</item>
Expand Down

0 comments on commit 2a88f6e

Please sign in to comment.