ShowHabitActivity: Re-enable menus and actions

This commit is contained in:
2020-12-23 10:06:31 -06:00
parent 85de69bca7
commit 19e221bb32
16 changed files with 599 additions and 655 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2016-2020 Á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.core.ui.callbacks
import org.isoron.uhabits.core.models.*
interface OnToggleCheckmarkListener {
fun onToggleCheckmark(timestamp: Timestamp, value: Int) {}
}

View File

@@ -1,87 +0,0 @@
/*
* Copyright (C) 2017 Á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.core.ui.screens.habits.show;
import androidx.annotation.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.screens.habits.list.*;
import javax.inject.*;
public class ShowHabitBehavior
{
private HabitList habitList;
@NonNull
private final Habit habit;
@NonNull
private final CommandRunner commandRunner;
@NonNull
private Screen screen;
@Inject
public ShowHabitBehavior(@NonNull HabitList habitList,
@NonNull CommandRunner commandRunner,
@NonNull Habit habit,
@NonNull Screen screen)
{
this.habitList = habitList;
this.habit = habit;
this.commandRunner = commandRunner;
this.screen = screen;
}
public void onEditHistory()
{
screen.showEditHistoryScreen();
}
public void onToggleCheckmark(Timestamp timestamp, int value)
{
if (habit.isNumerical()) {
CheckmarkList checkmarks = habit.getCheckmarks();
double oldValue = checkmarks.getValues(timestamp, timestamp)[0];
screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue ->
{
newValue = Math.round(newValue * 1000);
commandRunner.execute(
new CreateRepetitionCommand(habitList, habit, timestamp, (int) newValue),
habit.getId());
});
} else {
commandRunner.execute(
new CreateRepetitionCommand(habitList, habit, timestamp, value), null);
}
}
public interface Screen
{
void showEditHistoryScreen();
void showNumberPicker(double value,
@NonNull String unit,
@NonNull ListHabitsBehavior.NumberPickerCallback callback);
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2017 Á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.core.ui.screens.habits.show
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.ui.callbacks.*
import org.isoron.uhabits.core.ui.screens.habits.list.*
class ShowHabitBehavior(
private val habitList: HabitList,
private val commandRunner: CommandRunner,
private val habit: Habit,
private val screen: Screen,
private val preferences: Preferences,
) : OnToggleCheckmarkListener {
fun onScoreCardSpinnerPosition(position: Int) {
preferences.scoreCardSpinnerPosition = position
screen.updateWidgets()
screen.refresh()
}
fun onBarCardBoolSpinnerPosition(position: Int) {
preferences.barCardBoolSpinnerPosition = position
screen.updateWidgets()
screen.refresh()
}
fun onBarCardNumericalSpinnerPosition(position: Int) {
preferences.barCardNumericalSpinnerPosition = position
screen.refresh()
screen.updateWidgets()
}
fun onClickEditHistory() {
screen.showHistoryEditorDialog(this)
}
override fun onToggleCheckmark(timestamp: Timestamp, value: Int) {
if (habit.isNumerical) {
val checkmarks = habit.checkmarks
val oldValue = checkmarks.getValues(timestamp, timestamp)[0].toDouble()
screen.showNumberPicker(oldValue / 1000, habit.unit) { newValue: Double ->
val thousands = Math.round(newValue * 1000).toInt()
commandRunner.execute(
CreateRepetitionCommand(
habitList,
habit,
timestamp,
thousands,
),
habit.getId(),
)
}
} else {
commandRunner.execute(
CreateRepetitionCommand(
habitList,
habit,
timestamp,
value,
),
null,
)
}
}
interface Screen {
fun showNumberPicker(value: Double,
unit: String,
callback: ListHabitsBehavior.NumberPickerCallback)
fun updateWidgets()
fun refresh()
fun showHistoryEditorDialog(listener: OnToggleCheckmarkListener)
}
}

View File

@@ -1,146 +0,0 @@
/*
* Copyright (C) 2017 Á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.core.ui.screens.habits.show;
import androidx.annotation.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.callbacks.*;
import org.isoron.uhabits.core.utils.*;
import java.io.*;
import java.util.*;
import javax.inject.*;
import static java.lang.Math.*;
public class ShowHabitMenuBehavior
{
private HabitList habitList;
@NonNull
private final Habit habit;
@NonNull
private final TaskRunner taskRunner;
@NonNull
private Screen screen;
@NonNull
private System system;
@NonNull
private CommandRunner commandRunner;
@Inject
public ShowHabitMenuBehavior(@NonNull HabitList habitList,
@NonNull Habit habit,
@NonNull TaskRunner taskRunner,
@NonNull Screen screen,
@NonNull System system,
@NonNull CommandRunner commandRunner)
{
this.habitList = habitList;
this.habit = habit;
this.taskRunner = taskRunner;
this.screen = screen;
this.system = system;
this.commandRunner = commandRunner;
}
public void onEditHabit()
{
screen.showEditHabitScreen(habit);
}
public void onExportCSV()
{
List<Habit> selected = Collections.singletonList(habit);
File outputDir = system.getCSVOutputDir();
taskRunner.execute(
new ExportCSVTask(habitList, selected, outputDir, filename ->
{
if (filename != null) screen.showSendFileScreen(filename);
else screen.showMessage(Message.COULD_NOT_EXPORT);
}));
}
public void onDeleteHabit()
{
List<Habit> selected = Collections.singletonList(habit);
screen.showDeleteConfirmationScreen(() -> {
commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
null);
screen.close();
});
}
public void onRandomize()
{
Random random = new Random();
habit.getRepetitions().removeAll();
double strength = 50;
for (int i = 0; i < 365 * 5; i++)
{
if (i % 7 == 0) strength = max(0, min(100, strength + 10 * random.nextGaussian()));
if (random.nextInt(100) > strength) continue;
int value = Checkmark.YES_MANUAL;
if (habit.isNumerical())
value = (int) (1000 + 250 * random.nextGaussian() * strength / 100) * 1000;
habit.getRepetitions().setValue(DateUtils.getToday().minus(i), value);
}
habit.invalidateNewerThan(Timestamp.ZERO);
}
public enum Message
{
COULD_NOT_EXPORT, HABIT_DELETED
}
public interface Screen
{
void showEditHabitScreen(@NonNull Habit habit);
void showMessage(Message m);
void showSendFileScreen(String filename);
void showDeleteConfirmationScreen(
@NonNull OnConfirmedCallback callback);
void close();
}
public interface System
{
File getCSVOutputDir();
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2017 Á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.core.ui.screens.habits.show
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.tasks.*
import org.isoron.uhabits.core.ui.callbacks.*
import org.isoron.uhabits.core.utils.*
import java.io.*
import java.util.*
class ShowHabitMenuBehavior(
private val commandRunner: CommandRunner,
private val habit: Habit,
private val habitList: HabitList,
private val screen: Screen,
private val system: System,
private val taskRunner: TaskRunner,
) {
fun onEditHabit() {
screen.showEditHabitScreen(habit)
}
fun onExportCSV() {
val outputDir = system.getCSVOutputDir()
taskRunner.execute(ExportCSVTask(habitList, listOf(habit), outputDir) { filename: String? ->
if (filename != null) {
screen.showSendFileScreen(filename)
} else {
screen.showMessage(Message.COULD_NOT_EXPORT)
}
})
}
fun onDeleteHabit() {
screen.showDeleteConfirmationScreen {
commandRunner.execute(DeleteHabitsCommand(habitList, listOf(habit)), null)
screen.close()
}
}
fun onRandomize() {
val random = Random()
habit.repetitions.removeAll()
var strength = 50.0
for (i in 0 until 365 * 5) {
if (i % 7 == 0) strength = Math.max(0.0, Math.min(100.0, strength + 10 * random.nextGaussian()))
if (random.nextInt(100) > strength) continue
var value = Checkmark.YES_MANUAL
if (habit.isNumerical) value = (1000 + 250 * random.nextGaussian() * strength / 100).toInt() * 1000
habit.repetitions.setValue(DateUtils.getToday().minus(i), value)
}
habit.invalidateNewerThan(Timestamp.ZERO)
screen.refresh()
}
enum class Message {
COULD_NOT_EXPORT
}
interface Screen {
fun showEditHabitScreen(habit: Habit)
fun showMessage(m: Message?)
fun showSendFileScreen(filename: String)
fun showDeleteConfirmationScreen(
callback: OnConfirmedCallback)
fun close()
fun refresh()
}
interface System {
fun getCSVOutputDir(): File
}
}

View File

@@ -49,8 +49,7 @@ public class ShowHabitMenuBehaviorTest extends BaseUnitTest
screen = mock(ShowHabitMenuBehavior.Screen.class);
habit = fixtures.createShortHabit();
menu = new ShowHabitMenuBehavior(habitList, habit, taskRunner, screen,
system, commandRunner);
menu = new ShowHabitMenuBehavior(commandRunner, habit, habitList, screen, system, taskRunner);
}
@Test