mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Refactor FrequencyCard
This commit is contained in:
@@ -99,6 +99,7 @@ data class ShowHabitViewModel(
|
||||
val target: TargetCardViewModel,
|
||||
val streaks: StreakCardViewModel,
|
||||
val scores: ScoreCardViewModel,
|
||||
val frequency: FrequencyCardViewModel,
|
||||
)
|
||||
|
||||
class ShowHabitView(context: Context) : FrameLayout(context) {
|
||||
@@ -119,6 +120,7 @@ class ShowHabitView(context: Context) : FrameLayout(context) {
|
||||
binding.targetCard.update(data.target)
|
||||
binding.streakCard.update(data.streaks)
|
||||
binding.scoreCard.update(data.scores)
|
||||
binding.frequencyCard.update(data.frequency)
|
||||
if (data.isNumerical) {
|
||||
binding.overviewCard.visibility = GONE
|
||||
binding.streakCard.visibility = GONE
|
||||
@@ -148,6 +150,8 @@ class ShowHabitPresenter(
|
||||
private val streakCartPresenter = StreakCartPresenter(habit)
|
||||
private val scoreCardPresenter = ScoreCardPresenter(habit = habit,
|
||||
firstWeekday = preferences.firstWeekday)
|
||||
private val frequencyCardPresenter = FrequencyCardPresenter(habit = habit,
|
||||
firstWeekday = preferences.firstWeekday)
|
||||
|
||||
suspend fun present(): ShowHabitViewModel {
|
||||
return ShowHabitViewModel(
|
||||
@@ -159,7 +163,8 @@ class ShowHabitPresenter(
|
||||
notes = notesCardPresenter.present(),
|
||||
target = targetCardPresenter.present(),
|
||||
streaks = streakCartPresenter.present(),
|
||||
scores = scoreCardPresenter.present(preferences.defaultScoreSpinnerPosition)
|
||||
scores = scoreCardPresenter.present(preferences.defaultScoreSpinnerPosition),
|
||||
frequency = frequencyCardPresenter.present(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,110 +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.util.*;
|
||||
import android.widget.*;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.R;
|
||||
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.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import butterknife.*;
|
||||
|
||||
public class FrequencyCard extends HabitCard
|
||||
{
|
||||
@BindView(R.id.title)
|
||||
TextView title;
|
||||
|
||||
@BindView(R.id.frequencyChart)
|
||||
FrequencyChart chart;
|
||||
|
||||
@Nullable
|
||||
private Preferences prefs;
|
||||
|
||||
public FrequencyCard(Context context)
|
||||
{
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public FrequencyCard(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task createRefreshTask()
|
||||
{
|
||||
return new RefreshTask();
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
Context appContext = getContext().getApplicationContext();
|
||||
if (appContext instanceof HabitsApplication)
|
||||
{
|
||||
HabitsApplication app = (HabitsApplication) appContext;
|
||||
prefs = app.getComponent().getPreferences();
|
||||
}
|
||||
inflate(getContext(), R.layout.show_habit_frequency, this);
|
||||
ButterKnife.bind(this);
|
||||
if (isInEditMode()) initEditMode();
|
||||
}
|
||||
|
||||
private void initEditMode()
|
||||
{
|
||||
int color = PaletteUtils.getAndroidTestColor(1);
|
||||
title.setTextColor(color);
|
||||
chart.setColor(color);
|
||||
chart.populateWithRandomData();
|
||||
}
|
||||
|
||||
private class RefreshTask extends CancelableTask
|
||||
{
|
||||
@Override
|
||||
public void doInBackground()
|
||||
{
|
||||
if (isCanceled()) return;
|
||||
RepetitionList reps = getHabit().getRepetitions();
|
||||
HashMap<Timestamp, Integer[]> frequency = reps.getWeekdayFrequency();
|
||||
if(prefs != null) chart.setFirstWeekday(prefs.getFirstWeekday());
|
||||
chart.setFrequency(frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreExecute()
|
||||
{
|
||||
int color = PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
|
||||
title.setTextColor(color);
|
||||
chart.setColor(color);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.activities.habits.show.views
|
||||
|
||||
import android.content.*
|
||||
import android.util.*
|
||||
import android.view.*
|
||||
import android.widget.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.databinding.*
|
||||
import org.isoron.uhabits.utils.*
|
||||
import java.util.*
|
||||
|
||||
data class FrequencyCardViewModel(
|
||||
val frequency: HashMap<Timestamp, Array<Int>>,
|
||||
val firstWeekday: Int,
|
||||
val color: PaletteColor,
|
||||
)
|
||||
|
||||
class FrequencyCard(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
|
||||
|
||||
private var binding = ShowHabitFrequencyBinding.inflate(LayoutInflater.from(context), this)
|
||||
|
||||
fun update(data: FrequencyCardViewModel) {
|
||||
val androidColor = data.color.toThemedAndroidColor(context)
|
||||
binding.frequencyChart.setFrequency(data.frequency)
|
||||
binding.frequencyChart.setFirstWeekday(data.firstWeekday)
|
||||
binding.title.setTextColor(androidColor)
|
||||
binding.frequencyChart.setColor(androidColor)
|
||||
}
|
||||
}
|
||||
|
||||
class FrequencyCardPresenter(
|
||||
val habit: Habit,
|
||||
val firstWeekday: Int,
|
||||
) {
|
||||
fun present() = FrequencyCardViewModel(
|
||||
color = habit.color,
|
||||
frequency = habit.repetitions.weekdayFrequency,
|
||||
firstWeekday = firstWeekday,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user