Replace primitive int by PaletteColor

This commit is contained in:
2020-12-21 21:36:35 -06:00
parent 63f2b80515
commit 5d2ff40dc9
47 changed files with 164 additions and 151 deletions

View File

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

View File

@@ -123,21 +123,13 @@ public class Habit
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
public synchronized Integer getColor()
public synchronized PaletteColor getColor()
{
return data.color;
}
public synchronized void setColor(@NonNull Integer color)
public synchronized void setColor(@NonNull PaletteColor color)
{
data.color = color;
}
@@ -391,7 +383,7 @@ public class Habit
@NonNull
public Frequency frequency;
public int color;
public PaletteColor color;
public boolean archived;
@@ -413,7 +405,7 @@ public class Habit
public HabitData()
{
this.color = 8;
this.color = new PaletteColor(8);
this.archived = false;
this.frequency = new Frequency(3, 7);
this.type = YES_NO_HABIT;

View File

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

View File

@@ -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)
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -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
};
}

View File

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

View File

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

View File

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

View File

@@ -58,7 +58,7 @@ public class HabitTest extends BaseUnitTest
{
Habit model = modelFactory.buildHabit();
model.setArchived(true);
model.setColor(0);
model.setColor(new PaletteColor(0));
model.setFrequency(new Frequency(10, 20));
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));
String expected = "{id: <null>, data: {name: , description: ," +
" 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: ," +
" reminder: {hour: 22, minute: 30," +
" days: {weekdays: [true,true,true,true,true,true,true]}}," +

View File

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

View File

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