add some methods to TodoItemUtil

This commit is contained in:
hlq07 2018-05-27 23:04:58 +08:00
parent 0d89a13845
commit de651568e3
6 changed files with 235 additions and 171 deletions

View File

@ -10,6 +10,8 @@ import java.util.*;
*/
public class TodoItem {
// region type constants
/**
* Value of a type of TodoItem indicating the "others" type.
*/
@ -45,18 +47,30 @@ public class TodoItem {
* Value of a type of TodoItem indicating the "interview" type.
*/
public static final int INTERVIEW = 7;
/**
* Value of a name of possible string-type property index.
*/
public static final String REPEAT_WEEKS = "repeatWeeks";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "id1;id2;id3;...".
*/
public static final String CHILDREN_IDS = "children_ids";
private static String[] TYPE_NAME = {
"unknown",
"others",
"conference",
"date",
"trip",
"anniversary",
"course",
"interview"
};
public static String typeName(int type) {
if (1 <= type && type < TYPE_NAME.length) {
return TYPE_NAME[type];
} else {
return TYPE_NAME[0];
}
}
// endregion type constants
// region general string properties
/**
* Value of a name of possible string-type property index.
@ -82,44 +96,47 @@ public class TodoItem {
* Value of a name of possible string-type property index.
*/
public static final String IMPORTANCE = "importance";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "area", "dialog" or "none".
*/
public static final String ALARM_METHOD = "alarm_method";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "yyyy-mm-dd/hh:mm".
*/
public static final String ALARM_START = "alarm_start";
/**
* Value of a name of possible string-type property index.
*/
public static final String PARENT_ID = "parent_id";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "id1;id2;id3;...".
*/
public static final String CHILDREN_IDS = "children_ids";
/**
* Value of a name of possible string-type property index.
*/
public static final String REPEAT_WEEKS = "repeatWeeks";
/**
* Value of a name of possible string-type property index.
*/
public static final String FINISH = "finish";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "y;m;d;h;m".
*/
public static final String ALARM_INTERVAL = "alarm_interval";
private static String[] TYPE_NAME = {
"unknown",
"others",
"conference",
"date",
"trip",
"anniversary",
"course",
"interview"
};
private ArrayList<String> mPersonnel = new ArrayList<>();
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "area", "dialog" or "none".
*/
public static final String ALARM_METHOD = "alarm_method";
/**
* Value of a name of possible string-type property index.
* The corresponding value is formatted in "yyyy-mm-dd/hh:mm".
*/
public static final String ALARM_START = "alarm_start";
// endregion general string properties
@ -131,10 +148,16 @@ public class TodoItem {
private Calendar mEnd;
private int mType;
private ArrayList<String> mPersonnel = new ArrayList<>();
private List<String> mUnmodifiablePersonnel = Collections.unmodifiableList(mPersonnel);
private HashMap<String, String> mStringProperties = new HashMap<>();
private Map<String, String> mUnmodifiableStringProperties = Collections.unmodifiableMap(mStringProperties);
/**
* Constructs a {@code TodoItem} based on given arguments.
*
@ -186,14 +209,6 @@ public class TodoItem {
this(id, begin, end, type, null, null);
}
public static String typeName(int type) {
if (1 <= type && type < TYPE_NAME.length) {
return TYPE_NAME[type];
} else {
return TYPE_NAME[0];
}
}
/**
* Returns the id of the TodoItem.

View File

@ -28,7 +28,6 @@ public class TodoItemFactory {
public TodoItemFactory beginTime(@NotNull Calendar begin) {
mBgn = CalendarUtil.roundToMinute(begin);
return this;
@ -66,7 +65,6 @@ public class TodoItemFactory {
}
public TodoItemFactory stringProperties(String index, String value) {
if (value != null) {
mStringProperties.put(index, value);
@ -82,9 +80,6 @@ public class TodoItemFactory {
}
public TodoItemFactory copy(@NotNull TodoItem item) {
this.beginTime(item.getBgn()).
endTime(item.getEnd()).
@ -116,5 +111,4 @@ public class TodoItemFactory {
return buildId(manager.getNextId());
}
}

View File

@ -9,13 +9,17 @@ public class TodoItemManager {
private static TodoItemManager ourInstance;
private TodoItemManager() {
//empty
public static TodoItemManager getInstance() {
if (ourInstance == null) {
ourInstance = new TodoItemManager();
}
return ourInstance;
}
private TodoItemManager() {
//empty
}
@ -33,13 +37,6 @@ public class TodoItemManager {
private HashMap<Long, TodoItem> mIdCache = new HashMap<>();
public static TodoItemManager getInstance() {
if (ourInstance == null) {
ourInstance = new TodoItemManager();
}
return ourInstance;
}
public long getNextId() {
while (mIdCache.containsKey(mIdCount)) {
@ -124,8 +121,6 @@ public class TodoItemManager {
String jsonizeTodoItems() {
return TodoItemsStorage.toJson(mUnmodifiableItems);
}

View File

@ -31,7 +31,7 @@ public class TodoItemUtil {
return false;
}
public static List<GenericPair<Calendar, Calendar>> calculateRepeatArray(@NotNull TodoItem item) {
private static List<GenericPair<Calendar, Calendar>> calculateRepeatArray(@NotNull TodoItem item) {
ArrayList<GenericPair<Calendar, Calendar>> result = new ArrayList<>();
String repeatWeeks = item.getStringProperty(REPEAT_WEEKS);
@ -50,6 +50,8 @@ public class TodoItemUtil {
}
// region intersected part
private static boolean isIntersected(
@NotNull Calendar c1, @NotNull Calendar c2,
@NotNull Calendar p1, @NotNull Calendar p2) {
@ -140,7 +142,10 @@ public class TodoItemUtil {
}
}
// endregion intersected part
// region isIn part
private static boolean isIn(
@NotNull Calendar out1, @NotNull Calendar out2,
@NotNull Calendar in1, @NotNull Calendar in2) {
@ -227,6 +232,10 @@ public class TodoItemUtil {
}
}
// endregion isIn part
// region conflict check part
public static boolean isIntersectedConflict(@NotNull TodoItem i1, @NotNull TodoItem i2) {
int t1 = i1.getType();
@ -246,12 +255,20 @@ public class TodoItemUtil {
&& isIn(parent, child);
}
// endregion conflict check part
// region REPEAT_WEEKS part
public static int getRepeatWeeks(@NotNull TodoItem item) {
String repeat = item.getStringProperty(REPEAT_WEEKS);
return repeat == null ? 1 : Integer.parseInt(repeat);
}
// endregion REPEAT_WEEKS part
// region parent & children part
public static long getParentId(@NotNull TodoItem item) {
String id = item.getStringProperty(PARENT_ID);
@ -331,6 +348,10 @@ public class TodoItemUtil {
return new TodoItem[]{newP, newC};
}
// endregion parent & children part
// region finish part
public static boolean isFinished(@NotNull TodoItem item) {
if (item.getStringProperty(FINISH) != null) {
@ -364,6 +385,10 @@ public class TodoItemUtil {
.build();
}
// endregion finish part
// region alarm part
public static boolean isValidAlarmIntString(@NotNull String interval) {
String[] strSlices = interval.split("[;,]");
@ -401,6 +426,27 @@ public class TodoItemUtil {
return newTime;
}
public static Calendar getAlarmStartCalendar(@NotNull TodoItem item) {
String alarmStart = item.getStringProperty(ALARM_START);
if (alarmStart != null && CalendarUtil.isValidCalendarString(alarmStart)) {
return CalendarUtil.constructCalendar(alarmStart);
} else {
return item.getBgn();
}
}
public static boolean isNeedAlarm(@NotNull TodoItem item) {
String alarmMethod = item.getStringProperty(ALARM_METHOD);
if (alarmMethod == null || alarmMethod.equals("none")) {
return false;
} else {
Calendar start = getAlarmStartCalendar(item);
return !start.after(item.getBgn());
}
}
// endregion alarm part
public static boolean isBeforeStart(@NotNull TodoItem item) {
Calendar now = Calendar.getInstance();

View File

@ -12,62 +12,28 @@ public class CalendarUnit {
public static final int HOLIDAY_FLAG = 1 << 2;
public static final int WORKDAY_FLAG = 1 << 3;
private final String mFestival;
private int mFlags;
private final String mFestival;
private CalendarUnit(int flags, String festival) {
mFlags = flags;
mFestival = festival;
}
public static CalendarUnit constructUnit(int flags, String festival) {
return new CalendarUnit(flags, festival);
}
public static CalendarUnit constructUnit(
boolean todayFlag,
boolean haveTodoFlag,
boolean holidayFlag,
boolean workdayFlag,
String festival) {
int flags = 0;
if (todayFlag) {
flags |= TODAY_FLAG;
}
if (haveTodoFlag) {
flags |= HAVE_TODO_FLAG;
}
if (holidayFlag) {
flags |= HOLIDAY_FLAG;
}
if (workdayFlag) {
flags |= WORKDAY_FLAG;
}
return constructUnit(flags, festival);
}
public static CalendarUnit constructUnitWithoutFestival(int flags) {
return constructUnit(flags, null);
}
public static CalendarUnit constructUnitWithoutFestival(
boolean todayFlag,
boolean haveTodoFlag,
boolean holidayFlag,
boolean workdayFlag) {
return constructUnit(todayFlag, haveTodoFlag, holidayFlag, workdayFlag, null);
}
public int getFlags() {
return mFlags;
}
public boolean isSetFlag(int mask) {
return (mFlags & mask) != 0;
public boolean isSetFlag(int flag) {
return (mFlags & flag) != 0;
}
public boolean isFestival() {
return mFestival != null;
}
@ -76,4 +42,10 @@ public class CalendarUnit {
return mFestival;
}
public static CalendarUnit constructUnit(int flags, String festival) {
return new CalendarUnit(flags, festival);
}
}

View File

@ -7,93 +7,87 @@ import org.cutem.cutecalendar.model.TodoItemUtil;
import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.*;
/**
* @author hlq07
*/
public abstract class Presenter {
private static final Object LOCK = new Object();
private final TodoItemManager manager = TodoItemManager.getInstance();
private final HolidayManager holidayManager = HolidayManager.getInstance();
protected abstract Calendar getCalendarShowingDate();
private HashMap<TodoItem, Calendar> mAlarmSet = new HashMap<>();
protected abstract void showTodoItemDetail(TodoItem item);
protected void initialize() {
manager.load();
// todo
paintCalendar(Calendar.getInstance());
}
// region detail part
protected void showTodoItemDetail(@NotNull TodoItem item) {
throw new UnsupportedOperationException("not supported");
}
protected void showTimelessItemDetail(@NotNull String title) {
// empty: need to override
throw new UnsupportedOperationException("not supported");
}
protected abstract void showNewTodoItemStage();
// endregion detail part
// region list part
protected void listTodoItems(@NotNull Collection<TodoItem> items) {
throw new UnsupportedOperationException("not supported");
}
protected final void listTodoItemDuring(@NotNull Calendar from, @NotNull Calendar to) {
listTodoItems(manager.queryTodoItemsDuring(from, to));
}
protected void listTimelessItems(@NotNull Collection<String> items) {
throw new UnsupportedOperationException("not supported");
}
protected void listAlarmTodoItems(@NotNull Collection<TodoItem> items) {
throw new UnsupportedOperationException("not supported");
}
// endregion list part
// region new todoItem part
protected void showNewTodoItemStage() {
throw new UnsupportedOperationException("not supported");
}
protected void showNewChildTodoItemStage(@NotNull TodoItem parent) {
// empty: need to override
throw new UnsupportedOperationException("not supported");
}
private void save() {
Thread thread = new Thread(() -> {
synchronized (LOCK) {
manager.save();
}
});
thread.setDaemon(true);
thread.start();
// endregion new todoItem part
// region calendar part
protected Calendar getCalendarShowingDate() {
throw new UnsupportedOperationException("not supported");
}
protected final void addTodoItem(@NotNull TodoItem item) {
manager.add(item);
repaint();
save();
protected void paintCalendar(@NotNull Calendar date, @NotNull List<CalendarUnit> days) {
throw new UnsupportedOperationException("not supported");
}
protected final void removeTodoItem(@NotNull TodoItem item) {
manager.remove(item);
repaint();
save();
}
protected final void addTimelessItem(@NotNull String title) {
manager.addTimelessItem(title);
repaint();
save();
}
protected final void removeTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
repaint();
save();
}
protected final void finishTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
TodoItem item = TodoItemUtil.finishTimelessItem(title);
manager.add(item);
repaint();
save();
}
protected final void finishTodoItem(@NotNull TodoItem item) {
item = TodoItemUtil.finishSingleTodoItem(item);
manager.add(item);
// no need to repaint
save();
}
private void repaint() {
paintCalendar(getCalendarShowingDate());
}
protected abstract void paintCalendar(@NotNull Calendar date, @NotNull List<CalendarUnit> days);
public final void paintCalendar(@NotNull Calendar date) {
protected final void paintCalendar(@NotNull Calendar date) {
Calendar c1 = CalendarUtil.getMonthBegin(date);
Calendar c2 = CalendarUtil.getMonthEnd(date);
Calendar today = Calendar.getInstance();
@ -117,28 +111,76 @@ public abstract class Presenter {
CalendarUnit unit = CalendarUnit.constructUnit(flags, festivalName);
days.add(unit);
c1.add(Calendar.DAY_OF_MONTH, 1);
}
paintCalendar(date, days);
}
protected abstract void listTodoItems(List<TodoItem> items);
protected final void listTodoItemDuring(Calendar from, Calendar to) {
listTodoItems(manager.queryTodoItemsDuring(from, to));
protected void repaint() {
paintCalendar(getCalendarShowingDate());
}
protected void listTimelessItems(@NotNull Collection<String> items) {
// empty: need to override
throw new UnsupportedOperationException("not supported");
// endregion calendar part
protected void refresh() {
repaint();
}
protected void listAlarmTodoItems(@NotNull Collection<TodoItem> items) {
// empty: need to override
throw new UnsupportedOperationException("not supported");
protected void save() {
Thread thread = new Thread(() -> {
synchronized (LOCK) {
manager.save();
}
});
thread.setDaemon(true);
thread.start();
}
protected final void addTodoItem(@NotNull TodoItem item) {
manager.add(item);
refresh();
save();
}
protected final void removeTodoItem(@NotNull TodoItem item) {
manager.remove(item);
refresh();
save();
}
protected final void finishTodoItem(@NotNull TodoItem item) {
item = TodoItemUtil.finishSingleTodoItem(item);
manager.add(item);
save();
}
protected final void addTimelessItem(@NotNull String title) {
manager.addTimelessItem(title);
refresh();
save();
}
protected final void removeTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
refresh();
save();
}
protected final void finishTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
TodoItem item = TodoItemUtil.finishTimelessItem(title);
manager.add(item);
refresh();
save();
}
protected abstract void showAlert(int type, String text);
}