Fix some coloring issues with dark theme

This commit is contained in:
Stefan Niedermann 2020-06-01 22:09:23 +02:00
parent 9664561935
commit 93cb684791
5 changed files with 14 additions and 15 deletions

View file

@ -242,7 +242,7 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) {
if (binding != null && isAttachedToWindow(binding.editContent)) { if (binding != null && isAttachedToWindow(binding.editContent)) {
binding.editContent.clearFocus(); binding.editContent.clearFocus();
binding.editContent.setText(searchAndColor(new SpannableString(getContent()), newText, getResources(), current, mainColor, textColor), TextView.BufferType.SPANNABLE); binding.editContent.setText(searchAndColor(new SpannableString(getContent()), newText, requireContext(), current, mainColor, textColor), TextView.BufferType.SPANNABLE);
} }
} }
} }

View file

@ -185,7 +185,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) {
if (binding != null && ViewCompat.isAttachedToWindow(binding.singleNoteContent)) { if (binding != null && ViewCompat.isAttachedToWindow(binding.singleNoteContent)) {
binding.singleNoteContent.setText( binding.singleNoteContent.setText(
searchAndColor(new SpannableString(parseCompat(markdownProcessor, getContent())), newText, getResources(), current, mainColor, textColor), searchAndColor(new SpannableString(parseCompat(markdownProcessor, getContent())), newText, requireContext(), current, mainColor, textColor),
TextView.BufferType.SPANNABLE); TextView.BufferType.SPANNABLE);
} }
} }

View file

@ -149,7 +149,7 @@ public class NoteReadonlyFragment extends SearchableBaseNoteFragment {
@Override @Override
protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) {
if ((binding != null) && isAttachedToWindow(binding.singleNoteContent)) { if ((binding != null) && isAttachedToWindow(binding.singleNoteContent)) {
binding.singleNoteContent.setText(searchAndColor(new SpannableString(parseCompat(markdownProcessor, getContent())), newText, getResources(), current, mainColor, textColor), TextView.BufferType.SPANNABLE); binding.singleNoteContent.setText(searchAndColor(new SpannableString(parseCompat(markdownProcessor, getContent())), newText, requireContext(), current, mainColor, textColor), TextView.BufferType.SPANNABLE);
} }
} }

View file

@ -17,8 +17,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemBinding; import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemBinding;
import it.niedermann.owncloud.notes.util.ColorUtil;
import it.niedermann.owncloud.notes.util.Notes; import it.niedermann.owncloud.notes.util.Notes;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION; import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
@ -69,7 +69,7 @@ public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnLo
binding.noteFavorite.setOnClickListener(view -> noteClickListener.onNoteFavoriteClick(getAdapterPosition(), view)); binding.noteFavorite.setOnClickListener(view -> noteClickListener.onNoteFavoriteClick(getAdapterPosition(), view));
@ColorInt final int searchBackground = binding.noteExcerpt.getContext().getResources().getColor(R.color.bg_highlighted); @ColorInt final int searchBackground = binding.noteExcerpt.getContext().getResources().getColor(R.color.bg_highlighted);
@ColorInt final int searchForeground = ColorUtil.contrastRatioIsSufficient(mainColor, searchBackground) ? mainColor : Color.BLACK; @ColorInt final int searchForeground = BrandingUtil.getSecondaryForegroundColorDependingOnTheme(binding.noteExcerpt.getContext(), mainColor);
if (!TextUtils.isEmpty(searchQuery)) { if (!TextUtils.isEmpty(searchQuery)) {
final Pattern pattern = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE); final Pattern pattern = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE);
SpannableString spannableString = new SpannableString(note.getTitle()); SpannableString spannableString = new SpannableString(note.getTitle());

View file

@ -19,8 +19,7 @@
*/ */
package it.niedermann.owncloud.notes.util; package it.niedermann.owncloud.notes.util;
import android.content.res.Resources; import android.content.Context;
import android.graphics.Color;
import android.text.Spannable; import android.text.Spannable;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
@ -34,8 +33,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import static it.niedermann.owncloud.notes.util.ColorUtil.contrastRatioIsSufficient;
import static it.niedermann.owncloud.notes.util.ColorUtil.isColorDark; import static it.niedermann.owncloud.notes.util.ColorUtil.isColorDark;
public class DisplayUtils { public class DisplayUtils {
@ -44,7 +43,7 @@ public class DisplayUtils {
} }
public static Spannable searchAndColor(Spannable spannable, CharSequence searchText, @NonNull Resources resources, @Nullable Integer current, @ColorInt int mainColor, @ColorInt int textColor) { public static Spannable searchAndColor(Spannable spannable, CharSequence searchText, @NonNull Context context, @Nullable Integer current, @ColorInt int mainColor, @ColorInt int textColor) {
CharSequence text = spannable.toString(); CharSequence text = spannable.toString();
Object[] spansToRemove = spannable.getSpans(0, text.length(), Object.class); Object[] spansToRemove = spannable.getSpans(0, text.length(), Object.class);
@ -64,7 +63,7 @@ public class DisplayUtils {
while (m.find()) { while (m.find()) {
int start = m.start(); int start = m.start();
int end = m.end(); int end = m.end();
spannable.setSpan(new SearchSpan(resources, mainColor, textColor, (current != null && i == current)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(new SearchSpan(context, mainColor, textColor, (current != null && i == current)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
i++; i++;
} }
@ -76,14 +75,14 @@ public class DisplayUtils {
private final boolean current; private final boolean current;
@NonNull @NonNull
Resources resources; Context context;
@ColorInt @ColorInt
private final int mainColor; private final int mainColor;
@ColorInt @ColorInt
private final int textColor; private final int textColor;
SearchSpan(@NonNull Resources resources, @ColorInt int mainColor, @ColorInt int textColor, boolean current) { SearchSpan(@NonNull Context context, @ColorInt int mainColor, @ColorInt int textColor, boolean current) {
this.resources = resources; this.context = context;
this.mainColor = mainColor; this.mainColor = mainColor;
this.textColor = textColor; this.textColor = textColor;
this.current = current; this.current = current;
@ -91,8 +90,8 @@ public class DisplayUtils {
@Override @Override
public void updateDrawState(TextPaint tp) { public void updateDrawState(TextPaint tp) {
tp.bgColor = current ? isColorDark(mainColor) ? mainColor : textColor : resources.getColor(R.color.bg_highlighted); tp.bgColor = current ? isColorDark(mainColor) ? mainColor : textColor : context.getResources().getColor(R.color.bg_highlighted);
tp.setColor(current ? isColorDark(mainColor) ? textColor : mainColor : contrastRatioIsSufficient(mainColor, resources.getColor(R.color.bg_highlighted)) ? mainColor : Color.BLACK); tp.setColor(current ? isColorDark(mainColor) ? textColor : mainColor : BrandingUtil.getSecondaryForegroundColorDependingOnTheme(context, mainColor));
tp.setFakeBoldText(true); tp.setFakeBoldText(true);
} }