Skip to content

Commit

Permalink
Issue-125: search auto scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
animeshdeepak authored and deepaksaha committed Jul 13, 2024
1 parent f2fc28c commit d5f9da2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,6 +56,8 @@ internal class ContentFragment : Fragment(R.layout.pluto_network___fragment_cont
append("\n")
}
}

scrollToText(search.trim())
}
}
}
Expand Down Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.core.widget.NestedScrollView
android:id="@+id/content_nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
Expand Down

0 comments on commit d5f9da2

Please sign in to comment.