Refactor HistoryCard

pull/699/head
Alinson S. Xavier 5 years ago
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")
}
}

@ -25,6 +25,8 @@ import android.widget.*
import androidx.appcompat.app.*
import kotlinx.coroutines.*
import org.isoron.uhabits.*
import org.isoron.uhabits.activities.*
import org.isoron.uhabits.activities.common.dialogs.*
import org.isoron.uhabits.activities.habits.show.views.*
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.models.*
@ -50,6 +52,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
preferences = appComponent.preferences
commandRunner = appComponent.commandRunner
widgetUpdater = appComponent.widgetUpdater
AndroidThemeSwitcher(this, preferences).apply()
view = ShowHabitView(this)
presenter = ShowHabitPresenter(
@ -64,6 +67,14 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
updateViews()
}
view.onClickEditHistoryButton = {
val dialog = HistoryEditorDialog()
dialog.setHabit(habit)
dialog.setController(object : HistoryEditorDialog.Controller {
})
dialog.show(getSupportFragmentManager(), "historyEditor")
}
setContentView(view)
}
@ -100,16 +111,19 @@ data class ShowHabitViewModel(
val streaks: StreakCardViewModel,
val scores: ScoreCardViewModel,
val frequency: FrequencyCardViewModel,
val history: HistoryCardViewModel,
)
class ShowHabitView(context: Context) : FrameLayout(context) {
private val binding = ShowHabitBinding.inflate(LayoutInflater.from(context))
var onBucketSizeSelected: (position: Int) -> Unit = {}
var onClickEditHistoryButton: () -> Unit = {}
init {
addView(binding.root)
binding.scoreCard.onBucketSizeSelected = { position -> onBucketSizeSelected(position) }
binding.historyCard.onClickEditButton = { onClickEditHistoryButton() }
}
fun update(data: ShowHabitViewModel) {
@ -121,6 +135,7 @@ class ShowHabitView(context: Context) : FrameLayout(context) {
binding.streakCard.update(data.streaks)
binding.scoreCard.update(data.scores)
binding.frequencyCard.update(data.frequency)
binding.historyCard.update(data.history)
if (data.isNumerical) {
binding.overviewCard.visibility = GONE
binding.streakCard.visibility = GONE
@ -128,12 +143,6 @@ class ShowHabitView(context: Context) : FrameLayout(context) {
binding.targetCard.visibility = GONE
}
}
fun setController(controller: Controller) {
binding.historyCard.setController(controller)
}
interface Controller : HistoryCard.Controller
}
class ShowHabitPresenter(
@ -152,6 +161,9 @@ class ShowHabitPresenter(
firstWeekday = preferences.firstWeekday)
private val frequencyCardPresenter = FrequencyCardPresenter(habit = habit,
firstWeekday = preferences.firstWeekday)
private val historyCardViewModel = HistoryCardPresenter(habit = habit,
firstWeekday = preferences.firstWeekday,
isSkipEnabled = preferences.isSkipEnabled)
suspend fun present(): ShowHabitViewModel {
return ShowHabitViewModel(
@ -165,6 +177,7 @@ class ShowHabitPresenter(
streaks = streakCartPresenter.present(),
scores = scoreCardPresenter.present(preferences.defaultScoreSpinnerPosition),
frequency = frequencyCardPresenter.present(),
history = historyCardViewModel.present(),
)
}
}

@ -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…
Cancel
Save