From 1f00f426c78da61e4b969cecf36434caefdd8649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Mon, 30 May 2022 13:44:40 +0200 Subject: [PATCH] MessageSearchActivity: don't skip viewmodel when selecting message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- .../talk/messagesearch/MessageSearchActivity.kt | 17 ++++++++++------- .../messagesearch/MessageSearchViewModel.kt | 5 +++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt index 705f226cc..73aa57d50 100644 --- a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt @@ -118,6 +118,7 @@ class MessageSearchActivity : BaseActivity() { is MessageSearchViewModel.LoadedState -> showLoaded(state) MessageSearchViewModel.LoadingState -> showLoading() MessageSearchViewModel.ErrorState -> showError() + is MessageSearchViewModel.FinishedState -> onFinish() } } } @@ -169,9 +170,8 @@ class MessageSearchActivity : BaseActivity() { viewModel.loadMore() } MessageResultItem.VIEW_TYPE -> { - // TODO go through viewmodel val messageItem = item as MessageResultItem - finishWithResult(messageItem.messageEntry.messageId!!) + viewModel.selectMessage(messageItem.messageEntry) } } return false @@ -179,12 +179,15 @@ class MessageSearchActivity : BaseActivity() { }) } - private fun finishWithResult(messageId: String) { - val resultIntent = Intent().apply { - putExtra(RESULT_KEY_MESSAGE_ID, messageId) + private fun onFinish() { + val state = viewModel.state.value + if (state is MessageSearchViewModel.FinishedState) { + val resultIntent = Intent().apply { + putExtra(RESULT_KEY_MESSAGE_ID, state.selectedMessageId) + } + setResult(Activity.RESULT_OK, resultIntent) + finish() } - setResult(Activity.RESULT_OK, resultIntent) - finish() } private fun showInitial() { diff --git a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchViewModel.kt b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchViewModel.kt index 3ce532cfa..fce06e2c2 100644 --- a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchViewModel.kt @@ -58,6 +58,7 @@ class MessageSearchViewModel @Inject constructor(private val unifiedSearchReposi object EmptyState : ViewState() object ErrorState : ViewState() class LoadedState(val results: List, val hasMore: Boolean) : ViewState() + class FinishedState(val selectedMessageId: String) : ViewState() private lateinit var messageSearchHelper: MessageSearchHelper @@ -111,6 +112,10 @@ class MessageSearchViewModel @Inject constructor(private val unifiedSearchReposi query?.let { onQueryTextChange(it) } } + fun selectMessage(messageEntry: SearchMessageEntry) { + _state.value = FinishedState(messageEntry.messageId!!) + } + companion object { private val TAG = MessageSearchViewModel::class.simpleName private const val MIN_CHARS_FOR_SEARCH = 2