mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
#3772 Convert SearchImagesFragment to use Pagination - allow viewpager to load more data
This commit is contained in:
parent
c0fcf1a1c3
commit
37c7eb060a
3 changed files with 63 additions and 2 deletions
|
|
@ -51,7 +51,8 @@ abstract class BaseSearchFragment<T> : CommonsDaggerSupportFragment(),
|
||||||
override fun observeSearchResults(searchResults: LiveData<PagedList<T>>) {
|
override fun observeSearchResults(searchResults: LiveData<PagedList<T>>) {
|
||||||
this.searchResults?.removeObservers(viewLifecycleOwner)
|
this.searchResults?.removeObservers(viewLifecycleOwner)
|
||||||
this.searchResults = searchResults
|
this.searchResults = searchResults
|
||||||
searchResults.observe(viewLifecycleOwner, Observer(pagedListAdapter::submitList))
|
searchResults.observe(viewLifecycleOwner, Observer {
|
||||||
|
pagedListAdapter.submitList(it) })
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onAttach(context: Context) {
|
override fun onAttach(context: Context) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package fr.free.nrw.commons.explore.media
|
package fr.free.nrw.commons.explore.media
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
import fr.free.nrw.commons.Media
|
import fr.free.nrw.commons.Media
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.explore.BaseSearchFragment
|
import fr.free.nrw.commons.explore.BaseSearchFragment
|
||||||
|
|
@ -26,12 +28,30 @@ class SearchMediaFragment : BaseSearchFragment<Media>(), SearchMediaFragmentCont
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val simpleDataObserver = SimpleDataObserver { notifyViewPager() }
|
||||||
|
|
||||||
fun requestMoreImages() {
|
fun requestMoreImages() {
|
||||||
// This paradigm is not well suited to pagination
|
// This functionality is replaced by a dataSetObserver and by using loadAround
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
pagedListAdapter.registerAdapterDataObserver(simpleDataObserver)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
super.onDestroyView()
|
||||||
|
pagedListAdapter.unregisterAdapterDataObserver(simpleDataObserver)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun notifyViewPager() {
|
||||||
|
(activity as SearchActivity).viewPagerNotifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getImageAtPosition(position: Int): Media? =
|
fun getImageAtPosition(position: Int): Media? =
|
||||||
pagedListAdapter.currentList?.get(position)?.takeIf { it.filename != null }
|
pagedListAdapter.currentList?.get(position)?.takeIf { it.filename != null }
|
||||||
|
.also { pagedListAdapter.currentList?.loadAround(position) }
|
||||||
|
|
||||||
fun getTotalImagesCount(): Int = pagedListAdapter.itemCount
|
fun getTotalImagesCount(): Int = pagedListAdapter.itemCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package fr.free.nrw.commons.explore.media
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
|
class SimpleDataObserver(private val onAnyChange: () -> Unit) : RecyclerView.AdapterDataObserver() {
|
||||||
|
override fun onChanged() {
|
||||||
|
super.onChanged()
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeRemoved(positionStart, itemCount)
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStateRestorationPolicyChanged() {
|
||||||
|
super.onStateRestorationPolicyChanged()
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeInserted(positionStart, itemCount)
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeChanged(positionStart, itemCount)
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
|
||||||
|
super.onItemRangeChanged(positionStart, itemCount, payload)
|
||||||
|
onAnyChange.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue