diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt index 98a9aa1d..ace8d547 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt @@ -74,6 +74,7 @@ import io.reactivex.Observable import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import kotlinx.android.synthetic.main.compose_activity.* +import kotlinx.android.synthetic.main.main_activity.toolbar import java.text.SimpleDateFormat import java.util.* import javax.inject.Inject @@ -265,6 +266,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { // Don't set the adapters unless needed if (state.editingMode && chips.adapter == null) chips.adapter = chipsAdapter + toolbar.menu.findItem(R.id.select_all)?.isVisible = !state.editingMode && (messageAdapter.itemCount > 1) && state.selectedMessages != 0 toolbar.menu.findItem(R.id.add)?.isVisible = state.editingMode toolbar.menu.findItem(R.id.call)?.isVisible = !state.editingMode && state.selectedMessages == 0 && state.query.isEmpty() @@ -274,6 +276,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { toolbar.menu.findItem(R.id.details)?.isVisible = !state.editingMode && state.selectedMessages == 1 toolbar.menu.findItem(R.id.delete)?.isVisible = !state.editingMode && state.selectedMessages > 0 toolbar.menu.findItem(R.id.forward)?.isVisible = !state.editingMode && state.selectedMessages == 1 + toolbar.menu.findItem(R.id.show_status)?.isVisible = !state.editingMode && state.selectedMessages > 0 toolbar.menu.findItem(R.id.previous)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty() toolbar.menu.findItem(R.id.next)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty() toolbar.menu.findItem(R.id.clear)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty() @@ -331,6 +334,14 @@ class ComposeActivity : QkThemedActivity(), ComposeView { override fun clearSelection() = messageAdapter.clearSelection() + override fun toggleSelectAll() { + messageAdapter.toggleSelectAll() + } + + override fun expandMessages(messageIds: List, expand: Boolean) { + messageAdapter.expandMessages(messageIds, expand) + } + override fun showDetails(details: String) { AlertDialog.Builder(this) .setTitle(R.string.compose_details_title) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt index 8c15d694..af37016d 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt @@ -58,6 +58,8 @@ interface ComposeView : QkView { val confirmDeleteIntent: Observable> fun clearSelection() + fun toggleSelectAll() + fun expandMessages(messageIds: List, expand: Boolean) fun showDetails(details: String) fun requestDefaultSms() fun requestStoragePermission() diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index 77eac25a..bf65a224 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -310,6 +310,12 @@ class ComposeViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe { newState { copy() } } + // toggle select all / select none + view.optionsItemIntent + .filter { it == R.id.select_all } + .autoDisposable(view.scope()) + .subscribe { view.toggleSelectAll() } + // Open the phone dialer if the call button is clicked view.optionsItemIntent .filter { it == R.id.call } @@ -369,15 +375,25 @@ class ComposeViewModel @Inject constructor( // Forward the message view.optionsItemIntent - .filter { it == R.id.forward } - .withLatestFrom(view.messagesSelectedIntent) { _, messages -> - messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message -> - val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() } - navigator.showCompose(message.getText(), images) - } + .filter { it == R.id.forward } + .withLatestFrom(view.messagesSelectedIntent) { _, messages -> + messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message -> + val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() } + navigator.showCompose(message.getText(), images) } - .autoDisposable(view.scope()) - .subscribe { view.clearSelection() } + } + .autoDisposable(view.scope()) + .subscribe { view.clearSelection() } + + // expand message to show additional info + view.optionsItemIntent + .filter { it == R.id.show_status } + .withLatestFrom(view.messagesSelectedIntent) { _, messages -> messages } + .autoDisposable(view.scope()) + .subscribe { messageIds -> + view.expandMessages(messageIds, true) + view.clearSelection() + } // Show the previous search result view.optionsItemIntent diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt index 2bbdd5a0..d8dd5061 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt @@ -21,7 +21,6 @@ package dev.octoshrimpy.quik.feature.compose import android.animation.ObjectAnimator import android.content.Context import android.graphics.Typeface -import android.os.Build import android.text.Layout import android.text.Spannable import android.text.SpannableString @@ -315,6 +314,10 @@ class MessagesAdapter @Inject constructor( }) } + override fun getItemId(position: Int): Long { + return getItem(position)?.id ?: -1 + } + override fun getItemViewType(position: Int): Int { val message = getItem(position) ?: return -1 return when (message.isMe()) { @@ -323,6 +326,35 @@ class MessagesAdapter @Inject constructor( } } + fun toggleSelectAll() { + var needToSelectAll = false + + // 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 + } + + // 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) + // else, selecting all, toggle if not already selected + else if (!isSelected(messageId)) + toggleSelection(messageId) + } + + notifyDataSetChanged() + } + + fun expandMessages(messageIds: List, expand: Boolean) { + messageIds.forEach { messageId -> expanded[messageId] = expand } + notifyDataSetChanged() + } + /** * Cache the contacts in a map by the address, because the messages we're binding don't have * a reference to the contact. diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt index 49a5ddad..ec9ff36d 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt @@ -130,4 +130,28 @@ class ConversationsAdapter @Inject constructor( override fun getItemViewType(position: Int): Int { return if (getItem(position)?.unread == false) 0 else 1 } + + fun toggleSelectAll() { + var needToSelectAll = false + + // 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 + } + + // select or deselect item based on if toggling all selected of deselected + for (position in 0 until itemCount) { + val threadId = getItemId(position) + // if deselecting all then toggle selection (we know all items are selected) + if (!needToSelectAll) + toggleSelection(threadId) + // else, selecting all, toggle if not already selected + else if (!isSelected(threadId)) + toggleSelection(threadId) + } + + notifyDataSetChanged() + } } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt index ab6ead29..a1601152 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt @@ -227,6 +227,7 @@ class MainActivity : QkThemedActivity(), MainView { toolbarSearch.setVisible(state.page is Inbox && state.page.selected == 0 || state.page is Searching) toolbarTitle.setVisible(toolbarSearch.visibility != View.VISIBLE) + toolbar.menu.findItem(R.id.select_all)?.isVisible = (conversationsAdapter.itemCount > 1) && selectedConversations != 0 toolbar.menu.findItem(R.id.archive)?.isVisible = state.page is Inbox && selectedConversations != 0 toolbar.menu.findItem(R.id.unarchive)?.isVisible = state.page is Archived && selectedConversations != 0 toolbar.menu.findItem(R.id.delete)?.isVisible = selectedConversations != 0 @@ -383,6 +384,10 @@ class MainActivity : QkThemedActivity(), MainView { conversationsAdapter.clearSelection() } + override fun toggleSelectAll() { + conversationsAdapter.toggleSelectAll() + } + override fun themeChanged() { recyclerView.scrapViews() } @@ -413,8 +418,12 @@ class MainActivity : QkThemedActivity(), MainView { changelogDialog.show(changelog) } - override fun showArchivedSnackbar() { - Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_LONG).apply { + override fun showArchivedSnackbar(countConversationsArchived: Int) { + Snackbar.make( + drawerLayout, + resources.getQuantityString(R.plurals.toast_archived, countConversationsArchived, countConversationsArchived), + if (countConversationsArchived < 10) Snackbar.LENGTH_LONG else Snackbar.LENGTH_INDEFINITE + ).apply { setAction(R.string.button_undo) { undoArchiveIntent.onNext(Unit) } setActionTextColor(colors.theme().theme) show() diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt index 81fef28a..154440ea 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt @@ -48,12 +48,13 @@ interface MainView : QkView { fun requestPermissions() fun clearSearch() fun clearSelection() + fun toggleSelectAll() fun themeChanged() fun showBlockingDialog(conversations: List, block: Boolean) fun showDeleteDialog(conversations: List) fun showRenameDialog(conversationName: String) fun showChangelog(changelog: ChangelogManager.CumulativeChangelog) - fun showArchivedSnackbar() + fun showArchivedSnackbar(countConversationsArchived: Int) } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt index fe8db8ca..5469b3ad 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt @@ -81,6 +81,7 @@ class MainViewModel @Inject constructor( private val syncContacts: SyncContacts, private val syncMessages: SyncMessages ) : QkViewModel(MainState(page = Inbox(data = conversationRepo.getConversations(prefs.unreadAtTop.get())))) { + private val lastArchivedThreadIds = ArrayList() init { disposables += deleteConversations @@ -330,10 +331,18 @@ class MainViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe() + view.optionsItemIntent + .filter { itemId -> itemId == R.id.select_all } + .autoDisposable(view.scope()) + .subscribe { view.toggleSelectAll() } + view.optionsItemIntent .filter { itemId -> itemId == R.id.archive } .withLatestFrom(view.conversationsSelectedIntent) { _, conversations -> markArchived.execute(conversations) + lastArchivedThreadIds.clear() + conversations.forEach { conversation -> lastArchivedThreadIds.add(conversation) } + view.showArchivedSnackbar(conversations.count()) view.clearSelection() } .autoDisposable(view.scope()) @@ -343,6 +352,7 @@ class MainViewModel @Inject constructor( .filter { itemId -> itemId == R.id.unarchive } .withLatestFrom(view.conversationsSelectedIntent) { _, conversations -> markUnarchived.execute(conversations) + view.showArchivedSnackbar(conversations.count()) view.clearSelection() } .autoDisposable(view.scope()) @@ -502,7 +512,12 @@ class MainViewModel @Inject constructor( .subscribe { (threadId, direction) -> val action = if (direction == ItemTouchHelper.RIGHT) prefs.swipeRight.get() else prefs.swipeLeft.get() when (action) { - Preferences.SWIPE_ACTION_ARCHIVE -> markArchived.execute(listOf(threadId)) { view.showArchivedSnackbar() } + Preferences.SWIPE_ACTION_ARCHIVE -> markArchived.execute(listOf(threadId)) + { + lastArchivedThreadIds.clear() + lastArchivedThreadIds.add(threadId) + view.showArchivedSnackbar(1) + } Preferences.SWIPE_ACTION_DELETE -> view.showDeleteDialog(listOf(threadId)) Preferences.SWIPE_ACTION_BLOCK -> view.showBlockingDialog(listOf(threadId), true) Preferences.SWIPE_ACTION_CALL -> conversationRepo.getConversation(threadId)?.recipients?.firstOrNull()?.address?.let(navigator::makePhoneCall) @@ -513,9 +528,11 @@ class MainViewModel @Inject constructor( } view.undoArchiveIntent - .withLatestFrom(view.swipeConversationIntent) { _, pair -> pair.first } .autoDisposable(view.scope()) - .subscribe { threadId -> markUnarchived.execute(listOf(threadId)) } + .subscribe { + markUnarchived.execute(lastArchivedThreadIds) + lastArchivedThreadIds.clear() + } view.snackbarButtonIntent .withLatestFrom(state) { _, state -> diff --git a/presentation/src/main/res/menu/compose.xml b/presentation/src/main/res/menu/compose.xml index c39f25ac..2fbb86c9 100644 --- a/presentation/src/main/res/menu/compose.xml +++ b/presentation/src/main/res/menu/compose.xml @@ -34,6 +34,13 @@ android:visible="false" app:showAsAction="always" /> + + + app:showAsAction="always" /> + app:showAsAction="always" /> + + + + + app:showAsAction="always" /> + app:showAsAction="always" /> + app:showAsAction="always" /> Open navigation drawer %d selected Clear + Select all Archive Unarchive Delete @@ -123,6 +124,7 @@ Can\'t send to other participants Copy text Forward + Show status Delete Previous Next @@ -428,7 +430,11 @@ Undo Copied - Archived conversation + + Archived conversation + Archived %d conversations + + You must unlock QUIK+ to use this