-
-
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
c97468f
commit 5e83456
Showing
5 changed files
with
170 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
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
37 changes: 37 additions & 0 deletions
37
app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListPagingAdapter.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,37 @@ | ||
package org.schabi.newpipe.fragments.list.search | ||
|
||
import android.view.ViewGroup | ||
import androidx.paging.PagingDataAdapter | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.schabi.newpipe.databinding.ItemSearchSuggestionBinding | ||
|
||
class SuggestionListPagingAdapter : PagingDataAdapter<SuggestionItem, SuggestionItemHolder>(SuggestionListDiffCallback) { | ||
var listener: OnSuggestionItemSelectedListener? = null | ||
|
||
interface OnSuggestionItemSelectedListener { | ||
fun onSuggestionItemSelected(item: SuggestionItem) | ||
fun onSuggestionItemInserted(item: SuggestionItem) | ||
fun onSuggestionItemLongClick(item: SuggestionItem) | ||
} | ||
|
||
override fun onBindViewHolder(holder: SuggestionItemHolder, position: Int) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SuggestionItemHolder { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
private object SuggestionListDiffCallback : DiffUtil.ItemCallback<SuggestionItem>() { | ||
override fun areItemsTheSame(oldItem: SuggestionItem, newItem: SuggestionItem): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: SuggestionItem, newItem: SuggestionItem): Boolean { | ||
return oldItem.query == newItem.query | ||
} | ||
} | ||
|
||
class SuggestionItemHolder(val binding: ItemSearchSuggestionBinding) : RecyclerView.ViewHolder(binding.root) |