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