diff --git a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/ContentFragment.kt b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/ContentFragment.kt index 38080a41..24184b2b 100644 --- a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/ContentFragment.kt +++ b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/ContentFragment.kt @@ -2,6 +2,7 @@ package com.pluto.plugins.network.internal.interceptor.ui import android.os.Bundle import android.os.Parcelable +import android.text.Layout import android.view.View import android.view.View.VISIBLE import androidx.core.view.isVisible @@ -55,6 +56,8 @@ internal class ContentFragment : Fragment(R.layout.pluto_network___fragment_cont append("\n") } } + + scrollToText(search.trim()) } } } @@ -85,6 +88,32 @@ internal class ContentFragment : Fragment(R.layout.pluto_network___fragment_cont } } + /** + * helps to auto scroll to target search + */ + private fun scrollToText(targetText: String) { + if (targetText.isEmpty()) { + return + } + + val contentText = binding.content.getText().toString().lowercase() + val index = contentText.indexOf(targetText.lowercase()) + + if (index != -1) { + binding.content.post { + val layout: Layout? = binding.content.layout + if (layout != null) { + val lineNumber = layout.getLineForOffset(index) + val x = layout.getPrimaryHorizontal(index.plus(targetText.length)).toInt() + val y = layout.getLineTop(lineNumber) + + binding.horizontalScroll.smoothScrollTo(x / 2, 0) + binding.contentNestedScrollView.smoothScrollTo(0, y / 2) + } + } + } + } + companion object { internal const val DATA = "data" } diff --git a/pluto-plugins/plugins/network/core/lib/src/main/res/layout/pluto_network___fragment_content.xml b/pluto-plugins/plugins/network/core/lib/src/main/res/layout/pluto_network___fragment_content.xml index 1a324d8f..ac650877 100644 --- a/pluto-plugins/plugins/network/core/lib/src/main/res/layout/pluto_network___fragment_content.xml +++ b/pluto-plugins/plugins/network/core/lib/src/main/res/layout/pluto_network___fragment_content.xml @@ -183,6 +183,7 @@ app:layout_behavior="@string/appbar_scrolling_view_behavior">