mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
BarChart: allow user to pick interval
This commit is contained in:
@@ -77,7 +77,6 @@ public class BarChart extends ScrollableChart
|
|||||||
|
|
||||||
private int primaryColor;
|
private int primaryColor;
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private int bucketSize = 7;
|
private int bucketSize = 7;
|
||||||
|
|
||||||
private int backgroundColor;
|
private int backgroundColor;
|
||||||
@@ -127,7 +126,6 @@ public class BarChart extends ScrollableChart
|
|||||||
setTarget(0.5);
|
setTarget(0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void setBucketSize(int bucketSize)
|
public void setBucketSize(int bucketSize)
|
||||||
{
|
{
|
||||||
this.bucketSize = bucketSize;
|
this.bucketSize = bucketSize;
|
||||||
@@ -298,7 +296,6 @@ public class BarChart extends ScrollableChart
|
|||||||
|
|
||||||
boolean shouldPrintYear = true;
|
boolean shouldPrintYear = true;
|
||||||
if (yearText.equals(previousYearText)) shouldPrintYear = false;
|
if (yearText.equals(previousYearText)) shouldPrintYear = false;
|
||||||
if (bucketSize >= 365 && (year % 2) != 0) shouldPrintYear = false;
|
|
||||||
|
|
||||||
if (skipYear > 0)
|
if (skipYear > 0)
|
||||||
{
|
{
|
||||||
@@ -306,6 +303,8 @@ public class BarChart extends ScrollableChart
|
|||||||
shouldPrintYear = false;
|
shouldPrintYear = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bucketSize >= 365) shouldPrintYear = true;
|
||||||
|
|
||||||
if (shouldPrintYear)
|
if (shouldPrintYear)
|
||||||
{
|
{
|
||||||
previousYearText = yearText;
|
previousYearText = yearText;
|
||||||
@@ -314,6 +313,8 @@ public class BarChart extends ScrollableChart
|
|||||||
pText.setTextAlign(Paint.Align.CENTER);
|
pText.setTextAlign(Paint.Align.CENTER);
|
||||||
canvas.drawText(yearText, rect.centerX(), rect.bottom + em * 2.2f, pText);
|
canvas.drawText(yearText, rect.centerX(), rect.bottom + em * 2.2f, pText);
|
||||||
skipYear = 1;
|
skipYear = 1;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bucketSize < 365)
|
if (bucketSize < 365)
|
||||||
|
|||||||
@@ -29,15 +29,25 @@ import org.isoron.uhabits.R;
|
|||||||
import org.isoron.uhabits.activities.common.views.*;
|
import org.isoron.uhabits.activities.common.views.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.tasks.*;
|
import org.isoron.uhabits.core.tasks.*;
|
||||||
import org.isoron.uhabits.core.utils.*;
|
|
||||||
import org.isoron.uhabits.utils.*;
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import butterknife.*;
|
import butterknife.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.activities.habits.show.views.ScoreCard.getTruncateField;
|
||||||
|
|
||||||
public class BarCard extends HabitCard
|
public class BarCard extends HabitCard
|
||||||
{
|
{
|
||||||
|
public static final int[] NUMERICAL_BUCKET_SIZES = {1, 7, 31, 92, 365};
|
||||||
|
public static final int[] BOOLEAN_BUCKET_SIZES = {7, 31, 92, 365};
|
||||||
|
|
||||||
|
@BindView(R.id.numericalSpinner)
|
||||||
|
Spinner numericalSpinner;
|
||||||
|
|
||||||
|
@BindView(R.id.boolSpinner)
|
||||||
|
Spinner boolSpinner;
|
||||||
|
|
||||||
@BindView(R.id.barChart)
|
@BindView(R.id.barChart)
|
||||||
BarChart chart;
|
BarChart chart;
|
||||||
|
|
||||||
@@ -47,6 +57,8 @@ public class BarCard extends HabitCard
|
|||||||
@Nullable
|
@Nullable
|
||||||
private TaskRunner taskRunner;
|
private TaskRunner taskRunner;
|
||||||
|
|
||||||
|
private int bucketSize;
|
||||||
|
|
||||||
public BarCard(Context context)
|
public BarCard(Context context)
|
||||||
{
|
{
|
||||||
super(context);
|
super(context);
|
||||||
@@ -59,6 +71,20 @@ public class BarCard extends HabitCard
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnItemSelected(R.id.numericalSpinner)
|
||||||
|
public void onNumericalItemSelected(int position)
|
||||||
|
{
|
||||||
|
bucketSize = NUMERICAL_BUCKET_SIZES[position];
|
||||||
|
refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnItemSelected(R.id.boolSpinner)
|
||||||
|
public void onBoolItemSelected(int position)
|
||||||
|
{
|
||||||
|
bucketSize = BOOLEAN_BUCKET_SIZES[position];
|
||||||
|
refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void refreshData()
|
protected void refreshData()
|
||||||
{
|
{
|
||||||
@@ -71,22 +97,16 @@ public class BarCard extends HabitCard
|
|||||||
inflate(getContext(), R.layout.show_habit_bar, this);
|
inflate(getContext(), R.layout.show_habit_bar, this);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
|
boolSpinner.setSelection(1);
|
||||||
|
numericalSpinner.setSelection(2);
|
||||||
|
bucketSize = 7;
|
||||||
|
|
||||||
Context appContext = getContext().getApplicationContext();
|
Context appContext = getContext().getApplicationContext();
|
||||||
if (appContext instanceof HabitsApplication)
|
if (appContext instanceof HabitsApplication)
|
||||||
{
|
{
|
||||||
HabitsApplication app = (HabitsApplication) appContext;
|
HabitsApplication app = (HabitsApplication) appContext;
|
||||||
taskRunner = app.getComponent().getTaskRunner();
|
taskRunner = app.getComponent().getTaskRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isInEditMode()) initEditMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initEditMode()
|
|
||||||
{
|
|
||||||
int color = PaletteUtils.getAndroidTestColor(1);
|
|
||||||
title.setTextColor(color);
|
|
||||||
chart.setColor(color);
|
|
||||||
chart.populateWithRandomData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class RefreshTask implements Task
|
private class RefreshTask implements Task
|
||||||
@@ -101,10 +121,11 @@ public class BarCard extends HabitCard
|
|||||||
@Override
|
@Override
|
||||||
public void doInBackground()
|
public void doInBackground()
|
||||||
{
|
{
|
||||||
Timestamp today = DateUtils.getToday();
|
List<Checkmark> checkmarks;
|
||||||
List<Checkmark> checkmarks = habit.getCheckmarks().groupBy(
|
if (bucketSize == 1) checkmarks = habit.getCheckmarks().getAll();
|
||||||
DateUtils.TruncateField.MONTH);
|
else checkmarks = habit.getCheckmarks().groupBy(getTruncateField(bucketSize));
|
||||||
chart.setCheckmarks(checkmarks);
|
chart.setCheckmarks(checkmarks);
|
||||||
|
chart.setBucketSize(bucketSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -113,10 +134,16 @@ public class BarCard extends HabitCard
|
|||||||
int color = PaletteUtils.getColor(getContext(), habit.getColor());
|
int color = PaletteUtils.getColor(getContext(), habit.getColor());
|
||||||
title.setTextColor(color);
|
title.setTextColor(color);
|
||||||
chart.setColor(color);
|
chart.setColor(color);
|
||||||
if(habit.isNumerical())
|
if (habit.isNumerical())
|
||||||
|
{
|
||||||
|
boolSpinner.setVisibility(GONE);
|
||||||
chart.setTarget(habit.getTargetValue());
|
chart.setTarget(habit.getTargetValue());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
numericalSpinner.setVisibility(GONE);
|
||||||
chart.setTarget(0);
|
chart.setTarget(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?><!--
|
||||||
<!--
|
|
||||||
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
~
|
~
|
||||||
~ This file is part of Loop Habit Tracker.
|
~ This file is part of Loop Habit Tracker.
|
||||||
@@ -18,20 +17,46 @@
|
|||||||
~ with this program. If not, see <http://www.gnu.org/licenses/>.
|
~ with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<merge
|
<merge xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:clipToPadding="false"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:paddingBottom="0dp">
|
|
||||||
|
|
||||||
<TextView
|
<RelativeLayout
|
||||||
android:id="@+id/title"
|
|
||||||
style="@style/CardHeader"
|
|
||||||
android:text="@string/history"/>
|
|
||||||
|
|
||||||
<org.isoron.uhabits.activities.common.views.BarChart
|
|
||||||
android:id="@+id/barChart"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="220dp"/>
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<android.support.v7.widget.AppCompatSpinner
|
||||||
|
android:id="@+id/numericalSpinner"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="22dp"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:entries="@array/strengthIntervalNames"
|
||||||
|
android:theme="@style/SmallSpinner" />
|
||||||
|
|
||||||
|
<android.support.v7.widget.AppCompatSpinner
|
||||||
|
android:id="@+id/boolSpinner"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="22dp"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:entries="@array/strengthIntervalNamesWithoutDay"
|
||||||
|
android:theme="@style/SmallSpinner" />
|
||||||
|
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/title"
|
||||||
|
style="@style/CardHeader"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
android:layout_alignParentStart="true"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:text="@string/history" />
|
||||||
|
|
||||||
|
<org.isoron.uhabits.activities.common.views.BarChart
|
||||||
|
android:id="@+id/barChart"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="220dp"
|
||||||
|
android:layout_below="@id/title"/>
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
</merge>
|
</merge>
|
||||||
@@ -98,6 +98,13 @@
|
|||||||
<item>@string/year</item>
|
<item>@string/year</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
|
<string-array name="strengthIntervalNamesWithoutDay" translatable="false">
|
||||||
|
<item>@string/week</item>
|
||||||
|
<item>@string/month</item>
|
||||||
|
<item>@string/quarter</item>
|
||||||
|
<item>@string/year</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
<string-array name="strenghtIntervalValues" translatable="false">
|
<string-array name="strenghtIntervalValues" translatable="false">
|
||||||
<item>1</item>
|
<item>1</item>
|
||||||
<item>7</item>
|
<item>7</item>
|
||||||
|
|||||||
@@ -349,6 +349,12 @@ public abstract class CheckmarkList
|
|||||||
add(buildCheckmarksFromIntervals(reps, intervals));
|
add(buildCheckmarksFromIntervals(reps, intervals));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Checkmark> getAll() {
|
||||||
|
Repetition oldest = habit.getRepetitions().getOldest();
|
||||||
|
if(oldest == null) return new ArrayList<>();
|
||||||
|
return getByInterval(oldest.getTimestamp(), DateUtils.getToday());
|
||||||
|
}
|
||||||
|
|
||||||
static final class Interval
|
static final class Interval
|
||||||
{
|
{
|
||||||
final Timestamp begin;
|
final Timestamp begin;
|
||||||
@@ -408,9 +414,7 @@ public abstract class CheckmarkList
|
|||||||
@NonNull
|
@NonNull
|
||||||
public List<Checkmark> groupBy(DateUtils.TruncateField field)
|
public List<Checkmark> groupBy(DateUtils.TruncateField field)
|
||||||
{
|
{
|
||||||
Repetition oldest = habit.getRepetitions().getOldest();
|
List<Checkmark> checks = getAll();
|
||||||
if(oldest == null) return new ArrayList<>();
|
|
||||||
List<Checkmark> checks = getByInterval(oldest.getTimestamp(), DateUtils.getToday());
|
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
Timestamp truncatedTimestamps[] = new Timestamp[checks.size()];
|
Timestamp truncatedTimestamps[] = new Timestamp[checks.size()];
|
||||||
|
|||||||
Reference in New Issue
Block a user