mirror of https://github.com/tasks/tasks
Use material checkboxes
parent
c2918d1722
commit
70ba717d65
@ -0,0 +1,91 @@
|
||||
package org.tasks.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v4.graphics.drawable.DrawableCompat;
|
||||
|
||||
import org.tasks.R;
|
||||
import org.tasks.injection.ForApplication;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class CheckBoxes {
|
||||
|
||||
private static final int MAX_IMPORTANCE_INDEX = 3;
|
||||
|
||||
private static boolean initialized;
|
||||
private static List<Drawable> checkboxes;
|
||||
private static List<Drawable> repeatingCheckboxes;
|
||||
private static List<Drawable> completedCheckboxes;
|
||||
|
||||
@Inject
|
||||
public CheckBoxes(@ForApplication Context context) {
|
||||
if (!initialized) {
|
||||
Timber.d("Initializing checkboxes");
|
||||
checkboxes = wrapDrawable(context, R.drawable.ic_check_box_outline_blank_24dp);
|
||||
repeatingCheckboxes = wrapDrawable(context, R.drawable.ic_repeat_24dp);
|
||||
completedCheckboxes = wrapDrawable(context, R.drawable.ic_check_box_24dp);
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
List<Drawable> getCheckBoxes() {
|
||||
return checkboxes;
|
||||
}
|
||||
|
||||
List<Drawable> getRepeatingCheckBoxes() {
|
||||
return repeatingCheckboxes;
|
||||
}
|
||||
|
||||
List<Drawable> getCompletedCheckBoxes() {
|
||||
return completedCheckboxes;
|
||||
}
|
||||
|
||||
private static List<Drawable> wrapDrawable(Context context, int resId) {
|
||||
return asList(
|
||||
getDrawable(context, resId, 0),
|
||||
getDrawable(context, resId, 1),
|
||||
getDrawable(context, resId, 2),
|
||||
getDrawable(context, resId, 3));
|
||||
}
|
||||
|
||||
public Drawable getCompletedCheckbox(int importance) {
|
||||
return completedCheckboxes.get(Math.min(importance, MAX_IMPORTANCE_INDEX));
|
||||
}
|
||||
|
||||
public Drawable getRepeatingCheckBox(int importance) {
|
||||
return repeatingCheckboxes.get(Math.min(importance, MAX_IMPORTANCE_INDEX));
|
||||
}
|
||||
|
||||
public Drawable getCheckBox(int importance) {
|
||||
return checkboxes.get(Math.min(importance, MAX_IMPORTANCE_INDEX));
|
||||
}
|
||||
|
||||
private static Drawable getDrawable(Context context, int resId, int importance) {
|
||||
Resources resources = context.getResources();
|
||||
Drawable original = resources.getDrawable(resId);
|
||||
Drawable wrapped = DrawableCompat.wrap(original.mutate());
|
||||
DrawableCompat.setTint(wrapped, resources.getColor(getImportanceResId(importance)));
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
private static int getImportanceResId(int importance) {
|
||||
switch (importance) {
|
||||
case 0:
|
||||
return R.color.importance_1;
|
||||
case 1:
|
||||
return R.color.importance_2;
|
||||
case 2:
|
||||
return R.color.importance_3;
|
||||
default:
|
||||
return R.color.importance_4;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package org.tasks.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import org.tasks.injection.ForApplication;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
|
||||
public class WidgetCheckBoxes {
|
||||
|
||||
private static boolean initialized;
|
||||
private static List<Bitmap> checkboxes;
|
||||
private static List<Bitmap> repeatingCheckboxes;
|
||||
private static List<Bitmap> completedCheckboxes;
|
||||
|
||||
@Inject
|
||||
public WidgetCheckBoxes(@ForApplication Context context) {
|
||||
if (!initialized) {
|
||||
CheckBoxes checkBoxes = new CheckBoxes(context);
|
||||
Timber.d("Initializing widget checkboxes");
|
||||
checkboxes = convertToBitmap(checkBoxes.getCheckBoxes());
|
||||
repeatingCheckboxes = convertToBitmap(checkBoxes.getRepeatingCheckBoxes());
|
||||
completedCheckboxes = convertToBitmap(checkBoxes.getCompletedCheckBoxes());
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap getCompletedCheckbox(int importance) {
|
||||
return completedCheckboxes.get(importance);
|
||||
}
|
||||
|
||||
public Bitmap getRepeatingCheckBox(int importance) {
|
||||
return repeatingCheckboxes.get(importance);
|
||||
}
|
||||
|
||||
public Bitmap getCheckBox(int importance) {
|
||||
return checkboxes.get(importance);
|
||||
}
|
||||
|
||||
private static List<Bitmap> convertToBitmap(final List<Drawable> drawables) {
|
||||
return newArrayList(transform(drawables, new Function<Drawable, Bitmap>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public Bitmap apply(Drawable drawable) {
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
|
||||
if (bitmapDrawable.getBitmap() != null) {
|
||||
return bitmapDrawable.getBitmap();
|
||||
}
|
||||
}
|
||||
|
||||
Bitmap bitmap = drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0
|
||||
? Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
|
||||
: Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M19,3H5c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2V5c0,-1.1 -0.89,-2 -2,-2zm-9,14l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
|
||||
</vector>
|
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
|
||||
</vector>
|
Loading…
Reference in New Issue