improved timeline

This commit is contained in:
Mariotaku Lee 2017-10-20 19:05:47 +08:00
parent 73e07b15eb
commit df288ae750
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
3 changed files with 59 additions and 23 deletions

View File

@ -23,7 +23,8 @@ import android.arch.paging.PagedList
import android.arch.paging.PagedListAdapterHelper
import android.content.Context
import android.support.v4.widget.Space
import android.support.v7.recyclerview.extensions.DiffCallback
import android.support.v7.recyclerview.extensions.ListAdapterConfig
import android.support.v7.util.ListUpdateCallback
import android.support.v7.widget.RecyclerView
import android.util.SparseBooleanArray
import android.view.LayoutInflater
@ -53,6 +54,7 @@ import org.mariotaku.twidere.model.timeline.TimelineFilter
import org.mariotaku.twidere.util.StatusAdapterLinkClickHandler
import org.mariotaku.twidere.util.TwidereLinkify
import org.mariotaku.twidere.util.Utils
import org.mariotaku.twidere.util.paging.DiffCallbacks
import org.mariotaku.twidere.view.holder.*
import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder
import java.util.*
@ -108,6 +110,7 @@ class ParcelableStatusesAdapter(
var timelineFilter: TimelineFilter? = null
set(value) {
field = value
updateItemCount()
notifyDataSetChanged()
}
@ -120,17 +123,28 @@ class ParcelableStatusesAdapter(
private val showingFullTextStates = SparseBooleanArray()
private var pagedStatusesHelper = PagedListAdapterHelper<ParcelableStatus>(this, object : DiffCallback<ParcelableStatus>() {
override fun areContentsTheSame(oldItem: ParcelableStatus, newItem: ParcelableStatus): Boolean {
return oldItem == newItem
private var pagedStatusesHelper = PagedListAdapterHelper<ParcelableStatus>(object : ListUpdateCallback {
override fun onInserted(position: Int, count: Int) {
updateItemCount()
notifyItemRangeInserted(position, count)
}
override fun areItemsTheSame(oldItem: ParcelableStatus, newItem: ParcelableStatus): Boolean {
if (oldItem._id > 0 && newItem._id > 0) return oldItem._id == newItem._id
return oldItem == newItem
override fun onRemoved(position: Int, count: Int) {
updateItemCount()
notifyItemRangeRemoved(position, count)
}
})
override fun onMoved(fromPosition: Int, toPosition: Int) {
updateItemCount()
notifyItemMoved(fromPosition, toPosition)
}
override fun onChanged(position: Int, count: Int, payload: Any?) {
updateItemCount()
notifyItemRangeChanged(position, count, payload)
}
}, ListAdapterConfig.Builder<ParcelableStatus>().setDiffCallback(DiffCallbacks.status).build())
var statuses: PagedList<ParcelableStatus>?
get() = pagedStatusesHelper.currentList

View File

@ -21,11 +21,9 @@ package org.mariotaku.twidere.data.source
import android.arch.paging.TiledDataSource
import android.content.ContentResolver
import android.database.ContentObserver
import android.net.Uri
import android.os.Handler
import android.os.Looper
import org.mariotaku.ktextension.weak
import org.mariotaku.twidere.extension.queryAll
import org.mariotaku.twidere.extension.queryCount
@ -43,19 +41,6 @@ class CursorObjectTiledDataSource<T>(
val cls: Class<T>
) : TiledDataSource<T>() {
init {
val weakThis = weak()
val observer = object : ContentObserver(MainHandler) {
override fun onChange(selfChange: Boolean) {
weakThis.get()?.invalidate()
}
}
addInvalidatedCallback cb@ {
resolver.unregisterContentObserver(observer)
}
resolver.registerContentObserver(uri, false, observer)
}
override fun countItems() = resolver.queryCount(uri, selection, selectionArgs)
override fun loadRange(startPosition: Int, count: Int): List<T> {

View File

@ -0,0 +1,37 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.paging
import android.support.v7.recyclerview.extensions.DiffCallback
import org.mariotaku.twidere.model.ParcelableStatus
object DiffCallbacks {
val status: DiffCallback<ParcelableStatus> = object : DiffCallback<ParcelableStatus>() {
override fun areContentsTheSame(oldItem: ParcelableStatus, newItem: ParcelableStatus): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(oldItem: ParcelableStatus, newItem: ParcelableStatus): Boolean {
if (oldItem._id > 0 && newItem._id > 0) return oldItem._id == newItem._id
return oldItem == newItem
}
}
}