Skip to content

Commit

Permalink
Use dynamic number of streaks on widget
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jul 17, 2016
1 parent 33596a2 commit 35e93fd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.*;
import android.util.*;
import android.view.*;
import android.view.ViewGroup.*;

import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
Expand All @@ -31,6 +32,8 @@
import java.text.*;
import java.util.*;

import static android.view.View.MeasureSpec.*;

public class StreakChart extends View
{
private Paint paint;
Expand Down Expand Up @@ -79,18 +82,15 @@ public StreakChart(Context context, AttributeSet attrs)
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;
initColors();
}

public void setStreaks(List<Streak> streaks)
{
this.streaks = streaks;
initColors();
updateMaxMinLengths();
requestLayout();
return (int) Math.floor(getMeasuredHeight() / baseSize);
}

public void populateWithRandomData()
Expand All @@ -99,7 +99,7 @@ public void populateWithRandomData()
long start = DateUtils.getStartOfToday();
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);
long end = start + length * day;
Expand All @@ -116,6 +116,20 @@ public void setColor(int color)
postInvalidate();
}

public void setIsBackgroundTransparent(boolean isBackgroundTransparent)
{
this.isBackgroundTransparent = isBackgroundTransparent;
initColors();
}

public void setStreaks(List<Streak> streaks)
{
this.streaks = streaks;
initColors();
updateMaxMinLengths();
requestLayout();
}

@Override
protected void onDraw(Canvas canvas)
{
Expand All @@ -134,11 +148,17 @@ protected void onDraw(Canvas canvas)
@Override
protected void onMeasure(int widthSpec, int heightSpec)
{
int width = MeasureSpec.getSize(widthSpec);
int height = streaks.size() * baseSize;
LayoutParams params = getLayoutParams();

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.*;
import android.support.annotation.*;
import android.view.*;
import android.view.ViewGroup.*;

import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
Expand All @@ -32,6 +33,8 @@

import java.util.*;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class StreakWidget extends BaseWidget
{
@NonNull
Expand All @@ -57,8 +60,8 @@ public void refreshData(View view)

int color = ColorUtils.getColor(getContext(), habit.getColor());

// TODO: make this dynamic
List<Streak> streaks = habit.getStreaks().getBest(10);
int count = chart.getMaxStreakCount();
List<Streak> streaks = habit.getStreaks().getBest(count);

chart.setColor(color);
chart.setStreaks(streaks);
Expand All @@ -69,7 +72,9 @@ protected View buildView()
{
StreakChart dataView = new StreakChart(getContext());
GraphWidgetView view = new GraphWidgetView(getContext(), dataView);
LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
view.setTitle(habit.getName());
view.setLayoutParams(params);
return view;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,4 @@ protected BaseWidget getWidgetFromId(@NonNull Context context, int id)
Habit habit = getHabitFromWidgetId(id);
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);
// }
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/show_habit_streak.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
<org.isoron.uhabits.ui.common.views.StreakChart
android:id="@+id/streakChart"
android:layout_width="match_parent"
android:layout_height="200dp"/>
android:layout_height="wrap_content"/>
</merge>

0 comments on commit 35e93fd

Please sign in to comment.