mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -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.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()
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
if (params != null && params.height == LayoutParams.WRAP_CONTENT)
|
||||||
|
{
|
||||||
|
int width = getSize(widthSpec);
|
||||||
int height = streaks.size() * baseSize;
|
int height = streaks.size() * baseSize;
|
||||||
|
|
||||||
heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
|
heightSpec = makeMeasureSpec(height, EXACTLY);
|
||||||
widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
|
widthSpec = makeMeasureSpec(width, 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>
|
||||||
Reference in New Issue
Block a user