diff --git a/app/src/main/java/org/cutem/cutecalendar/model/TodoItem.java b/app/src/main/java/org/cutem/cutecalendar/model/TodoItem.java index 7df313e..95d0cb9 100644 --- a/app/src/main/java/org/cutem/cutecalendar/model/TodoItem.java +++ b/app/src/main/java/org/cutem/cutecalendar/model/TodoItem.java @@ -271,7 +271,7 @@ public class TodoItem { * @return the title property value if any; {@code null} otherwise */ public String getTitle() { - return mStringProperties.getOrDefault(TITLE, null); + return getStringProperty(TITLE); } /** @@ -280,7 +280,7 @@ public class TodoItem { * @return the location property value if any; {@code null} otherwise */ public String getLocation() { - return mStringProperties.getOrDefault(LOCATION, null); + return getStringProperty(LOCATION); } /** @@ -289,7 +289,7 @@ public class TodoItem { * @return the description property value if any; {@code null} otherwise */ public String getDescription() { - return mStringProperties.getOrDefault(DESCRIPTION, null); + return getStringProperty(DESCRIPTION); } /** 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 96736e8..a63fc58 100644 --- a/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java +++ b/app/src/main/java/org/cutem/cutecalendar/model/TodoItemManager.java @@ -83,6 +83,10 @@ public class TodoItemManager { return mIdCache.getOrDefault(id, null); } + public Set getItems() { + return mUnmodifiableItems; + } + public List queryTodoItemsDuring(@NotNull Calendar from, @NotNull Calendar to) { ArrayList result = new ArrayList<>(); for (TodoItem i : mItems) { diff --git a/app/src/main/java/org/cutem/cutecalendar/model/TodoItemUtil.java b/app/src/main/java/org/cutem/cutecalendar/model/TodoItemUtil.java index e43a2ef..5af62f8 100644 --- a/app/src/main/java/org/cutem/cutecalendar/model/TodoItemUtil.java +++ b/app/src/main/java/org/cutem/cutecalendar/model/TodoItemUtil.java @@ -131,7 +131,10 @@ public class TodoItemUtil { } else if (i2.getType() == ANNIVERSARY) { return isIntersectedWithAnniversary(i2, i1); } else { - return isIntersected(calculateRepeatArray(i1), calculateRepeatArray(i2)); + List> ints1 = calculateRepeatArray(i1); + List> ints2 = calculateRepeatArray(i2); + + return isIntersected(ints1, ints2); } } @@ -153,10 +156,10 @@ public class TodoItemUtil { } public static boolean isIn( - @NotNull Collection> l1, - @NotNull Collection> l2) { - for (GenericPair p1 : l1) { - for (GenericPair p2 : l2) { + @NotNull Collection> outInts, + @NotNull Collection> inInts) { + for (GenericPair p1 : outInts) { + for (GenericPair p2 : inInts) { Calendar c1 = p1.getFirst(); Calendar c2 = p1.getSecond(); Calendar c3 = p2.getFirst(); @@ -200,9 +203,7 @@ public class TodoItemUtil { @NotNull TodoItem anniversary, @NotNull TodoItem item) { if (item.getType() == ANNIVERSARY && anniversary.getBgn().after(item.getBgn())) { - TodoItem t = anniversary; - anniversary = item; - item = t; + return false; } List> list = calculateRepeatArray(item); @@ -221,13 +222,15 @@ public class TodoItemUtil { } } - public static boolean isIn(@NotNull TodoItem i1, @NotNull TodoItem i2) { - if (i1.getType() == ANNIVERSARY) { - return isInAnniversary(i1, i2); - } else if (i2.getType() == ANNIVERSARY) { - return isInAnniversary(i2, i1); + public static boolean isIn(@NotNull TodoItem outItem, @NotNull TodoItem inItem) { + if (outItem.getType() == ANNIVERSARY) { + return isInAnniversary(outItem, inItem); + } else if (inItem.getType() == ANNIVERSARY) { + return false; } else { - return isIn(calculateRepeatArray(i1), calculateRepeatArray(i2)); + List> ints1 = calculateRepeatArray(outItem); + List> ints2 = calculateRepeatArray(inItem); + return isIn(ints1, ints2); } } @@ -236,7 +239,8 @@ public class TodoItemUtil { int pType = parent.getType(); int cType = child.getType(); - return pType != cType && arrayContains(PARENT_CHILDREN_CONFLICT, pType) + return pType != cType + && arrayContains(PARENT_CHILDREN_CONFLICT, pType) && arrayContains(PARENT_CHILDREN_CONFLICT, cType) && isIn(parent, child); } @@ -254,6 +258,10 @@ public class TodoItemUtil { return result; } + public static boolean haveParent(@NotNull TodoItem item) { + return getParentId(item) != item.getId(); + } + public static long getParentId(@NotNull TodoItem item) { String id = item.getStringProperty(PARENT_ID); return id == null ? item.getId() : Long.parseLong(id); @@ -274,6 +282,7 @@ public class TodoItemUtil { return builder.toString(); } + public static TodoItem[] bind(@NotNull TodoItem parent, @NotNull TodoItem child) { TodoItemFactory fParent = new TodoItemFactory().copy(parent); TodoItemFactory fChild = new TodoItemFactory().copy(child); @@ -315,6 +324,7 @@ public class TodoItemUtil { return new TodoItem[]{newP, newC}; } + public static TodoItem finishSingleTodoItem(@NotNull TodoItem item) { TodoItemFactory factory = new TodoItemFactory(); return factory.copy(item).stringProperties(FINISH, "finish").buildId(item.getId()); @@ -328,4 +338,38 @@ public class TodoItemUtil { .build(); } + + public static boolean isFinished(@NotNull TodoItem item) { + if (item.getStringProperty(FINISH) != null) { + return true; + } else { + List ids = getAllChildrenIds(item); + if (ids.isEmpty()) { // leaf + return false; + } else { + for (long id : ids) { + TodoItem childItem = TodoItemManager.getInstance().get(id); + if (childItem != null && !isFinished(childItem)) { + return false; + } + } + return true; + } + } + } + + public static boolean isBeforeStart(@NotNull TodoItem item) { + Calendar now = Calendar.getInstance(); + return now.before(item.getBgn()); + } + + public static boolean isEnded(@NotNull TodoItem item) { + Calendar now = Calendar.getInstance(); + return now.after(item.getEnd()); + } + + public static boolean isRunning(@NotNull TodoItem item) { + return !isBeforeStart(item) && !isEnded(item); + } + }