Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'

classpath 'com.android.tools.build:gradle:3.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -18,6 +20,7 @@ allprojects {

repositories {
jcenter()

maven {
url "https://maven.google.com"
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
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 {

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.


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
Expand Up @@ -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;
Expand All @@ -40,6 +45,7 @@ public class TransactionPayloadFragment extends Fragment implements TransactionF

private int type;
private HttpTransaction transaction;
private String originalBody;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can maybe delete this field, and use transaction.getFormattedRequestBody() instead. No need to add a field if another field already holds the value.


public TransactionPayloadFragment() {
}
Expand All @@ -57,6 +63,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
type = getArguments().getInt(ARG_TYPE);
setRetainInstance(true);
setHasOptionsMenu(true);
}

@Override
Expand All @@ -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;
Expand Down Expand Up @@ -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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should originalBody here, no ?
body.setText(SearchHighlightUtil.format(originalBody, newText));

else
body.setText(originalBody);
return true;
}
}
28 changes: 19 additions & 9 deletions library/src/main/res/menu/chuck_transaction.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@
~ limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:title="@string/chuck_share"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/chuck_ic_search_white_24dp"
android:title="@string/chuck_search"
android:visible="false"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
<item
android:icon="@drawable/chuck_ic_share_white_24dp"
app:showAsAction="always" >
<menu >
<group >
<item android:title="@string/chuck_share_as_text"
android:id="@+id/share_text"/>
<item android:title="@string/chuck_share_as_curl"
android:id="@+id/share_curl"/>
android:title="@string/chuck_share"
app:showAsAction="always">
<menu>
<group>
<item
android:id="@+id/share_text"
android:title="@string/chuck_share_as_text" />
<item
android:id="@+id/share_curl"
android:title="@string/chuck_share_as_curl" />
</group>
</menu>
</item>
Expand Down