mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Move notifications and reminders to uhabits-core
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.core.reminders;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.commands.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.DateUtils.*;
|
||||
|
||||
@AppScope
|
||||
public class ReminderScheduler implements CommandRunner.Listener
|
||||
{
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private HabitList habitList;
|
||||
|
||||
private SystemScheduler sys;
|
||||
|
||||
@Inject
|
||||
public ReminderScheduler(@NonNull CommandRunner commandRunner,
|
||||
@NonNull HabitList habitList,
|
||||
@NonNull SystemScheduler sys)
|
||||
{
|
||||
this.commandRunner = commandRunner;
|
||||
this.habitList = habitList;
|
||||
this.sys = sys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@NonNull Command command,
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
if(command instanceof ToggleRepetitionCommand) return;
|
||||
if(command instanceof ChangeHabitColorCommand) return;
|
||||
scheduleAll();
|
||||
}
|
||||
|
||||
public void schedule(@NonNull Habit habit, @Nullable Long reminderTime)
|
||||
{
|
||||
if (!habit.hasReminder()) return;
|
||||
if (habit.isArchived()) return;
|
||||
Reminder reminder = habit.getReminder();
|
||||
if (reminderTime == null) reminderTime = getReminderTime(reminder);
|
||||
long timestamp = getStartOfDay(removeTimezone(reminderTime));
|
||||
|
||||
sys.scheduleShowReminder(reminderTime, habit, timestamp);
|
||||
}
|
||||
|
||||
public void scheduleAll()
|
||||
{
|
||||
HabitList reminderHabits =
|
||||
habitList.getFiltered(HabitMatcher.WITH_ALARM);
|
||||
for (Habit habit : reminderHabits)
|
||||
schedule(habit, null);
|
||||
}
|
||||
|
||||
public void startListening()
|
||||
{
|
||||
commandRunner.addListener(this);
|
||||
}
|
||||
|
||||
public void stopListening()
|
||||
{
|
||||
commandRunner.removeListener(this);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Long getReminderTime(@NonNull Reminder reminder)
|
||||
{
|
||||
Calendar calendar = DateUtils.getStartOfTodayCalendar();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
|
||||
calendar.set(Calendar.MINUTE, reminder.getMinute());
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
Long time = calendar.getTimeInMillis();
|
||||
|
||||
if (DateUtils.getLocalTime() > time)
|
||||
time += DateUtils.millisecondsInOneDay;
|
||||
|
||||
return applyTimezone(time);
|
||||
}
|
||||
|
||||
public interface SystemScheduler
|
||||
{
|
||||
void scheduleShowReminder(long reminderTime, Habit habit, long timestamp);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
@@ -19,9 +19,190 @@
|
||||
|
||||
package org.isoron.uhabits.core.ui;
|
||||
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
public interface NotificationTray
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.commands.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.preferences.*;
|
||||
import org.isoron.uhabits.core.tasks.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
@AppScope
|
||||
public class NotificationTray
|
||||
implements CommandRunner.Listener, Preferences.Listener
|
||||
{
|
||||
void cancel(Habit habit);
|
||||
@NonNull
|
||||
private final TaskRunner taskRunner;
|
||||
|
||||
@NonNull
|
||||
private final CommandRunner commandRunner;
|
||||
|
||||
@NonNull
|
||||
private final Preferences preferences;
|
||||
|
||||
private SystemTray systemTray;
|
||||
|
||||
@NonNull
|
||||
private final HashMap<Habit, NotificationData> active;
|
||||
|
||||
@Inject
|
||||
public NotificationTray(@NonNull TaskRunner taskRunner,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@NonNull Preferences preferences,
|
||||
@NonNull SystemTray systemTray)
|
||||
{
|
||||
this.taskRunner = taskRunner;
|
||||
this.commandRunner = commandRunner;
|
||||
this.preferences = preferences;
|
||||
this.systemTray = systemTray;
|
||||
this.active = new HashMap<>();
|
||||
}
|
||||
|
||||
public void cancel(@NonNull Habit habit)
|
||||
{
|
||||
int notificationId = getNotificationId(habit);
|
||||
systemTray.removeNotification(notificationId);
|
||||
active.remove(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@NonNull Command command,
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
if (command instanceof ToggleRepetitionCommand)
|
||||
{
|
||||
ToggleRepetitionCommand toggleCmd =
|
||||
(ToggleRepetitionCommand) command;
|
||||
|
||||
Habit habit = toggleCmd.getHabit();
|
||||
taskRunner.execute(() ->
|
||||
{
|
||||
if (habit.getCheckmarks().getTodayValue() !=
|
||||
Checkmark.UNCHECKED) cancel(habit);
|
||||
});
|
||||
}
|
||||
|
||||
if (command instanceof DeleteHabitsCommand)
|
||||
{
|
||||
DeleteHabitsCommand deleteCommand = (DeleteHabitsCommand) command;
|
||||
List<Habit> deleted = deleteCommand.getSelected();
|
||||
for (Habit habit : deleted)
|
||||
cancel(habit);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotificationsChanged()
|
||||
{
|
||||
reshowAll();
|
||||
}
|
||||
|
||||
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
|
||||
{
|
||||
NotificationData data = new NotificationData(timestamp, reminderTime);
|
||||
active.put(habit, data);
|
||||
taskRunner.execute(new ShowNotificationTask(habit, data));
|
||||
}
|
||||
|
||||
public void startListening()
|
||||
{
|
||||
commandRunner.addListener(this);
|
||||
preferences.addListener(this);
|
||||
}
|
||||
|
||||
public void stopListening()
|
||||
{
|
||||
commandRunner.removeListener(this);
|
||||
preferences.removeListener(this);
|
||||
}
|
||||
|
||||
private int getNotificationId(Habit habit)
|
||||
{
|
||||
Long id = habit.getId();
|
||||
if (id == null) return 0;
|
||||
return (int) (id % Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
private void reshowAll()
|
||||
{
|
||||
for (Habit habit : active.keySet())
|
||||
{
|
||||
NotificationData data = active.get(habit);
|
||||
taskRunner.execute(new ShowNotificationTask(habit, data));
|
||||
}
|
||||
}
|
||||
|
||||
public interface SystemTray
|
||||
{
|
||||
void removeNotification(int notificationId);
|
||||
|
||||
void showNotification(Habit habit,
|
||||
int notificationId,
|
||||
long timestamp,
|
||||
long reminderTime);
|
||||
}
|
||||
|
||||
class NotificationData
|
||||
{
|
||||
public final long timestamp;
|
||||
|
||||
public final long reminderTime;
|
||||
|
||||
public NotificationData(long timestamp, long reminderTime)
|
||||
{
|
||||
this.timestamp = timestamp;
|
||||
this.reminderTime = reminderTime;
|
||||
}
|
||||
}
|
||||
|
||||
private class ShowNotificationTask implements Task
|
||||
{
|
||||
int todayValue;
|
||||
|
||||
private final Habit habit;
|
||||
|
||||
private final long timestamp;
|
||||
|
||||
private final long reminderTime;
|
||||
|
||||
public ShowNotificationTask(Habit habit, NotificationData data)
|
||||
{
|
||||
this.habit = habit;
|
||||
this.timestamp = data.timestamp;
|
||||
this.reminderTime = data.reminderTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doInBackground()
|
||||
{
|
||||
todayValue = habit.getCheckmarks().getTodayValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPostExecute()
|
||||
{
|
||||
if (todayValue != Checkmark.UNCHECKED) return;
|
||||
if (!shouldShowReminderToday()) return;
|
||||
if (!habit.hasReminder()) return;
|
||||
|
||||
systemTray.showNotification(habit, getNotificationId(habit), timestamp,
|
||||
reminderTime);
|
||||
}
|
||||
|
||||
private boolean shouldShowReminderToday()
|
||||
{
|
||||
if (!habit.hasReminder()) return false;
|
||||
Reminder reminder = habit.getReminder();
|
||||
|
||||
boolean reminderDays[] = reminder.getDays().toArray();
|
||||
int weekday = DateUtils.getWeekday(timestamp);
|
||||
|
||||
return reminderDays[weekday];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ public class BaseUnitTest
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
validateMockitoUsage();
|
||||
DateUtils.setFixedLocalTime(null);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* 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.reminders;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ReminderSchedulerTest extends BaseUnitTest
|
||||
{
|
||||
private Habit habit;
|
||||
|
||||
private ReminderScheduler reminderScheduler;
|
||||
|
||||
@Mock
|
||||
private ReminderScheduler.SystemScheduler sys;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
reminderScheduler =
|
||||
new ReminderScheduler(commandRunner, habitList, sys);
|
||||
|
||||
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT-4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScheduleAll()
|
||||
{
|
||||
long now = timestamp(2015, 1, 26, 13, 0);
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
Habit h1 = fixtures.createEmptyHabit();
|
||||
Habit h2 = fixtures.createEmptyHabit();
|
||||
Habit h3 = fixtures.createEmptyHabit();
|
||||
h1.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
|
||||
h2.setReminder(new Reminder(18, 30, WeekdayList.EVERY_DAY));
|
||||
h3.setReminder(null);
|
||||
habitList.add(h1);
|
||||
habitList.add(h2);
|
||||
habitList.add(h3);
|
||||
|
||||
reminderScheduler.scheduleAll();
|
||||
|
||||
verify(sys).scheduleShowReminder(eq(timestamp(2015, 1, 27, 12, 30)),
|
||||
eq(h1), anyLong());
|
||||
verify(sys).scheduleShowReminder(eq(timestamp(2015, 1, 26, 22, 30)),
|
||||
eq(h2), anyLong());
|
||||
Mockito.verifyNoMoreInteractions(sys);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_atSpecificTime()
|
||||
{
|
||||
long atTime = timestamp(2015, 1, 30, 11, 30);
|
||||
long expectedCheckmarkTime = timestamp(2015, 1, 30, 0, 0);
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
|
||||
scheduleAndVerify(atTime, expectedCheckmarkTime, atTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_laterToday()
|
||||
{
|
||||
long now = timestamp(2015, 1, 26, 6, 30);
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
long expectedCheckmarkTime = timestamp(2015, 1, 26, 0, 0);
|
||||
long expectedReminderTime = timestamp(2015, 1, 26, 12, 30);
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
|
||||
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_tomorrow()
|
||||
{
|
||||
long now = timestamp(2015, 1, 26, 13, 0);
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
long expectedCheckmarkTime = timestamp(2015, 1, 27, 0, 0);
|
||||
long expectedReminderTime = timestamp(2015, 1, 27, 12, 30);
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
|
||||
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_withoutReminder()
|
||||
{
|
||||
reminderScheduler.schedule(habit, null);
|
||||
Mockito.verifyZeroInteractions(sys);
|
||||
}
|
||||
|
||||
public long timestamp(int year, int month, int day, int hour, int minute)
|
||||
{
|
||||
Calendar cal = DateUtils.getStartOfTodayCalendar();
|
||||
cal.set(year, month, day, hour, minute);
|
||||
return cal.getTimeInMillis();
|
||||
}
|
||||
|
||||
private void scheduleAndVerify(Long atTime,
|
||||
long expectedCheckmarkTime,
|
||||
long expectedReminderTime)
|
||||
{
|
||||
reminderScheduler.schedule(habit, atTime);
|
||||
verify(sys).scheduleShowReminder(expectedReminderTime, habit,
|
||||
expectedCheckmarkTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user