mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Use dynamic number of streaks on widget
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.*;
|
||||
import android.graphics.*;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
import android.view.ViewGroup.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
@@ -31,6 +32,8 @@ import org.isoron.uhabits.utils.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import static android.view.View.MeasureSpec.*;
|
||||
|
||||
public class StreakChart extends View
|
||||
{
|
||||
private Paint paint;
|
||||
@@ -79,18 +82,15 @@ public class StreakChart extends View
|
||||
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()
|
||||
@@ -99,7 +99,7 @@ public class StreakChart extends View
|
||||
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;
|
||||
@@ -116,6 +116,20 @@ public class StreakChart extends View
|
||||
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)
|
||||
{
|
||||
@@ -134,11 +148,17 @@ public class StreakChart extends View
|
||||
@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);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.app.*;
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
import android.view.*;
|
||||
import android.view.ViewGroup.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
@@ -32,6 +33,8 @@ import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
|
||||
public class StreakWidget extends BaseWidget
|
||||
{
|
||||
@NonNull
|
||||
@@ -57,8 +60,8 @@ public class StreakWidget extends BaseWidget
|
||||
|
||||
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);
|
||||
@@ -69,7 +72,9 @@ public class StreakWidget extends BaseWidget
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,16 +33,4 @@ public class StreakWidgetProvider extends BaseWidgetProvider
|
||||
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);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user