Skip to content

Commit

Permalink
Remove debounce and switch to rxSingle
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiagr committed Jul 28, 2023
1 parent 3650186 commit 0bd594e
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@ package au.com.shiftyjelly.pocketcasts.podcasts.helper.search
import au.com.shiftyjelly.pocketcasts.models.entity.Bookmark
import au.com.shiftyjelly.pocketcasts.repositories.bookmark.BookmarkManager
import io.reactivex.Observable
import io.reactivex.Single
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.rx2.rxSingle
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class BookmarkSearchHandler @Inject constructor(
private val bookmarkManager: BookmarkManager,
) : SearchHandler<Bookmark>() {
override val searchDebounce = 500L

override fun getSearchResultsObservable(podcastUuid: String): Observable<SearchResult> =
searchQueryRelay.debounce { // Only debounce when search has a value otherwise it slows down loading the pages
if (it.isEmpty()) {
Observable.empty()
} else {
Observable.timer(searchDebounce, TimeUnit.MILLISECONDS)
}
}.switchMapSingle { searchTerm ->
searchQueryRelay.switchMapSingle { searchTerm ->
if (searchTerm.length > 1) {
Single.just(bookmarkManager.searchInPodcastByTitle(podcastUuid, searchTerm))
rxSingle { bookmarkManager.searchInPodcastByTitle(podcastUuid, searchTerm) }
.map { SearchResult(searchTerm, it) }
.onErrorReturnItem(noSearchResult)
} else {
Single.just(noSearchResult)
rxSingle { noSearchResult }
}
}.distinctUntilChanged()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EpisodeSearchHandler @Inject constructor(
private val cacheServerManager: PodcastCacheServerManagerImpl,
private val analyticsTracker: AnalyticsTrackerWrapper,
) : SearchHandler<BaseEpisode>() {
override val searchDebounce = settings.getEpisodeSearchDebounceMs()
private val searchDebounce = settings.getEpisodeSearchDebounceMs()

override fun getSearchResultsObservable(podcastUuid: String): Observable<SearchResult> =
searchQueryRelay.debounce { // Only debounce when search has a value otherwise it slows down loading the pages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.reactivex.Observable

abstract class SearchHandler<T> {
private var searchTerm = ""
abstract val searchDebounce: Long
protected val searchQueryRelay = BehaviorRelay.create<String>()
.apply { accept("") }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class BookmarkDao {
AND (UPPER(bookmarks.title) LIKE UPPER(:title) OR UPPER(podcast_episodes.title) LIKE UPPER(:title))
AND deleted = :deleted"""
)
abstract fun searchInPodcastByTitle(
abstract suspend fun searchInPodcastByTitle(
podcastUuid: String,
title: String,
deleted: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ interface BookmarkManager {
suspend fun deleteSynced(bookmarkUuid: String)
suspend fun upsertSynced(bookmark: Bookmark): Bookmark
fun findBookmarksToSync(): List<Bookmark>
fun searchInPodcastByTitle(podcastUuid: String, title: String): List<String>
suspend fun searchInPodcastByTitle(podcastUuid: String, title: String): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class BookmarkManagerImpl @Inject constructor(
flowOf(helper.map { it.toBookmark() })
}

override fun searchInPodcastByTitle(podcastUuid: String, title: String) =
override suspend fun searchInPodcastByTitle(podcastUuid: String, title: String) =
bookmarkDao.searchInPodcastByTitle(podcastUuid, "%$title%").map { it.uuid }

/**
Expand Down

0 comments on commit 0bd594e

Please sign in to comment.