refactored magical numers & more cleanup

pull/499/head
thomas 6 years ago
parent 6daee8c5bb
commit 216dfd9024

@ -185,15 +185,6 @@
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="org.isoron.uhabits.ACTION_SAVE_NUMERICAL_VALUE"/>
<data
android:host="org.isoron.uhabits"
android:scheme="content"/>
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="org.isoron.uhabits.ACTION_TOGGLE_REPETITION"/>
<data

@ -269,7 +269,7 @@ public class BarChart extends ScrollableChart
float margin = baseSize * 0.225f;
int color = textColor;
if (value / 1000 >= target) color = primaryColor;
if (Habit.checkMarkValueToDouble(value) >= target) color = primaryColor;
rect.inset(-margin, 0);
setModeOrColor(pGraph, XFERMODE_CLEAR, backgroundColor);
@ -357,10 +357,10 @@ public class BarChart extends ScrollableChart
if (value == 0) return;
int activeColor = textColor;
if (value / 1000 >= target)
if (Habit.checkMarkValueToDouble(value) >= target)
activeColor = primaryColor;
String label = NumberButtonViewKt.toShortString(value / 1000);
String label = NumberButtonViewKt.toShortString(Habit.checkMarkValueToDouble(value));
Rect rText = new Rect();
pText.getTextBounds(label, 0, label.length(), rText);

@ -86,7 +86,7 @@ class HabitCardView(
get() = checkmarkPanel.values
set(values) {
checkmarkPanel.values = values
numberPanel.values = values.map { it / 1000.0 }.toDoubleArray()
numberPanel.values = values.map { Habit.checkMarkValueToDouble(it) }.toDoubleArray()
}
var threshold: Double

@ -127,7 +127,7 @@ public class HistoryCard extends HabitCard
chart.setColor(color);
if(habit.isNumerical())
{
chart.setTarget((int) (habit.getTargetValue() * 1000));
chart.setTarget(Habit.doubleToCheckMarkValue(habit.getTargetValue()));
chart.setNumerical(true);
}
}

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.receivers.WidgetReceiver
import org.isoron.uhabits.receivers.WidgetReceiver.Companion.ACTION_SET_NUMERICAL_VALUE
@ -28,15 +29,21 @@ class NumericalCheckmarkWidget(context: Context, widgetId: Int, habit: Habit) :
override fun refreshData(v: View) {
(v as NumericalCheckmarkWidgetView).apply {
Log.d("NumericalChckmarkWidget", "Refreshing data")
setPercentage(habit.scores.todayValue.toFloat())
setActiveColor(PaletteUtils.getColor(context, habit.color))
setName(habit.name)
setCheckmarkValue(habit.checkmarks.todayValue)
setCheckmarkState(getCheckmarkState())
refresh()
}
}
private fun getCheckmarkState():Int{
return if(habit.isCompletedToday){
Checkmark.CHECKED_EXPLICITLY
}else{
Checkmark.UNCHECKED
}
}
}

@ -43,7 +43,6 @@ class NumericalCheckmarkWidgetActivity : Activity() {
constructor(activity: NumericalCheckmarkWidgetActivity){
this.activity=activity
}
override fun onNumberPicked(newValue: Double) {

@ -69,10 +69,9 @@ public class CheckmarkWidgetView extends HabitWidgetView
int bgColor;
int fgColor;
switch (checkmarkValue)
switch (getCheckmarkState())
{
case Checkmark.CHECKED_EXPLICITLY:
text = getResources().getString(R.string.fa_check);
bgColor = activeColor;
fgColor = res.getColor(R.attr.highContrastReverseTextColor);
@ -84,7 +83,6 @@ public class CheckmarkWidgetView extends HabitWidgetView
break;
case Checkmark.CHECKED_IMPLICITLY:
text = getResources().getString(R.string.fa_check);
bgColor = res.getColor(R.attr.cardBackgroundColor);
fgColor = res.getColor(R.attr.mediumContrastTextColor);
@ -95,7 +93,6 @@ public class CheckmarkWidgetView extends HabitWidgetView
case Checkmark.UNCHECKED:
default:
text = getResources().getString(R.string.fa_times);
bgColor = res.getColor(R.attr.cardBackgroundColor);
fgColor = res.getColor(R.attr.mediumContrastTextColor);
@ -108,7 +105,7 @@ public class CheckmarkWidgetView extends HabitWidgetView
ring.setPercentage(percentage);
ring.setColor(fgColor);
ring.setBackgroundColor(bgColor);
ring.setText(text);
ring.setText(getText());
label.setText(name);
label.setTextColor(fgColor);
@ -117,6 +114,34 @@ public class CheckmarkWidgetView extends HabitWidgetView
postInvalidate();
}
/**
* @Return the state of the checkmark, either:
* - Checkmark.CHECKED_EXPLICITLY
* - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED
*/
protected int getCheckmarkState(){
return checkmarkValue;
}
/**
* @Return the text that should be displayed in the middle of the widget
*/
protected String getText(){
switch (getCheckmarkState())
{
case Checkmark.CHECKED_EXPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.CHECKED_IMPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.UNCHECKED:
default:
return getResources().getString(R.string.fa_times);
}
}
public void setActiveColor(int activeColor)
{
this.activeColor = activeColor;

@ -8,46 +8,35 @@ import org.isoron.uhabits.R
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
import org.isoron.uhabits.activities.habits.list.views.toShortString
import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
class NumericalCheckmarkWidgetView : CheckmarkWidgetView {
private var checkmarkState : Int = Checkmark.UNCHECKED
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
override fun refresh() {
if (backgroundPaint == null || frame == null || ring == null) return
//right now most of this code is copied over from the regular CheckmarkWidget. This is for testing purposes, just to get something working.
val res = StyledResources(context)
val text: String
val bgColor: Int
val fgColor: Int
val numberValue : Double = checkmarkValue / 1000.0
text = numberValue.toShortString()
bgColor = activeColor
fgColor = res.getColor(R.attr.highContrastReverseTextColor)
setShadowAlpha(0x4f)
rebuildBackground()
backgroundPaint!!.color = bgColor
frame.setBackgroundDrawable(background)
ring.percentage = percentage
ring.color = fgColor
ring.setBackgroundColor(bgColor)
ring.setText(text)
/**
* @param state the new state/style of the widget, either:
* - Checkmark.CHECKED_EXPLICITLY
* - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED
*/
fun setCheckmarkState(state : Int) {
checkmarkState = state
}
label.text = name
label.setTextColor(fgColor)
override fun getCheckmarkState(): Int {
return checkmarkState
}
requestLayout()
postInvalidate()
override fun getText(): String {
val numberValue : Double = Habit.checkMarkValueToDouble(checkmarkValue)
return numberValue.toShortString()
}
}

@ -94,6 +94,21 @@ public class Habit
observable = new ModelObservable();
}
public static double checkMarkValueToDouble(int a)
{
return ((double) a)/1000.0;
}
public static double checkMarkValueToDouble(double a)
{
return a/1000.0;
}
public static int doubleToCheckMarkValue(double a)
{
return (int)Math.round(a*1000);
}
/**
* Clears the reminder for a habit.
*/
@ -327,9 +342,9 @@ public class Habit
if (isNumerical())
{
if(getTargetType() == AT_LEAST)
return todayCheckmark >= data.targetValue;
return todayCheckmark >= doubleToCheckMarkValue(data.targetValue);
else
return todayCheckmark <= data.targetValue;
return todayCheckmark <= doubleToCheckMarkValue(data.targetValue);
}
else return (todayCheckmark != UNCHECKED);
}

@ -278,7 +278,7 @@ public abstract class ScoreList implements Iterable<Score>
if (habit.isNumerical())
{
value /= 1000;
value = Habit.checkMarkValueToDouble(value);
value /= habit.getTargetValue();
value = Math.min(1, value);
}

@ -83,11 +83,11 @@ public class ListHabitsBehavior
CheckmarkList checkmarks = habit.getCheckmarks();
double oldValue = checkmarks.getValues(timestamp, timestamp)[0];
screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue ->
screen.showNumberPicker(Habit.checkMarkValueToDouble(oldValue), habit.getUnit(), newValue ->
{
newValue = Math.round(newValue * 1000);
int intValue = Habit.doubleToCheckMarkValue(newValue);
commandRunner.execute(
new CreateRepetitionCommand(habit, timestamp, (int) newValue),
new CreateRepetitionCommand(habit, timestamp, intValue),
habit.getId());
});
}

Loading…
Cancel
Save