Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/src/main/java/com/anindra/messages/ui/BulkDelete.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.anindra.messages.ui

/**
* What deleting a multi-conversation selection actually does (#610).
*
* The batch goes through the same view-model call as a single chat's delete, so
* it follows the permanent-delete setting. The selection bar must not promise a
* Trash move and an Undo it cannot deliver, which is what it did while this
* decision lived only in the snackbar code.
*/
object BulkDelete {

/** True when the batch can still be restored from Trash afterwards. */
fun undoable(permanentDeleteEnabled: Boolean): Boolean = !permanentDeleteEnabled
}
30 changes: 20 additions & 10 deletions app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,15 @@ fun ConversationsScreen(
showing?.cancelAndJoin()
val entry = undoStack.peek() ?: continue
showing = launch {
val undo = entry.undo
val result = snackbarHostState.showSnackbar(
message = entry.message,
actionLabel = undoLabel,
actionLabel = if (undo == null) null else undoLabel,
duration = SnackbarDuration.Long
)
if (result == SnackbarResult.ActionPerformed) {
if (undo != null && result == SnackbarResult.ActionPerformed) {
undoStack.pop()
entry.undo()
undo()
// Reveal the next entry so repeated Undo keeps walking back.
undoRequests.trySend(Unit)
} else {
Expand All @@ -359,6 +360,12 @@ fun ConversationsScreen(
undoRequests.trySend(Unit)
}

/** Reports a result that has nothing to undo, so no action button is drawn. */
fun showNotice(message: String) {
undoStack.push(UndoEntry(message, null))
undoRequests.trySend(Unit)
}

fun moveToTrash(convo: Conversation) {
if (vm.settings.permanentDeleteEnabled) {
if (vm.settings.permanentDeleteWarn) {
Expand Down Expand Up @@ -470,16 +477,19 @@ fun ConversationsScreen(
selectedIds = ConversationSelection.toggle(selectedIds, convo.id)
}

/** Bulk delete is always recoverable: it moves to Trash with one undo for
* the whole batch. Permanent deletion stays a per-conversation choice,
* because its confirmation dialog names the single thread it will erase. */
/** The batch follows the permanent-delete setting like any other delete, so
* it may only offer an Undo when it really went to Trash (#610). */
fun trashSelection() {
val ids = ConversationSelection.trashTargets(selectedIds, displayed.map { it.id })
if (ids.isEmpty()) return
selectedIds = emptySet()
ids.forEach { vm.deleteConversation(it) }
showUndo(context.getString(R.string.convo_moved_to_trash_count, ids.size)) {
ids.forEach { vm.restoreFromTrash(it) }
if (BulkDelete.undoable(vm.settings.permanentDeleteEnabled)) {
showUndo(context.getString(R.string.convo_moved_to_trash_count, ids.size)) {
ids.forEach { vm.restoreFromTrash(it) }
}
} else {
showNotice(context.getString(R.string.convo_deleted_count, ids.size))
}
}

Expand Down Expand Up @@ -1492,8 +1502,8 @@ internal fun StartChatFab(
}


/** One recoverable inbox action; [message] is what the snackbar says it did. */
private class UndoEntry(val message: String, val undo: () -> Unit)
/** One inbox result; [undo] is null when the action cannot be taken back. */
private class UndoEntry(val message: String, val undo: (() -> Unit)?)


/** Home-search scopes (#394). All/Unread filter conversations; Starred lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,29 @@ fun ConversationsScreen(
selectedIds = com.anindra.messages.ui.ConversationSelection.toggle(selectedIds, convo.id)
}

/** Bulk delete is always recoverable: it moves to Trash with one undo for the
* whole batch. Permanent deletion stays a per-conversation choice. */
/** The batch follows the permanent-delete setting like any other delete, so
* it may only offer an Undo when it really went to Trash (#610). */
fun trashSelection() {
val ids = com.anindra.messages.ui.ConversationSelection
.trashTargets(selectedIds, displayed.map { it.id })
if (ids.isEmpty()) return
selectedIds = emptySet()
ids.forEach { vm.deleteConversation(it) }
val undoable = com.anindra.messages.ui.BulkDelete
.undoable(vm.settings.permanentDeleteEnabled)
scope.launch {
val result = snackbarHostState.showSnackbar(
message = context.getString(R.string.convo_moved_to_trash_count, ids.size),
actionLabel = context.getString(R.string.action_undo),
message = context.getString(
if (undoable) R.string.convo_moved_to_trash_count
else R.string.convo_deleted_count,
ids.size
),
actionLabel = if (undoable) context.getString(R.string.action_undo) else null,
duration = SnackbarDuration.Long
)
if (result == SnackbarResult.ActionPerformed) ids.forEach { vm.restoreFromTrash(it) }
if (undoable && result == SnackbarResult.ActionPerformed) {
ids.forEach { vm.restoreFromTrash(it) }
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">تم الحفظ في Downloads/Messages</string>
<string name="crash_report_clip_label">تقرير أعطال تطبيق Messages</string>
<string name="convo_moved_to_trash_count">تم نقل %1$d محادثات إلى سلة المحذوفات</string>
<string name="convo_deleted_count">تم حذف %1$d محادثات</string>
<string name="convo_archived_count">تمت أرشفة %1$d محادثات</string>
<string name="sheet_multi_select">تحديد المزيد</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="crash_report_saved">In Downloads/Messages gespeichert</string>
<string name="crash_report_clip_label">Absturzbericht von Messages</string>
<string name="convo_moved_to_trash_count">%1$d Unterhaltungen in den Papierkorb verschoben</string>
<string name="convo_deleted_count">%1$d Unterhaltungen gelöscht</string>
<string name="convo_archived_count">%1$d Unterhaltungen archiviert</string>
<string name="sheet_multi_select">Mehr auswählen</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Guardado en Downloads/Messages</string>
<string name="crash_report_clip_label">Informe de errores de Messages</string>
<string name="convo_moved_to_trash_count">%1$d conversaciones movidas a la papelera</string>
<string name="convo_deleted_count">%1$d conversaciones eliminadas</string>
<string name="convo_archived_count">%1$d conversaciones archivadas</string>
<string name="sheet_multi_select">Seleccionar más</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fa/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<string name="crash_report_saved">در Downloads/Messages ذخیره شد</string>
<string name="crash_report_clip_label">گزارش خرابی پیام‌ها</string>
<string name="convo_moved_to_trash_count">%1$d گفتگو به سطل زباله منتقل شد</string>
<string name="convo_deleted_count">%1$d گفتگو حذف شد</string>
<string name="convo_archived_count">%1$d گفتگو بایگانی شد</string>
<string name="sheet_multi_select">انتخاب بیشتر</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="crash_report_saved">Enregistré dans Downloads/Messages</string>
<string name="crash_report_clip_label">Rapport d\'incident de Messages</string>
<string name="convo_moved_to_trash_count">%1$d conversations déplacées dans la corbeille</string>
<string name="convo_deleted_count">%1$d conversations supprimées</string>
<string name="convo_archived_count">%1$d conversations archivées</string>
<string name="sheet_multi_select">Sélectionner plus</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-hi/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Downloads/Messages में सहेजा गया</string>
<string name="crash_report_clip_label">Messages क्रैश रिपोर्ट</string>
<string name="convo_moved_to_trash_count">%1$d संवाद कूड़े में ले जाए गए</string>
<string name="convo_deleted_count">%1$d संवाद हटाए गए</string>
<string name="convo_archived_count">%1$d संवाद संग्रहीत किए गए</string>
<string name="sheet_multi_select">और चुनें</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Downloads/Messages に保存しました</string>
<string name="crash_report_clip_label">Messages のクラッシュレポート</string>
<string name="convo_moved_to_trash_count">%1$d 件の会話をゴミ箱に移動しました</string>
<string name="convo_deleted_count">%1$d 件の会話を削除しました</string>
<string name="convo_archived_count">%1$d 件の会話をアーカイブしました</string>
<string name="sheet_multi_select">さらに選択</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ko/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Downloads/Messages에 저장했습니다</string>
<string name="crash_report_clip_label">Messages 크래시 보고서</string>
<string name="convo_moved_to_trash_count">%1$d개의 대화가 휴지통으로 이동되었습니다</string>
<string name="convo_deleted_count">%1$d개의 대화가 삭제되었습니다</string>
<string name="convo_archived_count">%1$d개의 대화가 보관되었습니다</string>
<string name="sheet_multi_select">더 선택</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pl/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Zapisano w Downloads/Messages</string>
<string name="crash_report_clip_label">Raport awarii Messages</string>
<string name="convo_moved_to_trash_count">Przeniesiono %1$d rozmów do kosza</string>
<string name="convo_deleted_count">Usunięto %1$d rozmów</string>
<string name="convo_archived_count">Zarchiwizowano %1$d rozmów</string>
<string name="sheet_multi_select">Wybierz więcej</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Salvo em Downloads/Messages</string>
<string name="crash_report_clip_label">Relatório de falha do Messages</string>
<string name="convo_moved_to_trash_count">%1$d conversas movidas para a lixeira</string>
<string name="convo_deleted_count">%1$d conversas excluídas</string>
<string name="convo_archived_count">%1$d conversas arquivadas</string>
<string name="sheet_multi_select">Selecionar mais</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<string name="crash_report_saved">Сохранено в Downloads/Messages</string>
<string name="crash_report_clip_label">Отчёт о сбое Messages</string>
<string name="convo_moved_to_trash_count">%1$d переписок перемещено в корзину</string>
<string name="convo_deleted_count">%1$d переписок удалено</string>
<string name="convo_archived_count">%1$d переписок заархивировано</string>
<string name="sheet_multi_select">Выбрать ещё</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="crash_report_saved">已保存到 Downloads/Messages</string>
<string name="crash_report_clip_label">Messages 崩溃报告</string>
<string name="convo_moved_to_trash_count">%1$d 个对话已移至回收站</string>
<string name="convo_deleted_count">%1$d 个对话已删除</string>
<string name="convo_archived_count">%1$d 个对话已封存</string>
<string name="sheet_multi_select">选择更多</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="crash_report_saved">已儲存至 Downloads/Messages</string>
<string name="crash_report_clip_label">Messages 當機報告</string>
<string name="convo_moved_to_trash_count">%1$d 個對話已移至垃圾桶</string>
<string name="convo_deleted_count">%1$d 個對話已刪除</string>
<string name="convo_archived_count">%1$d 個對話已封存</string>
<string name="sheet_multi_select">選擇更多</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<string name="crash_report_saved">Saved to Downloads/Messages</string>
<string name="crash_report_clip_label">Messages crash report</string>
<string name="convo_moved_to_trash_count">%1$d conversations moved to trash</string>
<string name="convo_deleted_count">%1$d conversations deleted</string>
<string name="convo_archived_count">%1$d conversations archived</string>
<string name="sheet_multi_select">Select more</string>
</resources>
23 changes: 23 additions & 0 deletions app/src/test/java/com/anindra/messages/ui/BulkDeleteTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.anindra.messages.ui

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
* #610: the selection bar promised "moved to trash" and an Undo after a batch
* delete that the permanent-delete setting had erased. The batch follows the
* setting, so the bar's message and its Undo must follow it too.
*/
class BulkDeleteTest {

@Test
fun aDefaultDeleteStillGoesToTrashAndCanBeUndone() {
assertTrue(BulkDelete.undoable(permanentDeleteEnabled = false))
}

@Test
fun permanentDeleteErasesTheBatchSoThereIsNothingToUndo() {
assertFalse(BulkDelete.undoable(permanentDeleteEnabled = true))
}
}
48 changes: 48 additions & 0 deletions app/src/test/java/com/anindra/messages/ui/BulkDeleteWiringTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.anindra.messages.ui

import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

/**
* #610 both conversation lists carried the same unconditional "moved to trash"
* snackbar, so the fix has to land in both. Reading the source keeps a rename
* from silently restoring the lie on one of them.
*/
class BulkDeleteWiringTest {

private fun read(path: String): String {
val main = generateSequence(File("").absoluteFile) { it.parentFile }
.map { File(it, "app/src/main") }
.firstOrNull { File(it, "java").isDirectory }
?: error("app/src/main not found")
return File(main, "java/com/anindra/messages/ui/$path").readText()
}

private fun selectionBody(source: String): String =
Regex("fun trashSelection\\(\\) \\{([\\s\\S]*?)\\n \\}")
.find(source)?.groupValues?.get(1)
?: error("trashSelection not found")

@Test
fun theRedesignedListDecidesBeforePromisingAnUndo() {
val body = selectionBody(read("ConversationsScreen.kt"))
assertTrue("bulk delete must ask BulkDelete", body.contains("BulkDelete.undoable("))
assertTrue(
"an erased batch must not be called a Trash move",
body.contains("R.string.convo_deleted_count") &&
body.contains("R.string.convo_moved_to_trash_count")
)
}

@Test
fun theLegacyListDecidesBeforePromisingAnUndo() {
val body = selectionBody(read("legacy/LegacyConversationsScreen.kt"))
assertTrue("bulk delete must ask BulkDelete", body.contains("BulkDelete"))
assertTrue(
"an erased batch must not be called a Trash move",
body.contains("R.string.convo_deleted_count") &&
body.contains("R.string.convo_moved_to_trash_count")
)
}
}