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