Skip to content

Commit

Permalink
Adapt colors based on chosen medicine background color
Browse files Browse the repository at this point in the history
  • Loading branch information
Futsch1 committed Feb 19, 2024
1 parent e6f18d4 commit 1e545ce
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.futsch1.medtimer.helpers;

import android.content.Context;
import android.util.TypedValue;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.graphics.ColorUtils;

import com.google.android.material.card.MaterialCardView;

public class ViewColorHelper {
public static void setCardBackground(MaterialCardView cardView, TextView[] textViews, int backgroundColor) {
int defaultTextViewColor = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorOnSurfaceVariant);
double contrastTextView = ColorUtils.calculateContrast(defaultTextViewColor, backgroundColor);
int cardDefaultBackground = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorSurface);
double contrastBackground = ColorUtils.calculateContrast(cardDefaultBackground, backgroundColor);
setTextColor(textViews, contrastTextView < contrastBackground ? cardDefaultBackground : defaultTextViewColor);
cardView.setCardBackgroundColor(backgroundColor);
}

private static int getThemeColor(Context context, int attribute) {
TypedValue themeColor = new TypedValue();
context.getTheme().resolveAttribute(attribute, themeColor, true);
return themeColor.data;
}

private static void setTextColor(TextView[] textViews, int color) {
for (TextView textView : textViews) {
textView.setTextColor(color);
}
}

public static void setButtonBackground(Button button, int backgroundColor) {
int primaryColor = getThemeColor(button.getContext(), com.google.android.material.R.attr.colorPrimary);
int onPrimaryColor = getThemeColor(button.getContext(), com.google.android.material.R.attr.colorOnPrimary);
double primaryContrast = ColorUtils.calculateContrast(primaryColor, backgroundColor);
double onPrimaryContrast = ColorUtils.calculateContrast(onPrimaryColor, backgroundColor);
setTextColor(button, primaryContrast > onPrimaryContrast ? primaryColor : onPrimaryColor);
button.setBackgroundColor(backgroundColor);
}

private static void setTextColor(Button button, int buttonTextColor) {
button.setTextColor(buttonTextColor);
}

public static void setDefaultColors(MaterialCardView cardView, TextView[] textViews) {
int defaultTextViewColor = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorOnSurfaceVariant);
int cardDefaultBackground = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorSurface);
cardView.setCardBackgroundColor(cardDefaultBackground);
setTextColor(textViews, defaultTextViewColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.futsch1.medtimer.database.Medicine;
import com.futsch1.medtimer.database.Reminder;
import com.futsch1.medtimer.helpers.SwipeHelper;
import com.futsch1.medtimer.helpers.ViewColorHelper;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.google.android.material.textfield.TextInputEditText;
Expand Down Expand Up @@ -86,14 +87,14 @@ protected void onCreate(Bundle savedInstanceState) {

color = getIntent().getIntExtra(EXTRA_COLOR, Color.DKGRAY);
colorButton = findViewById(R.id.selectColor);
colorButton.setBackgroundColor(color);
ViewColorHelper.setButtonBackground(colorButton, color);
colorButton.setOnClickListener(v -> {
ColorPickerDialog.Builder builder = new ColorPickerDialog.Builder(this)
.setTitle(R.string.color)
.setPositiveButton(getString(R.string.confirm),
(ColorEnvelopeListener) (envelope, fromUser) -> {
color = envelope.getColor();
colorButton.setBackgroundColor(color);
ViewColorHelper.setButtonBackground(colorButton, color);
Toast.makeText(this, R.string.change_color_toast, Toast.LENGTH_LONG).show();
})
.setNegativeButton(getString(R.string.cancel),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.futsch1.medtimer.R;
import com.futsch1.medtimer.database.MedicineWithReminders;
import com.futsch1.medtimer.helpers.TimeHelper;
import com.futsch1.medtimer.helpers.ViewColorHelper;
import com.google.android.material.card.MaterialCardView;

import java.util.ArrayList;
Expand Down Expand Up @@ -81,10 +82,9 @@ public void bind(MedicineWithReminders medicineWithReminders, DeleteCallback del
itemView.setOnClickListener(view -> startEditActivity(medicineWithReminders));

if (medicineWithReminders.medicine.useColor) {
((MaterialCardView) itemView).setCardBackgroundColor(medicineWithReminders.medicine.color);
ViewColorHelper.setCardBackground((MaterialCardView) itemView, new TextView[]{medicineNameView, remindersSummaryView}, medicineWithReminders.medicine.color);
} else {
int defaultColor = new MaterialCardView(itemView.getContext()).getCardBackgroundColor().getDefaultColor();
((MaterialCardView) itemView).setCardBackgroundColor(defaultColor);
ViewColorHelper.setDefaultColors((MaterialCardView) itemView, new TextView[]{medicineNameView, remindersSummaryView});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.futsch1.medtimer.R;
import com.futsch1.medtimer.database.ReminderEvent;
import com.futsch1.medtimer.helpers.ViewColorHelper;
import com.futsch1.medtimer.reminders.ReminderProcessor;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.chip.Chip;
Expand Down Expand Up @@ -66,10 +67,9 @@ public void bind(ReminderEvent reminderEvent) {
});

if (reminderEvent.useColor) {
((MaterialCardView) itemView).setCardBackgroundColor(reminderEvent.color);
ViewColorHelper.setCardBackground((MaterialCardView) itemView, new TextView[]{reminderEventText}, reminderEvent.color);
} else {
int defaultColor = new MaterialCardView(itemView.getContext()).getCardBackgroundColor().getDefaultColor();
((MaterialCardView) itemView).setCardBackgroundColor(defaultColor);
ViewColorHelper.setDefaultColors((MaterialCardView) itemView, new TextView[]{reminderEventText});
}
}
}

0 comments on commit 1e545ce

Please sign in to comment.