Skip to content

Commit

Permalink
Merge pull request openedx#288 from touchapp/fix/discussion_search
Browse files Browse the repository at this point in the history
fix: search results are displayed on the search screen
  • Loading branch information
volodymyr-chekyrta authored Apr 24, 2024
2 parents 7dd29ef + c96d959 commit d5deabd
Showing 1 changed file with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.openedx.core.BaseViewModel
import org.openedx.core.R
import org.openedx.core.SingleEventLiveData
Expand All @@ -13,11 +24,6 @@ import org.openedx.core.system.ResourceManager
import org.openedx.discussion.domain.interactor.DiscussionInteractor
import org.openedx.discussion.system.notifier.DiscussionNotifier
import org.openedx.discussion.system.notifier.DiscussionThreadDataChanged
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.launch

class DiscussionSearchThreadViewModel(
private val interactor: DiscussionInteractor,
Expand Down Expand Up @@ -51,6 +57,7 @@ class DiscussionSearchThreadViewModel(
private var currentQuery: String? = null
private val threadsList = mutableListOf<org.openedx.discussion.domain.model.Thread>()
private var isLoading = false
private var loadNextJob: Job? = null

private val queryChannel = MutableSharedFlow<String>(replay = 0, extraBufferCapacity = 0)

Expand Down Expand Up @@ -84,26 +91,26 @@ class DiscussionSearchThreadViewModel(
queryChannel
.asSharedFlow()
.debounce(400)
.collect {
.collect { query ->
nextPage = 1
currentQuery = it
threadsList.clear()
_uiState.value = DiscussionSearchThreadUIState.Loading
loadThreadsInternal(currentQuery!!, nextPage!!)
if (query.isNotEmpty()) {
currentQuery = query
_uiState.value = DiscussionSearchThreadUIState.Loading
loadThreadsInternal(currentQuery!!, nextPage!!)
} else {
loadNextJob?.cancel()
currentQuery = null
_uiState.value = DiscussionSearchThreadUIState.Threads(emptyList(), 0)
_canLoadMore.value = false
}
}
}
}

fun searchThreads(query: String) {
viewModelScope.launch {
if (query.trim().isNotEmpty()) {
queryChannel.emit(query.trim())
} else {
currentQuery = null
nextPage = 1
threadsList.clear()
_uiState.value = DiscussionSearchThreadUIState.Threads(emptyList(), 0)
}
queryChannel.emit(query.trim())
}
}

Expand All @@ -126,10 +133,13 @@ class DiscussionSearchThreadViewModel(
}

private fun loadThreadsInternal(query: String, page: Int) {
viewModelScope.launch {
try {
loadNextJob?.cancel()
loadNextJob = flow {
emit(interactor.searchThread(courseId, query, page))
}
.cancellable()
.onEach { response ->
isLoading = true
val response = interactor.searchThread(courseId, query, page)
if (response.pagination.next.isNotEmpty() && page < response.pagination.numPages) {
_canLoadMore.value = true
nextPage = page + 1
Expand All @@ -139,20 +149,23 @@ class DiscussionSearchThreadViewModel(
}
threadsList.addAll(response.results)
_uiState.value =
DiscussionSearchThreadUIState.Threads(threadsList, response.pagination.count)
} catch (e: Exception) {
DiscussionSearchThreadUIState.Threads(
threadsList, response.pagination.count
)
isLoading = false
_isUpdating.value = false
}.catch { e ->
if (e.isInternetError()) {
_uiMessage.value =
UIMessage.SnackBarMessage(resourceManager.getString(R.string.core_error_no_connection))
} else {
_uiMessage.value =
UIMessage.SnackBarMessage(resourceManager.getString(R.string.core_error_unknown_error))
}
} finally {
isLoading = false
_isUpdating.value = false
}
}
.launchIn(viewModelScope)
}

}

0 comments on commit d5deabd

Please sign in to comment.