|
|
|
@ -20,11 +20,12 @@
|
|
|
|
|
package org.isoron.uhabits.activities.habits.list.views;
|
|
|
|
|
|
|
|
|
|
import android.content.*;
|
|
|
|
|
import android.content.res.*;
|
|
|
|
|
import android.graphics.*;
|
|
|
|
|
import android.support.annotation.*;
|
|
|
|
|
import android.text.*;
|
|
|
|
|
import android.util.*;
|
|
|
|
|
import android.view.*;
|
|
|
|
|
import android.widget.*;
|
|
|
|
|
|
|
|
|
|
import org.isoron.uhabits.*;
|
|
|
|
|
import org.isoron.uhabits.activities.habits.list.controllers.*;
|
|
|
|
@ -33,9 +34,12 @@ import org.isoron.uhabits.utils.*;
|
|
|
|
|
import static org.isoron.uhabits.utils.AttributeSetUtils.*;
|
|
|
|
|
import static org.isoron.uhabits.utils.ColorUtils.*;
|
|
|
|
|
|
|
|
|
|
public class NumberButtonView extends TextView
|
|
|
|
|
public class NumberButtonView extends View
|
|
|
|
|
{
|
|
|
|
|
private static Typeface TYPEFACE =
|
|
|
|
|
private static Typeface BOLD_TYPEFACE =
|
|
|
|
|
Typeface.create("sans-serif-condensed", Typeface.BOLD);
|
|
|
|
|
|
|
|
|
|
private static Typeface NORMAL_TYPEFACE =
|
|
|
|
|
Typeface.create("sans-serif-condensed", Typeface.NORMAL);
|
|
|
|
|
|
|
|
|
|
private int color;
|
|
|
|
@ -44,7 +48,19 @@ public class NumberButtonView extends TextView
|
|
|
|
|
|
|
|
|
|
private int threshold;
|
|
|
|
|
|
|
|
|
|
private StyledResources res;
|
|
|
|
|
private String unit;
|
|
|
|
|
|
|
|
|
|
private RectF rect;
|
|
|
|
|
|
|
|
|
|
private TextPaint pRegular;
|
|
|
|
|
|
|
|
|
|
private Resources res;
|
|
|
|
|
|
|
|
|
|
private TextPaint pBold;
|
|
|
|
|
|
|
|
|
|
private int grey;
|
|
|
|
|
|
|
|
|
|
private float em;
|
|
|
|
|
|
|
|
|
|
public NumberButtonView(@Nullable Context context)
|
|
|
|
|
{
|
|
|
|
@ -52,33 +68,34 @@ public class NumberButtonView extends TextView
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NumberButtonView(@Nullable Context context,
|
|
|
|
|
@Nullable AttributeSet attrs)
|
|
|
|
|
public NumberButtonView(@Nullable Context ctx, @Nullable AttributeSet attrs)
|
|
|
|
|
{
|
|
|
|
|
super(context, attrs);
|
|
|
|
|
super(ctx, attrs);
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
|
|
if (context != null && attrs != null)
|
|
|
|
|
if (ctx != null && attrs != null)
|
|
|
|
|
{
|
|
|
|
|
int color = getIntAttribute(context, attrs, "color", 0);
|
|
|
|
|
int value = getIntAttribute(context, attrs, "value", 0);
|
|
|
|
|
int threshold = getIntAttribute(context, attrs, "threshold", 1);
|
|
|
|
|
int color = getIntAttribute(ctx, attrs, "color", 0);
|
|
|
|
|
int value = getIntAttribute(ctx, attrs, "value", 0);
|
|
|
|
|
int threshold = getIntAttribute(ctx, attrs, "threshold", 1);
|
|
|
|
|
String unit = getAttribute(ctx, attrs, "unit", "min");
|
|
|
|
|
setColor(getAndroidTestColor(color));
|
|
|
|
|
setThreshold(threshold);
|
|
|
|
|
setValue(value);
|
|
|
|
|
setUnit(unit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String formatValue(int v)
|
|
|
|
|
{
|
|
|
|
|
double fv = (double) v;
|
|
|
|
|
if(v >= 1e9) return String.format("%.1fG", fv / 1e9);
|
|
|
|
|
if(v >= 1e8) return String.format("%.0fM", fv / 1e6);
|
|
|
|
|
if(v >= 1e7) return String.format("%.1fM", fv / 1e6);
|
|
|
|
|
if(v >= 1e6) return String.format("%.1fM", fv / 1e6);
|
|
|
|
|
if(v >= 1e5) return String.format("%.0fk", fv / 1e3);
|
|
|
|
|
if(v >= 1e4) return String.format("%.1fk", fv / 1e3);
|
|
|
|
|
if(v >= 1e3) return String.format("%.1fk", fv / 1e3);
|
|
|
|
|
if (v >= 1e9) return String.format("%.1fG", fv / 1e9);
|
|
|
|
|
if (v >= 1e8) return String.format("%.0fM", fv / 1e6);
|
|
|
|
|
if (v >= 1e7) return String.format("%.1fM", fv / 1e6);
|
|
|
|
|
if (v >= 1e6) return String.format("%.1fM", fv / 1e6);
|
|
|
|
|
if (v >= 1e5) return String.format("%.0fk", fv / 1e3);
|
|
|
|
|
if (v >= 1e4) return String.format("%.1fk", fv / 1e3);
|
|
|
|
|
if (v >= 1e3) return String.format("%.1fk", fv / 1e3);
|
|
|
|
|
return String.format("%d", v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,35 +114,72 @@ public class NumberButtonView extends TextView
|
|
|
|
|
public void setThreshold(int threshold)
|
|
|
|
|
{
|
|
|
|
|
this.threshold = threshold;
|
|
|
|
|
updateText();
|
|
|
|
|
postInvalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setUnit(String unit)
|
|
|
|
|
{
|
|
|
|
|
this.unit = unit;
|
|
|
|
|
postInvalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setValue(int value)
|
|
|
|
|
{
|
|
|
|
|
this.value = value;
|
|
|
|
|
updateText();
|
|
|
|
|
postInvalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void init()
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas)
|
|
|
|
|
{
|
|
|
|
|
res = new StyledResources(getContext());
|
|
|
|
|
if(value < threshold)
|
|
|
|
|
{
|
|
|
|
|
pRegular.setColor(grey);
|
|
|
|
|
pBold.setColor(grey);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pRegular.setColor(color);
|
|
|
|
|
pBold.setColor(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setWillNotDraw(false);
|
|
|
|
|
String fv = formatValue(value);
|
|
|
|
|
|
|
|
|
|
setMinHeight(
|
|
|
|
|
getResources().getDimensionPixelSize(R.dimen.checkmarkHeight));
|
|
|
|
|
setMinWidth(
|
|
|
|
|
getResources().getDimensionPixelSize(R.dimen.checkmarkWidth));
|
|
|
|
|
rect.set(0, 0, getWidth(), getHeight());
|
|
|
|
|
rect.offset(0, - 0.1f * em);
|
|
|
|
|
canvas.drawText(fv, rect.centerX(), rect.centerY(), pBold);
|
|
|
|
|
|
|
|
|
|
setFocusable(false);
|
|
|
|
|
setGravity(Gravity.CENTER);
|
|
|
|
|
setTypeface(TYPEFACE);
|
|
|
|
|
rect.offset(0, 1.25f * em);
|
|
|
|
|
canvas.drawText(unit, rect.centerX(), rect.centerY(), pRegular);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateText()
|
|
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
|
|
|
|
{
|
|
|
|
|
int width = (int) res.getDimension(R.dimen.checkmarkWidth);
|
|
|
|
|
int height = (int) res.getDimension(R.dimen.checkmarkHeight);
|
|
|
|
|
setMeasuredDimension(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void init()
|
|
|
|
|
{
|
|
|
|
|
int lowColor = res.getColor(R.attr.lowContrastTextColor);
|
|
|
|
|
setTextColor(value >= threshold ? color : lowColor);
|
|
|
|
|
setText(formatValue(value));
|
|
|
|
|
StyledResources sr = new StyledResources(getContext());
|
|
|
|
|
res = getContext().getResources();
|
|
|
|
|
|
|
|
|
|
rect = new RectF();
|
|
|
|
|
pRegular = new TextPaint();
|
|
|
|
|
pRegular.setTextSize(res.getDimension(R.dimen.smallerTextSize));
|
|
|
|
|
pRegular.setTypeface(NORMAL_TYPEFACE);
|
|
|
|
|
pRegular.setAntiAlias(true);
|
|
|
|
|
pRegular.setTextAlign(Paint.Align.CENTER);
|
|
|
|
|
|
|
|
|
|
pBold = new TextPaint();
|
|
|
|
|
pBold.setTextSize(res.getDimension(R.dimen.smallTextSize));
|
|
|
|
|
pBold.setTypeface(BOLD_TYPEFACE);
|
|
|
|
|
pBold.setAntiAlias(true);
|
|
|
|
|
pBold.setTextAlign(Paint.Align.CENTER);
|
|
|
|
|
|
|
|
|
|
em = pBold.measureText("m");
|
|
|
|
|
grey = sr.getColor(R.attr.lowContrastTextColor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|