From 93cb68479158dcc31bb82426c3987c8272c285b3 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Mon, 1 Jun 2020 22:09:23 +0200 Subject: [PATCH] Fix some coloring issues with dark theme --- .../android/fragment/NoteEditFragment.java | 2 +- .../android/fragment/NotePreviewFragment.java | 2 +- .../fragment/NoteReadonlyFragment.java | 2 +- .../owncloud/notes/model/NoteViewHolder.java | 4 ++-- .../owncloud/notes/util/DisplayUtils.java | 19 +++++++++---------- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java index 3f390f61..7580c1a8 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java @@ -242,7 +242,7 @@ public class NoteEditFragment extends SearchableBaseNoteFragment { protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { if (binding != null && isAttachedToWindow(binding.editContent)) { 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); } } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java index af7c36c5..635dd66a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java @@ -185,7 +185,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { if (binding != null && ViewCompat.isAttachedToWindow(binding.singleNoteContent)) { 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); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteReadonlyFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteReadonlyFragment.java index c52a9640..55c5e70d 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteReadonlyFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteReadonlyFragment.java @@ -149,7 +149,7 @@ public class NoteReadonlyFragment extends SearchableBaseNoteFragment { @Override protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) { 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); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/model/NoteViewHolder.java b/app/src/main/java/it/niedermann/owncloud/notes/model/NoteViewHolder.java index 7535c278..ac9c4c8b 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/model/NoteViewHolder.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/model/NoteViewHolder.java @@ -17,8 +17,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; 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.util.ColorUtil; import it.niedermann.owncloud.notes.util.Notes; 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)); @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)) { final Pattern pattern = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE); SpannableString spannableString = new SpannableString(note.getTitle()); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/DisplayUtils.java b/app/src/main/java/it/niedermann/owncloud/notes/util/DisplayUtils.java index a0e8ca03..e55e497e 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/util/DisplayUtils.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/DisplayUtils.java @@ -19,8 +19,7 @@ */ package it.niedermann.owncloud.notes.util; -import android.content.res.Resources; -import android.graphics.Color; +import android.content.Context; import android.text.Spannable; import android.text.TextPaint; import android.text.TextUtils; @@ -34,8 +33,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; 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; 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(); Object[] spansToRemove = spannable.getSpans(0, text.length(), Object.class); @@ -64,7 +63,7 @@ public class DisplayUtils { while (m.find()) { int start = m.start(); 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++; } @@ -76,14 +75,14 @@ public class DisplayUtils { private final boolean current; @NonNull - Resources resources; + Context context; @ColorInt private final int mainColor; @ColorInt private final int textColor; - SearchSpan(@NonNull Resources resources, @ColorInt int mainColor, @ColorInt int textColor, boolean current) { - this.resources = resources; + SearchSpan(@NonNull Context context, @ColorInt int mainColor, @ColorInt int textColor, boolean current) { + this.context = context; this.mainColor = mainColor; this.textColor = textColor; this.current = current; @@ -91,8 +90,8 @@ public class DisplayUtils { @Override public void updateDrawState(TextPaint tp) { - tp.bgColor = current ? isColorDark(mainColor) ? mainColor : textColor : resources.getColor(R.color.bg_highlighted); - tp.setColor(current ? isColorDark(mainColor) ? textColor : mainColor : contrastRatioIsSufficient(mainColor, resources.getColor(R.color.bg_highlighted)) ? mainColor : Color.BLACK); + tp.bgColor = current ? isColorDark(mainColor) ? mainColor : textColor : context.getResources().getColor(R.color.bg_highlighted); + tp.setColor(current ? isColorDark(mainColor) ? textColor : mainColor : BrandingUtil.getSecondaryForegroundColorDependingOnTheme(context, mainColor)); tp.setFakeBoldText(true); }