Skip to content

Commit

Permalink
Added ability to iterate through search results in response pane usin…
Browse files Browse the repository at this point in the history
…g IME search action button
  • Loading branch information
kmayoral committed Jan 21, 2020
1 parent 9040470 commit b635888
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chuckerteam.chucker.internal.ui.transaction

import android.content.Context
import android.graphics.Bitmap
import android.text.SpannableStringBuilder
import android.text.Spanned
Expand All @@ -8,6 +9,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.internal.support.highlightWithDefinedColors
Expand All @@ -18,9 +20,16 @@ import com.chuckerteam.chucker.internal.support.highlightWithDefinedColors
* performances when loading big payloads.
*/
internal class TransactionBodyAdapter(
private val bodyItems: List<TransactionPayloadItem>
private val bodyItems: List<TransactionPayloadItem>,
private val layoutManager: RecyclerView.LayoutManager,
val context: Context
) : RecyclerView.Adapter<TransactionPayloadViewHolder>() {

private var linesWithHighlightsByIndex = mutableListOf<Int>()
private var currentHightLineIndex = 0
private var currentSearchString: String? = null
private val scrollOffsetPx = context.dpToPx(20)

override fun onBindViewHolder(holder: TransactionPayloadViewHolder, position: Int) {
holder.bind(bodyItems[position])
}
Expand Down Expand Up @@ -54,10 +63,14 @@ internal class TransactionBodyAdapter(
}

internal fun highlightQueryWithColors(newText: String, backgroundColor: Int, foregroundColor: Int) {
linesWithHighlightsByIndex.clear()
currentHightLineIndex = 0
currentSearchString = newText
bodyItems.filterIsInstance<TransactionPayloadItem.BodyLineItem>()
.withIndex()
.forEach { (index, item) ->
if (newText in item.line) {
linesWithHighlightsByIndex.add(index)
item.line.clearSpans()
item.line = item.line.toString()
.highlightWithDefinedColors(newText, backgroundColor, foregroundColor)
Expand All @@ -73,6 +86,38 @@ internal class TransactionBodyAdapter(
}
}

internal fun hasSearchResults(): Boolean = linesWithHighlightsByIndex.isNotEmpty()

internal fun goToNextHighlightLine(backgroundColor: Int, foregroundColor: Int, hightlightBackgroundColor: Int, highlightForegroundColor: Int) {
val oldLineIdx = linesWithHighlightsByIndex[currentHightLineIndex]
val nextIdx = (++currentHightLineIndex).takeIf { it < linesWithHighlightsByIndex.size } ?: 0
currentHightLineIndex = nextIdx
val newLineIdx = linesWithHighlightsByIndex[currentHightLineIndex]

val lines = bodyItems.filterIsInstance<TransactionPayloadItem.BodyLineItem>()

// Update old
lines[oldLineIdx].let { item ->
item.line.clearSpans()
item.line = item.line.toString()
.highlightWithDefinedColors(currentSearchString!!, backgroundColor, foregroundColor)
notifyItemChanged(oldLineIdx + 1)
}

// set new
lines[newLineIdx].let { item ->
item.line.clearSpans()
item.line = item.line.toString()
.highlightWithDefinedColors(currentSearchString!!, hightlightBackgroundColor, highlightForegroundColor)
notifyItemChanged(newLineIdx + 1)
(layoutManager as? LinearLayoutManager)?.let {
it.scrollToPositionWithOffset(newLineIdx, scrollOffsetPx)
} ?: run {
layoutManager.scrollToPosition(newLineIdx)
}
}
}

internal fun resetHighlight() {
bodyItems.filterIsInstance<TransactionPayloadItem.BodyLineItem>()
.withIndex()
Expand Down Expand Up @@ -128,3 +173,7 @@ internal sealed class TransactionPayloadItem {
internal class BodyLineItem(var line: SpannableStringBuilder) : TransactionPayloadItem()
internal class ImageItem(val image: Bitmap) : TransactionPayloadItem()
}

fun Context.dpToPx(dp: Int): Int {
return (dp * resources.displayMetrics.density).toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ internal class TransactionPayloadFragment :
private var backgroundSpanColor: Int = Color.YELLOW
private var foregroundSpanColor: Int = Color.RED

private var highlightBackgroundSpanColor: Int = Color.BLUE
private var hightlightForegroundSpanColor: Int = Color.WHITE

private var type: Int = 0

private lateinit var viewModel: TransactionViewModel
Expand Down Expand Up @@ -140,6 +143,8 @@ internal class TransactionPayloadFragment :
super.onAttach(context)
backgroundSpanColor = ContextCompat.getColor(context, R.color.chucker_background_span_color)
foregroundSpanColor = ContextCompat.getColor(context, R.color.chucker_foreground_span_color)
highlightBackgroundSpanColor = ContextCompat.getColor(context, R.color.chucker_highlight_background_span_color)
hightlightForegroundSpanColor = ContextCompat.getColor(context, R.color.chucker_highlight_foreground_span_color)
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
Expand Down Expand Up @@ -174,7 +179,14 @@ internal class TransactionPayloadFragment :
}
}

override fun onQueryTextSubmit(query: String): Boolean = false
override fun onQueryTextSubmit(query: String): Boolean {
val adapter = (transactionContentList.adapter as TransactionBodyAdapter)
if (adapter.hasSearchResults()) {
adapter.goToNextHighlightLine(backgroundSpanColor, foregroundSpanColor, highlightBackgroundSpanColor, hightlightForegroundSpanColor)
return true
}
return false
}

override fun onQueryTextChange(newText: String): Boolean {
val adapter = (transactionContentList.adapter as TransactionBodyAdapter)
Expand Down Expand Up @@ -248,7 +260,7 @@ internal class TransactionPayloadFragment :
val recyclerView: RecyclerView? = fragment.view?.findViewById(R.id.transaction_content)
progressBar?.visibility = View.INVISIBLE
recyclerView?.visibility = View.VISIBLE
recyclerView?.adapter = TransactionBodyAdapter(result)
recyclerView?.adapter = TransactionBodyAdapter(result, recyclerView?.layoutManager!!, recyclerView.context)
}
}

Expand Down
3 changes: 3 additions & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@

<color name="chucker_background_span_color">#ffffff00</color>
<color name="chucker_foreground_span_color">#ffff0000</color>

<color name="chucker_highlight_background_span_color">#ff0000ff</color>
<color name="chucker_highlight_foreground_span_color">#ffffffff</color>
</resources>

0 comments on commit b635888

Please sign in to comment.