From 99cda131e780c7789ce7e66ce09e7d1e55e8c93f Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Wed, 10 May 2017 12:59:01 +0800 Subject: [PATCH] fixed detailed status position lost in interactions timeline quick & dirty fix to #822 --- .../ktextension/RecyclerViewExtension.kt | 5 +- .../twidere/fragment/StatusFragment.kt | 54 ++++++++----------- .../mariotaku/twidere/util/HtmlSpanBuilder.kt | 8 ++- twidere/src/main/res/menu/menu_status.xml | 27 ---------- 4 files changed, 29 insertions(+), 65 deletions(-) delete mode 100644 twidere/src/main/res/menu/menu_status.xml diff --git a/twidere/src/main/kotlin/org/mariotaku/ktextension/RecyclerViewExtension.kt b/twidere/src/main/kotlin/org/mariotaku/ktextension/RecyclerViewExtension.kt index 7daa3d659..1990c9155 100644 --- a/twidere/src/main/kotlin/org/mariotaku/ktextension/RecyclerViewExtension.kt +++ b/twidere/src/main/kotlin/org/mariotaku/ktextension/RecyclerViewExtension.kt @@ -6,8 +6,5 @@ import android.support.v7.widget.RecyclerView * Created by mariotaku on 16/8/21. */ fun RecyclerView.Adapter<*>.findPositionByItemId(itemId: Long): Int { - for (i in 0 until itemCount) { - if (getItemId(i) == itemId) return i - } - return RecyclerView.NO_POSITION + return (0 until itemCount).firstOrNull { getItemId(it) == itemId } ?: RecyclerView.NO_POSITION } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt index 7ae10084c..2308b901b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt @@ -62,7 +62,10 @@ import kotlinx.android.synthetic.main.header_status.view.* import kotlinx.android.synthetic.main.layout_content_fragment_common.* import org.mariotaku.abstask.library.TaskStarter import org.mariotaku.kpreferences.get -import org.mariotaku.ktextension.* +import org.mariotaku.ktextension.applyFontFamily +import org.mariotaku.ktextension.contains +import org.mariotaku.ktextension.findPositionByItemId +import org.mariotaku.ktextension.hideIfEmpty import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException @@ -430,35 +433,6 @@ class StatusFragment : BaseFragment(), LoaderCallbacks>) { } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.menu_status, menu) - } - - override fun onPrepareOptionsMenu(menu: Menu) { - menu.setItemAvailability(R.id.current_status, adapter.status != null) - super.onPrepareOptionsMenu(menu) - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.current_status -> { - if (adapter.status != null) { - val position = adapter.getFirstPositionOfItem(StatusAdapter.ITEM_IDX_STATUS) - recyclerView.smoothScrollToPosition(position) - } - return true - } - } - return super.onOptionsItemSelected(item) - } - - private fun setConversation(data: List?) { - val readPosition = saveReadPosition() - val changed = adapter.setData(data) - hasMoreConversation = data != null && changed - restoreReadPosition(readPosition) - } - override val refreshing: Boolean get() = loaderManager.hasRunningLoadersSafe() @@ -546,6 +520,19 @@ class StatusFragment : BaseFragment(), LoaderCallbacks?) { + val readPosition = saveReadPosition() + val changed = adapter.setData(data) + hasMoreConversation = data != null && changed + restoreReadPosition(readPosition) + } + + private fun scrollToCurrent() { + if (adapter.status != null) { + val position = adapter.getFirstPositionOfItem(StatusAdapter.ITEM_IDX_STATUS) + recyclerView.smoothScrollToPosition(position) + } + } private fun displayTranslation(translation: TranslationResult) { adapter.translationResult = translation @@ -1782,10 +1769,13 @@ class StatusFragment : BaseFragment(), LoaderCallbacks { - return (countIndex.toLong() shl 32) or getStatus(position).hashCode().toLong() + val status = getStatus(position) + val hashCode = ParcelableStatus.calculateHashCode(status.account_key, status.id) + return (countIndex.toLong() shl 32) or hashCode.toLong() } } - return (countIndex.toLong() shl 32) or getItemType(position).toLong() + val countPos = (position - getItemStartPosition(countIndex)).toLong() + return (countIndex.toLong() shl 32) or countPos } override fun getItemCount(): Int { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/HtmlSpanBuilder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/HtmlSpanBuilder.kt index 53f09f02a..49baa9573 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/HtmlSpanBuilder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/HtmlSpanBuilder.kt @@ -20,13 +20,13 @@ package org.mariotaku.twidere.util import android.graphics.Typeface -import android.net.Uri import android.text.Editable import android.text.Spannable import android.text.SpannableStringBuilder import android.text.Spanned import android.text.style.StyleSpan import android.text.style.URLSpan +import okhttp3.HttpUrl import org.attoparser.ParseException import org.attoparser.config.ParseConfiguration import org.attoparser.simple.AbstractSimpleMarkupHandler @@ -76,7 +76,11 @@ object HtmlSpanBuilder { private fun createSpan(info: TagInfo): Any? { when (info.nameLower) { "a" -> { - return URLSpan(info.getAttribute("href")) + var href = info.getAttribute("href") ?: return null + if (HttpUrl.parse(href)?.scheme() == null) { + href = "https://" + href + } + return URLSpan(href) } "b", "strong" -> { return StyleSpan(Typeface.BOLD) diff --git a/twidere/src/main/res/menu/menu_status.xml b/twidere/src/main/res/menu/menu_status.xml deleted file mode 100644 index fb918859b..000000000 --- a/twidere/src/main/res/menu/menu_status.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - \ No newline at end of file