mirror of https://github.com/iSoron/uhabits.git
parent
c230b5c40d
commit
816ab71d83
@ -1,66 +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 androidx.test.filters.*;
|
|
||||||
import androidx.test.runner.*;
|
|
||||||
import android.view.*;
|
|
||||||
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
|
||||||
import org.junit.*;
|
|
||||||
import org.junit.runner.*;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
@MediumTest
|
|
||||||
public class HistoryCardTest extends BaseViewTest
|
|
||||||
{
|
|
||||||
public static final String PATH = "habits/show/HistoryCard/";
|
|
||||||
|
|
||||||
private HistoryCard view;
|
|
||||||
|
|
||||||
private Habit habit;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
@Override
|
|
||||||
public void setUp()
|
|
||||||
{
|
|
||||||
super.setUp();
|
|
||||||
habit = fixtures.createLongHabit();
|
|
||||||
|
|
||||||
view = (HistoryCard) LayoutInflater
|
|
||||||
.from(targetContext)
|
|
||||||
.inflate(R.layout.show_habit, null)
|
|
||||||
.findViewById(R.id.historyCard);
|
|
||||||
|
|
||||||
view.setHabit(habit);
|
|
||||||
view.refreshData();
|
|
||||||
|
|
||||||
measureView(view, 800, 600);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRender() throws Exception
|
|
||||||
{
|
|
||||||
assertRenders(view, PATH + "render.png");
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.view.*
|
||||||
|
import androidx.test.ext.junit.runners.*
|
||||||
|
import androidx.test.filters.*
|
||||||
|
import org.isoron.uhabits.*
|
||||||
|
import org.junit.*
|
||||||
|
import org.junit.runner.*
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
@MediumTest
|
||||||
|
class HistoryCardTest : BaseViewTest() {
|
||||||
|
private lateinit var view: HistoryCard
|
||||||
|
val PATH = "habits/show/HistoryCard/"
|
||||||
|
|
||||||
|
@Before
|
||||||
|
override fun setUp() {
|
||||||
|
super.setUp()
|
||||||
|
val habit = fixtures.createLongHabit()
|
||||||
|
view = LayoutInflater
|
||||||
|
.from(targetContext)
|
||||||
|
.inflate(R.layout.show_habit, null)
|
||||||
|
.findViewById<View>(R.id.historyCard) as HistoryCard
|
||||||
|
view.update(HistoryCardPresenter(habit = habit,
|
||||||
|
firstWeekday = 1,
|
||||||
|
isSkipEnabled = false).present())
|
||||||
|
measureView(view, 800f, 600f)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testRender() {
|
||||||
|
assertRenders(view, PATH + "render.png")
|
||||||
|
}
|
||||||
|
}
|
@ -1,144 +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 butterknife.*;
|
|
||||||
|
|
||||||
public class HistoryCard extends HabitCard
|
|
||||||
{
|
|
||||||
@BindView(R.id.historyChart)
|
|
||||||
HistoryChart chart;
|
|
||||||
|
|
||||||
@BindView(R.id.title)
|
|
||||||
TextView title;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Controller controller;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Preferences prefs;
|
|
||||||
|
|
||||||
public HistoryCard(Context context)
|
|
||||||
{
|
|
||||||
super(context);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HistoryCard(Context context, AttributeSet attrs)
|
|
||||||
{
|
|
||||||
super(context, attrs);
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnClick(R.id.edit)
|
|
||||||
public void onClickEditButton()
|
|
||||||
{
|
|
||||||
if(controller != null) controller.onEditHistoryButtonClick();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setController(@Nullable Controller controller)
|
|
||||||
{
|
|
||||||
this.controller = controller;
|
|
||||||
}
|
|
||||||
|
|
||||||
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_history, this);
|
|
||||||
ButterKnife.bind(this);
|
|
||||||
controller = new Controller() {};
|
|
||||||
if (isInEditMode()) initEditMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initEditMode()
|
|
||||||
{
|
|
||||||
int color = PaletteUtils.getAndroidTestColor(1);
|
|
||||||
title.setTextColor(color);
|
|
||||||
chart.setColor(color);
|
|
||||||
chart.populateWithRandomData();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Task createRefreshTask()
|
|
||||||
{
|
|
||||||
return new RefreshTask(getHabit());
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Controller
|
|
||||||
{
|
|
||||||
default void onEditHistoryButtonClick() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class RefreshTask extends CancelableTask
|
|
||||||
{
|
|
||||||
private final Habit habit;
|
|
||||||
|
|
||||||
private RefreshTask(Habit habit)
|
|
||||||
{
|
|
||||||
this.habit = habit;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void doInBackground()
|
|
||||||
{
|
|
||||||
if (isCanceled()) return;
|
|
||||||
int[] checkmarks = habit.getCheckmarks().getAllValues();
|
|
||||||
if(prefs != null)
|
|
||||||
{
|
|
||||||
chart.setFirstWeekday(prefs.getFirstWeekday());
|
|
||||||
chart.setSkipEnabled(prefs.isSkipEnabled());
|
|
||||||
}
|
|
||||||
chart.setCheckmarks(checkmarks);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPreExecute()
|
|
||||||
{
|
|
||||||
int color = PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext());
|
|
||||||
title.setTextColor(color);
|
|
||||||
chart.setColor(color);
|
|
||||||
if(habit.isNumerical())
|
|
||||||
{
|
|
||||||
chart.setNumerical(true);
|
|
||||||
chart.setTarget(habit.getTargetValue() / habit.getFrequency().getDenominator());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 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.*
|
||||||
|
|
||||||
|
data class HistoryCardViewModel(
|
||||||
|
val checkmarks: IntArray,
|
||||||
|
val color: PaletteColor,
|
||||||
|
val firstWeekday: Int,
|
||||||
|
val isNumerical: Boolean,
|
||||||
|
val isSkipEnabled: Boolean,
|
||||||
|
val target: Double,
|
||||||
|
)
|
||||||
|
|
||||||
|
class HistoryCard(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
|
||||||
|
|
||||||
|
private var binding = ShowHabitHistoryBinding.inflate(LayoutInflater.from(context), this)
|
||||||
|
|
||||||
|
var onClickEditButton: () -> Unit = {}
|
||||||
|
|
||||||
|
init {
|
||||||
|
binding.edit.setOnClickListener { onClickEditButton() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun update(data: HistoryCardViewModel) {
|
||||||
|
binding.historyChart.setFirstWeekday(data.firstWeekday)
|
||||||
|
binding.historyChart.setSkipEnabled(data.isSkipEnabled)
|
||||||
|
binding.historyChart.setCheckmarks(data.checkmarks)
|
||||||
|
val androidColor = data.color.toThemedAndroidColor(context)
|
||||||
|
binding.title.setTextColor(androidColor)
|
||||||
|
binding.historyChart.setColor(androidColor)
|
||||||
|
if (data.isNumerical) {
|
||||||
|
binding.historyChart.setNumerical(true)
|
||||||
|
binding.historyChart.setTarget(data.target)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HistoryCardPresenter(
|
||||||
|
val habit: Habit,
|
||||||
|
val firstWeekday: Int,
|
||||||
|
val isSkipEnabled: Boolean,
|
||||||
|
) {
|
||||||
|
fun present() = HistoryCardViewModel(
|
||||||
|
checkmarks = habit.checkmarks.allValues,
|
||||||
|
color = habit.color,
|
||||||
|
firstWeekday = firstWeekday,
|
||||||
|
isNumerical = habit.isNumerical,
|
||||||
|
isSkipEnabled = isSkipEnabled,
|
||||||
|
target = habit.targetValue / habit.frequency.denominator,
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in new issue