mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Fix some issues with reminders and print more logs
This commit is contained in:
@@ -25,6 +25,8 @@ import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.commands.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.DateUtils.*;
|
||||
@@ -59,22 +61,44 @@ public class ReminderScheduler implements CommandRunner.Listener
|
||||
|
||||
public void schedule(@NonNull Habit habit)
|
||||
{
|
||||
if (!habit.hasReminder()) return;
|
||||
Long reminderTime = habit.getReminder().getTimeInMillis();
|
||||
if (!habit.hasReminder()) {
|
||||
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
long reminderTime = habit.getReminder().getTimeInMillis();
|
||||
scheduleAtTime(habit, reminderTime);
|
||||
}
|
||||
|
||||
public void scheduleAtTime(@NonNull Habit habit, @NonNull Long reminderTime)
|
||||
public void scheduleAtTime(@NonNull Habit habit, long reminderTime)
|
||||
{
|
||||
if (reminderTime == null) throw new IllegalArgumentException();
|
||||
if (!habit.hasReminder()) return;
|
||||
if (habit.isArchived()) return;
|
||||
sys.log("ReminderScheduler", "Scheduling alarm for habit=" + habit.id);
|
||||
|
||||
if (!habit.hasReminder()) {
|
||||
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (habit.isArchived()) {
|
||||
sys.log("ReminderScheduler", "habit=" + habit.id + " is archived. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
long timestamp = getStartOfDay(removeTimezone(reminderTime));
|
||||
sys.log("ReminderScheduler",
|
||||
String.format(
|
||||
Locale.US,
|
||||
"reminderTime=%d removeTimezone=%d timestamp=%d",
|
||||
reminderTime,
|
||||
removeTimezone(reminderTime),
|
||||
timestamp));
|
||||
|
||||
sys.scheduleShowReminder(reminderTime, habit, timestamp);
|
||||
}
|
||||
|
||||
public synchronized void scheduleAll()
|
||||
{
|
||||
sys.log("ReminderScheduler", "Scheduling all alarms");
|
||||
HabitList reminderHabits =
|
||||
habitList.getFiltered(HabitMatcher.WITH_ALARM);
|
||||
for (Habit habit : reminderHabits)
|
||||
@@ -101,5 +125,7 @@ public class ReminderScheduler implements CommandRunner.Listener
|
||||
public interface SystemScheduler
|
||||
{
|
||||
void scheduleShowReminder(long reminderTime, Habit habit, long timestamp);
|
||||
|
||||
void log(String componentName, String msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +147,8 @@ public class NotificationTray
|
||||
int notificationId,
|
||||
Timestamp timestamp,
|
||||
long reminderTime);
|
||||
|
||||
void log(String msg);
|
||||
}
|
||||
|
||||
class NotificationData
|
||||
@@ -188,9 +190,31 @@ public class NotificationTray
|
||||
@Override
|
||||
public void onPostExecute()
|
||||
{
|
||||
if (todayValue != Checkmark.UNCHECKED) return;
|
||||
if (!shouldShowReminderToday()) return;
|
||||
if (!habit.hasReminder()) return;
|
||||
systemTray.log("Showing notification for habit=" + habit.id);
|
||||
|
||||
if (todayValue != Checkmark.UNCHECKED) {
|
||||
systemTray.log(String.format(
|
||||
Locale.US,
|
||||
"Habit %d already checked. Skipping.",
|
||||
habit.id));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!shouldShowReminderToday()) {
|
||||
systemTray.log(String.format(
|
||||
Locale.US,
|
||||
"Habit %d not supposed to run today. Skipping.",
|
||||
habit.id));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!habit.hasReminder()) {
|
||||
systemTray.log(String.format(
|
||||
Locale.US,
|
||||
"Habit %d does not have a reminder. Skipping.",
|
||||
habit.id));
|
||||
return;
|
||||
}
|
||||
|
||||
systemTray.showNotification(habit, getNotificationId(habit), timestamp,
|
||||
reminderTime);
|
||||
|
||||
@@ -37,21 +37,11 @@ public abstract class DateUtils
|
||||
|
||||
private static Locale fixedLocale = null;
|
||||
|
||||
/**
|
||||
* Time of the day when the new day starts.
|
||||
*/
|
||||
public static final int NEW_DAY_OFFSET = 3;
|
||||
|
||||
/**
|
||||
* Number of milliseconds in one day.
|
||||
*/
|
||||
public static final long DAY_LENGTH = 24 * 60 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Number of milliseconds in one hour.
|
||||
*/
|
||||
public static final long HOUR_LENGTH = 60 * 60 * 1000;
|
||||
|
||||
public static long applyTimezone(long localTimestamp)
|
||||
{
|
||||
TimeZone tz = getTimezone();
|
||||
@@ -182,13 +172,12 @@ public abstract class DateUtils
|
||||
|
||||
public static long getStartOfToday()
|
||||
{
|
||||
return getStartOfDay(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
|
||||
return getStartOfDay(getLocalTime());
|
||||
}
|
||||
|
||||
public static long millisecondsUntilTomorrow()
|
||||
{
|
||||
return getStartOfToday() + DAY_LENGTH -
|
||||
(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
|
||||
return getStartOfToday() + DAY_LENGTH - getLocalTime();
|
||||
}
|
||||
|
||||
public static GregorianCalendar getStartOfTodayCalendar()
|
||||
@@ -271,7 +260,7 @@ public abstract class DateUtils
|
||||
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
||||
calendar.set(Calendar.MINUTE, minute);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
Long time = calendar.getTimeInMillis();
|
||||
long time = calendar.getTimeInMillis();
|
||||
|
||||
if (DateUtils.getLocalTime() > time)
|
||||
time += DateUtils.DAY_LENGTH;
|
||||
|
||||
Reference in New Issue
Block a user