MessageSearchActivity: don't skip viewmodel when selecting message

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-05-30 13:44:40 +02:00
parent 0d21ce4f17
commit 1f00f426c7
No known key found for this signature in database
GPG key ID: 2585783189A62105
2 changed files with 15 additions and 7 deletions

View file

@ -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() {

View file

@ -58,6 +58,7 @@ class MessageSearchViewModel @Inject constructor(private val unifiedSearchReposi
object EmptyState : ViewState()
object ErrorState : ViewState()
class LoadedState(val results: List<SearchMessageEntry>, 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