-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use paging library in SearchFragment.
- Loading branch information
1 parent
33a265b
commit ba477dc
Showing
7 changed files
with
194 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.schabi.newpipe.fragments.list.search | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import org.schabi.newpipe.extractor.Page | ||
import org.schabi.newpipe.extractor.search.SearchInfo | ||
import org.schabi.newpipe.util.ExtractorHelper | ||
|
||
class SearchPagingSource( | ||
private val serviceId: Int, | ||
private val query: String, | ||
private val contentFilter: List<String>, | ||
private val sortFilter: String, | ||
) : PagingSource<Page, SearchInfo>() { | ||
override fun getRefreshKey(state: PagingState<Page, SearchInfo>): Page? { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, SearchInfo> { | ||
TODO("Not yet implemented") | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchRemoteMediator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.schabi.newpipe.fragments.list.search | ||
|
||
import androidx.paging.ExperimentalPagingApi | ||
import androidx.paging.LoadType | ||
import androidx.paging.PagingState | ||
import androidx.paging.RemoteMediator | ||
import org.schabi.newpipe.extractor.Page | ||
import org.schabi.newpipe.extractor.search.SearchInfo | ||
|
||
@OptIn(ExperimentalPagingApi::class) | ||
class SearchRemoteMediator( | ||
private val serviceId: Int, | ||
private val query: String, | ||
private val contentFilter: List<String>, | ||
private val sortFilter: String | ||
) : RemoteMediator<Page, SearchInfo>() { | ||
override suspend fun load( | ||
loadType: LoadType, | ||
state: PagingState<Page, SearchInfo> | ||
): MediatorResult { | ||
TODO("Not yet implemented") | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.schabi.newpipe.fragments.list.search | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import kotlinx.coroutines.flow.Flow | ||
import org.schabi.newpipe.extractor.search.SearchInfo | ||
|
||
fun getSearchResultStream( | ||
serviceId: Int, query: String, contentFilter: List<String>, sortFilter: String | ||
): Flow<PagingData<SearchInfo>> { | ||
return Pager( | ||
config = PagingConfig(pageSize = 50, enablePlaceholders = false), | ||
pagingSourceFactory = { SearchPagingSource(serviceId, query, contentFilter, sortFilter) } | ||
).flow | ||
} |
101 changes: 101 additions & 0 deletions
101
app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.schabi.newpipe.fragments.list.search | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import io.reactivex.rxjava3.core.Observable | ||
import io.reactivex.rxjava3.core.Single | ||
import io.reactivex.rxjava3.subjects.PublishSubject | ||
import org.schabi.newpipe.App | ||
import org.schabi.newpipe.local.history.HistoryRecordManager | ||
import org.schabi.newpipe.util.ExtractorHelper | ||
import java.util.concurrent.TimeUnit | ||
|
||
class SearchViewModel( | ||
application: Application, | ||
private val serviceId: Int, | ||
private val showLocalSuggestions: Boolean, | ||
private val showRemoteSuggestions: Boolean | ||
) : ViewModel() { | ||
private val historyRecordManager = HistoryRecordManager(application) | ||
private val suggestionPublisher = PublishSubject.create<String>() | ||
|
||
private val suggestionMutableLiveData = MutableLiveData<SuggestionItemResponse>() | ||
val suggestionLiveData: LiveData<SuggestionItemResponse> get() = suggestionMutableLiveData | ||
|
||
private val suggestionDisposable = suggestionPublisher | ||
.debounce(SUGGESTIONS_DEBOUNCE, TimeUnit.MILLISECONDS) | ||
.switchMap { query: String -> | ||
// Only show remote suggestions if they are enabled in settings and | ||
// the query length is at least THRESHOLD_NETWORK_SUGGESTION | ||
val shallShowRemoteSuggestionsNow = (showRemoteSuggestions && query.length >= THRESHOLD_NETWORK_SUGGESTION) | ||
if (showLocalSuggestions && shallShowRemoteSuggestionsNow) { | ||
Observable.zip( | ||
getLocalSuggestionsObservable(query, 3), | ||
getRemoteSuggestionsObservable(query) | ||
) { local, remote -> (local + remote).distinct() } | ||
} else if (showLocalSuggestions) { | ||
getLocalSuggestionsObservable(query, 25) | ||
} else if (shallShowRemoteSuggestionsNow) { | ||
getRemoteSuggestionsObservable(query) | ||
} else { | ||
Single.just(emptyList<SuggestionItem>()).toObservable() | ||
} | ||
} | ||
.subscribe({ suggestions -> | ||
suggestions.forEach { suggestionMutableLiveData.postValue(SuggestionItemSuccess(it)) } | ||
}) { | ||
suggestionMutableLiveData.postValue(SuggestionItemError(it)) | ||
} | ||
|
||
override fun onCleared() { | ||
suggestionDisposable.dispose() | ||
} | ||
|
||
fun updateSearchQuery(query: String) { | ||
suggestionPublisher.onNext(query) | ||
} | ||
|
||
private fun getLocalSuggestionsObservable(query: String, similarQueryLimit: Int): Observable<List<SuggestionItem>> { | ||
return historyRecordManager.getRelatedSearches(query, similarQueryLimit, 25) | ||
.toObservable() | ||
.map { entries -> entries.map { SuggestionItem(true, it) } } | ||
} | ||
|
||
private fun getRemoteSuggestionsObservable(query: String): Observable<List<SuggestionItem>> { | ||
return ExtractorHelper.suggestionsFor(serviceId, query) | ||
.toObservable() | ||
.map { entries -> entries.map { SuggestionItem(false, it) } } | ||
} | ||
|
||
companion object { | ||
/** | ||
* How much time have to pass without emitting a item (i.e. the user stop typing) | ||
* to fetch/show the suggestions, in milliseconds. | ||
*/ | ||
private const val SUGGESTIONS_DEBOUNCE = 120L // ms | ||
|
||
/** | ||
* The suggestions will only be fetched from network if the query meet this threshold (>=). | ||
* (local ones will be fetched regardless of the length) | ||
*/ | ||
private const val THRESHOLD_NETWORK_SUGGESTION = 1 | ||
|
||
fun getFactory( | ||
serviceId: Int, | ||
showLocalSuggestions: Boolean, | ||
showRemoteSuggestions: Boolean | ||
) = viewModelFactory { | ||
initializer { | ||
SearchViewModel(App.getApp(), serviceId, showLocalSuggestions, showRemoteSuggestions) | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed class SuggestionItemResponse | ||
class SuggestionItemSuccess(val item: SuggestionItem) : SuggestionItemResponse() | ||
class SuggestionItemError(val throwable: Throwable) : SuggestionItemResponse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters