mirror of https://github.com/iSoron/uhabits.git
parent
8fd8c2802b
commit
8bd1734f44
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.widgets
|
||||||
|
|
||||||
|
import android.content.*
|
||||||
|
import android.view.*
|
||||||
|
import org.isoron.uhabits.core.models.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import org.isoron.uhabits.widgets.views.*
|
||||||
|
|
||||||
|
class CurrentStreakWidget(
|
||||||
|
context: Context,
|
||||||
|
widgetId: Int,
|
||||||
|
private val habit: Habit
|
||||||
|
) : BaseWidget(context, widgetId) {
|
||||||
|
|
||||||
|
override fun getOnClickPendingIntent(context: Context) =
|
||||||
|
pendingIntentFactory.toggleCheckmark(habit, null)
|
||||||
|
|
||||||
|
override fun refreshData(v: View) {
|
||||||
|
val activeStreak = habit.streaks.activeStreak
|
||||||
|
val numReps = if (activeStreak != null) {
|
||||||
|
habit.repetitions.getByInterval(activeStreak.start, activeStreak.end).size.toString()
|
||||||
|
} else {
|
||||||
|
"0"
|
||||||
|
}
|
||||||
|
(v as CurrentStreakWidgetView).apply {
|
||||||
|
setBackgroundAlpha(preferedBackgroundAlpha)
|
||||||
|
setCurrentStreak(numReps)
|
||||||
|
setPercentage(habit.scores.todayValue.toFloat())
|
||||||
|
setActiveColor(PaletteUtils.getColor(context, habit.color))
|
||||||
|
setName(habit.name)
|
||||||
|
setCheckmarkValue(habit.checkmarks.todayValue)
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun buildView() = CurrentStreakWidgetView(context)
|
||||||
|
override fun getDefaultHeight() = 125
|
||||||
|
override fun getDefaultWidth() = 125
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package org.isoron.uhabits.widgets
|
||||||
|
|
||||||
|
import android.content.*
|
||||||
|
|
||||||
|
class CurrentStreakWidgetProvider : BaseWidgetProvider() {
|
||||||
|
override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
|
||||||
|
val habits = getHabitsFromWidgetId(id)
|
||||||
|
if (habits.size == 1) return CurrentStreakWidget(context, id, habits[0])
|
||||||
|
else return StackWidget(context, id, StackWidgetType.CHECKMARK, habits)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,189 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.widgets.views;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.TypedValue;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.isoron.androidbase.utils.StyledResources;
|
||||||
|
import org.isoron.uhabits.R;
|
||||||
|
import org.isoron.uhabits.activities.common.views.RingView;
|
||||||
|
import org.isoron.uhabits.core.models.Checkmark;
|
||||||
|
import org.isoron.uhabits.utils.PaletteUtils;
|
||||||
|
|
||||||
|
import static org.isoron.androidbase.utils.InterfaceUtils.getDimension;
|
||||||
|
|
||||||
|
public class CurrentStreakWidgetView extends HabitWidgetView
|
||||||
|
{
|
||||||
|
private int activeColor;
|
||||||
|
|
||||||
|
private float percentage;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private RingView ring;
|
||||||
|
|
||||||
|
private TextView label;
|
||||||
|
|
||||||
|
private int checkmarkValue;
|
||||||
|
private String currentStreak;
|
||||||
|
|
||||||
|
public CurrentStreakWidgetView(Context context)
|
||||||
|
{
|
||||||
|
super(context);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CurrentStreakWidgetView(Context context, AttributeSet attrs)
|
||||||
|
{
|
||||||
|
super(context, attrs);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refresh()
|
||||||
|
{
|
||||||
|
if (backgroundPaint == null || frame == null || ring == null) return;
|
||||||
|
|
||||||
|
StyledResources res = new StyledResources(getContext());
|
||||||
|
|
||||||
|
String text;
|
||||||
|
int bgColor;
|
||||||
|
int fgColor;
|
||||||
|
|
||||||
|
switch (checkmarkValue)
|
||||||
|
{
|
||||||
|
case Checkmark.CHECKED_EXPLICITLY:
|
||||||
|
bgColor = activeColor;
|
||||||
|
fgColor = res.getColor(R.attr.highContrastReverseTextColor);
|
||||||
|
setShadowAlpha(0x4f);
|
||||||
|
backgroundPaint.setColor(bgColor);
|
||||||
|
frame.setBackgroundDrawable(background);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Checkmark.CHECKED_IMPLICITLY:
|
||||||
|
case Checkmark.UNCHECKED:
|
||||||
|
default:
|
||||||
|
bgColor = res.getColor(R.attr.cardBgColor);
|
||||||
|
fgColor = res.getColor(R.attr.mediumContrastTextColor);
|
||||||
|
setShadowAlpha(0x00);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
text = currentStreak;
|
||||||
|
|
||||||
|
ring.setPercentage(percentage);
|
||||||
|
ring.setColor(fgColor);
|
||||||
|
ring.setBackgroundColor(bgColor);
|
||||||
|
ring.setText(text);
|
||||||
|
|
||||||
|
label.setText(name);
|
||||||
|
label.setTextColor(fgColor);
|
||||||
|
|
||||||
|
requestLayout();
|
||||||
|
postInvalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActiveColor(int activeColor)
|
||||||
|
{
|
||||||
|
this.activeColor = activeColor;
|
||||||
|
}
|
||||||
|
public void setCurrentStreak(String currentStreak)
|
||||||
|
{
|
||||||
|
this.currentStreak = currentStreak;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCheckmarkValue(int checkmarkValue)
|
||||||
|
{
|
||||||
|
this.checkmarkValue = checkmarkValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(@NonNull String name)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPercentage(float percentage)
|
||||||
|
{
|
||||||
|
this.percentage = percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NonNull
|
||||||
|
protected Integer getInnerLayoutId()
|
||||||
|
{
|
||||||
|
return R.layout.widget_checkmark;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
||||||
|
{
|
||||||
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
||||||
|
int height = MeasureSpec.getSize(heightMeasureSpec);
|
||||||
|
|
||||||
|
float w = width;
|
||||||
|
float h = width * 1.25f;
|
||||||
|
float scale = Math.min(width / w, height / h);
|
||||||
|
|
||||||
|
w *= scale;
|
||||||
|
h *= scale;
|
||||||
|
|
||||||
|
if (h < getDimension(getContext(), R.dimen.checkmarkWidget_heightBreakpoint))
|
||||||
|
ring.setVisibility(GONE);
|
||||||
|
else
|
||||||
|
ring.setVisibility(VISIBLE);
|
||||||
|
|
||||||
|
widthMeasureSpec =
|
||||||
|
MeasureSpec.makeMeasureSpec((int) w, MeasureSpec.EXACTLY);
|
||||||
|
heightMeasureSpec =
|
||||||
|
MeasureSpec.makeMeasureSpec((int) h, MeasureSpec.EXACTLY);
|
||||||
|
|
||||||
|
float textSize = 0.15f * h;
|
||||||
|
float maxTextSize = getDimension(getContext(), R.dimen.smallerTextSize);
|
||||||
|
textSize = Math.min(textSize, maxTextSize);
|
||||||
|
|
||||||
|
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
||||||
|
ring.setTextSize(textSize);
|
||||||
|
ring.setThickness(0.15f * textSize);
|
||||||
|
|
||||||
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init()
|
||||||
|
{
|
||||||
|
ring = (RingView) findViewById(R.id.scoreRing);
|
||||||
|
label = (TextView) findViewById(R.id.label);
|
||||||
|
|
||||||
|
if (ring != null) ring.setIsTransparencyEnabled(true);
|
||||||
|
|
||||||
|
if (isInEditMode())
|
||||||
|
{
|
||||||
|
percentage = 0.75f;
|
||||||
|
name = "Wake up early";
|
||||||
|
activeColor = PaletteUtils.getAndroidTestColor(6);
|
||||||
|
checkmarkValue = Checkmark.CHECKED_EXPLICITLY;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/currentStreakStackWidgetView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:loopViews="true" />
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/currentStreakkWidgetEmptyView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:text="@string/checkmark_stack_widget"
|
||||||
|
android:gravity="center"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textSize="16sp" />
|
||||||
|
</FrameLayout>
|
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
~
|
||||||
|
~ This file is part of Loop Habit Tracker.
|
||||||
|
~
|
||||||
|
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
~ it under the terms of the GNU General Public License as published by the
|
||||||
|
~ Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
~ option) any later version.
|
||||||
|
~
|
||||||
|
~ Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
~ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
~ more details.
|
||||||
|
~
|
||||||
|
~ You should have received a copy of the GNU General Public License along
|
||||||
|
~ with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:minHeight="40dp"
|
||||||
|
android:minWidth="40dp"
|
||||||
|
android:initialLayout="@layout/widget_wrapper"
|
||||||
|
android:previewImage="@drawable/widget_preview_checkmark"
|
||||||
|
android:resizeMode="none"
|
||||||
|
android:updatePeriodMillis="3600000"
|
||||||
|
android:configure="org.isoron.uhabits.widgets.HabitPickerDialog"
|
||||||
|
android:widgetCategory="home_screen">
|
||||||
|
</appwidget-provider>
|
Loading…
Reference in new issue