Skip to content

Commit

Permalink
Add new highlighting
Browse files Browse the repository at this point in the history
Add displayHighlighted method

RecordAdapter now owns a FuzzyScore instance for each query.
RecordAdapter.updateResults now takes query in parameter to create the
fuzzy score instance.
The fuzzyScore instance is given in parameter to the display method
so it can use it in the new displayHighlighted method.
  • Loading branch information
saveman71 committed May 20, 2018
1 parent 8288fb0 commit 858e132
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
11 changes: 9 additions & 2 deletions app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;

import fr.neamar.kiss.KissApplication;
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.result.AppResult;
import fr.neamar.kiss.result.ContactsResult;
import fr.neamar.kiss.result.PhoneResult;
Expand All @@ -24,10 +25,12 @@
import fr.neamar.kiss.result.ShortcutsResult;
import fr.neamar.kiss.searcher.QueryInterface;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;

public class RecordAdapter extends BaseAdapter implements SectionIndexer {
private final Context context;
private final QueryInterface parent;
private FuzzyScore fuzzyScore;

/**
* Array list containing all the results currently displayed
Expand All @@ -43,6 +46,7 @@ public RecordAdapter(Context context, QueryInterface parent, ArrayList<Result> r
this.context = context;
this.parent = parent;
this.results = results;
this.fuzzyScore = null;
}

@Override
Expand Down Expand Up @@ -102,7 +106,7 @@ else if ((Integer) convertView.getTag() != getItemViewType(position)) {
convertView = null;
}
}
View view = results.get(position).display(context, results.size() - position, convertView);
View view = results.get(position).display(context, results.size() - position, convertView, fuzzyScore);
//Log.d( "TBog", "getView pos " + position + " convertView " + ((convertView == null) ? "null" : convertView.toString()) + " will return " + view.toString() );
view.setTag(getItemViewType(position));
return view;
Expand Down Expand Up @@ -148,8 +152,11 @@ public void removeResult(Result result) {
notifyDataSetChanged();
}

public void updateResults(List<Result> results) {
public void updateResults(List<Result> results, String query) {
this.results = results;
StringNormalizer.Result queryNormalized = StringNormalizer.normalizeWithResult(query, false);

fuzzyScore = new FuzzyScore(queryNormalized.codePoints, true);
notifyDataSetChanged();
}

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/fr/neamar/kiss/result/AppResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.pojo.AppPojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.SpaceTokenizer;

public class AppResult extends Result {
Expand All @@ -45,7 +46,7 @@ public class AppResult extends Result {
}

@Override
public View display(final Context context, int position, View convertView) {
public View display(final Context context, int position, View convertView, FuzzyScore fuzzyScore) {
View view = convertView;
if (convertView == null) {
view = inflateFromId(context, R.layout.item_app);
Expand All @@ -54,7 +55,8 @@ public View display(final Context context, int position, View convertView) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

TextView appName = view.findViewById(R.id.item_app_name);
appName.setText(appPojo.getName());

displayHighlighted(appPojo.normalizedName, appPojo.getName(), fuzzyScore, appName, context);

TextView tagsView = view.findViewById(R.id.item_app_tag);
// Hide tags view if tags are empty or if user has selected to hide them when query doesn't match
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/fr/neamar/kiss/result/ContactsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import fr.neamar.kiss.searcher.QueryInterface;
import fr.neamar.kiss.ui.ImprovedQuickContactBadge;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;

public class ContactsResult extends Result {
private final ContactsPojo contactPojo;
Expand All @@ -41,14 +42,14 @@ public class ContactsResult extends Result {
}

@Override
public View display(Context context, int position, View convertView) {
public View display(Context context, int position, View convertView, FuzzyScore fuzzyScore) {
View view = convertView;
if (convertView == null)
view = inflateFromId(context, R.layout.item_contact);

// Contact name
TextView contactName = view.findViewById(R.id.item_contact_name);
contactName.setText(contactPojo.getName());
displayHighlighted(contactPojo.normalizedName, contactPojo.getName(), fuzzyScore, contactName, context);

// Contact phone
TextView contactPhone = view.findViewById(R.id.item_contact_phone);
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/fr/neamar/kiss/result/PhoneResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import fr.neamar.kiss.forwarder.Permission;
import fr.neamar.kiss.pojo.PhonePojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;

public class PhoneResult extends Result {
private final PhonePojo phonePojo;
Expand All @@ -28,7 +29,7 @@ public class PhoneResult extends Result {
}

@Override
public View display(Context context, int position, View v) {
public View display(Context context, int position, View v, FuzzyScore fuzzyScore) {
if (v == null)
v = inflateFromId(context, R.layout.item_phone);

Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/fr/neamar/kiss/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.support.annotation.StringRes;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.Pair;
Expand All @@ -19,6 +18,7 @@
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;
Expand All @@ -30,6 +30,7 @@
import fr.neamar.kiss.UIColors;
import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.db.DBHelper;
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.pojo.AppPojo;
import fr.neamar.kiss.pojo.ContactsPojo;
import fr.neamar.kiss.pojo.PhonePojo;
Expand All @@ -39,6 +40,7 @@
import fr.neamar.kiss.pojo.ShortcutsPojo;
import fr.neamar.kiss.searcher.QueryInterface;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;

public abstract class Result {
/**
Expand Down Expand Up @@ -80,9 +82,32 @@ public String toString() {
*
* @param context android context
* @param convertView a view to be recycled
* @param fuzzyScore
* @return a view to display as item
*/
public abstract View display(Context context, int position, View convertView);
public abstract View display(Context context, int position, View convertView, FuzzyScore fuzzyScore);

public void displayHighlighted(StringNormalizer.Result normalized, String text, FuzzyScore fuzzyScore, TextView view, Context context) {
FuzzyScore.MatchInfo matchInfo = fuzzyScore.match(normalized.codePoints);

if (!matchInfo.match) {
view.setText(text);
return;
}

SpannableString enriched = new SpannableString(text);
int primaryColor = UIColors.getPrimaryColor(context);

for (Pair<Integer, Integer> position : matchInfo.getMatchedSequences()) {
enriched.setSpan(
new ForegroundColorSpan(primaryColor),
normalized.mapPosition(position.first),
normalized.mapPosition(position.second),
Spannable.SPAN_INCLUSIVE_INCLUSIVE
);
}
view.setText(enriched);
}

public String getSection() {
// get the normalized first letter of the pojo
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/fr/neamar/kiss/result/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.pojo.SearchPojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;

import static fr.neamar.kiss.R.drawable.search;

Expand All @@ -26,7 +27,7 @@ public class SearchResult extends Result {
}

@Override
public View display(Context context, int position, View v) {
public View display(Context context, int position, View v, FuzzyScore fuzzyScore) {
if (v == null)
v = inflateFromId(context, R.layout.item_search);

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/fr/neamar/kiss/result/SettingsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import fr.neamar.kiss.R;
import fr.neamar.kiss.pojo.SettingsPojo;
import fr.neamar.kiss.utils.FuzzyScore;

public class SettingsResult extends Result {
private final SettingsPojo settingPojo;
Expand All @@ -23,12 +24,12 @@ public class SettingsResult extends Result {
}

@Override
public View display(Context context, int position, View v) {
public View display(Context context, int position, View v, FuzzyScore fuzzyScore) {
if (v == null)
v = inflateFromId(context, R.layout.item_setting);

TextView settingName = v.findViewById(R.id.item_setting_name);
settingName.setText(settingPojo.getName());
displayHighlighted(settingPojo.normalizedName, settingPojo.getName(), fuzzyScore, settingName, context);

ImageView settingIcon = v.findViewById(R.id.item_setting_icon);
settingIcon.setImageDrawable(getDrawable(context));
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.pojo.ShortcutsPojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.SpaceTokenizer;

public class ShortcutsResult extends Result {
Expand All @@ -46,7 +47,7 @@ public class ShortcutsResult extends Result {
}

@Override
public View display(final Context context, int position, View v) {
public View display(final Context context, int position, View v, FuzzyScore fuzzyScore) {
if (v == null)
v = inflateFromId(context, R.layout.item_shortcut);

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/searcher/Searcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void onPostExecute(Void param) {
}
activity.beforeListChange();

activity.adapter.updateResults(results);
activity.adapter.updateResults(results, query);

activity.afterListChange();
}
Expand Down

0 comments on commit 858e132

Please sign in to comment.