diff --git a/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java b/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java index 3406531..85fc36d 100644 --- a/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java +++ b/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java @@ -16,8 +16,6 @@ public class TodoItemManager { private HashMap idCache = new HashMap<>(); - private HashMap> dateCache = new HashMap<>(); - private TodoItemManager() { } @@ -36,17 +34,12 @@ public class TodoItemManager { return idCount++; } - private static boolean isIntersectedDuring(@NotNull TodoItem item, @NotNull Calendar from, @NotNull Calendar to) { - return !from.after(item.getEnd()) && !to.before(item.getBgn()); - } - public void add(@NotNull TodoItem item) { if (idCache.containsKey(item.getId())) { remove(item.getId()); } idCache.put(item.getId(), item); items.add(item); - addDateCache(item); } public void add(@NotNull Collection items) { @@ -61,72 +54,31 @@ public class TodoItemManager { if (idCache.containsKey(id)) { TodoItem item = idCache.remove(id); items.remove(item); - removeDateCache(item); } } - private void addDateCache(@NotNull TodoItem item) { - Calendar from = CalendarUtil.getWholeDayPeriod(item.getBgn())[0]; - Calendar to = CalendarUtil.getWholeDayPeriod(item.getEnd())[1]; - - while (!from.after(to)) { - String date = CalendarUtil.getDatePartString(from); - - if (!dateCache.containsKey(date)) { - dateCache.put(date, new ArrayList<>()); - } - List list = dateCache.get(date); - - if (!list.contains(item)) { - list.add(item); - } - - from.add(Calendar.DAY_OF_MONTH, 1); - } + private static boolean isIntersectedDuring(@NotNull TodoItem item, @NotNull Calendar from, @NotNull Calendar to) { + return !from.after(item.getEnd()) && !to.before(item.getBgn()); } - private void removeDateCache(@NotNull TodoItem item) { - Calendar from = CalendarUtil.getWholeDayPeriod(item.getBgn())[0]; - Calendar to = CalendarUtil.getWholeDayPeriod(item.getEnd())[1]; - - while (!from.after(to)) { - String date = CalendarUtil.getDatePartString(from); - List list = dateCache.getOrDefault(date, Collections.emptyList()); - - list.remove(item); - - from.add(Calendar.DAY_OF_MONTH, 1); - } + public TodoItem get(long id) { + return idCache.getOrDefault(id, null); } public List queryTodoItemsDuring(@NotNull Calendar from, @NotNull Calendar to) { ArrayList result = new ArrayList<>(); - - Calendar c1 = CalendarUtil.getWholeDayPeriod(from)[0]; - Calendar c2 = CalendarUtil.getWholeDayPeriod(to)[1]; - - while (!c1.after(c2)) { - String date = CalendarUtil.getDatePartString(c1); - List list = dateCache.getOrDefault(date, Collections.emptyList()); - - list.forEach(item -> { - if (isIntersectedDuring(item, from, to) - && !result.contains(item)) { - result.add(item); - } - }); - - c1.add(Calendar.DAY_OF_MONTH, 1); + for (TodoItem i : items) { + if (isIntersectedDuring(i, from, to)) { + result.add(i); + } } - return result; } public boolean haveTodoItemOnSomeday(@NotNull Calendar someday) { - String date = CalendarUtil.getDatePartString(someday); - List items = dateCache.getOrDefault(date, Collections.emptyList()); - - return !items.isEmpty(); + Calendar[] whole = CalendarUtil.getWholeDayPeriod(someday); + List l = queryTodoItemsDuring(whole[0], whole[1]); + return !l.isEmpty(); } @@ -153,10 +105,4 @@ public class TodoItemManager { } - public TodoItem get(long id) { - return idCache.getOrDefault(id, null); - } - - - }