improved timeline load more

This commit is contained in:
Mariotaku Lee 2017-11-02 15:01:56 +08:00
parent f4e5c533e4
commit 6ae76509bb
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 41 additions and 24 deletions

View File

@ -57,6 +57,7 @@ import org.mariotaku.twidere.constant.newDocumentApiKey
import org.mariotaku.twidere.constant.readFromBottomKey
import org.mariotaku.twidere.data.CursorObjectLivePagedListProvider
import org.mariotaku.twidere.data.CursorObjectLivePagedListProvider.CursorObjectProcessor
import org.mariotaku.twidere.data.ExtendedPagedListProvider
import org.mariotaku.twidere.extension.model.activityStatus
import org.mariotaku.twidere.extension.model.getAccountType
import org.mariotaku.twidere.extension.queryOne
@ -109,8 +110,11 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
*/
protected abstract val contentUri: Uri
protected lateinit var activities: LiveData<PagedList<ParcelableActivity>?>
private set
protected var activities: LiveData<PagedList<ParcelableActivity>?>? = null
private set(value) {
field?.removeObservers(this)
field = value
}
protected val accountKeys: Array<UserKey>
get() = Utils.getAccountKeys(context, arguments) ?: if (isStandalone) {
@ -121,16 +125,18 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
private val busEventHandler = BusEventHandler()
private val scrollHandler = ScrollHandler()
private var dataController: ExtendedPagedListProvider.DataController? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
registerForContextMenu(recyclerView)
adapter.activityClickListener = ActivityClickHandler()
if (!isStandalone) {
adapter.loadMoreSupportedPosition = LoadMorePosition.END
adapter.loadMoreSupportedPosition = if (isStandalone) {
LoadMorePosition.NONE
} else {
LoadMorePosition.END
}
activities = createLiveData()
activities.observe(this, Observer { onDataLoaded(it) })
setupLiveData()
showProgress()
}
@ -231,7 +237,7 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
}
override fun onLoadMoreContents(position: Int) {
if (isStandalone || position != LoadMorePosition.END) return
if (isStandalone || !refreshEnabled || position != LoadMorePosition.END) return
val started = getActivities(object : ContentRefreshParam {
override val accountKeys by lazy {
this@AbsActivitiesFragment.accountKeys
@ -249,9 +255,8 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
get() = this@AbsActivitiesFragment.tabId
})
if (started) {
adapter.loadMoreIndicatorPosition = position
}
if (!started) return
super.onLoadMoreContents(position)
}
override fun scrollToPositionWithOffset(position: Int, offset: Int) {
@ -268,11 +273,14 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
fun reloadAll() {
val controller = dataController
if (controller != null) {
if (!controller.invalidate()) return
} else {
adapter.activities = null
setupLiveData()
}
showProgress()
adapter.activities = null
activities.removeObservers(this)
activities = createLiveData()
activities.observe(this, Observer { onDataLoaded(it) })
}
@ -342,8 +350,9 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
protected abstract fun onCreateCursorObjectProcessor(): CursorObjectProcessor<ParcelableActivity>
private fun createLiveData(): LiveData<PagedList<ParcelableActivity>?> {
return if (isStandalone) onCreateStandaloneLiveData() else onCreateDatabaseLiveData()
private fun setupLiveData() {
activities = if (isStandalone) onCreateStandaloneLiveData() else onCreateDatabaseLiveData()
activities?.observe(this, Observer { onDataLoaded(it) })
}
private fun onCreateStandaloneLiveData(): LiveData<PagedList<ParcelableActivity>?> {
@ -368,6 +377,7 @@ abstract class AbsActivitiesFragment : AbsContentRecyclerViewFragment<Parcelable
Activities.COLUMNS, Expression.and(*expressions.toTypedArray()).sql,
expressionArgs.toTypedArray(), Activities.DEFAULT_SORT_ORDER,
ParcelableActivity::class.java, onCreateCursorObjectProcessor())
dataController = provider.obtainDataController()
return provider.create(null, PagedList.Config.Builder()
.setPageSize(50).setEnablePlaceholders(false).build())
}

View File

@ -32,9 +32,13 @@ import org.mariotaku.twidere.model.tab.extra.InteractionsTabExtras
import org.mariotaku.twidere.provider.TwidereDataStore.Activities
class InteractionsActivitiesFragment : AbsActivitiesFragment() {
override val filterScope: Int = FilterScope.INTERACTIONS
override val contentUri: Uri = Activities.AboutMe.CONTENT_URI
override val readPositionTag: String? = ReadPositionTag.ACTIVITIES_ABOUT_ME
override fun getActivities(param: ContentRefreshParam): Boolean {
twitterWrapper.getActivitiesAboutMeAsync(param)
return true

View File

@ -151,6 +151,11 @@ abstract class AbsTimelineFragment : AbsContentRecyclerViewFragment<ParcelableSt
super.onActivityCreated(savedInstanceState)
registerForContextMenu(recyclerView)
adapter.statusClickListener = StatusClickHandler()
adapter.loadMoreSupportedPosition = if (isStandalone) {
LoadMorePosition.NONE
} else {
LoadMorePosition.END
}
setupLiveData()
showProgress()
}
@ -257,8 +262,7 @@ abstract class AbsTimelineFragment : AbsContentRecyclerViewFragment<ParcelableSt
}
override fun onLoadMoreContents(position: Int) {
if (isStandalone) return
if (position != LoadMorePosition.END) return
if (isStandalone || !refreshEnabled || position != LoadMorePosition.END) return
val started = getStatuses(object : ContentRefreshParam {
override val accountKeys by lazy {
this@AbsTimelineFragment.accountKeys
@ -276,9 +280,8 @@ abstract class AbsTimelineFragment : AbsContentRecyclerViewFragment<ParcelableSt
get() = this@AbsTimelineFragment.tabId
})
if (started) {
adapter.loadMoreIndicatorPosition = position
}
if (!started) return
super.onLoadMoreContents(position)
}
override fun scrollToPositionWithOffset(position: Int, offset: Int) {
@ -379,9 +382,8 @@ abstract class AbsTimelineFragment : AbsContentRecyclerViewFragment<ParcelableSt
}
private fun setupLiveData() {
statuses = (if (isStandalone) onCreateStandaloneLiveData() else onCreateDatabaseLiveData()).also { ld ->
ld.observe(this, Observer { onDataLoaded(it) })
}
statuses = if (isStandalone) onCreateStandaloneLiveData() else onCreateDatabaseLiveData()
statuses?.observe(this, Observer { onDataLoaded(it) })
}
private fun onCreateStandaloneLiveData(): LiveData<PagedList<ParcelableStatus>?> {

View File

@ -36,6 +36,7 @@ import org.mariotaku.twidere.provider.TwidereDataStore.Statuses
import java.util.*
class HomeTimelineFragment : AbsTimelineFragment() {
override val filterScope: Int = FilterScope.HOME
override val contentUri: Uri = Statuses.HomeTimeline.CONTENT_URI