-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added search highlight in response tab. #71
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Wed Mar 15 11:04:59 GMT 2017 | ||
#Sat Feb 17 18:16:34 MYT 2018 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.readystatesoftware.chuck.internal.support; | ||
|
||
import android.graphics.Color; | ||
import android.text.SpannableStringBuilder; | ||
import android.text.Spanned; | ||
import android.text.style.BackgroundColorSpan; | ||
import android.text.style.ForegroundColorSpan; | ||
import android.text.style.UnderlineSpan; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class SearchHighlightUtil { | ||
|
||
public static SpannableStringBuilder format(String text, String criteria) { | ||
List<Integer> startIndexes = indexesOf(text, criteria); | ||
return applySpannable(text, startIndexes, criteria.length()); | ||
} | ||
|
||
private static List<Integer> indexesOf(String text, String criteria) { | ||
List<Integer> startPositions = new ArrayList<>(); | ||
int index = text.indexOf(criteria); | ||
do { | ||
startPositions.add(index); | ||
index = text.indexOf(criteria, index + 1); | ||
} while (index >= 0); | ||
return startPositions; | ||
} | ||
|
||
private static SpannableStringBuilder applySpannable(String text, List<Integer> indexes, int length) { | ||
SpannableStringBuilder builder = new SpannableStringBuilder(text); | ||
for (Integer position : indexes) { | ||
builder.setSpan(new UnderlineSpan(), | ||
position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||
builder.setSpan(new ForegroundColorSpan(Color.RED), | ||
position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||
builder.setSpan(new BackgroundColorSpan(Color.YELLOW), | ||
position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||
} | ||
return builder; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,22 @@ | |
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v7.widget.SearchView; | ||
import android.text.Html; | ||
import android.text.TextUtils; | ||
import android.view.LayoutInflater; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.readystatesoftware.chuck.R; | ||
import com.readystatesoftware.chuck.internal.data.HttpTransaction; | ||
import com.readystatesoftware.chuck.internal.support.SearchHighlightUtil; | ||
|
||
public class TransactionPayloadFragment extends Fragment implements TransactionFragment { | ||
public class TransactionPayloadFragment extends Fragment implements TransactionFragment, SearchView.OnQueryTextListener { | ||
|
||
public static final int TYPE_REQUEST = 0; | ||
public static final int TYPE_RESPONSE = 1; | ||
|
@@ -40,6 +45,7 @@ public class TransactionPayloadFragment extends Fragment implements TransactionF | |
|
||
private int type; | ||
private HttpTransaction transaction; | ||
private String originalBody; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can maybe delete this field, and use |
||
|
||
public TransactionPayloadFragment() { | ||
} | ||
|
@@ -57,6 +63,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | ||
type = getArguments().getInt(ARG_TYPE); | ||
setRetainInstance(true); | ||
setHasOptionsMenu(true); | ||
} | ||
|
||
@Override | ||
|
@@ -74,6 +81,19 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
populateUI(); | ||
} | ||
|
||
@Override | ||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | ||
if(type == TYPE_RESPONSE){ | ||
MenuItem searchMenuItem = menu.findItem(R.id.search); | ||
searchMenuItem.setVisible(true); | ||
SearchView searchView = (SearchView) searchMenuItem.getActionView(); | ||
searchView.setOnQueryTextListener(this); | ||
searchView.setIconifiedByDefault(true); | ||
} | ||
|
||
super.onCreateOptionsMenu(menu, inflater); | ||
} | ||
|
||
@Override | ||
public void transactionUpdated(HttpTransaction transaction) { | ||
this.transaction = transaction; | ||
|
@@ -103,5 +123,22 @@ private void setText(String headersString, String bodyString, boolean isPlainTex | |
} else { | ||
body.setText(bodyString); | ||
} | ||
originalBody = body.getText().toString(); | ||
|
||
} | ||
|
||
@Override | ||
public boolean onQueryTextSubmit(String query) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean onQueryTextChange(String newText) { | ||
String text = body.getText().toString(); | ||
if (newText.trim().length() > 0 && text.contains(newText.trim())) | ||
body.setText(SearchHighlightUtil.format(body.getText().toString(), newText)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should |
||
else | ||
body.setText(originalBody); | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid Javadoc. Take advantage of this fix for adding description to this new class.