From bed6ef764f37445640e0f0d6e9f8a978ca2764ac Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 5 Apr 2020 19:46:37 +0200 Subject: [PATCH] allow copying a message value into the clipboard easily --- app/build.gradle | 2 +- .../smsmessenger/adapters/ThreadAdapter.kt | 16 ++++++++++++++-- app/src/main/res/menu/cab_thread.xml | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/menu/cab_thread.xml diff --git a/app/build.gradle b/app/build.gradle index 8f419662..6df03903 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,7 +36,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.24.15' + implementation 'com.simplemobiletools:commons:5.24.16' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4' implementation 'org.greenrobot:eventbus:3.2.0' } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index 3bac542e..5ebb2da7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -31,9 +31,13 @@ class ThreadAdapter( setupDragListener(true) } - override fun getActionMenuId() = R.menu.cab_messages + override fun getActionMenuId() = R.menu.cab_thread - override fun prepareActionMode(menu: Menu) {} + override fun prepareActionMode(menu: Menu) { + menu.apply { + findItem(R.id.cab_copy_to_clipboard).isVisible = isOneItemSelected() + } + } override fun actionItemPressed(id: Int) { if (selectedKeys.isEmpty()) { @@ -41,6 +45,7 @@ class ThreadAdapter( } when (id) { + R.id.cab_copy_to_clipboard -> copyToClipboard() R.id.cab_select_all -> selectAll() R.id.cab_delete -> askConfirmDelete() } @@ -92,6 +97,11 @@ class ThreadAdapter( } } + private fun copyToClipboard() { + val firstItem = getSelectedItems().first() as? Message ?: return + activity.copyToClipboard(firstItem.body) + } + private fun askConfirmDelete() { val itemsCnt = selectedKeys.size val items = resources.getQuantityString(R.plurals.delete_messages, itemsCnt, itemsCnt) @@ -128,6 +138,8 @@ class ThreadAdapter( } } + private fun getSelectedItems() = messages.filter { selectedKeys.contains((it as? Message)?.id ?: 0) } as ArrayList + private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime private fun setupView(view: View, message: Message) { diff --git a/app/src/main/res/menu/cab_thread.xml b/app/src/main/res/menu/cab_thread.xml new file mode 100644 index 00000000..ce1a7e27 --- /dev/null +++ b/app/src/main/res/menu/cab_thread.xml @@ -0,0 +1,19 @@ + + + + + +