Use dynamic number of streaks on widget

pull/151/head
Alinson S. Xavier 9 years ago
parent 33596a2797
commit 35e93fddc6

@ -23,6 +23,7 @@ import android.content.*;
import android.graphics.*; import android.graphics.*;
import android.util.*; import android.util.*;
import android.view.*; import android.view.*;
import android.view.ViewGroup.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
@ -31,6 +32,8 @@ import org.isoron.uhabits.utils.*;
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
import static android.view.View.MeasureSpec.*;
public class StreakChart extends View public class StreakChart extends View
{ {
private Paint paint; private Paint paint;
@ -79,18 +82,15 @@ public class StreakChart extends View
init(); init();
} }
public void setIsBackgroundTransparent(boolean isBackgroundTransparent) /**
* Returns the maximum number of streaks this view is able to show, given
* its current size.
*
* @return max number of visible streaks
*/
public int getMaxStreakCount()
{ {
this.isBackgroundTransparent = isBackgroundTransparent; return (int) Math.floor(getMeasuredHeight() / baseSize);
initColors();
}
public void setStreaks(List<Streak> streaks)
{
this.streaks = streaks;
initColors();
updateMaxMinLengths();
requestLayout();
} }
public void populateWithRandomData() public void populateWithRandomData()
@ -99,7 +99,7 @@ public class StreakChart extends View
long start = DateUtils.getStartOfToday(); long start = DateUtils.getStartOfToday();
LinkedList<Streak> streaks = new LinkedList<>(); LinkedList<Streak> streaks = new LinkedList<>();
for(int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
int length = new Random().nextInt(100); int length = new Random().nextInt(100);
long end = start + length * day; long end = start + length * day;
@ -116,6 +116,20 @@ public class StreakChart extends View
postInvalidate(); postInvalidate();
} }
public void setIsBackgroundTransparent(boolean isBackgroundTransparent)
{
this.isBackgroundTransparent = isBackgroundTransparent;
initColors();
}
public void setStreaks(List<Streak> streaks)
{
this.streaks = streaks;
initColors();
updateMaxMinLengths();
requestLayout();
}
@Override @Override
protected void onDraw(Canvas canvas) protected void onDraw(Canvas canvas)
{ {
@ -134,11 +148,17 @@ public class StreakChart extends View
@Override @Override
protected void onMeasure(int widthSpec, int heightSpec) protected void onMeasure(int widthSpec, int heightSpec)
{ {
int width = MeasureSpec.getSize(widthSpec); LayoutParams params = getLayoutParams();
int height = streaks.size() * baseSize;
if (params != null && params.height == LayoutParams.WRAP_CONTENT)
{
int width = getSize(widthSpec);
int height = streaks.size() * baseSize;
heightSpec = makeMeasureSpec(height, EXACTLY);
widthSpec = makeMeasureSpec(width, EXACTLY);
}
heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
setMeasuredDimension(widthSpec, heightSpec); setMeasuredDimension(widthSpec, heightSpec);
} }

@ -23,6 +23,7 @@ import android.app.*;
import android.content.*; import android.content.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.view.*; import android.view.*;
import android.view.ViewGroup.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
@ -32,6 +33,8 @@ import org.isoron.uhabits.utils.*;
import java.util.*; import java.util.*;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
public class StreakWidget extends BaseWidget public class StreakWidget extends BaseWidget
{ {
@NonNull @NonNull
@ -57,8 +60,8 @@ public class StreakWidget extends BaseWidget
int color = ColorUtils.getColor(getContext(), habit.getColor()); int color = ColorUtils.getColor(getContext(), habit.getColor());
// TODO: make this dynamic int count = chart.getMaxStreakCount();
List<Streak> streaks = habit.getStreaks().getBest(10); List<Streak> streaks = habit.getStreaks().getBest(count);
chart.setColor(color); chart.setColor(color);
chart.setStreaks(streaks); chart.setStreaks(streaks);
@ -69,7 +72,9 @@ public class StreakWidget extends BaseWidget
{ {
StreakChart dataView = new StreakChart(getContext()); StreakChart dataView = new StreakChart(getContext());
GraphWidgetView view = new GraphWidgetView(getContext(), dataView); GraphWidgetView view = new GraphWidgetView(getContext(), dataView);
LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
view.setTitle(habit.getName()); view.setTitle(habit.getName());
view.setLayoutParams(params);
return view; return view;
} }

@ -33,16 +33,4 @@ public class StreakWidgetProvider extends BaseWidgetProvider
Habit habit = getHabitFromWidgetId(id); Habit habit = getHabitFromWidgetId(id);
return new StreakWidget(context, id, habit); return new StreakWidget(context, id, habit);
} }
// GraphWidgetView widgetView = (GraphWidgetView) view;
// StreakChart chart = (StreakChart) widgetView.getDataView();
//
// int color = ColorUtils.getColor(context, habit.getColor());
//
// // TODO: make this dynamic
// List<Streak> streaks = habit.getStreaks().getBest(10);
//
// chart.setColor(color);
// chart.setStreaks(streaks);
// }
} }

@ -27,5 +27,5 @@
<org.isoron.uhabits.ui.common.views.StreakChart <org.isoron.uhabits.ui.common.views.StreakChart
android:id="@+id/streakChart" android:id="@+id/streakChart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="200dp"/> android:layout_height="wrap_content"/>
</merge> </merge>
Loading…
Cancel
Save