Merge pull request #368 from gavine99/improve-convo-select-all-and-copy-text-speed-342-1

speed up convo activity (de)select all
This commit is contained in:
octoshrimpy
2025-03-17 21:48:58 -04:00
committed by GitHub
4 changed files with 42 additions and 42 deletions
@@ -50,7 +50,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
private var selection = listOf<Long>()
private var selection = mutableListOf<Long>()
/**
* Mark this message as highlighted
@@ -69,20 +69,16 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
* If we are currently in selection mode (we have an active selection), then the state will
* toggle. If we are not in selection mode, then we will only toggle if [force]
*/
protected fun toggleSelection(
id: Long,
force: Boolean = true,
useChangeEvent: Boolean = true
): Boolean {
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
if (!force && selection.isEmpty()) return false
selection = when (selection.contains(id)) {
true -> selection - id
false -> selection + id
when (selection.contains(id)) {
true -> selection -= id
false -> selection += id
}
if (useChangeEvent)
selectionChanges.onNext(selection)
selectionChanges.onNext(selection)
return true
}
@@ -91,32 +87,21 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
}
fun clearSelection() {
selection = listOf()
selection.clear()
selectionChanges.onNext(selection)
notifyDataSetChanged()
}
fun toggleSelectAll() {
var needToSelectAll = false
val needToSelectAll = (selection.size != itemCount)
// if a non-selected item is found, then we need to select all, otherwise deselect all
for (position in 0 until itemCount)
if (!isSelected(getItemId(position))) {
needToSelectAll = true
break
selection.clear()
if (needToSelectAll) {
for (position in 0 until itemCount)
selection += getItemId(position)
}
// select or deselect item based on if toggling all selected of deselected
for (position in 0 until itemCount) {
val messageId = getItemId(position)
// if deselecting all then toggle selection (we know all items are selected)
if (!needToSelectAll)
toggleSelection(messageId, useChangeEvent = false)
// else, selecting all, toggle if not already selected
else if (!isSelected(messageId))
toggleSelection(messageId, useChangeEvent = false)
}
// fire a single change event now
selectionChanges.onNext(selection)
@@ -21,13 +21,25 @@ package dev.octoshrimpy.quik.common.util
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.Toast
import dev.octoshrimpy.quik.R
object ClipboardUtils {
fun copy(context: Context, string: String) {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("SMS", string)
clipboard.setPrimaryClip(clip)
try {
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
.setPrimaryClip(ClipData.newPlainText("SMS", string))
} catch (e: Exception) {
Toast.makeText(
context,
if ((e is RuntimeException) &&
e.message?.startsWith("android.os.TransactionTooLargeException") == true
) R.string.clipboard_too_large_to_copy
else R.string.clipboard_unable_to_copy_to,
Toast.LENGTH_LONG
).show()
}
}
}
@@ -381,18 +381,18 @@ class ComposeViewModel @Inject constructor(
.filter { it == R.id.copy }
.withLatestFrom(view.messagesSelectedIntent) { _, messageIds ->
val messages = messageIds.mapNotNull(messageRepo::getMessage).sortedBy { it.date }
val text = when (messages.size) {
1 -> messages.first().getText()
else -> messages.foldIndexed("") { index, acc, message ->
ClipboardUtils.copy(
context,
messages.foldIndexed(StringBuilder()) { index, acc, message ->
when {
index == 0 -> message.getText()
messages[index - 1].compareSender(message) -> "$acc\n${message.getText()}"
else -> "$acc\n\n${message.getText()}"
index == 0 ->
acc.append(message.getText())
messages[index - 1].compareSender(message) ->
acc.appendLine().append(message.getText())
else ->
acc.appendLine().appendLine().append(message.getText())
}
}
}
ClipboardUtils.copy(context, text)
}.toString())
}
.autoDisposable(view.scope())
.subscribe { view.clearSelection() }
@@ -237,6 +237,9 @@
<string name="backup_progress_finished">Finished!</string>
<string name="backup_notification_channel_name">Backup and restore</string>
<string name="clipboard_too_large_to_copy">Data size exceeds clipboard limit</string>
<string name="clipboard_unable_to_copy_to">Unable to copy to the clipboard</string>
<string name="scheduled_title">Scheduled</string>
<string name="scheduled_empty_description">Automatically send a message, at the exact moment you\'d like</string>
<string name="scheduled_empty_message_1">Hey! When was your birthday again?</string>