Merge branch 'dev' into feature/stackview

This commit is contained in:
2018-02-04 09:52:16 -06:00
29 changed files with 381 additions and 92 deletions

View File

@@ -22,8 +22,9 @@ package org.isoron.uhabits.core.models;
import android.support.annotation.*;
import org.apache.commons.lang3.builder.*;
import org.isoron.uhabits.core.utils.*;
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
import static org.isoron.uhabits.core.utils.StringUtils.*;
public final class Reminder
{
@@ -56,6 +57,11 @@ public final class Reminder
return minute;
}
public long getTimeInMillis()
{
return DateUtils.getUpcomingTimeInMillis(hour, minute);
}
@Override
public boolean equals(Object o)
{
@@ -66,29 +72,29 @@ public final class Reminder
Reminder reminder = (Reminder) o;
return new EqualsBuilder()
.append(hour, reminder.hour)
.append(minute, reminder.minute)
.append(days, reminder.days)
.isEquals();
.append(hour, reminder.hour)
.append(minute, reminder.minute)
.append(days, reminder.days)
.isEquals();
}
@Override
public int hashCode()
{
return new HashCodeBuilder(17, 37)
.append(hour)
.append(minute)
.append(days)
.toHashCode();
.append(hour)
.append(minute)
.append(days)
.toHashCode();
}
@Override
public String toString()
{
return new ToStringBuilder(this, defaultToStringStyle())
.append("hour", hour)
.append("minute", minute)
.append("days", days)
.toString();
.append("hour", hour)
.append("minute", minute)
.append("days", days)
.toString();
}
}

View File

@@ -164,7 +164,7 @@ public class MemoryHabitList extends HabitList
@Override
public synchronized Iterator<Habit> iterator()
{
return Collections.unmodifiableCollection(list).iterator();
return new ArrayList<>(list).iterator();
}
@Override

View File

@@ -24,9 +24,6 @@ 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.*;
@@ -55,19 +52,24 @@ public class ReminderScheduler implements CommandRunner.Listener
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
{
if(command instanceof ToggleRepetitionCommand) return;
if(command instanceof ChangeHabitColorCommand) return;
if (command instanceof ToggleRepetitionCommand) return;
if (command instanceof ChangeHabitColorCommand) return;
scheduleAll();
}
public void schedule(@NonNull Habit habit, @Nullable Long reminderTime)
public void schedule(@NonNull Habit habit)
{
if (!habit.hasReminder()) return;
if (habit.isArchived()) return;
Reminder reminder = habit.getReminder();
if (reminderTime == null) reminderTime = getReminderTime(reminder);
long timestamp = getStartOfDay(removeTimezone(reminderTime));
Long reminderTime = habit.getReminder().getTimeInMillis();
scheduleAtTime(habit, reminderTime);
}
public void scheduleAtTime(@NonNull Habit habit, @NonNull Long reminderTime)
{
if (reminderTime == null) throw new IllegalArgumentException();
if (!habit.hasReminder()) return;
if (habit.isArchived()) return;
long timestamp = getStartOfDay(removeTimezone(reminderTime));
sys.scheduleShowReminder(reminderTime, habit, timestamp);
}
@@ -76,7 +78,7 @@ public class ReminderScheduler implements CommandRunner.Listener
HabitList reminderHabits =
habitList.getFiltered(HabitMatcher.WITH_ALARM);
for (Habit habit : reminderHabits)
schedule(habit, null);
schedule(habit);
}
public void startListening()
@@ -89,19 +91,11 @@ public class ReminderScheduler implements CommandRunner.Listener
commandRunner.removeListener(this);
}
@NonNull
private Long getReminderTime(@NonNull Reminder reminder)
public void scheduleMinutesFromNow(Habit habit, long minutes)
{
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.DAY_LENGTH;
return applyTimezone(time);
long now = applyTimezone(getLocalTime());
long reminderTime = now + minutes * 60 * 1000;
scheduleAtTime(habit, reminderTime);
}
public interface SystemScheduler

View File

@@ -21,8 +21,10 @@ package org.isoron.uhabits.core.ui.screens.habits.show;
import android.support.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 java.io.*;
import java.util.*;
@@ -45,18 +47,23 @@ public class ShowHabitMenuBehavior
@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 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()
@@ -77,9 +84,20 @@ public class ShowHabitMenuBehavior
}));
}
public void onDeleteHabit()
{
List<Habit> selected = Collections.singletonList(habit);
screen.showDeleteConfirmationScreen(() -> {
commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
null);
screen.close();
});
}
public enum Message
{
COULD_NOT_EXPORT
COULD_NOT_EXPORT, HABIT_DELETED
}
public interface Screen
@@ -89,6 +107,11 @@ public class ShowHabitMenuBehavior
void showMessage(Message m);
void showSendFileScreen(String filename);
void showDeleteConfirmationScreen(
@NonNull OnConfirmedCallback callback);
void close();
}
public interface System

View File

@@ -48,14 +48,15 @@ public class WidgetBehavior
public void onAddRepetition(@NonNull Habit habit, Timestamp timestamp)
{
notificationTray.cancel(habit);
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep != null) return;
performToggle(habit, timestamp);
notificationTray.cancel(habit);
}
public void onRemoveRepetition(@NonNull Habit habit, Timestamp timestamp)
{
notificationTray.cancel(habit);
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep == null) return;
performToggle(habit, timestamp);

View File

@@ -240,6 +240,20 @@ public abstract class DateUtils
}
}
public static long getUpcomingTimeInMillis(int hour, int minute)
{
Calendar calendar = DateUtils.getStartOfTodayCalendar();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time)
time += DateUtils.DAY_LENGTH;
return applyTimezone(time);
}
public enum TruncateField
{
MONTH, WEEK_NUMBER, YEAR, QUARTER

View File

@@ -118,7 +118,7 @@ public class ReminderSchedulerTest extends BaseUnitTest
@Test
public void testSchedule_withoutReminder()
{
reminderScheduler.schedule(habit, null);
reminderScheduler.schedule(habit);
Mockito.verifyZeroInteractions(sys);
}
@@ -133,7 +133,8 @@ public class ReminderSchedulerTest extends BaseUnitTest
long expectedCheckmarkTime,
long expectedReminderTime)
{
reminderScheduler.schedule(habit, atTime);
if(atTime == null) reminderScheduler.schedule(habit);
else reminderScheduler.scheduleAtTime(habit, atTime);
verify(sys).scheduleShowReminder(expectedReminderTime, habit,
expectedCheckmarkTime);
}

View File

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