Replace primitive int by PaletteColor

pull/699/head
Alinson S. Xavier 5 years ago
parent 63f2b80515
commit 5d2ff40dc9

@ -101,28 +101,28 @@ public class BaseUserInterfaceTest
h1.setName("Wake up early"); h1.setName("Wake up early");
h1.setQuestion("Did you wake up early today?"); h1.setQuestion("Did you wake up early today?");
h1.setDescription("test description 1"); h1.setDescription("test description 1");
h1.setColor(5); h1.setColor(new PaletteColor(5));
habitList.update(h1); habitList.update(h1);
Habit h2 = fixtures.createShortHabit(); Habit h2 = fixtures.createShortHabit();
h2.setName("Track time"); h2.setName("Track time");
h2.setQuestion("Did you track your time?"); h2.setQuestion("Did you track your time?");
h2.setDescription("test description 2"); h2.setDescription("test description 2");
h2.setColor(5); h2.setColor(new PaletteColor(5));
habitList.update(h2); habitList.update(h2);
Habit h3 = fixtures.createLongHabit(); Habit h3 = fixtures.createLongHabit();
h3.setName("Meditate"); h3.setName("Meditate");
h3.setQuestion("Did meditate today?"); h3.setQuestion("Did meditate today?");
h3.setDescription("test description 3"); h3.setDescription("test description 3");
h3.setColor(10); h3.setColor(new PaletteColor(10));
habitList.update(h3); habitList.update(h3);
Habit h4 = fixtures.createEmptyHabit(); Habit h4 = fixtures.createEmptyHabit();
h4.setName(EMPTY_DESCRIPTION_HABIT_NAME); h4.setName(EMPTY_DESCRIPTION_HABIT_NAME);
h4.setQuestion("Did you read books today?"); h4.setQuestion("Did you read books today?");
h4.setDescription(""); h4.setDescription("");
h4.setColor(2); h4.setColor(new PaletteColor(2));
habitList.update(h4); habitList.update(h4);
} }

@ -56,7 +56,7 @@ public class HabitFixtures
habit.setName("Meditate"); habit.setName("Meditate");
habit.setQuestion("Did you meditate this morning?"); habit.setQuestion("Did you meditate this morning?");
habit.setDescription("This is a test description"); habit.setDescription("This is a test description");
habit.setColor(5); habit.setColor(new PaletteColor(5));
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habit.setId(id); habit.setId(id);
habitList.add(habit); habitList.add(habit);
@ -67,7 +67,7 @@ public class HabitFixtures
{ {
Habit habit = createEmptyHabit(); Habit habit = createEmptyHabit();
habit.setFrequency(new Frequency(3, 7)); habit.setFrequency(new Frequency(3, 7));
habit.setColor(7); habit.setColor(new PaletteColor(7));
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getToday();
int marks[] = { 0, 1, 3, 5, 7, 8, 9, 10, 12, 14, 15, 17, 19, 20, 26, 27, int marks[] = { 0, 1, 3, 5, 7, 8, 9, 10, 12, 14, 15, 17, 19, 20, 26, 27,
@ -84,7 +84,7 @@ public class HabitFixtures
{ {
Habit habit = createEmptyHabit(); Habit habit = createEmptyHabit();
habit.setFrequency(new Frequency(1, 2)); habit.setFrequency(new Frequency(1, 2));
habit.setColor(11); habit.setColor(new PaletteColor(11));
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getToday();
int marks[] = {0, 3, 5, 6, 7, 10, 13, 14, 15, 18, 21, 22, 23, 24, 27, 28, 30, 31, 34, 37, int marks[] = {0, 3, 5, 6, 7, 10, 13, 14, 15, 18, 21, 22, 23, 24, 27, 28, 30, 31, 34, 37,

@ -20,7 +20,6 @@
package org.isoron.uhabits.activities.common.views; package org.isoron.uhabits.activities.common.views;
import androidx.test.filters.*; import androidx.test.filters.*;
import androidx.test.runner.*;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
@ -49,7 +48,7 @@ public class BarChartTest extends BaseViewTest
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getToday();
CheckmarkList checkmarks = habit.getCheckmarks(); CheckmarkList checkmarks = habit.getCheckmarks();
view.setCheckmarks(checkmarks.getByInterval(today.minus(20), today)); view.setCheckmarks(checkmarks.getByInterval(today.minus(20), today));
view.setColor(PaletteUtils.getColor(targetContext, habit.getColor())); view.setColor(PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), targetContext));
view.setTarget(200.0); view.setTarget(200.0);
measureView(view, dpToPixels(300), dpToPixels(200)); measureView(view, dpToPixels(300), dpToPixels(200));
} }

@ -19,10 +19,8 @@
package org.isoron.uhabits.activities.common.views; package org.isoron.uhabits.activities.common.views;
import androidx.test.ext.junit.runners.*;
import androidx.test.filters.*; import androidx.test.filters.*;
import androidx.test.runner.*;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
@ -49,7 +47,7 @@ public class FrequencyChartTest extends BaseViewTest
view = new FrequencyChart(targetContext); view = new FrequencyChart(targetContext);
view.setFrequency(habit.getRepetitions().getWeekdayFrequency()); view.setFrequency(habit.getRepetitions().getWeekdayFrequency());
view.setColor(PaletteUtils.getAndroidTestColor(habit.getColor())); view.setColor(PaletteUtilsKt.toFixedAndroidColor(habit.getColor()));
measureView(view, dpToPixels(300), dpToPixels(100)); measureView(view, dpToPixels(300), dpToPixels(100));
} }

@ -58,7 +58,7 @@ public class HistoryChartTest extends BaseViewTest
chart = new HistoryChart(targetContext); chart = new HistoryChart(targetContext);
chart.setSkipEnabled(true); chart.setSkipEnabled(true);
chart.setCheckmarks(habit.getCheckmarks().getAllValues()); chart.setCheckmarks(habit.getCheckmarks().getAllValues());
chart.setColor(PaletteUtils.getAndroidTestColor(habit.getColor())); chart.setColor(PaletteUtilsKt.toFixedAndroidColor(habit.getColor()));
measureView(chart, dpToPixels(400), dpToPixels(200)); measureView(chart, dpToPixels(400), dpToPixels(200));
controller = mock(HistoryChart.Controller.class); controller = mock(HistoryChart.Controller.class);

@ -54,7 +54,7 @@ public class ScoreChartTest extends BaseViewTest
view = new ScoreChart(targetContext); view = new ScoreChart(targetContext);
view.setScores(habit.getScores().toList()); view.setScores(habit.getScores().toList());
view.setColor(PaletteUtils.getColor(targetContext, habit.getColor())); view.setColor(PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), targetContext));
view.setBucketSize(7); view.setBucketSize(7);
measureView(view, dpToPixels(300), dpToPixels(200)); measureView(view, dpToPixels(300), dpToPixels(200));
} }

@ -48,7 +48,7 @@ public class StreakChartTest extends BaseViewTest
Habit habit = fixtures.createLongHabit(); Habit habit = fixtures.createLongHabit();
view = new StreakChart(targetContext); view = new StreakChart(targetContext);
view.setColor(PaletteUtils.getAndroidTestColor(habit.getColor())); view.setColor(PaletteUtilsKt.toFixedAndroidColor(habit.getColor()));
view.setStreaks(habit.getStreaks().getBest(5)); view.setStreaks(habit.getStreaks().getBest(5));
measureView(view, dpToPixels(300), dpToPixels(100)); measureView(view, dpToPixels(300), dpToPixels(100));
} }

@ -79,7 +79,7 @@ class HabitCardViewTest : BaseViewTest() {
@Test @Test
fun testChangeModel() { fun testChangeModel() {
habit1.name = "Wake up early" habit1.name = "Wake up early"
habit1.color = 2 habit1.color = PaletteColor(2)
habit1.observable.notifyListeners() habit1.observable.notifyListeners()
Thread.sleep(500) Thread.sleep(500)
assertRenders(view, "$PATH/render_changed.png") assertRenders(view, "$PATH/render_changed.png")

@ -47,7 +47,7 @@ public class TargetWidgetTest extends BaseViewTest
prefs.setWidgetOpacity(255); prefs.setWidgetOpacity(255);
habit = fixtures.createLongNumericalHabit(); habit = fixtures.createLongNumericalHabit();
habit.setColor(11); habit.setColor(new PaletteColor(11));
TargetWidget widget = new TargetWidget(targetContext, 0, habit); TargetWidget widget = new TargetWidget(targetContext, 0, habit);
view = convertToView(widget, 400, 400); view = convertToView(widget, 400, 400);
} }

@ -19,6 +19,7 @@
package org.isoron.uhabits.activities.common.dialogs; package org.isoron.uhabits.activities.common.dialogs;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.callbacks.*; import org.isoron.uhabits.core.ui.callbacks.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
@ -31,8 +32,8 @@ public class ColorPickerDialog extends com.android.colorpicker.ColorPickerDialog
{ {
super.setOnColorSelectedListener(c -> super.setOnColorSelectedListener(c ->
{ {
c = PaletteUtils.colorToPaletteIndex(getContext(), c); PaletteColor pc = PaletteUtilsKt.toPaletteColor(c, getContext());
callback.onColorPicked(c); callback.onColorPicked(pc);
}); });
} }
} }

@ -24,6 +24,7 @@ import android.content.*;
import org.isoron.androidbase.activities.*; import org.isoron.androidbase.activities.*;
import org.isoron.androidbase.utils.*; import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import javax.inject.*; import javax.inject.*;
@ -39,14 +40,14 @@ public class ColorPickerDialogFactory
this.context = context; this.context = context;
} }
public ColorPickerDialog create(int paletteColor) public ColorPickerDialog create(PaletteColor color)
{ {
ColorPickerDialog dialog = new ColorPickerDialog(); ColorPickerDialog dialog = new ColorPickerDialog();
StyledResources res = new StyledResources(context); StyledResources res = new StyledResources(context);
int color = PaletteUtils.getColor(context, paletteColor); int androidColor = PaletteUtilsKt.toThemedAndroidColor(color, context);
dialog.initialize(R.string.color_picker_default_title, res.getPalette(), dialog.initialize(R.string.color_picker_default_title, res.getPalette(),
color, 4, com.android.colorpicker.ColorPickerDialog.SIZE_SMALL); androidColor, 4, com.android.colorpicker.ColorPickerDialog.SIZE_SMALL);
return dialog; return dialog;
} }

@ -179,7 +179,7 @@ public class HistoryEditorDialog extends AppCompatDialogFragment
if (getContext() == null || habit == null || historyChart == null) if (getContext() == null || habit == null || historyChart == null)
return; return;
int color = PaletteUtils.getColor(getContext(), habit.getColor()); int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext());
historyChart.setColor(color); historyChart.setColor(color);
historyChart.setCheckmarks(checkmarks); historyChart.setCheckmarks(checkmarks);
historyChart.setNumerical(habit.isNumerical()); historyChart.setNumerical(habit.isNumerical());

@ -21,12 +21,12 @@ package org.isoron.uhabits.activities.common.views;
import android.content.*; import android.content.*;
import android.graphics.*; import android.graphics.*;
import android.graphics.Color;
import android.graphics.Paint.*; import android.graphics.Paint.*;
import android.util.*; import android.util.*;
import android.view.*; import android.view.*;
import androidx.annotation.NonNull; import androidx.annotation.*;
import androidx.annotation.Nullable;
import org.isoron.androidbase.utils.*; import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;

@ -48,7 +48,7 @@ class EditHabitActivity : AppCompatActivity() {
var habitId = -1L var habitId = -1L
var habitType = -1 var habitType = -1
var unit = "" var unit = ""
var paletteColor = 11 var color = PaletteColor(11)
var androidColor = 0 var androidColor = 0
var freqNum = 1 var freqNum = 1
var freqDen = 1 var freqDen = 1
@ -71,7 +71,7 @@ class EditHabitActivity : AppCompatActivity() {
habitId = intent.getLongExtra("habitId", -1) habitId = intent.getLongExtra("habitId", -1)
val habit = component.habitList.getById(habitId)!! val habit = component.habitList.getById(habitId)!!
habitType = habit.type habitType = habit.type
paletteColor = habit.color color = habit.color
freqNum = habit.frequency.numerator freqNum = habit.frequency.numerator
freqDen = habit.frequency.denominator freqDen = habit.frequency.denominator
if (habit.hasReminder()) { if (habit.hasReminder()) {
@ -91,7 +91,7 @@ class EditHabitActivity : AppCompatActivity() {
if (state != null) { if (state != null) {
habitId = state.getLong("habitId") habitId = state.getLong("habitId")
habitType = state.getInt("habitType") habitType = state.getInt("habitType")
paletteColor = state.getInt("paletteColor") color = PaletteColor(state.getInt("paletteColor"))
freqNum = state.getInt("freqNum") freqNum = state.getInt("freqNum")
freqDen = state.getInt("freqDen") freqDen = state.getInt("freqDen")
reminderHour = state.getInt("reminderHour") reminderHour = state.getInt("reminderHour")
@ -117,9 +117,9 @@ class EditHabitActivity : AppCompatActivity() {
val colorPickerDialogFactory = ColorPickerDialogFactory(this) val colorPickerDialogFactory = ColorPickerDialogFactory(this)
binding.colorButton.setOnClickListener { binding.colorButton.setOnClickListener {
val dialog = colorPickerDialogFactory.create(paletteColor) val dialog = colorPickerDialogFactory.create(color)
dialog.setListener { paletteColor -> dialog.setListener { paletteColor ->
this.paletteColor = paletteColor this.color = paletteColor
updateColors() updateColors()
} }
dialog.show(supportFragmentManager, "colorPicker") dialog.show(supportFragmentManager, "colorPicker")
@ -165,6 +165,7 @@ class EditHabitActivity : AppCompatActivity() {
reminderMin = minute reminderMin = minute
populateReminder() populateReminder()
} }
override fun onTimeCleared(view: RadialPickerLayout?) { override fun onTimeCleared(view: RadialPickerLayout?) {
reminderHour = -1 reminderHour = -1
reminderMin = -1 reminderMin = -1
@ -208,7 +209,7 @@ class EditHabitActivity : AppCompatActivity() {
habit.name = nameInput.text.trim().toString() habit.name = nameInput.text.trim().toString()
habit.question = questionInput.text.trim().toString() habit.question = questionInput.text.trim().toString()
habit.description = notesInput.text.trim().toString() habit.description = notesInput.text.trim().toString()
habit.color = paletteColor habit.color = color
if (reminderHour >= 0) { if (reminderHour >= 0) {
habit.setReminder(Reminder(reminderHour, reminderMin, reminderDays)) habit.setReminder(Reminder(reminderHour, reminderMin, reminderDays))
} else { } else {
@ -283,7 +284,7 @@ class EditHabitActivity : AppCompatActivity() {
} }
private fun updateColors() { private fun updateColors() {
androidColor = PaletteUtils.getColor(this, paletteColor) androidColor = color.toThemedAndroidColor(this)
binding.colorButton.backgroundTintList = ColorStateList.valueOf(androidColor) binding.colorButton.backgroundTintList = ColorStateList.valueOf(androidColor)
if (!themeSwitcher.isNightMode) { if (!themeSwitcher.isNightMode) {
val darkerAndroidColor = ColorUtils.mixColors(Color.BLACK, androidColor, 0.15f) val darkerAndroidColor = ColorUtils.mixColors(Color.BLACK, androidColor, 0.15f)
@ -297,7 +298,7 @@ class EditHabitActivity : AppCompatActivity() {
with(state) { with(state) {
putLong("habitId", habitId) putLong("habitId", habitId)
putInt("habitType", habitType) putInt("habitType", habitType)
putInt("paletteColor", paletteColor) putInt("paletteColor", color.paletteIndex)
putInt("androidColor", androidColor) putInt("androidColor", androidColor)
putInt("freqNum", freqNum) putInt("freqNum", freqNum)
putInt("freqDen", freqDen) putInt("freqDen", freqDen)

@ -202,7 +202,7 @@ class ListHabitsScreen
activity.startActivityForResult(intent, REQUEST_SETTINGS) activity.startActivityForResult(intent, REQUEST_SETTINGS)
} }
override fun showColorPicker(defaultColor: Int, override fun showColorPicker(defaultColor: PaletteColor,
callback: OnColorPickedCallback) { callback: OnColorPickedCallback) {
val picker = colorPickerFactory.create(defaultColor) val picker = colorPickerFactory.create(defaultColor)
picker.setListener(callback) picker.setListener(callback)

@ -195,7 +195,7 @@ class HabitCardView(
fun getActiveColor(habit: Habit): Int { fun getActiveColor(habit: Habit): Int {
return when (habit.isArchived) { return when (habit.isArchived) {
true -> sres.getColor(R.attr.mediumContrastTextColor) true -> sres.getColor(R.attr.mediumContrastTextColor)
false -> PaletteUtils.getColor(context, habit.color) false -> habit.color.toThemedAndroidColor(context)
} }
} }

@ -104,7 +104,7 @@ public class ShowHabitRootView extends BaseRootView
if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) if (!res.getBoolean(R.attr.useHabitColorAsPrimary))
return super.getToolbarColor(); return super.getToolbarColor();
return PaletteUtils.getColor(getContext(), habit.getColor()); return PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext());
} }
@Override @Override

@ -143,7 +143,7 @@ public class BarCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
int color = PaletteUtils.getColor(getContext(), habit.getColor()); int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext());
title.setTextColor(color); title.setTextColor(color);
chart.setColor(color); chart.setColor(color);
if (habit.isNumerical()) if (habit.isNumerical())

@ -102,8 +102,7 @@ public class FrequencyCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
int paletteColor = getHabit().getColor(); int color = PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
int color = PaletteUtils.getColor(getContext(), paletteColor);
title.setTextColor(color); title.setTextColor(color);
chart.setColor(color); chart.setColor(color);
} }

@ -131,7 +131,7 @@ public class HistoryCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
int color = PaletteUtils.getColor(getContext(), habit.getColor()); int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext());
title.setTextColor(color); title.setTextColor(color);
chart.setColor(color); chart.setColor(color);
if(habit.isNumerical()) if(habit.isNumerical())

@ -173,7 +173,7 @@ public class OverviewCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
color = PaletteUtils.getColor(getContext(), getHabit().getColor()); color = PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
refreshColors(); refreshColors();
} }
} }

@ -160,8 +160,7 @@ public class ScoreCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
int color = int color = PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
PaletteUtils.getColor(getContext(), getHabit().getColor());
title.setTextColor(color); title.setTextColor(color);
chart.setColor(color); chart.setColor(color);
} }

@ -100,7 +100,7 @@ public class StreakCard extends HabitCard
public void onPreExecute() public void onPreExecute()
{ {
int color = int color =
PaletteUtils.getColor(getContext(), getHabit().getColor()); PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
title.setTextColor(color); title.setTextColor(color);
streakChart.setColor(color); streakChart.setColor(color);
} }

@ -30,7 +30,6 @@ import org.isoron.uhabits.core.tasks.*
import org.isoron.uhabits.databinding.* import org.isoron.uhabits.databinding.*
import org.isoron.uhabits.utils.* import org.isoron.uhabits.utils.*
import org.isoron.uhabits.utils.PaletteUtils.getAndroidTestColor import org.isoron.uhabits.utils.PaletteUtils.getAndroidTestColor
import org.isoron.uhabits.utils.PaletteUtils.getColor
import java.util.* import java.util.*
class SubtitleCard(context: Context?, attrs: AttributeSet?) : HabitCard(context, attrs) { class SubtitleCard(context: Context?, attrs: AttributeSet?) : HabitCard(context, attrs) {
@ -43,7 +42,7 @@ class SubtitleCard(context: Context?, attrs: AttributeSet?) : HabitCard(context,
public override fun refreshData() { public override fun refreshData() {
val habit = habit val habit = habit
val color = getColor(context, habit.color) val color = habit.color.toThemedAndroidColor(context)
if (habit.isNumerical) { if (habit.isNumerical) {
binding.targetText.text = "${habit.targetValue.toShortString()} ${habit.unit}" binding.targetText.text = "${habit.targetValue.toShortString()} ${habit.unit}"
} else { } else {

@ -171,7 +171,7 @@ public class TargetCard extends HabitCard
@Override @Override
public void onPreExecute() public void onPreExecute()
{ {
int color = PaletteUtils.getColor(context, habit.getColor()); int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), context);
if(title != null) title.setTextColor(color); if(title != null) title.setTextColor(color);
chart.setColor(color); chart.setColor(color);
} }

@ -25,9 +25,9 @@ import android.graphics.*
import android.graphics.BitmapFactory.* import android.graphics.BitmapFactory.*
import android.os.* import android.os.*
import android.os.Build.VERSION.* import android.os.Build.VERSION.*
import android.util.*
import androidx.core.app.* import androidx.core.app.*
import androidx.core.app.NotificationCompat.* import androidx.core.app.NotificationCompat.*
import android.util.*
import org.isoron.androidbase.* import org.isoron.androidbase.*
import org.isoron.uhabits.R import org.isoron.uhabits.R
import org.isoron.uhabits.core.* import org.isoron.uhabits.core.*
@ -44,7 +44,7 @@ class AndroidNotificationTray
private val pendingIntents: PendingIntentFactory, private val pendingIntents: PendingIntentFactory,
private val preferences: Preferences, private val preferences: Preferences,
private val ringtoneManager: RingtoneManager private val ringtoneManager: RingtoneManager
) : NotificationTray.SystemTray { ) : NotificationTray.SystemTray {
private var active = HashSet<Int>() private var active = HashSet<Int>()
override fun log(msg: String) { override fun log(msg: String) {

@ -4,18 +4,25 @@ import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.util.Log import android.util.Log
import org.isoron.androidbase.utils.StyledResources import org.isoron.androidbase.utils.StyledResources
import org.isoron.uhabits.core.models.*
object PaletteUtils { object PaletteUtils {
@JvmStatic @JvmStatic
fun colorToPaletteIndex(context: Context, color: Int): Int { fun getAndroidTestColor(index: Int) = PaletteColor(index).toFixedAndroidColor()
val palette = StyledResources(context).getPalette() }
return palette.indexOf(color)
fun PaletteColor.toThemedAndroidColor(context: Context): Int {
val palette = StyledResources(context).getPalette()
return if (paletteIndex in palette.indices) {
palette[paletteIndex]
} else {
Log.w("ColorHelper", "Invalid color: $paletteIndex. Returning default.")
palette[0]
} }
}
@JvmStatic fun PaletteColor.toFixedAndroidColor(): Int {
fun getAndroidTestColor(index: Int): Int { return intArrayOf(
val palette = intArrayOf(
Color.parseColor("#D32F2F"), // 0 red Color.parseColor("#D32F2F"), // 0 red
Color.parseColor("#E64A19"), // 1 deep orange Color.parseColor("#E64A19"), // 1 deep orange
Color.parseColor("#F57C00"), // 2 orange Color.parseColor("#F57C00"), // 2 orange
@ -36,19 +43,10 @@ object PaletteUtils {
Color.parseColor("#303030"), // 17 dark grey Color.parseColor("#303030"), // 17 dark grey
Color.parseColor("#757575"), // 18 grey Color.parseColor("#757575"), // 18 grey
Color.parseColor("#aaaaaa") // 19 light grey Color.parseColor("#aaaaaa") // 19 light grey
) )[paletteIndex]
}
return palette[index]
}
@JvmStatic fun Int.toPaletteColor(context: Context): PaletteColor {
fun getColor(context: Context, paletteColor: Int): Int { val palette = StyledResources(context).getPalette()
val palette = StyledResources(context).getPalette() return PaletteColor(palette.indexOf(this))
return if (paletteColor in palette.indices) {
palette[paletteColor]
} else {
Log.w("ColorHelper", "Invalid color: $paletteColor. Returning default.")
palette[0]
}
}
} }

@ -44,7 +44,7 @@ open class CheckmarkWidget(
(v as CheckmarkWidgetView).apply { (v as CheckmarkWidgetView).apply {
setBackgroundAlpha(preferedBackgroundAlpha) setBackgroundAlpha(preferedBackgroundAlpha)
setActiveColor(PaletteUtils.getColor(context, habit.color)) setActiveColor(habit.color.toThemedAndroidColor(context))
setName(habit.name) setName(habit.name)
setCheckmarkValue(habit.checkmarks.todayValue) setCheckmarkValue(habit.checkmarks.todayValue)
if (habit.isNumerical) { if (habit.isNumerical) {

@ -43,7 +43,7 @@ class FrequencyWidget(
if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f) if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f)
(widgetView.dataView as FrequencyChart).apply { (widgetView.dataView as FrequencyChart).apply {
setFirstWeekday(firstWeekday) setFirstWeekday(firstWeekday)
setColor(PaletteUtils.getColor(context, habit.color)) setColor(habit.color.toThemedAndroidColor(context))
setFrequency(habit.repetitions.weekdayFrequency) setFrequency(habit.repetitions.weekdayFrequency)
} }
} }

@ -45,7 +45,7 @@ class HistoryWidget(
(widgetView.dataView as HistoryChart).apply { (widgetView.dataView as HistoryChart).apply {
setFirstWeekday(firstWeekday) setFirstWeekday(firstWeekday)
setSkipEnabled(prefs.isSkipEnabled) setSkipEnabled(prefs.isSkipEnabled)
setColor(PaletteUtils.getColor(context, habit.color)) setColor(habit.color.toThemedAndroidColor(context))
setCheckmarks(habit.checkmarks.allValues) setCheckmarks(habit.checkmarks.allValues)
setNumerical(habit.isNumerical) setNumerical(habit.isNumerical)
setTarget(habit.targetValue / habit.frequency.denominator) setTarget(habit.targetValue / habit.frequency.denominator)

@ -50,7 +50,7 @@ class ScoreWidget(
(widgetView.dataView as ScoreChart).apply { (widgetView.dataView as ScoreChart).apply {
setIsTransparencyEnabled(true) setIsTransparencyEnabled(true)
setBucketSize(size) setBucketSize(size)
setColor(PaletteUtils.getColor(context, habit.color)) setColor(habit.color.toThemedAndroidColor(context))
setScores(scores) setScores(scores)
} }
} }

@ -42,7 +42,7 @@ class StreakWidget(
widgetView.setBackgroundAlpha(preferedBackgroundAlpha) widgetView.setBackgroundAlpha(preferedBackgroundAlpha)
if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f) if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f)
(widgetView.dataView as StreakChart).apply { (widgetView.dataView as StreakChart).apply {
setColor(PaletteUtils.getColor(context, habit.color)) setColor(habit.color.toThemedAndroidColor(context))
setStreaks(habit.streaks.getBest(maxStreakCount)) setStreaks(habit.streaks.getBest(maxStreakCount))
} }
} }

@ -37,14 +37,14 @@ public class ChangeHabitColorCommand extends Command
final List<Habit> selected; final List<Habit> selected;
@NonNull @NonNull
final List<Integer> originalColors; final List<PaletteColor> originalColors;
@NonNull @NonNull
final Integer newColor; final PaletteColor newColor;
public ChangeHabitColorCommand(@NonNull HabitList habitList, public ChangeHabitColorCommand(@NonNull HabitList habitList,
@NonNull List<Habit> selected, @NonNull List<Habit> selected,
@NonNull Integer newColor) @NonNull PaletteColor newColor)
{ {
this.habitList = habitList; this.habitList = habitList;
this.selected = selected; this.selected = selected;
@ -92,7 +92,7 @@ public class ChangeHabitColorCommand extends Command
public Record(ChangeHabitColorCommand command) public Record(ChangeHabitColorCommand command)
{ {
id = command.getId(); id = command.getId();
color = command.newColor; color = command.newColor.getPaletteIndex();
habits = new LinkedList<>(); habits = new LinkedList<>();
for (Habit h : command.selected) for (Habit h : command.selected)
{ {
@ -107,7 +107,7 @@ public class ChangeHabitColorCommand extends Command
for (Long id : this.habits) selected.add(habitList.getById(id)); for (Long id : this.habits) selected.add(habitList.getById(id));
ChangeHabitColorCommand command; ChangeHabitColorCommand command;
command = new ChangeHabitColorCommand(habitList, selected, color); command = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(color));
command.setId(id); command.setId(id);
return command; return command;
} }

@ -123,21 +123,13 @@ public class Habit
return checkmarks; return checkmarks;
} }
/**
* Color of the habit.
* <p>
* This number is not an android.graphics.Color, but an index to the
* activity color palette, which changes according to the theme. To convert
* this color into an android.graphics.Color, use ColorHelper.getColor(context,
* habit.color).
*/
@NonNull @NonNull
public synchronized Integer getColor() public synchronized PaletteColor getColor()
{ {
return data.color; return data.color;
} }
public synchronized void setColor(@NonNull Integer color) public synchronized void setColor(@NonNull PaletteColor color)
{ {
data.color = color; data.color = color;
} }
@ -391,7 +383,7 @@ public class Habit
@NonNull @NonNull
public Frequency frequency; public Frequency frequency;
public int color; public PaletteColor color;
public boolean archived; public boolean archived;
@ -413,7 +405,7 @@ public class Habit
public HabitData() public HabitData()
{ {
this.color = 8; this.color = new PaletteColor(8);
this.archived = false; this.archived = false;
this.frequency = new Frequency(3, 7); this.frequency = new Frequency(3, 7);
this.type = YES_NO_HABIT; this.type = YES_NO_HABIT;

@ -251,7 +251,7 @@ public abstract class HabitList implements Iterable<Habit>
habit.getDescription(), habit.getDescription(),
Integer.toString(freq.getNumerator()), Integer.toString(freq.getNumerator()),
Integer.toString(freq.getDenominator()), Integer.toString(freq.getDenominator()),
ColorConstants.CSV_PALETTE[habit.getColor()] habit.getColor().toCsvColor(),
}; };
csv.writeNext(cols, false); csv.writeNext(cols, false);

@ -0,0 +1,51 @@
/*
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.models
data class PaletteColor(val paletteIndex: Int) {
fun toCsvColor(): String {
return arrayOf(
"#D32F2F", // 0 red
"#E64A19", // 1 deep orange
"#F57C00", // 2 orange
"#FF8F00", // 3 amber
"#F9A825", // 4 yellow
"#AFB42B", // 5 lime
"#7CB342", // 6 light green
"#388E3C", // 7 green
"#00897B", // 8 teal
"#00ACC1", // 9 cyan
"#039BE5", // 10 light blue
"#1976D2", // 11 blue
"#303F9F", // 12 indigo
"#5E35B1", // 13 deep purple
"#8E24AA", // 14 purple
"#D81B60", // 15 pink
"#5D4037", // 16 brown
"#303030", // 17 dark grey
"#757575", // 18 grey
"#aaaaaa" // 19 light grey
)[paletteIndex]
}
fun compareTo(other: PaletteColor): Int {
return paletteIndex.compareTo(other.paletteIndex)
}
}

@ -90,7 +90,7 @@ public class HabitRecord
this.name = model.getName(); this.name = model.getName();
this.description = model.getDescription(); this.description = model.getDescription();
this.highlight = 0; this.highlight = 0;
this.color = model.getColor(); this.color = model.getColor().getPaletteIndex();
this.archived = model.isArchived() ? 1 : 0; this.archived = model.isArchived() ? 1 : 0;
this.type = model.getType(); this.type = model.getType();
this.targetType = model.getTargetType(); this.targetType = model.getTargetType();
@ -123,7 +123,7 @@ public class HabitRecord
habit.setDescription(this.description); habit.setDescription(this.description);
habit.setQuestion(this.question); habit.setQuestion(this.question);
habit.setFrequency(new Frequency(this.freqNum, this.freqDen)); habit.setFrequency(new Frequency(this.freqNum, this.freqDen));
habit.setColor(this.color); habit.setColor(new PaletteColor(this.color));
habit.setArchived(this.archived != 0); habit.setArchived(this.archived != 0);
habit.setType(this.type); habit.setType(this.type);
habit.setTargetType(this.targetType); habit.setTargetType(this.targetType);

@ -46,7 +46,7 @@ public class HabitFixtures
Habit habit = modelFactory.buildHabit(); Habit habit = modelFactory.buildHabit();
habit.setName("Meditate"); habit.setName("Meditate");
habit.setQuestion("Did you meditate this morning?"); habit.setQuestion("Did you meditate this morning?");
habit.setColor(3); habit.setColor(new PaletteColor(3));
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
saveIfSQLite(habit); saveIfSQLite(habit);
@ -57,7 +57,7 @@ public class HabitFixtures
{ {
Habit habit = createEmptyHabit(); Habit habit = createEmptyHabit();
habit.setFrequency(new Frequency(3, 7)); habit.setFrequency(new Frequency(3, 7));
habit.setColor(4); habit.setColor(new PaletteColor(4));
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getToday();
int marks[] = {0, 1, 3, 5, 7, 8, 9, 10, 12, 14, 15, 17, 19, 20, 26, 27, int marks[] = {0, 1, 3, 5, 7, 8, 9, 10, 12, 14, 15, 17, 19, 20, 26, 27,
@ -79,7 +79,7 @@ public class HabitFixtures
habit.setUnit("miles"); habit.setUnit("miles");
habit.setTargetType(Habit.AT_LEAST); habit.setTargetType(Habit.AT_LEAST);
habit.setTargetValue(2.0); habit.setTargetValue(2.0);
habit.setColor(1); habit.setColor(new PaletteColor(1));
saveIfSQLite(habit); saveIfSQLite(habit);
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getToday();
@ -104,7 +104,7 @@ public class HabitFixtures
habit.setUnit("steps"); habit.setUnit("steps");
habit.setTargetType(Habit.AT_LEAST); habit.setTargetType(Habit.AT_LEAST);
habit.setTargetValue(100); habit.setTargetValue(100);
habit.setColor(1); habit.setColor(new PaletteColor(1));
saveIfSQLite(habit); saveIfSQLite(habit);
int times[] = {0, 5, 9, 15, 17, 21, 23, 27, 28, 35, 41, 45, 47, 53, 56, 62, 70, 73, 78, int times[] = {0, 5, 9, 15, 17, 21, 23, 27, 28, 35, 41, 45, 47, 53, 56, 62, 70, 73, 78,

@ -19,7 +19,9 @@
package org.isoron.uhabits.core.ui.callbacks; package org.isoron.uhabits.core.ui.callbacks;
import org.isoron.uhabits.core.models.*;
public interface OnColorPickedCallback public interface OnColorPickedCallback
{ {
void onColorPicked(int color); void onColorPicked(PaletteColor color);
} }

@ -133,7 +133,7 @@ public class ListHabitsSelectionMenuBehavior
public interface Screen public interface Screen
{ {
void showColorPicker(int defaultColor, void showColorPicker(PaletteColor defaultColor,
@NonNull OnColorPickedCallback callback); @NonNull OnColorPickedCallback callback);
void showDeleteConfirmationScreen( void showDeleteConfirmationScreen(

@ -1,27 +0,0 @@
package org.isoron.uhabits.core.utils;
public class ColorConstants
{
public static String CSV_PALETTE[] = {
"#D32F2F", // 0 red
"#E64A19", // 1 deep orange
"#F57C00", // 2 orange
"#FF8F00", // 3 amber
"#F9A825", // 4 yellow
"#AFB42B", // 5 lime
"#7CB342", // 6 light green
"#388E3C", // 7 green
"#00897B", // 8 teal
"#00ACC1", // 9 cyan
"#039BE5", // 10 light blue
"#1976D2", // 11 blue
"#303F9F", // 12 indigo
"#5E35B1", // 13 deep purple
"#8E24AA", // 14 purple
"#D81B60", // 15 pink
"#5D4037", // 16 brown
"#303030", // 17 dark grey
"#757575", // 18 grey
"#aaaaaa" // 19 light grey
};
}

@ -45,12 +45,12 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
Habit habit = fixtures.createShortHabit(); Habit habit = fixtures.createShortHabit();
habit.setColor(i + 1); habit.setColor(new PaletteColor(i + 1));
selected.add(habit); selected.add(habit);
habitList.add(habit); habitList.add(habit);
} }
command = new ChangeHabitColorCommand(habitList, selected, 0); command = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(0));
} }
@Test @Test
@ -81,13 +81,13 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
private void checkNewColors() private void checkNewColors()
{ {
for (Habit h : selected) for (Habit h : selected)
assertThat(h.getColor(), equalTo(0)); assertThat(h.getColor(), equalTo(new PaletteColor(0)));
} }
private void checkOriginalColors() private void checkOriginalColors()
{ {
int k = 0; int k = 0;
for (Habit h : selected) for (Habit h : selected)
assertThat(h.getColor(), equalTo(++k)); assertThat(h.getColor(), equalTo(new PaletteColor(++k)));
} }
} }

@ -66,7 +66,7 @@ public class CommandParserTest extends BaseUnitTest
public void testDecodeChangeColorCommand() throws JSONException public void testDecodeChangeColorCommand() throws JSONException
{ {
ChangeHabitColorCommand original, decoded; ChangeHabitColorCommand original, decoded;
original = new ChangeHabitColorCommand(habitList, selected, 20); original = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(20));
decoded = (ChangeHabitColorCommand) parser.parse(original.toJson()); decoded = (ChangeHabitColorCommand) parser.parse(original.toJson());
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId())); MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
@ -122,7 +122,7 @@ public class CommandParserTest extends BaseUnitTest
{ {
Habit modified = modelFactory.buildHabit(); Habit modified = modelFactory.buildHabit();
modified.setName("Edited JSON"); modified.setName("Edited JSON");
modified.setColor(2); modified.setColor(new PaletteColor(2));
EditHabitCommand original, decoded; EditHabitCommand original, decoded;
original = new EditHabitCommand(modelFactory, habitList, habit, modified); original = new EditHabitCommand(modelFactory, habitList, habit, modified);

@ -113,22 +113,22 @@ public class HabitListTest extends BaseUnitTest
{ {
Habit h1 = fixtures.createEmptyHabit(); Habit h1 = fixtures.createEmptyHabit();
h1.setName("A Habit"); h1.setName("A Habit");
h1.setColor(2); h1.setColor(new PaletteColor(2));
h1.setPosition(1); h1.setPosition(1);
Habit h2 = fixtures.createEmptyHabit(); Habit h2 = fixtures.createEmptyHabit();
h2.setName("B Habit"); h2.setName("B Habit");
h2.setColor(2); h2.setColor(new PaletteColor(2));
h2.setPosition(3); h2.setPosition(3);
Habit h3 = fixtures.createEmptyHabit(); Habit h3 = fixtures.createEmptyHabit();
h3.setName("C Habit"); h3.setName("C Habit");
h3.setColor(0); h3.setColor(new PaletteColor(0));
h3.setPosition(0); h3.setPosition(0);
Habit h4 = fixtures.createEmptyHabit(); Habit h4 = fixtures.createEmptyHabit();
h4.setName("D Habit"); h4.setName("D Habit");
h4.setColor(1); h4.setColor(new PaletteColor(1));
h4.setPosition(2); h4.setPosition(2);
HabitList list = modelFactory.buildHabitList(); HabitList list = modelFactory.buildHabitList();
@ -246,14 +246,14 @@ public class HabitListTest extends BaseUnitTest
h1.setQuestion("Did you meditate this morning?"); h1.setQuestion("Did you meditate this morning?");
h1.setDescription("this is a test description"); h1.setDescription("this is a test description");
h1.setFrequency(Frequency.DAILY); h1.setFrequency(Frequency.DAILY);
h1.setColor(3); h1.setColor(new PaletteColor(3));
Habit h2 = fixtures.createEmptyHabit(); Habit h2 = fixtures.createEmptyHabit();
h2.setName("Wake up early"); h2.setName("Wake up early");
h2.setQuestion("Did you wake up before 6am?"); h2.setQuestion("Did you wake up before 6am?");
h2.setDescription(""); h2.setDescription("");
h2.setFrequency(new Frequency(2, 3)); h2.setFrequency(new Frequency(2, 3));
h2.setColor(5); h2.setColor(new PaletteColor(5));
list.add(h1); list.add(h1);
list.add(h2); list.add(h2);

@ -58,7 +58,7 @@ public class HabitTest extends BaseUnitTest
{ {
Habit model = modelFactory.buildHabit(); Habit model = modelFactory.buildHabit();
model.setArchived(true); model.setArchived(true);
model.setColor(0); model.setColor(new PaletteColor(0));
model.setFrequency(new Frequency(10, 20)); model.setFrequency(new Frequency(10, 20));
model.setReminder(new Reminder(8, 30, new WeekdayList(1))); model.setReminder(new Reminder(8, 30, new WeekdayList(1)));
@ -152,7 +152,7 @@ public class HabitTest extends BaseUnitTest
h.setReminder(new Reminder(22, 30, WeekdayList.EVERY_DAY)); h.setReminder(new Reminder(22, 30, WeekdayList.EVERY_DAY));
String expected = "{id: <null>, data: {name: , description: ," + String expected = "{id: <null>, data: {name: , description: ," +
" frequency: {numerator: 3, denominator: 7}," + " frequency: {numerator: 3, denominator: 7}," +
" color: 8, archived: false, targetType: 0," + " color: PaletteColor(paletteIndex=8), archived: false, targetType: 0," +
" targetValue: 100.0, type: 0, unit: ," + " targetValue: 100.0, type: 0, unit: ," +
" reminder: {hour: 22, minute: 30," + " reminder: {hour: 22, minute: 30," +
" days: {weekdays: [true,true,true,true,true,true,true]}}," + " days: {weekdays: [true,true,true,true,true,true,true]}}," +

@ -37,7 +37,7 @@ public class HabitRecordTest extends BaseUnitTest
Habit original = modelFactory.buildHabit(); Habit original = modelFactory.buildHabit();
original.setName("Hello world"); original.setName("Hello world");
original.setQuestion("Did you greet the world today?"); original.setQuestion("Did you greet the world today?");
original.setColor(1); original.setColor(new PaletteColor(1));
original.setArchived(true); original.setArchived(true);
original.setFrequency(Frequency.THREE_TIMES_PER_WEEK); original.setFrequency(Frequency.THREE_TIMES_PER_WEEK);
original.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY)); original.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
@ -59,7 +59,7 @@ public class HabitRecordTest extends BaseUnitTest
Habit original = modelFactory.buildHabit(); Habit original = modelFactory.buildHabit();
original.setName("Hello world"); original.setName("Hello world");
original.setQuestion("Did you greet the world today?"); original.setQuestion("Did you greet the world today?");
original.setColor(5); original.setColor(new PaletteColor(5));
original.setArchived(false); original.setArchived(false);
original.setFrequency(Frequency.DAILY); original.setFrequency(Frequency.DAILY);
original.setReminder(null); original.setReminder(null);

@ -94,15 +94,15 @@ public class ListHabitsSelectionMenuBehaviorTest extends BaseUnitTest
@Test @Test
public void onChangeColor() throws Exception public void onChangeColor() throws Exception
{ {
assertThat(habit1.getColor(), equalTo(8)); assertThat(habit1.getColor(), equalTo(new PaletteColor(8)));
assertThat(habit2.getColor(), equalTo(8)); assertThat(habit2.getColor(), equalTo(new PaletteColor(8)));
when(adapter.getSelected()).thenReturn(asList(habit1, habit2)); when(adapter.getSelected()).thenReturn(asList(habit1, habit2));
behavior.onChangeColor(); behavior.onChangeColor();
verify(screen).showColorPicker(eq(8), colorPickerCallback.capture()); verify(screen).showColorPicker(eq(new PaletteColor(8)), colorPickerCallback.capture());
colorPickerCallback.getValue().onColorPicked(30); colorPickerCallback.getValue().onColorPicked(new PaletteColor(30));
assertThat(habit1.getColor(), equalTo(30)); assertThat(habit1.getColor(), equalTo(new PaletteColor(30)));
} }
@Test @Test

Loading…
Cancel
Save