Add tests for NumberView

This commit is contained in:
2016-03-31 06:03:16 -04:00
parent f9da90a93a
commit 72f5ca5531
8 changed files with 129 additions and 36 deletions

View File

@@ -23,7 +23,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
@@ -71,26 +70,31 @@ public abstract class DialogHelper
return prefs.getInt("launch_count", 0);
}
public static String getAttribute(Context context, AttributeSet attrs, String name)
public static String getAttribute(Context context, AttributeSet attrs, String name,
String defaultValue)
{
int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0);
if (resId != 0) return context.getResources().getString(resId);
else return attrs.getAttributeValue(ISORON_NAMESPACE, name);
String value = attrs.getAttributeValue(ISORON_NAMESPACE, name);
if(value != null) return value;
else return defaultValue;
}
public static int getIntAttribute(Context context, AttributeSet attrs, String name)
public static int getIntAttribute(Context context, AttributeSet attrs, String name,
int defaultValue)
{
String number = getAttribute(context, attrs, name);
String number = getAttribute(context, attrs, name, null);
if(number != null) return Integer.parseInt(number);
else return 0;
else return defaultValue;
}
public static float getFloatAttribute(Context context, AttributeSet attrs, String name)
public static float getFloatAttribute(Context context, AttributeSet attrs, String name,
float defaultValue)
{
String number = getAttribute(context, attrs, name);
String number = getAttribute(context, attrs, name, null);
if(number != null) return Float.parseFloat(number);
else return 0;
else return defaultValue;
}
public static float dpToPixels(Context context, float dp)

View File

@@ -31,13 +31,12 @@ import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import org.isoron.uhabits.R;
import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.helpers.DialogHelper;
public class NumberView extends View
{
private int size;
private int color;
private int number;
private float labelMarginTop;
@@ -48,19 +47,30 @@ public class NumberView extends View
private int width;
private int height;
private float textSize;
private float labelTextSize;
private float numberTextSize;
private StaticLayout numberLayout;
public NumberView(Context context)
{
super(context);
this.textSize = getResources().getDimension(R.dimen.regularTextSize);
init();
}
public NumberView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.label = DialogHelper.getAttribute(context, attrs, "label");
this.number = DialogHelper.getIntAttribute(context, attrs, "number");
this.textSize = DialogHelper.getFloatAttribute(context, attrs, "textSize");
this.textSize = DialogHelper.spToPixels(getContext(), textSize);
this.textSize = getResources().getDimension(R.dimen.regularTextSize);
this.label = DialogHelper.getAttribute(context, attrs, "label", "Number");
this.number = DialogHelper.getIntAttribute(context, attrs, "number", 0);
this.textSize = DialogHelper.getFloatAttribute(context, attrs, "textSize",
getResources().getDimension(R.dimen.regularTextSize));
this.color = ColorHelper.palette[7];
init();
}
@@ -75,12 +85,21 @@ public class NumberView extends View
public void setLabel(String label)
{
this.label = label;
requestLayout();
postInvalidate();
}
public void setNumber(int number)
{
this.number = number;
createNumberLayout();
requestLayout();
postInvalidate();
}
public void setTextSize(float textSize)
{
this.textSize = textSize;
requestLayout();
postInvalidate();
}
@@ -106,7 +125,10 @@ public class NumberView extends View
labelMarginTop = textSize * 0.35f;
numberTextSize = textSize * 2.85f;
createNumberLayout();
pText.setTextSize(numberTextSize);
numberLayout = new StaticLayout(Integer.toString(number), pText, width,
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
int numberWidth = numberLayout.getWidth();
int numberHeight = numberLayout.getHeight();
@@ -122,20 +144,10 @@ public class NumberView extends View
setMeasuredDimension(width, height);
}
private void createNumberLayout()
{
pText.setTextSize(numberTextSize);
numberLayout = new StaticLayout(Integer.toString(number), pText, width,
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
pText.setColor(color);
pText.setTextSize(size * 0.4f);
rect.set(0, 0, width, height);
canvas.save();

View File

@@ -22,6 +22,7 @@ package org.isoron.uhabits.views;
import android.content.Context;
import android.util.AttributeSet;
import org.isoron.uhabits.R;
import org.isoron.uhabits.helpers.DateHelper;
import org.isoron.uhabits.helpers.DialogHelper;
import org.isoron.uhabits.models.Habit;
@@ -37,13 +38,12 @@ public class RepetitionCountView extends NumberView implements HabitDataView
public RepetitionCountView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.interval = DialogHelper.getIntAttribute(context, attrs, "interval");
this.interval = DialogHelper.getIntAttribute(context, attrs, "interval", 7);
int labelValue = DialogHelper.getIntAttribute(context, attrs, "labelValue", 7);
String labelFormat = DialogHelper.getAttribute(context, attrs, "labelFormat",
getResources().getString(R.string.last_x_days));
int labelValue = DialogHelper.getIntAttribute(context, attrs, "labelValue");
String labelFormat = DialogHelper.getAttribute(context, attrs, "labelFormat");
if(labelValue > 0)
setLabel(String.format(labelFormat, labelValue));
setLabel(String.format(labelFormat, labelValue));
refreshData();
}

View File

@@ -63,8 +63,8 @@ public class RingView extends View
{
super(context, attrs);
this.label = DialogHelper.getAttribute(context, attrs, "label");
this.maxDiameter = DialogHelper.getFloatAttribute(context, attrs, "maxDiameter");
this.label = DialogHelper.getAttribute(context, attrs, "label", "Label");
this.maxDiameter = DialogHelper.getFloatAttribute(context, attrs, "maxDiameter", 100);
this.maxDiameter = DialogHelper.dpToPixels(context, maxDiameter);
init();
}