forked from upstream-mirrors/quik
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:
@@ -50,7 +50,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
|
|||||||
|
|
||||||
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
||||||
|
|
||||||
private var selection = listOf<Long>()
|
private var selection = mutableListOf<Long>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark this message as highlighted
|
* 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
|
* 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]
|
* toggle. If we are not in selection mode, then we will only toggle if [force]
|
||||||
*/
|
*/
|
||||||
protected fun toggleSelection(
|
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
|
||||||
id: Long,
|
|
||||||
force: Boolean = true,
|
|
||||||
useChangeEvent: Boolean = true
|
|
||||||
): Boolean {
|
|
||||||
if (!force && selection.isEmpty()) return false
|
if (!force && selection.isEmpty()) return false
|
||||||
|
|
||||||
selection = when (selection.contains(id)) {
|
when (selection.contains(id)) {
|
||||||
true -> selection - id
|
true -> selection -= id
|
||||||
false -> selection + id
|
false -> selection += id
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useChangeEvent)
|
selectionChanges.onNext(selection)
|
||||||
selectionChanges.onNext(selection)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,32 +87,21 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun clearSelection() {
|
fun clearSelection() {
|
||||||
selection = listOf()
|
selection.clear()
|
||||||
selectionChanges.onNext(selection)
|
selectionChanges.onNext(selection)
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toggleSelectAll() {
|
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
|
selection.clear()
|
||||||
for (position in 0 until itemCount)
|
|
||||||
if (!isSelected(getItemId(position))) {
|
if (needToSelectAll) {
|
||||||
needToSelectAll = true
|
for (position in 0 until itemCount)
|
||||||
break
|
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
|
// fire a single change event now
|
||||||
selectionChanges.onNext(selection)
|
selectionChanges.onNext(selection)
|
||||||
|
|
||||||
|
|||||||
@@ -21,13 +21,25 @@ package dev.octoshrimpy.quik.common.util
|
|||||||
import android.content.ClipData
|
import android.content.ClipData
|
||||||
import android.content.ClipboardManager
|
import android.content.ClipboardManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.widget.Toast
|
||||||
|
import dev.octoshrimpy.quik.R
|
||||||
|
|
||||||
object ClipboardUtils {
|
object ClipboardUtils {
|
||||||
|
|
||||||
fun copy(context: Context, string: String) {
|
fun copy(context: Context, string: String) {
|
||||||
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
try {
|
||||||
val clip = ClipData.newPlainText("SMS", string)
|
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
|
||||||
clipboard.setPrimaryClip(clip)
|
.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 }
|
.filter { it == R.id.copy }
|
||||||
.withLatestFrom(view.messagesSelectedIntent) { _, messageIds ->
|
.withLatestFrom(view.messagesSelectedIntent) { _, messageIds ->
|
||||||
val messages = messageIds.mapNotNull(messageRepo::getMessage).sortedBy { it.date }
|
val messages = messageIds.mapNotNull(messageRepo::getMessage).sortedBy { it.date }
|
||||||
val text = when (messages.size) {
|
ClipboardUtils.copy(
|
||||||
1 -> messages.first().getText()
|
context,
|
||||||
else -> messages.foldIndexed("") { index, acc, message ->
|
messages.foldIndexed(StringBuilder()) { index, acc, message ->
|
||||||
when {
|
when {
|
||||||
index == 0 -> message.getText()
|
index == 0 ->
|
||||||
messages[index - 1].compareSender(message) -> "$acc\n${message.getText()}"
|
acc.append(message.getText())
|
||||||
else -> "$acc\n\n${message.getText()}"
|
messages[index - 1].compareSender(message) ->
|
||||||
|
acc.appendLine().append(message.getText())
|
||||||
|
else ->
|
||||||
|
acc.appendLine().appendLine().append(message.getText())
|
||||||
}
|
}
|
||||||
}
|
}.toString())
|
||||||
}
|
|
||||||
|
|
||||||
ClipboardUtils.copy(context, text)
|
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { view.clearSelection() }
|
.subscribe { view.clearSelection() }
|
||||||
|
|||||||
@@ -237,6 +237,9 @@
|
|||||||
<string name="backup_progress_finished">Finished!</string>
|
<string name="backup_progress_finished">Finished!</string>
|
||||||
<string name="backup_notification_channel_name">Backup and restore</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_title">Scheduled</string>
|
||||||
<string name="scheduled_empty_description">Automatically send a message, at the exact moment you\'d like</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>
|
<string name="scheduled_empty_message_1">Hey! When was your birthday again?</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user