mirror of https://github.com/iSoron/uhabits.git
parent
95b3028c95
commit
c016b0aecc
@ -1,168 +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.NonNull;
|
|
||||||
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.core.utils.*;
|
|
||||||
import org.isoron.uhabits.utils.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import butterknife.*;
|
|
||||||
|
|
||||||
public class ScoreCard extends HabitCard
|
|
||||||
{
|
|
||||||
public static final int[] BUCKET_SIZES = { 1, 7, 31, 92, 365 };
|
|
||||||
|
|
||||||
@BindView(R.id.spinner)
|
|
||||||
Spinner spinner;
|
|
||||||
|
|
||||||
@BindView(R.id.scoreView)
|
|
||||||
ScoreChart chart;
|
|
||||||
|
|
||||||
@BindView(R.id.title)
|
|
||||||
TextView title;
|
|
||||||
|
|
||||||
private int bucketSize;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Preferences prefs;
|
|
||||||
|
|
||||||
public ScoreCard(Context context)
|
|
||||||
{
|
|
||||||
super(context);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScoreCard(Context context, AttributeSet attrs)
|
|
||||||
{
|
|
||||||
super(context, attrs);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public static DateUtils.TruncateField getTruncateField(int bucketSize)
|
|
||||||
{
|
|
||||||
if (bucketSize == 7) return DateUtils.TruncateField.WEEK_NUMBER;
|
|
||||||
if (bucketSize == 31) return DateUtils.TruncateField.MONTH;
|
|
||||||
if (bucketSize == 92) return DateUtils.TruncateField.QUARTER;
|
|
||||||
if (bucketSize == 365) return DateUtils.TruncateField.YEAR;
|
|
||||||
|
|
||||||
Log.e("ScoreCard",
|
|
||||||
String.format("Unknown bucket size: %d", bucketSize));
|
|
||||||
|
|
||||||
return DateUtils.TruncateField.MONTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnItemSelected(R.id.spinner)
|
|
||||||
public void onItemSelected(int position)
|
|
||||||
{
|
|
||||||
setBucketSizeFromPosition(position);
|
|
||||||
HabitsApplication app =
|
|
||||||
(HabitsApplication) getContext().getApplicationContext();
|
|
||||||
app.getComponent().getWidgetUpdater().updateWidgets();
|
|
||||||
refreshData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getDefaultSpinnerPosition()
|
|
||||||
{
|
|
||||||
if(prefs == null) return 0;
|
|
||||||
return prefs.getDefaultScoreSpinnerPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
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_score, this);
|
|
||||||
ButterKnife.bind(this);
|
|
||||||
|
|
||||||
int defaultPosition = getDefaultSpinnerPosition();
|
|
||||||
setBucketSizeFromPosition(defaultPosition);
|
|
||||||
spinner.setSelection(defaultPosition);
|
|
||||||
|
|
||||||
if (isInEditMode())
|
|
||||||
{
|
|
||||||
spinner.setVisibility(GONE);
|
|
||||||
title.setTextColor(PaletteUtils.getAndroidTestColor(1));
|
|
||||||
chart.setColor(PaletteUtils.getAndroidTestColor(1));
|
|
||||||
chart.populateWithRandomData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setBucketSizeFromPosition(int position)
|
|
||||||
{
|
|
||||||
if(prefs == null) return;
|
|
||||||
prefs.setDefaultScoreSpinnerPosition(position);
|
|
||||||
bucketSize = BUCKET_SIZES[position];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Task createRefreshTask()
|
|
||||||
{
|
|
||||||
return new RefreshTask();
|
|
||||||
}
|
|
||||||
|
|
||||||
private class RefreshTask extends CancelableTask
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void doInBackground()
|
|
||||||
{
|
|
||||||
if (isCanceled()) return;
|
|
||||||
List<Score> scores;
|
|
||||||
ScoreList scoreList = getHabit().getScores();
|
|
||||||
int firstWeekday = Calendar.SATURDAY;
|
|
||||||
if (prefs != null) firstWeekday = prefs.getFirstWeekday();
|
|
||||||
Log.d("ScoreCard", "firstWeekday="+firstWeekday);
|
|
||||||
|
|
||||||
if (bucketSize == 1) scores = scoreList.toList();
|
|
||||||
else scores = scoreList.groupBy(getTruncateField(bucketSize), firstWeekday);
|
|
||||||
|
|
||||||
chart.setScores(scores);
|
|
||||||
chart.reset();
|
|
||||||
chart.setBucketSize(bucketSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPreExecute()
|
|
||||||
{
|
|
||||||
int color = PaletteUtilsKt.toThemedAndroidColor(getHabit().getColor(), getContext());
|
|
||||||
title.setTextColor(color);
|
|
||||||
chart.setColor(color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.utils.*
|
||||||
|
import org.isoron.uhabits.core.utils.DateUtils.TruncateField.*
|
||||||
|
import org.isoron.uhabits.databinding.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
|
||||||
|
data class ScoreCardViewModel(
|
||||||
|
val scores: List<Score>,
|
||||||
|
val bucketSize: Int,
|
||||||
|
val spinnerPosition: Int,
|
||||||
|
val color: PaletteColor,
|
||||||
|
)
|
||||||
|
|
||||||
|
class ScoreCard(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
|
||||||
|
private var binding = ShowHabitScoreBinding.inflate(LayoutInflater.from(context), this)
|
||||||
|
|
||||||
|
var onBucketSizeSelected: (position: Int) -> Unit = {}
|
||||||
|
|
||||||
|
fun update(data: ScoreCardViewModel) {
|
||||||
|
val androidColor = data.color.toThemedAndroidColor(context)
|
||||||
|
binding.title.setTextColor(androidColor)
|
||||||
|
binding.spinner.setSelection(data.spinnerPosition)
|
||||||
|
binding.scoreView.setScores(data.scores)
|
||||||
|
binding.scoreView.reset()
|
||||||
|
binding.scoreView.setBucketSize(data.bucketSize)
|
||||||
|
binding.scoreView.setColor(androidColor)
|
||||||
|
|
||||||
|
binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||||
|
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||||
|
onBucketSizeSelected(position)
|
||||||
|
}
|
||||||
|
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ScoreCardPresenter(
|
||||||
|
val habit: Habit,
|
||||||
|
val firstWeekday: Int,
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
val BUCKET_SIZES = intArrayOf(1, 7, 31, 92, 365)
|
||||||
|
fun getTruncateField(bucketSize: Int): DateUtils.TruncateField {
|
||||||
|
when (bucketSize) {
|
||||||
|
7 -> return WEEK_NUMBER
|
||||||
|
31 -> return MONTH
|
||||||
|
92 -> return QUARTER
|
||||||
|
365 -> return YEAR
|
||||||
|
else -> return MONTH
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun present(spinnerPosition: Int): ScoreCardViewModel {
|
||||||
|
val bucketSize = BUCKET_SIZES[spinnerPosition]
|
||||||
|
val scoreList = habit.scores
|
||||||
|
val scores = if (bucketSize == 1) scoreList.toList()
|
||||||
|
else scoreList.groupBy(getTruncateField(bucketSize), firstWeekday)
|
||||||
|
return ScoreCardViewModel(
|
||||||
|
color = habit.color,
|
||||||
|
scores = scores,
|
||||||
|
bucketSize = bucketSize,
|
||||||
|
spinnerPosition = spinnerPosition,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue