mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Refactor TargetCard
This commit is contained in:
@@ -21,12 +21,12 @@ package org.isoron.uhabits.activities.habits.show
|
|||||||
|
|
||||||
import android.annotation.*
|
import android.annotation.*
|
||||||
import android.content.*
|
import android.content.*
|
||||||
import android.content.res.*
|
|
||||||
import org.isoron.androidbase.activities.*
|
import org.isoron.androidbase.activities.*
|
||||||
import org.isoron.uhabits.*
|
import org.isoron.uhabits.*
|
||||||
import org.isoron.uhabits.activities.habits.list.views.*
|
import org.isoron.uhabits.activities.habits.list.views.*
|
||||||
import org.isoron.uhabits.core.commands.*
|
import org.isoron.uhabits.core.commands.*
|
||||||
import org.isoron.uhabits.core.models.*
|
import org.isoron.uhabits.core.models.*
|
||||||
|
import org.isoron.uhabits.core.preferences.*
|
||||||
import org.isoron.uhabits.core.utils.*
|
import org.isoron.uhabits.core.utils.*
|
||||||
import org.isoron.uhabits.utils.*
|
import org.isoron.uhabits.utils.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -37,6 +37,7 @@ class ShowHabitPresenter
|
|||||||
@Inject constructor(
|
@Inject constructor(
|
||||||
val habit: Habit,
|
val habit: Habit,
|
||||||
val commandRunner: CommandRunner,
|
val commandRunner: CommandRunner,
|
||||||
|
val preferences: Preferences,
|
||||||
@ActivityContext val context: Context,
|
@ActivityContext val context: Context,
|
||||||
) : CommandRunner.Listener {
|
) : CommandRunner.Listener {
|
||||||
|
|
||||||
@@ -76,18 +77,60 @@ class ShowHabitPresenter
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun refresh() {
|
private fun refresh() {
|
||||||
val scores = habit.scores
|
|
||||||
val today = DateUtils.getTodayWithOffset()
|
val today = DateUtils.getTodayWithOffset()
|
||||||
val lastMonth = today.minus(30)
|
val lastMonth = today.minus(30)
|
||||||
val lastYear = today.minus(365)
|
val lastYear = today.minus(365)
|
||||||
val scoreToday = scores.todayValue.toFloat()
|
|
||||||
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
|
||||||
val scoreLastYear = scores.getValue(lastYear).toFloat()
|
|
||||||
val reminderText = if (habit.hasReminder()) {
|
val reminderText = if (habit.hasReminder()) {
|
||||||
formatTime(context, habit.reminder.hour, habit.reminder.minute)!!
|
formatTime(context, habit.reminder.hour, habit.reminder.minute)!!
|
||||||
} else {
|
} else {
|
||||||
resources.getString(R.string.reminder_off)
|
resources.getString(R.string.reminder_off)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val scores = habit.scores
|
||||||
|
val scoreToday = scores.todayValue.toFloat()
|
||||||
|
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
||||||
|
val scoreLastYear = scores.getValue(lastYear).toFloat()
|
||||||
|
|
||||||
|
val checkmarks = habit.checkmarks
|
||||||
|
val valueToday = checkmarks.todayValue / 1e3
|
||||||
|
val valueThisWeek = checkmarks.getThisWeekValue(preferences.firstWeekday) / 1e3
|
||||||
|
val valueThisMonth = checkmarks.thisMonthValue / 1e3
|
||||||
|
val valueThisQuarter = checkmarks.thisQuarterValue / 1e3
|
||||||
|
val valueThisYear = checkmarks.thisYearValue / 1e3
|
||||||
|
|
||||||
|
val cal = DateUtils.getStartOfTodayCalendarWithOffset()
|
||||||
|
val daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
|
||||||
|
val daysInQuarter = 91
|
||||||
|
val daysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR)
|
||||||
|
|
||||||
|
val targetToday = habit.getTargetValue() / habit.frequency.denominator
|
||||||
|
val targetThisWeek = targetToday * 7
|
||||||
|
val targetThisMonth = targetToday * daysInMonth
|
||||||
|
val targetThisQuarter = targetToday * daysInQuarter
|
||||||
|
val targetThisYear = targetToday * daysInYear
|
||||||
|
|
||||||
|
val targetCompleted = ArrayList<Double>()
|
||||||
|
if (habit.frequency.denominator <= 1) targetCompleted.add(valueToday)
|
||||||
|
if (habit.frequency.denominator <= 7) targetCompleted.add(valueThisWeek)
|
||||||
|
targetCompleted.add(valueThisMonth)
|
||||||
|
targetCompleted.add(valueThisQuarter)
|
||||||
|
targetCompleted.add(valueThisYear)
|
||||||
|
|
||||||
|
val targetTotal = ArrayList<Double>()
|
||||||
|
if (habit.frequency.denominator <= 1) targetTotal.add(targetToday)
|
||||||
|
if (habit.frequency.denominator <= 7) targetTotal.add(targetThisWeek)
|
||||||
|
targetTotal.add(targetThisMonth)
|
||||||
|
targetTotal.add(targetThisQuarter)
|
||||||
|
targetTotal.add(targetThisYear)
|
||||||
|
|
||||||
|
val targetLabels = ArrayList<String>()
|
||||||
|
if (habit.frequency.denominator <= 1) targetLabels.add(resources.getString(R.string.today))
|
||||||
|
if (habit.frequency.denominator <= 7) targetLabels.add(resources.getString(R.string.week))
|
||||||
|
targetLabels.add(resources.getString(R.string.month))
|
||||||
|
targetLabels.add(resources.getString(R.string.quarter))
|
||||||
|
targetLabels.add(resources.getString(R.string.year))
|
||||||
|
|
||||||
data = ShowHabitViewModel(
|
data = ShowHabitViewModel(
|
||||||
title = habit.name,
|
title = habit.name,
|
||||||
description = habit.description,
|
description = habit.description,
|
||||||
@@ -101,6 +144,9 @@ class ShowHabitPresenter
|
|||||||
targetText = "${habit.targetValue.toShortString()} ${habit.unit}",
|
targetText = "${habit.targetValue.toShortString()} ${habit.unit}",
|
||||||
frequencyText = habit.frequency.format(),
|
frequencyText = habit.frequency.format(),
|
||||||
reminderText = reminderText,
|
reminderText = reminderText,
|
||||||
|
targetCompleted = targetCompleted,
|
||||||
|
targetTotal = targetTotal,
|
||||||
|
targetLabels = targetLabels,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,13 +47,13 @@ class ShowHabitRootView
|
|||||||
binding.overviewCard.presenter = presenter
|
binding.overviewCard.presenter = presenter
|
||||||
binding.notesCard.presenter = presenter
|
binding.notesCard.presenter = presenter
|
||||||
binding.subtitleCard.presenter = presenter
|
binding.subtitleCard.presenter = presenter
|
||||||
|
binding.targetCard.presenter = presenter
|
||||||
|
|
||||||
binding.scoreCard.habit = habit
|
binding.scoreCard.habit = habit
|
||||||
binding.historyCard.habit = habit
|
binding.historyCard.habit = habit
|
||||||
binding.streakCard.habit = habit
|
binding.streakCard.habit = habit
|
||||||
binding.frequencyCard.habit = habit
|
binding.frequencyCard.habit = habit
|
||||||
binding.barCard.habit = habit
|
binding.barCard.habit = habit
|
||||||
binding.targetCard.habit = habit
|
|
||||||
|
|
||||||
initToolbar()
|
initToolbar()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,4 +34,7 @@ data class ShowHabitViewModel(
|
|||||||
val targetText: String = "",
|
val targetText: String = "",
|
||||||
val frequencyText: String = "",
|
val frequencyText: String = "",
|
||||||
val reminderText: String = "",
|
val reminderText: String = "",
|
||||||
|
val targetCompleted: List<Double> = listOf(),
|
||||||
|
val targetTotal: List<Double> = listOf(),
|
||||||
|
val targetLabels: List<String> = listOf(),
|
||||||
)
|
)
|
||||||
@@ -1,179 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.activities.habits.show.views;
|
|
||||||
|
|
||||||
import android.content.*;
|
|
||||||
import android.content.res.*;
|
|
||||||
import android.util.*;
|
|
||||||
import android.widget.*;
|
|
||||||
|
|
||||||
import androidx.annotation.*;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.R;
|
|
||||||
import org.isoron.uhabits.*;
|
|
||||||
import org.isoron.uhabits.activities.common.views.*;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
|
||||||
import org.isoron.uhabits.core.preferences.*;
|
|
||||||
import org.isoron.uhabits.core.tasks.*;
|
|
||||||
import org.isoron.uhabits.core.utils.*;
|
|
||||||
import org.isoron.uhabits.utils.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import butterknife.*;
|
|
||||||
|
|
||||||
public class TargetCard extends HabitCard
|
|
||||||
{
|
|
||||||
@BindView(R.id.title)
|
|
||||||
TextView title;
|
|
||||||
|
|
||||||
@BindView(R.id.targetChart)
|
|
||||||
TargetChart targetChart;
|
|
||||||
|
|
||||||
int firstWeekday = Calendar.SATURDAY;
|
|
||||||
|
|
||||||
public TargetCard(Context context)
|
|
||||||
{
|
|
||||||
super(context);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TargetCard(Context context, AttributeSet attrs)
|
|
||||||
{
|
|
||||||
super(context, attrs);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init()
|
|
||||||
{
|
|
||||||
inflate(getContext(), R.layout.show_habit_target, this);
|
|
||||||
ButterKnife.bind(this);
|
|
||||||
setOrientation(VERTICAL);
|
|
||||||
|
|
||||||
Context app = getContext().getApplicationContext();
|
|
||||||
if (app instanceof HabitsApplication) {
|
|
||||||
HabitsApplication habitsApp = (HabitsApplication) app;
|
|
||||||
Preferences prefs = habitsApp.getComponent().getPreferences();
|
|
||||||
firstWeekday = prefs.getFirstWeekday();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isInEditMode()) initEditMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initEditMode()
|
|
||||||
{
|
|
||||||
int color = PaletteUtils.getAndroidTestColor(1);
|
|
||||||
title.setTextColor(color);
|
|
||||||
targetChart.setColor(color);
|
|
||||||
targetChart.populateWithRandomData();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Task createRefreshTask()
|
|
||||||
{
|
|
||||||
return new RefreshTask(getContext(), getHabit(), firstWeekday, targetChart, title);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class RefreshTask extends CancelableTask
|
|
||||||
{
|
|
||||||
double todayValue;
|
|
||||||
double thisWeekValue;
|
|
||||||
double thisMonthValue;
|
|
||||||
double thisQuarterValue;
|
|
||||||
double thisYearValue;
|
|
||||||
|
|
||||||
private Context context;
|
|
||||||
private Habit habit;
|
|
||||||
private int firstWeekday;
|
|
||||||
private TargetChart chart;
|
|
||||||
private TextView title;
|
|
||||||
|
|
||||||
public RefreshTask(@NonNull Context context,
|
|
||||||
@NonNull Habit habit,
|
|
||||||
int firstWeekday,
|
|
||||||
@NonNull TargetChart chart,
|
|
||||||
@Nullable TextView title)
|
|
||||||
{
|
|
||||||
this.context = context;
|
|
||||||
this.habit = habit;
|
|
||||||
this.firstWeekday = firstWeekday;
|
|
||||||
this.chart = chart;
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void doInBackground()
|
|
||||||
{
|
|
||||||
if (isCanceled()) return;
|
|
||||||
CheckmarkList checkmarks = habit.getCheckmarks();
|
|
||||||
todayValue = checkmarks.getTodayValue() / 1e3;
|
|
||||||
thisWeekValue = checkmarks.getThisWeekValue(firstWeekday) / 1e3;
|
|
||||||
thisMonthValue = checkmarks.getThisMonthValue() / 1e3;
|
|
||||||
thisQuarterValue = checkmarks.getThisQuarterValue() / 1e3;
|
|
||||||
thisYearValue = checkmarks.getThisYearValue() / 1e3;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPostExecute()
|
|
||||||
{
|
|
||||||
if (isCanceled()) return;
|
|
||||||
Calendar cal = DateUtils.getStartOfTodayCalendarWithOffset();
|
|
||||||
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
||||||
int daysInQuarter = 91;
|
|
||||||
int daysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
|
|
||||||
|
|
||||||
int den = habit.getFrequency().getDenominator();
|
|
||||||
double dailyTarget = habit.getTargetValue() / den;
|
|
||||||
Resources res = context.getResources();
|
|
||||||
|
|
||||||
ArrayList<Double> values = new ArrayList<>();
|
|
||||||
if (den <= 1) values.add(todayValue);
|
|
||||||
if (den <= 7) values.add(thisWeekValue);
|
|
||||||
values.add(thisMonthValue);
|
|
||||||
values.add(thisQuarterValue);
|
|
||||||
values.add(thisYearValue);
|
|
||||||
chart.setValues(values);
|
|
||||||
|
|
||||||
ArrayList<Double> targets = new ArrayList<>();
|
|
||||||
if (den <= 1) targets.add(dailyTarget);
|
|
||||||
if (den <= 7) targets.add(dailyTarget * 7);
|
|
||||||
targets.add(dailyTarget * daysInMonth);
|
|
||||||
targets.add(dailyTarget * daysInQuarter);
|
|
||||||
targets.add(dailyTarget * daysInYear);
|
|
||||||
chart.setTargets(targets);
|
|
||||||
|
|
||||||
ArrayList<String> labels = new ArrayList<>();
|
|
||||||
if (den <= 1) labels.add(res.getString(R.string.today));
|
|
||||||
if (den <= 7) labels.add(res.getString(R.string.week));
|
|
||||||
labels.add(res.getString(R.string.month));
|
|
||||||
labels.add(res.getString(R.string.quarter));
|
|
||||||
labels.add(res.getString(R.string.year));
|
|
||||||
chart.setLabels(labels);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPreExecute()
|
|
||||||
{
|
|
||||||
int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), context);
|
|
||||||
if(title != null) title.setTextColor(color);
|
|
||||||
chart.setColor(color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.activities.habits.show.views
|
||||||
|
|
||||||
|
import android.content.*
|
||||||
|
import android.util.*
|
||||||
|
import android.view.*
|
||||||
|
import android.widget.*
|
||||||
|
import org.isoron.uhabits.activities.habits.show.*
|
||||||
|
import org.isoron.uhabits.databinding.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
|
||||||
|
class TargetCard(
|
||||||
|
context: Context,
|
||||||
|
attrs: AttributeSet,
|
||||||
|
) : LinearLayout(context, attrs), ShowHabitPresenter.Listener {
|
||||||
|
|
||||||
|
private val binding = ShowHabitTargetBinding.inflate(LayoutInflater.from(context), this)
|
||||||
|
lateinit var presenter: ShowHabitPresenter
|
||||||
|
|
||||||
|
override fun onAttachedToWindow() {
|
||||||
|
super.onAttachedToWindow()
|
||||||
|
presenter.addListener(this)
|
||||||
|
presenter.requestData(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDetachedFromWindow() {
|
||||||
|
presenter.removeListener(this)
|
||||||
|
super.onDetachedFromWindow()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onData(data: ShowHabitViewModel) {
|
||||||
|
val androidColor = data.color.toThemedAndroidColor(context)
|
||||||
|
binding.targetChart.setValues(data.targetCompleted)
|
||||||
|
binding.targetChart.setTargets(data.targetTotal)
|
||||||
|
binding.targetChart.setLabels(data.targetLabels)
|
||||||
|
binding.title.setTextColor(androidColor)
|
||||||
|
binding.targetChart.setColor(androidColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,15 +38,15 @@ class TargetWidget(
|
|||||||
pendingIntentFactory.showHabit(habit)
|
pendingIntentFactory.showHabit(habit)
|
||||||
|
|
||||||
override fun refreshData(view: View) {
|
override fun refreshData(view: View) {
|
||||||
val widgetView = view as GraphWidgetView
|
// val widgetView = view as GraphWidgetView
|
||||||
widgetView.setBackgroundAlpha(preferedBackgroundAlpha)
|
// widgetView.setBackgroundAlpha(preferedBackgroundAlpha)
|
||||||
if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f)
|
// if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f)
|
||||||
val chart = (widgetView.dataView as TargetChart)
|
// val chart = (widgetView.dataView as TargetChart)
|
||||||
with(TargetCard.RefreshTask(context, habit, prefs.firstWeekday, chart, null)) {
|
// with(TargetCard.RefreshTask(context, habit, prefs.firstWeekday, chart, null)) {
|
||||||
onPreExecute()
|
// onPreExecute()
|
||||||
doInBackground()
|
// doInBackground()
|
||||||
onPostExecute()
|
// onPostExecute()
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun buildView(): View {
|
override fun buildView(): View {
|
||||||
|
|||||||
Reference in New Issue
Block a user