improved activity item click

This commit is contained in:
Mariotaku Lee 2017-04-28 23:25:44 +08:00
parent 6dfc4897de
commit fa6d658394
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
8 changed files with 70 additions and 36 deletions

View File

@ -41,6 +41,7 @@ import org.mariotaku.twidere.provider.TwidereDataStore.Statuses;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

View File

@ -20,11 +20,11 @@ fun <T> MutableCollection<T>.addAllEnhanced(collection: Collection<T>, ignoreDup
}
}
fun <T> Collection<T>.addAllTo(collection: MutableCollection<T>): Boolean {
fun <T> Collection<T>.addAllTo(collection: MutableCollection<in T>): Boolean {
return collection.addAll(this)
}
fun <T> Array<T>.addAllTo(collection: MutableCollection<T>): Boolean {
fun <T> Array<T>.addAllTo(collection: MutableCollection<in T>): Boolean {
return collection.addAll(this)
}

View File

@ -339,6 +339,14 @@ class ParcelableActivitiesAdapter(
}, defValue = null, raw = raw)
}
fun getRowId(adapterPosition: Int, raw: Boolean = false): Long {
return getFieldValue(adapterPosition, readCursorValueAction = { cursor, indices ->
cursor.safeGetLong(indices[Activities._ID])
}, readStatusValueAction = { activity ->
activity._id
}, defValue = -1L, raw = raw)
}
fun getData(): List<ParcelableActivity>? {
return data
}

View File

@ -23,6 +23,8 @@ import android.annotation.SuppressLint
import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
import org.mariotaku.ktextension.useCursor
import org.mariotaku.library.objectcursor.ObjectCursor
import org.mariotaku.twidere.util.TwidereQueryBuilder
@SuppressLint("Recycle")
@ -31,3 +33,12 @@ fun ContentResolver.rawQuery(sql: String, selectionArgs: Array<String>?, notifyU
return query(rawUri, null, null, selectionArgs, null)
}
fun <T> ContentResolver.queryOne(uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String? = null, cls: Class<T>): T? {
val cursor = this.query(uri, projection, selection, selectionArgs, sortOrder)
if (!cursor.moveToFirst()) return null
return cursor.useCursor {
val indices = ObjectCursor.indicesFrom(cursor, cls)
return@useCursor indices.newObject(cursor)
}
}

View File

@ -46,6 +46,7 @@ fun Notification.toParcelable(accountKey: UserKey): ParcelableActivity {
result.max_sort_position = result.timestamp
result.sources = toSources(accountKey)
result.user_key = result.sources?.firstOrNull()?.key ?: UserKey("multiple", null)
when (type) {
Notification.Type.MENTION -> {

View File

@ -37,10 +37,7 @@ import com.bumptech.glide.Glide
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_recyclerview.*
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.addAllTo
import org.mariotaku.ktextension.coerceInOr
import org.mariotaku.ktextension.isNullOrEmpty
import org.mariotaku.ktextension.rangeOfSize
import org.mariotaku.ktextension.*
import org.mariotaku.microblog.library.twitter.model.Activity
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.R
@ -65,7 +62,6 @@ import org.mariotaku.twidere.model.analyzer.Share
import org.mariotaku.twidere.model.event.StatusListChangedEvent
import org.mariotaku.twidere.model.pagination.SinceMaxPagination
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.model.util.ParcelableActivityUtils
import org.mariotaku.twidere.model.util.activityStatus
import org.mariotaku.twidere.provider.TwidereDataStore.Activities
import org.mariotaku.twidere.util.*
@ -77,8 +73,6 @@ import org.mariotaku.twidere.view.holder.ActivityTitleSummaryViewHolder
import org.mariotaku.twidere.view.holder.GapViewHolder
import org.mariotaku.twidere.view.holder.StatusViewHolder
import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder
import java.util.*
import kotlin.collections.ArrayList
abstract class AbsActivitiesFragment protected constructor() :
AbsContentListRecyclerViewFragment<ParcelableActivitiesAdapter>(),
@ -344,14 +338,14 @@ abstract class AbsActivitiesFragment protected constructor() :
}
override fun onActivityClick(holder: ActivityTitleSummaryViewHolder, position: Int) {
val activity = adapter.getActivity(position)
val list: ArrayList<ParcelableStatus> = ArrayList()
// if (activity.target_objects?.statuses != null) {
// activity.target_objects?.statuses?.addAllTo(list)
// } else if (activity.targets?.statuses != null) {
// activity.targets?.statuses?.addAllTo(list)
// }
// list.addAll(ParcelableActivityUtils.getAfterFilteredSources(activity))
val activity = getFullActivity(position) ?: return
val list = ArrayList<Parcelable>()
if (activity.target_objects?.statuses.isNotNullOrEmpty()) {
activity.target_objects?.statuses?.addAllTo(list)
} else if (activity.targets?.statuses.isNotNullOrEmpty()) {
activity.targets?.statuses?.addAllTo(list)
}
activity.sources?.addAllTo(list)
IntentUtils.openItems(getActivity(), list)
}
@ -375,7 +369,11 @@ abstract class AbsActivitiesFragment protected constructor() :
IntentUtils.openStatus(context, status.account_key, status.quoted_id)
}
private fun getActivityStatus(position: Int): ParcelableStatus? {
protected open fun getFullActivity(position: Int): ParcelableActivity? {
return adapter.getActivity(position)
}
protected open fun getActivityStatus(position: Int): ParcelableStatus? {
return adapter.getActivity(position).activityStatus
}
@ -456,7 +454,7 @@ abstract class AbsActivitiesFragment protected constructor() :
readPositionTag?.let { positionTag ->
accountKeys.forEach { accountKey ->
val tag = Utils.getReadPositionTagWithAccount(positionTag, accountKey)
if (readStateManager.setPosition(tag, item.timestamp)) {
if (readStateManager.setPosition(tag, item.position_key)) {
positionUpdated = true
}
}
@ -464,7 +462,7 @@ abstract class AbsActivitiesFragment protected constructor() :
timelineSyncManager?.setPosition(positionTag, syncTag, item.position_key)
}
currentReadPositionTag?.let { currentTag ->
readStateManager.setPosition(currentTag, item.timestamp, true)
readStateManager.setPosition(currentTag, item.position_key, true)
}
}

View File

@ -38,6 +38,7 @@ import org.mariotaku.twidere.R
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter.IndicatorPosition
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_FROM_USER
import org.mariotaku.twidere.extension.queryOne
import org.mariotaku.twidere.loader.ExtendedObjectCursorLoader
import org.mariotaku.twidere.model.*
import org.mariotaku.twidere.model.event.*
@ -201,6 +202,14 @@ abstract class CursorActivitiesFragment : AbsActivitiesFragment() {
clearNotifications()
}
}
override fun getFullActivity(position: Int): ParcelableActivity? {
val _id = adapter.getRowId(position)
val where = Expression.equals(Activities._ID, _id).sql
return context.contentResolver.queryOne(contentUri, Activities.COLUMNS, where, null, null,
ParcelableActivity::class.java)
}
protected fun getFiltersWhere(table: String): Expression? {
if (!isFilterEnabled) return null
return DataStoreUtils.buildActivityFilterWhereClause(table, null)

View File

@ -11,9 +11,6 @@ val ParcelableActivity.activityStatus: ParcelableActivity?
else -> null
}
val ParcelableActivity.id2: String
get() = "$min_position-$max_position"
val ParcelableActivity.reachedCountLimit: Boolean get() {
return sources.reachedCountLimit() || targets.reachedCountLimit() ||
target_objects.reachedCountLimit()
@ -41,23 +38,15 @@ fun ParcelableActivity.prependSources(another: ParcelableActivity) {
sources = uniqCombine(another.sources, sources)
}
fun ParcelableActivity.prependTargets(another: ParcelableActivity) {
fun ParcelableActivity.prependTargets(from: ParcelableActivity) {
this.targets = (this.targets ?: ParcelableActivity.RelatedObject()).prepend(from.targets)
}
fun ParcelableActivity.prependTargetObjects(another: ParcelableActivity) {
fun ParcelableActivity.prependTargetObjects(from: ParcelableActivity) {
this.target_objects = (this.target_objects ?: ParcelableActivity.RelatedObject())
.prepend(from.target_objects)
}
private inline fun <reified T> uniqCombine(vararg arrays: Array<T>?): Array<T> {
val set = mutableSetOf<T>()
arrays.forEach { array -> array?.addAllTo(set) }
return set.toTypedArray()
}
private fun Array<*>?.reachedCountLimit() = if (this == null) false else size > 10
private fun List<*>?.reachedCountLimit() = if (this == null) false else size > 10
private fun ParcelableActivity.RelatedObject?.reachedCountLimit() = if (this == null) false else size > 10
inline val ParcelableActivity.RelatedObject.size get() = when {
statuses != null -> statuses.size
users != null -> users.size
@ -68,4 +57,21 @@ inline val ParcelableActivity.RelatedObject.size get() = when {
fun ParcelableActivity.RelatedObject?.isNullOrEmpty(): Boolean {
if (this == null) return true
return size == 0
}
private inline fun <reified T> uniqCombine(vararg arrays: Array<T>?): Array<T> {
val set = mutableSetOf<T>()
arrays.forEach { array -> array?.addAllTo(set) }
return set.toTypedArray()
}
private fun Array<*>?.reachedCountLimit() = if (this == null) false else size > 10
private fun ParcelableActivity.RelatedObject?.reachedCountLimit() = if (this == null) false else size > 10
private fun ParcelableActivity.RelatedObject.prepend(from: ParcelableActivity.RelatedObject?): ParcelableActivity.RelatedObject {
this.statuses = uniqCombine(this.statuses, from?.statuses)
this.users = uniqCombine(this.users, from?.users)
this.user_lists = uniqCombine(this.user_lists, from?.user_lists)
return this
}