refactored magical numers & more cleanup

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

@ -185,15 +185,6 @@
<intent-filter> <intent-filter>
<category android:name="android.intent.category.DEFAULT"/> <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"/> <action android:name="org.isoron.uhabits.ACTION_TOGGLE_REPETITION"/>
<data <data

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

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

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

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.receivers.WidgetReceiver import org.isoron.uhabits.receivers.WidgetReceiver
import org.isoron.uhabits.receivers.WidgetReceiver.Companion.ACTION_SET_NUMERICAL_VALUE 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) { override fun refreshData(v: View) {
(v as NumericalCheckmarkWidgetView).apply { (v as NumericalCheckmarkWidgetView).apply {
Log.d("NumericalChckmarkWidget", "Refreshing data")
setPercentage(habit.scores.todayValue.toFloat()) setPercentage(habit.scores.todayValue.toFloat())
setActiveColor(PaletteUtils.getColor(context, habit.color)) setActiveColor(PaletteUtils.getColor(context, habit.color))
setName(habit.name) setName(habit.name)
setCheckmarkValue(habit.checkmarks.todayValue) setCheckmarkValue(habit.checkmarks.todayValue)
setCheckmarkState(getCheckmarkState())
refresh() 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){ constructor(activity: NumericalCheckmarkWidgetActivity){
this.activity=activity this.activity=activity
} }
override fun onNumberPicked(newValue: Double) { override fun onNumberPicked(newValue: Double) {

@ -69,10 +69,9 @@ public class CheckmarkWidgetView extends HabitWidgetView
int bgColor; int bgColor;
int fgColor; int fgColor;
switch (checkmarkValue) switch (getCheckmarkState())
{ {
case Checkmark.CHECKED_EXPLICITLY: case Checkmark.CHECKED_EXPLICITLY:
text = getResources().getString(R.string.fa_check);
bgColor = activeColor; bgColor = activeColor;
fgColor = res.getColor(R.attr.highContrastReverseTextColor); fgColor = res.getColor(R.attr.highContrastReverseTextColor);
@ -84,7 +83,6 @@ public class CheckmarkWidgetView extends HabitWidgetView
break; break;
case Checkmark.CHECKED_IMPLICITLY: case Checkmark.CHECKED_IMPLICITLY:
text = getResources().getString(R.string.fa_check);
bgColor = res.getColor(R.attr.cardBackgroundColor); bgColor = res.getColor(R.attr.cardBackgroundColor);
fgColor = res.getColor(R.attr.mediumContrastTextColor); fgColor = res.getColor(R.attr.mediumContrastTextColor);
@ -95,7 +93,6 @@ public class CheckmarkWidgetView extends HabitWidgetView
case Checkmark.UNCHECKED: case Checkmark.UNCHECKED:
default: default:
text = getResources().getString(R.string.fa_times);
bgColor = res.getColor(R.attr.cardBackgroundColor); bgColor = res.getColor(R.attr.cardBackgroundColor);
fgColor = res.getColor(R.attr.mediumContrastTextColor); fgColor = res.getColor(R.attr.mediumContrastTextColor);
@ -108,7 +105,7 @@ public class CheckmarkWidgetView extends HabitWidgetView
ring.setPercentage(percentage); ring.setPercentage(percentage);
ring.setColor(fgColor); ring.setColor(fgColor);
ring.setBackgroundColor(bgColor); ring.setBackgroundColor(bgColor);
ring.setText(text); ring.setText(getText());
label.setText(name); label.setText(name);
label.setTextColor(fgColor); label.setTextColor(fgColor);
@ -117,6 +114,34 @@ public class CheckmarkWidgetView extends HabitWidgetView
postInvalidate(); 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) public void setActiveColor(int activeColor)
{ {
this.activeColor = 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.common.dialogs.NumberPickerFactory
import org.isoron.uhabits.activities.habits.list.views.toShortString import org.isoron.uhabits.activities.habits.list.views.toShortString
import org.isoron.uhabits.core.models.Checkmark import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
class NumericalCheckmarkWidgetView : CheckmarkWidgetView { class NumericalCheckmarkWidgetView : CheckmarkWidgetView {
private var checkmarkState : Int = Checkmark.UNCHECKED
constructor(context: Context) : super(context) {} constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {} constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
override fun refresh() { /**
if (backgroundPaint == null || frame == null || ring == null) return * @param state the new state/style of the widget, either:
* - Checkmark.CHECKED_EXPLICITLY
//right now most of this code is copied over from the regular CheckmarkWidget. This is for testing purposes, just to get something working. * - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED
val res = StyledResources(context) */
fun setCheckmarkState(state : Int) {
val text: String checkmarkState = state
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)
label.text = name override fun getCheckmarkState(): Int {
label.setTextColor(fgColor) return checkmarkState
}
requestLayout() override fun getText(): String {
postInvalidate() val numberValue : Double = Habit.checkMarkValueToDouble(checkmarkValue)
return numberValue.toShortString()
} }
} }

@ -94,6 +94,21 @@ public class Habit
observable = new ModelObservable(); 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. * Clears the reminder for a habit.
*/ */
@ -327,9 +342,9 @@ public class Habit
if (isNumerical()) if (isNumerical())
{ {
if(getTargetType() == AT_LEAST) if(getTargetType() == AT_LEAST)
return todayCheckmark >= data.targetValue; return todayCheckmark >= doubleToCheckMarkValue(data.targetValue);
else else
return todayCheckmark <= data.targetValue; return todayCheckmark <= doubleToCheckMarkValue(data.targetValue);
} }
else return (todayCheckmark != UNCHECKED); else return (todayCheckmark != UNCHECKED);
} }

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

@ -83,11 +83,11 @@ public class ListHabitsBehavior
CheckmarkList checkmarks = habit.getCheckmarks(); CheckmarkList checkmarks = habit.getCheckmarks();
double oldValue = checkmarks.getValues(timestamp, timestamp)[0]; 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( commandRunner.execute(
new CreateRepetitionCommand(habit, timestamp, (int) newValue), new CreateRepetitionCommand(habit, timestamp, intValue),
habit.getId()); habit.getId());
}); });
} }

Loading…
Cancel
Save