improved item id calculation

fixed status filter label
This commit is contained in:
Mariotaku Lee 2017-03-31 18:59:44 +08:00
parent 5c3b62dbe2
commit 27b2bcf3c4
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
7 changed files with 50 additions and 34 deletions

View File

@ -1 +1 @@
4204ab37b28019cfa0f51195034d1d0b5f2d31df 9ae153916806db7a16fffd9b76ef03d7ff4077a8

View File

@ -60,6 +60,8 @@ public class UserTimelineFilter implements TimelineFilter, Parcelable {
return context.getString(R.string.label_statuses_retweets_replies); return context.getString(R.string.label_statuses_retweets_replies);
} else if (includeReplies) { } else if (includeReplies) {
return context.getString(R.string.label_statuses_replies); return context.getString(R.string.label_statuses_replies);
} else if (includeRetweets) {
return context.getString(R.string.label_statuses_retweets);
} }
return context.getString(R.string.label_statuses); return context.getString(R.string.label_statuses);
} }

View File

@ -123,19 +123,27 @@ class ParcelableActivitiesAdapter(
} }
override fun getItemId(position: Int): Long { override fun getItemId(position: Int): Long {
val dataPosition = position - activityStartIndex val countIndex = itemCounts.getItemCountIndex(position)
if (data is ObjectCursor) { when (countIndex) {
val cursor = (data as ObjectCursor).cursor ITEM_INDEX_ACTIVITY -> {
if (!cursor.moveToPosition(dataPosition)) return -1 val dataPosition = position - activityStartIndex
val indices = (data as ObjectCursor).indices if (data is ObjectCursor) {
val accountKey = UserKey.valueOf(cursor.getString(indices[Activities.ACCOUNT_KEY])) val cursor = (data as ObjectCursor).cursor
val timestamp = cursor.getLong(indices[Activities.TIMESTAMP]) if (!cursor.moveToPosition(dataPosition)) return -1
val maxPosition = cursor.getLong(indices[Activities.MAX_SORT_POSITION]) val indices = (data as ObjectCursor).indices
val minPosition = cursor.getLong(indices[Activities.MIN_SORT_POSITION]) val accountKey = UserKey.valueOf(cursor.getString(indices[Activities.ACCOUNT_KEY]))
return ParcelableActivity.calculateHashCode(accountKey, timestamp, maxPosition, val timestamp = cursor.getLong(indices[Activities.TIMESTAMP])
minPosition).toLong() val maxPosition = cursor.getLong(indices[Activities.MAX_SORT_POSITION])
val minPosition = cursor.getLong(indices[Activities.MIN_SORT_POSITION])
return ParcelableActivity.calculateHashCode(accountKey, timestamp, maxPosition,
minPosition).toLong()
}
return (countIndex.toLong() shl 32) or getActivity(position, false).hashCode().toLong()
}
else -> {
return (countIndex.toLong() shl 32) or getItemViewType(position).toLong()
}
} }
return getActivity(position, false).hashCode().toLong()
} }
fun getTimestamp(adapterPosition: Int, raw: Boolean = false): Long { fun getTimestamp(adapterPosition: Int, raw: Boolean = false): Long {

View File

@ -213,17 +213,16 @@ abstract class ParcelableStatusesAdapter(
when (countIndex) { when (countIndex) {
ITEM_INDEX_PINNED_STATUS -> { ITEM_INDEX_PINNED_STATUS -> {
val status = pinnedStatuses!![position - getItemStartPosition(ITEM_INDEX_PINNED_STATUS)] val status = pinnedStatuses!![position - getItemStartPosition(ITEM_INDEX_PINNED_STATUS)]
val mask = ITEM_INDEX_PINNED_STATUS.toLong() shl 32 return (countIndex.toLong() shl 32) or status.hashCode().toLong()
return mask + status.hashCode()
} }
ITEM_INDEX_STATUS -> return getFieldValue(position, { cursor, indices -> ITEM_INDEX_STATUS -> return (countIndex.toLong() shl 32) or getFieldValue(position, { cursor, indices ->
val accountKey = UserKey.valueOf(cursor.getString(indices[Statuses.ACCOUNT_KEY])) val accountKey = UserKey.valueOf(cursor.getString(indices[Statuses.ACCOUNT_KEY]))
val id = cursor.getString(indices[Statuses.STATUS_ID]) val id = cursor.getString(indices[Statuses.STATUS_ID])
return@getFieldValue ParcelableStatus.calculateHashCode(accountKey, id).toLong() return@getFieldValue ParcelableStatus.calculateHashCode(accountKey, id)
}, { status -> }, { status ->
return@getFieldValue status.hashCode().toLong() return@getFieldValue status.hashCode()
}, -1L) }, -1).toLong()
else -> return (countIndex.toLong() shl 32) + position else -> return (countIndex.toLong() shl 32) or position.toLong()
} }
} }

View File

@ -90,8 +90,8 @@ class SelectableUsersAdapter(
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
val countIndex = itemCounts.getItemCountIndex(position) val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) { when (countIndex) {
0, 2 -> ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR ITEM_TYPE_START_INDICATOR, ITEM_TYPE_END_INDICATOR -> ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR
1 -> return ITEM_VIEW_TYPE_USER ITEM_TYPE_USER -> return ITEM_VIEW_TYPE_USER
} }
throw UnsupportedOperationException("Unsupported countIndex $countIndex, position $position") throw UnsupportedOperationException("Unsupported countIndex $countIndex, position $position")
} }
@ -99,8 +99,8 @@ class SelectableUsersAdapter(
override fun getItemId(position: Int): Long { override fun getItemId(position: Int): Long {
val countIndex = itemCounts.getItemCountIndex(position) val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) { when (countIndex) {
0, 2 -> return Integer.MAX_VALUE.toLong() + countIndex ITEM_TYPE_START_INDICATOR, ITEM_TYPE_END_INDICATOR -> return (countIndex.toLong() shl 32)
1 -> return getUser(position)!!.hashCode().toLong() ITEM_TYPE_USER -> return (countIndex.toLong() shl 32) or getUser(position)!!.hashCode().toLong()
} }
throw UnsupportedOperationException("Unsupported countIndex $countIndex, position $position") throw UnsupportedOperationException("Unsupported countIndex $countIndex, position $position")
} }
@ -111,9 +111,9 @@ class SelectableUsersAdapter(
private fun updateItemCounts() { private fun updateItemCounts() {
val position = loadMoreIndicatorPosition val position = loadMoreIndicatorPosition
itemCounts[0] = if (position and ILoadMoreSupportAdapter.START != 0L) 1 else 0 itemCounts[ITEM_TYPE_START_INDICATOR] = if (position and ILoadMoreSupportAdapter.START != 0L) 1 else 0
itemCounts[1] = userCount itemCounts[ITEM_TYPE_USER] = userCount
itemCounts[2] = if (position and ILoadMoreSupportAdapter.END != 0L) 1 else 0 itemCounts[ITEM_TYPE_END_INDICATOR] = if (position and ILoadMoreSupportAdapter.END != 0L) 1 else 0
} }
fun getUser(position: Int): ParcelableUser? { fun getUser(position: Int): ParcelableUser? {
@ -227,4 +227,10 @@ class SelectableUsersAdapter(
fun clearSelection() { fun clearSelection() {
checkedState.clear() checkedState.clear()
} }
companion object {
const val ITEM_TYPE_START_INDICATOR = 0
const val ITEM_TYPE_USER = 1
const val ITEM_TYPE_END_INDICATOR = 2
}
} }

View File

@ -1822,12 +1822,13 @@ class StatusFragment : BaseFragment(), LoaderCallbacks<SingleResponse<Parcelable
} }
override fun getItemId(position: Int): Long { override fun getItemId(position: Int): Long {
when (getItemCountIndex(position)) { val countIndex = getItemCountIndex(position)
when (countIndex) {
ITEM_IDX_CONVERSATION, ITEM_IDX_STATUS, ITEM_IDX_REPLY -> { ITEM_IDX_CONVERSATION, ITEM_IDX_STATUS, ITEM_IDX_REPLY -> {
return getStatus(position).hashCode().toLong() return (countIndex.toLong() shl 32) or getStatus(position).hashCode().toLong()
} }
} }
return getItemType(position).toLong() return (countIndex.toLong() shl 32) or getItemType(position).toLong()
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {

View File

@ -31,7 +31,6 @@ import android.os.Bundle
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentActivity import android.support.v4.app.FragmentActivity
import android.support.v4.app.LoaderManager import android.support.v4.app.LoaderManager
import android.support.v4.content.AsyncTaskLoader
import android.support.v4.content.FixedAsyncTaskLoader import android.support.v4.content.FixedAsyncTaskLoader
import android.support.v4.content.Loader import android.support.v4.content.Loader
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
@ -582,13 +581,14 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
} }
override fun getItemId(position: Int): Long { override fun getItemId(position: Int): Long {
when (itemCounts.getItemCountIndex(position)) { val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
ITEM_INDEX_ITEM -> { ITEM_INDEX_ITEM -> {
val user = getUser(position)!! val user = getUser(position)!!
return user.hashCode().toLong() return (countIndex.toLong() shl 32) or user.hashCode().toLong()
} }
else -> { else -> {
return Integer.MAX_VALUE.toLong() + getItemViewType(position) return (countIndex.toLong() shl 32) or getItemViewType(position).toLong()
} }
} }
} }