mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Write tests for ListHabits controller, menu and screen
This commit is contained in:
@@ -26,6 +26,8 @@ import android.support.annotation.*;
|
||||
import com.activeandroid.*;
|
||||
|
||||
import org.isoron.uhabits.notifications.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.isoron.uhabits.widgets.*;
|
||||
|
||||
@@ -107,6 +109,16 @@ public class HabitsApplication extends Application
|
||||
notificationTray = component.getNotificationTray();
|
||||
notificationTray.startListening();
|
||||
|
||||
Preferences prefs = component.getPreferences();
|
||||
prefs.initialize();
|
||||
prefs.updateLastAppVersion();
|
||||
|
||||
TaskRunner taskRunner = component.getTaskRunner();
|
||||
taskRunner.execute(() -> {
|
||||
reminderScheduler.scheduleAll();
|
||||
widgetUpdater.updateWidgets();
|
||||
});
|
||||
|
||||
DatabaseUtils.initializeActiveAndroid();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ import android.view.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
|
||||
import static android.R.anim.*;
|
||||
|
||||
/**
|
||||
* Base class for all activities in the application.
|
||||
* <p>
|
||||
@@ -75,6 +77,17 @@ abstract public class BaseActivity extends AppCompatActivity
|
||||
return baseMenu.onItemSelected(item);
|
||||
}
|
||||
|
||||
public void restartWithFade()
|
||||
{
|
||||
new Handler().postDelayed(() -> {
|
||||
Intent intent = new Intent(this, MainActivity.class);
|
||||
finish();
|
||||
overridePendingTransition(fade_in, fade_out);
|
||||
startActivity(intent);
|
||||
|
||||
}, 500); // HACK: Let the menu disappear first
|
||||
}
|
||||
|
||||
public void setBaseMenu(@Nullable BaseMenu baseMenu)
|
||||
{
|
||||
this.baseMenu = baseMenu;
|
||||
|
||||
@@ -66,6 +66,17 @@ public class ThemeSwitcher
|
||||
return getTheme() == THEME_DARK;
|
||||
}
|
||||
|
||||
public void refreshTheme()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void toggleNightMode()
|
||||
{
|
||||
if (isNightMode()) setTheme(THEME_LIGHT);
|
||||
else setTheme(THEME_DARK);
|
||||
}
|
||||
|
||||
private void applyDarkTheme()
|
||||
{
|
||||
if (preferences.isPureBlackEnabled())
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
package org.isoron.uhabits.activities.habits.list;
|
||||
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
@@ -69,6 +68,8 @@ public class ListHabitsController
|
||||
|
||||
private ImportDataTaskFactory importTaskFactory;
|
||||
|
||||
private ExportCSVTaskFactory exportCSVFactory;
|
||||
|
||||
@Inject
|
||||
public ListHabitsController(@NonNull BaseSystem system,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@@ -79,7 +80,8 @@ public class ListHabitsController
|
||||
@NonNull ReminderScheduler reminderScheduler,
|
||||
@NonNull TaskRunner taskRunner,
|
||||
@NonNull WidgetUpdater widgetUpdater,
|
||||
@NonNull ImportDataTaskFactory importTaskFactory)
|
||||
@NonNull ImportDataTaskFactory importTaskFactory,
|
||||
@NonNull ExportCSVTaskFactory exportCSVFactory)
|
||||
{
|
||||
this.adapter = adapter;
|
||||
this.commandRunner = commandRunner;
|
||||
@@ -91,6 +93,7 @@ public class ListHabitsController
|
||||
this.reminderScheduler = reminderScheduler;
|
||||
this.widgetUpdater = widgetUpdater;
|
||||
this.importTaskFactory = importTaskFactory;
|
||||
this.exportCSVFactory = exportCSVFactory;
|
||||
}
|
||||
|
||||
public void onExportCSV()
|
||||
@@ -98,7 +101,7 @@ public class ListHabitsController
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Habit h : habitList) selected.add(h);
|
||||
|
||||
taskRunner.execute(new ExportCSVTask(habitList, selected, filename -> {
|
||||
taskRunner.execute(exportCSVFactory.create(selected, filename -> {
|
||||
if (filename != null) screen.showSendFileScreen(filename);
|
||||
else screen.showMessage(R.string.could_not_export);
|
||||
}));
|
||||
@@ -179,15 +182,8 @@ public class ListHabitsController
|
||||
|
||||
public void onStartup()
|
||||
{
|
||||
prefs.initialize();
|
||||
prefs.incrementLaunchCount();
|
||||
prefs.updateLastAppVersion();
|
||||
if (prefs.isFirstRun()) onFirstRun();
|
||||
|
||||
new Handler().postDelayed(() -> taskRunner.execute(() -> {
|
||||
reminderScheduler.scheduleAll();
|
||||
widgetUpdater.updateWidgets();
|
||||
}), 1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -67,10 +67,10 @@ public class ListHabitsMenu extends BaseMenu
|
||||
@Override
|
||||
public void onCreate(@NonNull Menu menu)
|
||||
{
|
||||
MenuItem nightModeItem = menu.findItem(R.id.action_night_mode);
|
||||
MenuItem nightModeItem = menu.findItem(R.id.actionToggleNightMode);
|
||||
nightModeItem.setChecked(themeSwitcher.isNightMode());
|
||||
|
||||
MenuItem showArchivedItem = menu.findItem(R.id.action_show_archived);
|
||||
MenuItem showArchivedItem = menu.findItem(R.id.actionShowArchived);
|
||||
showArchivedItem.setChecked(showArchived);
|
||||
|
||||
MenuItem showCompletedItem = menu.findItem(R.id.actionShowCompleted);
|
||||
@@ -82,27 +82,27 @@ public class ListHabitsMenu extends BaseMenu
|
||||
{
|
||||
switch (item.getItemId())
|
||||
{
|
||||
case R.id.action_night_mode:
|
||||
case R.id.actionToggleNightMode:
|
||||
screen.toggleNightMode();
|
||||
return true;
|
||||
|
||||
case R.id.action_add:
|
||||
case R.id.actionAdd:
|
||||
screen.showCreateHabitScreen();
|
||||
return true;
|
||||
|
||||
case R.id.action_faq:
|
||||
case R.id.actionFAQ:
|
||||
screen.showFAQScreen();
|
||||
return true;
|
||||
|
||||
case R.id.action_about:
|
||||
case R.id.actionAbout:
|
||||
screen.showAboutScreen();
|
||||
return true;
|
||||
|
||||
case R.id.action_settings:
|
||||
case R.id.actionSettings:
|
||||
screen.showSettingsScreen();
|
||||
return true;
|
||||
|
||||
case R.id.action_show_archived:
|
||||
case R.id.actionShowArchived:
|
||||
toggleShowArchived();
|
||||
invalidate();
|
||||
return true;
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package org.isoron.uhabits.activities.habits.list;
|
||||
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
@@ -233,23 +232,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
|
||||
public void toggleNightMode()
|
||||
{
|
||||
if (themeSwitcher.isNightMode())
|
||||
themeSwitcher.setTheme(ThemeSwitcher.THEME_LIGHT);
|
||||
else themeSwitcher.setTheme(ThemeSwitcher.THEME_DARK);
|
||||
|
||||
refreshTheme();
|
||||
}
|
||||
|
||||
private void refreshTheme()
|
||||
{
|
||||
new Handler().postDelayed(() -> {
|
||||
Intent intent = new Intent(activity, MainActivity.class);
|
||||
|
||||
activity.finish();
|
||||
activity.overridePendingTransition(android.R.anim.fade_in,
|
||||
android.R.anim.fade_out);
|
||||
activity.startActivity(intent);
|
||||
|
||||
}, 500); // HACK: Let the menu disappear first
|
||||
themeSwitcher.toggleNightMode();
|
||||
activity.restartWithFade();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.notifications.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
@@ -32,10 +33,14 @@ public class WidgetController
|
||||
@NonNull
|
||||
private final CommandRunner commandRunner;
|
||||
|
||||
private NotificationTray notificationTray;
|
||||
|
||||
@Inject
|
||||
public WidgetController(@NonNull CommandRunner commandRunner)
|
||||
public WidgetController(@NonNull CommandRunner commandRunner,
|
||||
@NonNull NotificationTray notificationTray)
|
||||
{
|
||||
this.commandRunner = commandRunner;
|
||||
this.notificationTray = notificationTray;
|
||||
}
|
||||
|
||||
public void onAddRepetition(@NonNull Habit habit, long timestamp)
|
||||
@@ -43,6 +48,7 @@ public class WidgetController
|
||||
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
|
||||
if (rep != null) return;
|
||||
performToggle(habit, timestamp);
|
||||
notificationTray.cancel(habit);
|
||||
}
|
||||
|
||||
public void onRemoveRepetition(@NonNull Habit habit, long timestamp)
|
||||
|
||||
@@ -21,6 +21,8 @@ package org.isoron.uhabits.tasks;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
@@ -28,6 +30,7 @@ import org.isoron.uhabits.utils.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
@AutoFactory(allowSubclasses = true)
|
||||
public class ExportCSVTask implements Task
|
||||
{
|
||||
private String archiveFilename;
|
||||
@@ -41,7 +44,7 @@ public class ExportCSVTask implements Task
|
||||
@NonNull
|
||||
private final HabitList habitList;
|
||||
|
||||
public ExportCSVTask(@NonNull HabitList habitList,
|
||||
public ExportCSVTask(@Provided @NonNull HabitList habitList,
|
||||
@NonNull List<Habit> selectedHabits,
|
||||
@NonNull Listener listener)
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.isoron.uhabits.io.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@AutoFactory
|
||||
@AutoFactory(allowSubclasses = true)
|
||||
public class ImportDataTask implements Task
|
||||
{
|
||||
public static final int FAILED = 3;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_add"
|
||||
android:id="@+id/actionAdd"
|
||||
android:icon="?iconAdd"
|
||||
android:title="@string/add_habit"
|
||||
app:showAsAction="ifRoom"/>
|
||||
@@ -35,7 +35,7 @@
|
||||
app:showAsAction="ifRoom">
|
||||
<menu>
|
||||
<item
|
||||
android:id="@+id/action_show_archived"
|
||||
android:id="@+id/actionShowArchived"
|
||||
android:checkable="true"
|
||||
android:enabled="true"
|
||||
android:title="@string/show_archived"/>
|
||||
@@ -49,7 +49,7 @@
|
||||
</item>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_night_mode"
|
||||
android:id="@+id/actionToggleNightMode"
|
||||
android:checkable="true"
|
||||
android:enabled="true"
|
||||
android:orderInCategory="50"
|
||||
@@ -57,19 +57,19 @@
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:id="@+id/actionSettings"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/action_settings"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_faq"
|
||||
android:id="@+id/actionFAQ"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/help"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_about"
|
||||
android:id="@+id/actionAbout"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/about"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
Reference in New Issue
Block a user