Enhance global note search highlight

This commit is contained in:
Stefan Niedermann 2020-03-26 13:10:20 +01:00
parent 9a95143f4e
commit d2d33d8508
2 changed files with 19 additions and 14 deletions

View file

@ -1,11 +1,13 @@
package it.niedermann.owncloud.notes.persistence;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import androidx.annotation.NonNull;
@ -23,6 +25,7 @@ import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.model.Item;
import it.niedermann.owncloud.notes.model.SectionItem;
import it.niedermann.owncloud.notes.util.DisplayUtils;
import it.niedermann.owncloud.notes.util.NoteUtil;
public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@ -32,6 +35,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
private final Category category;
private final CharSequence searchQuery;
private final long accountId;
private final int searchForeground;
private final int searchBackground;
public LoadNotesListTask(long accountId, @NonNull Context context, @NonNull NotesLoadedListener callback, @NonNull Category category, @Nullable CharSequence searchQuery) {
this.context = context;
@ -39,6 +44,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
this.category = category;
this.searchQuery = searchQuery;
this.accountId = accountId;
this.searchBackground = context.getResources().getColor(R.color.bg_highlighted);
this.searchForeground = DisplayUtils.getForeground(Integer.toHexString(this.searchBackground)) ? Color.WHITE : context.getResources().getColor(R.color.primary);
}
@Override
@ -59,8 +66,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
SpannableString spannableString = new SpannableString(dbNote.getTitle());
Matcher matcher = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE).matcher(spannableString);
while (matcher.find()) {
spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.primary_dark)),
matcher.start(), matcher.end(), 0);
spannableString.setSpan(new ForegroundColorSpan(searchForeground), matcher.start(), matcher.end(), 0);
spannableString.setSpan(new BackgroundColorSpan(searchBackground), matcher.start(), matcher.end(), 0);
}
dbNote.setTitle(Html.toHtml(spannableString));
@ -68,8 +75,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
spannableString = new SpannableString(dbNote.getExcerpt());
matcher = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE).matcher(spannableString);
while (matcher.find()) {
spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.primary_dark)),
matcher.start(), matcher.end(), 0);
spannableString.setSpan(new ForegroundColorSpan(searchForeground), matcher.start(), matcher.end(), 0);
spannableString.setSpan(new BackgroundColorSpan(searchBackground), matcher.start(), matcher.end(), 0);
}
dbNote.setExcerpt(Html.toHtml(spannableString));

View file

@ -26,7 +26,6 @@ import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.MetricAffectingSpan;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -84,7 +83,7 @@ public class DisplayUtils {
@Override
public void updateDrawState(TextPaint tp) {
tp.bgColor = current ? bgColorPrimary : bgColorSecondary;
tp.setColor(current ? getForeground(Integer.toHexString(tp.bgColor)) : bgColorPrimary);
tp.setColor(current ? (getForeground(Integer.toHexString(tp.bgColor)) ? Color.WHITE : Color.BLACK) : bgColorPrimary);
tp.setFakeBoldText(true);
}
@ -92,14 +91,13 @@ public class DisplayUtils {
public void updateMeasureState(@NonNull TextPaint tp) {
tp.setFakeBoldText(true);
}
}
private static @ColorInt
int getForeground(String backgroundColorHex) {
return ((float) (
0.2126 * Integer.valueOf(backgroundColorHex.substring(1, 3), 16)
+ 0.7152 * Integer.valueOf(backgroundColorHex.substring(3, 5), 16)
+ 0.0722 * Integer.valueOf(backgroundColorHex.substring(5, 7), 16)
) < 140) ? Color.WHITE : Color.BLACK;
}
public static boolean getForeground(String backgroundColorHex) {
return ((float) (
0.2126 * Integer.valueOf(backgroundColorHex.substring(1, 3), 16)
+ 0.7152 * Integer.valueOf(backgroundColorHex.substring(3, 5), 16)
+ 0.0722 * Integer.valueOf(backgroundColorHex.substring(5, 7), 16)
) < 140);
}
}