mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Move ListHabitsBehavior to uhabits-core
This commit is contained in:
@@ -22,5 +22,13 @@ package org.isoron.uhabits.preferences;
|
||||
public interface Preferences
|
||||
{
|
||||
|
||||
void incrementLaunchCount();
|
||||
|
||||
boolean isFirstRun();
|
||||
|
||||
void setDeveloper(boolean isDeveloper);
|
||||
|
||||
void setFirstRun(boolean b);
|
||||
|
||||
void updateLastHint(int i, long startOfToday);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* 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.ui.habits.list;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class ListHabitsBehavior
|
||||
{
|
||||
private HabitList habitList;
|
||||
|
||||
private System system;
|
||||
|
||||
private TaskRunner taskRunner;
|
||||
|
||||
private Screen screen;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private Preferences prefs;
|
||||
|
||||
@Inject
|
||||
public ListHabitsBehavior(@NonNull HabitList habitList,
|
||||
@NonNull System system,
|
||||
@NonNull TaskRunner taskRunner,
|
||||
@NonNull Screen screen,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@NonNull Preferences prefs)
|
||||
{
|
||||
this.habitList = habitList;
|
||||
this.system = system;
|
||||
this.taskRunner = taskRunner;
|
||||
this.screen = screen;
|
||||
this.commandRunner = commandRunner;
|
||||
this.prefs = prefs;
|
||||
}
|
||||
|
||||
public void onClickHabit(@NonNull Habit h)
|
||||
{
|
||||
screen.showHabitScreen(h);
|
||||
}
|
||||
|
||||
// public void onExportDB()
|
||||
// {
|
||||
// taskRunner.execute(exportDBFactory.create(filename -> {
|
||||
// if (filename != null) screen.showSendFileScreen(filename);
|
||||
// else screen.showMessage(R.string.could_not_export);
|
||||
// }));
|
||||
// }
|
||||
|
||||
public void onEdit(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
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(habit, timestamp, (int) newValue),
|
||||
habit.getId());
|
||||
});
|
||||
}
|
||||
|
||||
public void onExportCSV()
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Habit h : habitList) selected.add(h);
|
||||
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 onImportData(@NonNull File file,
|
||||
// @NonNull OnFinishedListener finishedListener)
|
||||
// {
|
||||
// taskRunner.execute(importTaskFactory.create(file, result -> {
|
||||
// switch (result)
|
||||
// {
|
||||
// case ImportDataTask.SUCCESS:
|
||||
// adapter.refresh();
|
||||
// screen.showMessage(R.string.habits_imported);
|
||||
// break;
|
||||
//
|
||||
// case ImportDataTask.NOT_RECOGNIZED:
|
||||
// screen.showMessage(R.string.file_not_recognized);
|
||||
// break;
|
||||
//
|
||||
// default:
|
||||
// screen.showMessage(R.string.could_not_import);
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// finishedListener.onFinish();
|
||||
// }));
|
||||
// }
|
||||
|
||||
public void onReorderHabit(@NonNull Habit from, @NonNull Habit to)
|
||||
{
|
||||
taskRunner.execute(() -> habitList.reorder(from, to));
|
||||
}
|
||||
|
||||
public void onRepairDB()
|
||||
{
|
||||
taskRunner.execute(() ->
|
||||
{
|
||||
habitList.repair();
|
||||
screen.showMessage(Message.DATABASE_REPAIRED);
|
||||
});
|
||||
}
|
||||
|
||||
public void onSendBugReport()
|
||||
{
|
||||
system.dumpBugReportToFile();
|
||||
|
||||
try
|
||||
{
|
||||
String log = system.getBugReport();
|
||||
screen.showSendBugReportToDeveloperScreen(log);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
screen.showMessage(Message.COULD_NOT_GENERATE_BUG_REPORT);
|
||||
}
|
||||
}
|
||||
|
||||
public void onStartup()
|
||||
{
|
||||
prefs.incrementLaunchCount();
|
||||
if (prefs.isFirstRun()) onFirstRun();
|
||||
}
|
||||
|
||||
public void onToggle(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp),
|
||||
habit.getId());
|
||||
}
|
||||
|
||||
public void onFirstRun()
|
||||
{
|
||||
prefs.setFirstRun(false);
|
||||
prefs.updateLastHint(-1, DateUtils.getStartOfToday());
|
||||
screen.showIntroScreen();
|
||||
}
|
||||
|
||||
public enum Message
|
||||
{
|
||||
COULD_NOT_EXPORT, IMPORT_SUCCESSFUL, IMPORT_FAILED, DATABASE_REPAIRED,
|
||||
COULD_NOT_GENERATE_BUG_REPORT, FILE_NOT_RECOGNIZED
|
||||
}
|
||||
|
||||
public interface NumberPickerCallback
|
||||
{
|
||||
void onNumberPicked(double newValue);
|
||||
}
|
||||
|
||||
public interface OnFinishedListener
|
||||
{
|
||||
void onFinish();
|
||||
}
|
||||
|
||||
public interface Screen
|
||||
{
|
||||
void showHabitScreen(@NonNull Habit h);
|
||||
|
||||
void showIntroScreen();
|
||||
|
||||
void showMessage(@NonNull Message m);
|
||||
|
||||
void showNumberPicker(double value,
|
||||
@NonNull String unit,
|
||||
@NonNull NumberPickerCallback callback);
|
||||
|
||||
void showSendBugReportToDeveloperScreen(String log);
|
||||
|
||||
void showSendFileScreen(@NonNull String filename);
|
||||
}
|
||||
|
||||
public interface System
|
||||
{
|
||||
void dumpBugReportToFile();
|
||||
|
||||
String getBugReport() throws IOException;
|
||||
|
||||
File getCSVOutputDir();
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.models.memory.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
@@ -27,6 +28,8 @@ import org.junit.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class BaseUnitTest
|
||||
{
|
||||
protected HabitList habitList;
|
||||
@@ -37,6 +40,8 @@ public class BaseUnitTest
|
||||
|
||||
protected SingleThreadTaskRunner taskRunner;
|
||||
|
||||
protected CommandRunner commandRunner;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
@@ -45,9 +50,10 @@ public class BaseUnitTest
|
||||
DateUtils.setFixedLocalTime(fixed_local_time);
|
||||
|
||||
modelFactory = new MemoryModelFactory();
|
||||
habitList = modelFactory.buildHabitList();
|
||||
habitList = spy(modelFactory.buildHabitList());
|
||||
fixtures = new HabitFixtures(modelFactory);
|
||||
taskRunner = new SingleThreadTaskRunner();
|
||||
commandRunner = new CommandRunner(taskRunner);
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
@@ -62,6 +62,31 @@ public class HabitFixtures
|
||||
return habit;
|
||||
}
|
||||
|
||||
public Habit createNumericalHabit()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setType(Habit.NUMBER_HABIT);
|
||||
habit.setName("Run");
|
||||
habit.setDescription("How many miles did you run today?");
|
||||
habit.setUnit("miles");
|
||||
habit.setTargetType(Habit.AT_LEAST);
|
||||
habit.setTargetValue(2.0);
|
||||
habit.setColor(1);
|
||||
|
||||
long day = DateUtils.millisecondsInOneDay;
|
||||
long today = DateUtils.getStartOfToday();
|
||||
int times[] = { 0, 1, 3, 5, 7, 8, 9, 10 };
|
||||
int values[] = { 100, 200, 300, 400, 500, 600, 700, 800 };
|
||||
|
||||
for(int i = 0; i < times.length; i++)
|
||||
{
|
||||
long timestamp = today - times[i] * day;
|
||||
habit.getRepetitions().add(new Repetition(timestamp, values[i]));
|
||||
}
|
||||
|
||||
return habit;
|
||||
}
|
||||
|
||||
public Habit createShortHabit()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.ui.habits.list;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.*;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.isoron.uhabits.ui.habits.list.ListHabitsBehavior.Message.*;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
{
|
||||
@Mock
|
||||
private ListHabitsBehavior.System system;
|
||||
|
||||
@Mock
|
||||
private Preferences prefs;
|
||||
|
||||
private ListHabitsBehavior behavior;
|
||||
|
||||
@Mock
|
||||
private ListHabitsBehavior.Screen screen;
|
||||
|
||||
private Habit habit1, habit2;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> captor;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
habit1 = fixtures.createShortHabit();
|
||||
habit2 = fixtures.createNumericalHabit();
|
||||
habitList.add(habit1);
|
||||
habitList.add(habit2);
|
||||
clearInvocations(habitList);
|
||||
|
||||
behavior = new ListHabitsBehavior(habitList, system, taskRunner, screen,
|
||||
commandRunner, prefs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnEdit()
|
||||
{
|
||||
behavior.onEdit(habit2, DateUtils.getStartOfToday());
|
||||
verify(screen).showNumberPicker(eq(0.1), eq("miles"), captor.capture());
|
||||
captor.getValue().onNumberPicked(100);
|
||||
assertThat(habit2.getCheckmarks().getTodayValue(), equalTo(100000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnHabitClick()
|
||||
{
|
||||
behavior.onClickHabit(habit1);
|
||||
verify(screen).showHabitScreen(habit1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnHabitReorder()
|
||||
{
|
||||
Habit from = habit1;
|
||||
Habit to = habit2;
|
||||
behavior.onReorderHabit(from, to);
|
||||
verify(habitList).reorder(from, to);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnRepairDB()
|
||||
{
|
||||
behavior.onRepairDB();
|
||||
verify(habitList).repair();
|
||||
verify(screen).showMessage(DATABASE_REPAIRED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnStartup_firstLaunch()
|
||||
{
|
||||
long today = DateUtils.getStartOfToday();
|
||||
|
||||
when(prefs.isFirstRun()).thenReturn(true);
|
||||
behavior.onStartup();
|
||||
verify(prefs).setFirstRun(false);
|
||||
verify(prefs).updateLastHint(-1, today);
|
||||
verify(screen).showIntroScreen();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnStartup_notFirstLaunch()
|
||||
{
|
||||
when(prefs.isFirstRun()).thenReturn(false);
|
||||
behavior.onStartup();
|
||||
verify(prefs).incrementLaunchCount();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnToggle()
|
||||
{
|
||||
assertTrue(habit1.isCompletedToday());
|
||||
behavior.onToggle(habit1, DateUtils.getStartOfToday());
|
||||
assertFalse(habit1.isCompletedToday());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user