refactor presenter and strategies

This commit is contained in:
hlq07 2018-05-28 01:42:46 +08:00
parent 5e13d0735f
commit 8e463a3ec2
4 changed files with 261 additions and 221 deletions

View File

@ -1,104 +1,39 @@
package org.cutem.cutecalendar.presenter;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import org.cutem.cutecalendar.model.TodoItem;
import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
import java.util.List;
import java.util.Collection;
/**
* @author TinTin
*/
public class ListTodoItemsStrategy {
private static final String[] TYPE = {"", "conference", "date", "others"};
private Presenter mPresenter;
private VBox mListBox;
public VBox timelessList;
public HBox alarmList;
public TextField fromDate;
public TextField toDate;
public TextField fromTime;
public TextField toTime;
public VBox searchList;
public Button searchBtn;
public Button resetBtn;
public ListTodoItemsStrategy(@NotNull Pane pane) {
initialize(pane);
public void initialize() {
searchBtn.setOnAction(e -> onClickSearch());
resetBtn.setOnAction(e -> onClickReset());
}
private TextField fromDate;
private TextField toDate;
private TextField fromTime;
private TextField toTime;
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
private void initialize(@NotNull Pane pane) {
Button searchBt = new Button("search");
searchBt.setOnAction(e -> onClickSearch());
Button resetBt = new Button("reset");
resetBt.setOnAction(e -> onClickReset());
HBox btBox = new HBox(searchBt, resetBt);
Label fromLabel = new Label("from");
fromDate = new TextField();
fromTime = new TextField();
HBox fromBox = new HBox(fromDate, fromTime);
Label toLabel = new Label("to");
toDate = new TextField();
toTime = new TextField();
HBox toBox = new HBox(toDate, toTime);
mListBox = new VBox();
VBox mPane = new VBox(fromLabel, fromBox, toLabel, toBox, btBox, mListBox);
pane.getChildren().add(mPane);
}
public Pane getListBox() {
return mListBox;
}
public void listTodoItems(@NotNull List<TodoItem> items) {
mListBox.getChildren().clear();
items.forEach(item -> {
Button btn = new Button(
pickTodoItem(item)
);
btn.setOnAction(e -> onClickTodoItem(item));
mListBox.getChildren().add(btn);
});
}
private String pickTodoItem(TodoItem item) {
if (item != null) {
StringBuilder builder = new StringBuilder();
builder.append(TYPE[item.getType()]);
builder.append(": ").append(item.getStringProperty(TodoItem.TITLE));
builder.append("\n").append(
CalendarUtil.getDatePartString(item.getBgn())
);
builder.append("\n").append(
CalendarUtil.getDatePartString(item.getEnd())
);
return builder.toString();
} else {
return null;
}
}
private void onClickReset() {
fromDate.setText("");
@ -126,4 +61,73 @@ public class ListTodoItemsStrategy {
private void onClickTodoItem(TodoItem item) {
mPresenter.showTodoItemDetail(item);
}
private void onClickTimelessItem(String title) {
mPresenter.showTimelessItemDetail(title);
}
private String pickTodoItem(TodoItem item) {
if (item != null) {
StringBuilder builder = new StringBuilder();
builder.append(item.getTypeName());
builder.append(": ").append(item.getStringProperty(TodoItem.TITLE));
builder.append("\n").append(
CalendarUtil.getDatePartString(item.getBgn())
);
builder.append("\n").append(
CalendarUtil.getDatePartString(item.getEnd())
);
return builder.toString();
} else {
return "";
}
}
// region Presenter list
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
public void listTodoItems(@NotNull Collection<TodoItem> items) {
searchList.getChildren().clear();
for (TodoItem item : items) {
String info = pickTodoItem(item);
Button btn = new Button(info);
btn.setOnAction(e -> onClickTodoItem(item));
searchList.getChildren().add(btn);
}
}
public void listTimelessItems(@NotNull Collection<String> items) {
timelessList.getChildren().clear();
for (String title : items) {
Button btn = new Button(title);
btn.setOnAction(e -> onClickTimelessItem(title));
timelessList.getChildren().add(btn);
}
}
public void listAlarmTodoItems(@NotNull Collection<TodoItem> items) {
alarmList.getChildren().clear();
for (TodoItem item : items) {
String info = item.getTypeName() + ": " + item.getStringProperty(TodoItem.TITLE)
+ "\nstart at: " + CalendarUtil.toString(item.getBgn());
String method = item.getStringProperty(TodoItem.ALARM_METHOD);
if (method != null && method.equals("dialog")) {
mPresenter.showAlert(0, info);
} else {
Button btn = new Button(info);
btn.setOnAction(e -> onClickTodoItem(item));
alarmList.getChildren().add(btn);
}
}
}
// endregion Presenter list
}

View File

@ -23,56 +23,38 @@ public class PaintCalendarStrategy {
private static final String CSS_HAVE_TODO = "calendar_have_todo";
private GridPane mPane;
private TextField mYear;
private TextField mMonth;
private Presenter mPresenter;
private Calendar mNow;
private Calendar mNow = Calendar.getInstance();
private Button[][] mUnits;
public PaintCalendarStrategy(
@NotNull GridPane pane,
@NotNull TextField year,
@NotNull TextField month
) {
mPane = pane;
mYear = year;
mMonth = month;
}
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
public GridPane calendarPane;
public TextField yearText;
public TextField monthText;
public Button gotoBtn;
public Button todayBtn;
private void initialize() {
mPane.getChildren().clear();
public void initialize() {
calendarPane.getChildren().clear();
for (int w = 0; w < 7; w++) {
Button btn = new Button(WEEKDAY_NAME[w]);
mPane.add(btn, w, 0);
calendarPane.add(btn, w, 0);
}
mUnits = new Button[6][7];
for (int r = 1; r < 7; r++) {
for (int w = 0; w < 7; w++) {
Button b = new Button("");
mPane.add(b, w, r);
calendarPane.add(b, w, r);
mUnits[r - 1][w] = b;
}
}
todayBtn.setOnAction(e -> onClickToday());
gotoBtn.setOnAction(e -> onClickSearch());
}
private Button[][] getUnits() {
if (mUnits == null) {
initialize();
}
return mUnits;
}
private void emptyButton(Button btn) {
if (btn != null) {
@ -114,17 +96,47 @@ public class PaintCalendarStrategy {
}
private void onClickUnit(@Nullable Calendar date) {
if (date != null) {
Calendar[] whole = CalendarUtil.getWholeDayPeriod(date);
mPresenter.listTodoItemDuring(whole[0], whole[1]);
}
}
private void onClickSearch() {
String regex = "\\d+";
String y = yearText.getText();
String m = monthText.getText();
if (y.matches(regex) && m.matches(regex)) {
String time = String.format("%s-%s-1/0:0", y, m);
mPresenter.paintCalendar(CalendarUtil.constructCalendar(time));
} else {
mPresenter.showAlert(0, "give a number");
}
}
private void onClickToday() {
mPresenter.paintCalendar(Calendar.getInstance());
}
// region Presenter calendar
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
public void paintCalendar(@NotNull Calendar date, @NotNull List<CalendarUnit> days) {
mNow = (Calendar) date.clone();
mYear.setText(Integer.toString(date.get(Calendar.YEAR)));
mMonth.setText(Integer.toString(date.get(Calendar.MONTH) + 1));
yearText.setText(Integer.toString(date.get(Calendar.YEAR)));
monthText.setText(Integer.toString(date.get(Calendar.MONTH) + 1));
Calendar monthBegin = CalendarUtil.getMonthBegin(date);
Calendar monthEnd = CalendarUtil.getMonthEnd(date);
int firstWeekDay = monthBegin.get(Calendar.DAY_OF_WEEK) - 1;
Iterator<CalendarUnit> it = days.iterator();
Button[][] bs = getUnits();
Button[][] bs = mUnits;
for (int r = 0; r < bs.length; r++) {
for (int w = 0; w < bs[r].length; w++) {
@ -140,37 +152,10 @@ public class PaintCalendarStrategy {
}
}
/**
* Returns a Calendar value this calendar showing
*
* @return the Calendar value
* @throws NullPointerException if no invoking paintCalendar before
*/
public Calendar getCalendarShowingDate() {
return (Calendar) mNow.clone();
}
private void onClickUnit(@Nullable Calendar date) {
if (date != null) {
Calendar[] whole = CalendarUtil.getWholeDayPeriod(date);
mPresenter.listTodoItemDuring(whole[0], whole[1]);
}
}
public void onClickSearch() {
String regex = "\\d+";
String y = mYear.getText();
String m = mMonth.getText();
if (y.matches(regex) && m.matches(regex)) {
String time = String.format("%s-%s-1/0:0", y, m);
mPresenter.paintCalendar(CalendarUtil.constructCalendar(time));
} else {
mPresenter.showAlert(0, "give a number");
}
}
public void onClickToday() {
mPresenter.paintCalendar(Calendar.getInstance());
}
// endregion Presenter calendar
}

View File

@ -1,5 +1,7 @@
package org.cutem.cutecalendar.presenter;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import org.cutem.cutecalendar.model.HolidayManager;
import org.cutem.cutecalendar.model.TodoItem;
import org.cutem.cutecalendar.model.TodoItemManager;
@ -22,10 +24,72 @@ public abstract class Presenter {
private HashMap<TodoItem, Calendar> mAlarmSet = new HashMap<>();
private Timer mTimer = new Timer(false);
private TimerTask mTask = new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> listAlarmTodoItems(scanAlarmSet()));
}
};
private static boolean isNeedNextAlarm(@NotNull TodoItem item, @NotNull Calendar nextAlarm) {
return !nextAlarm.after(item.getBgn());
}
private void addAlarm(@NotNull TodoItem item) {
if (TodoItemUtil.isNeedAlarm(item)) {
Calendar nxt = TodoItemUtil.getAlarmStartCalendar(item);
String interval = item.getStringProperty(TodoItem.ALARM_INTERVAL);
nxt = TodoItemUtil.getNextCalendar(nxt, interval);
if (isNeedNextAlarm(item, nxt)) {
mAlarmSet.put(item, nxt);
}
}
}
private void removeAlarm(@NotNull TodoItem item) {
mAlarmSet.remove(item);
}
private List<TodoItem> scanAlarmSet() {
Calendar now = Calendar.getInstance();
ArrayList<TodoItem> alarms = new ArrayList<>();
for (TodoItem item : mAlarmSet.keySet()) {
Calendar old = mAlarmSet.get(item);
if (!now.before(old)) {
alarms.add(item);
String interval = item.getStringProperty(TodoItem.ALARM_INTERVAL);
Calendar nxt = TodoItemUtil.getNextCalendar(old, interval);
if (isNeedNextAlarm(item, nxt)) {
mAlarmSet.put(item, nxt);
} else {
removeAlarm(item);
}
}
}
return alarms;
}
protected void initialize() {
manager.load();
// todo
for (TodoItem v : manager.getItems()) {
addAlarm(v);
}
Calendar nxtMin = CalendarUtil.roundToMinute(Calendar.getInstance());
nxtMin.add(Calendar.MINUTE, 1);
mTimer.scheduleAtFixedRate(mTask, nxtMin.getTimeInMillis() - System.currentTimeMillis(), 60000);
listAlarmTodoItems(scanAlarmSet());
listTimelessItems(manager.getTimelessItems());
paintCalendar(Calendar.getInstance());
}
@ -125,11 +189,6 @@ public abstract class Presenter {
// endregion calendar part
protected void refresh() {
repaint();
}
protected void save() {
Thread thread = new Thread(() -> {
synchronized (LOCK) {
@ -143,44 +202,47 @@ public abstract class Presenter {
protected final void addTodoItem(@NotNull TodoItem item) {
manager.add(item);
refresh();
addAlarm(item);
save();
repaint();
}
protected final void removeTodoItem(@NotNull TodoItem item) {
manager.remove(item);
refresh();
removeAlarm(item);
save();
repaint();
}
protected final void finishTodoItem(@NotNull TodoItem item) {
item = TodoItemUtil.finishSingleTodoItem(item);
manager.add(item);
save();
addTodoItem(item);
}
protected final void addTimelessItem(@NotNull String title) {
manager.addTimelessItem(title);
refresh();
save();
listTimelessItems(manager.getTimelessItems());
}
protected final void removeTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
refresh();
save();
listTimelessItems(manager.getTimelessItems());
}
protected final void finishTimelessItem(@NotNull String title) {
manager.removeTimelessItem(title);
TodoItem item = TodoItemUtil.finishTimelessItem(title);
manager.add(item);
refresh();
save();
addTodoItem(item);
}
protected abstract void showAlert(int type, String text);
protected void showAlert(int type, @NotNull String text) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText(text);
alert.show();
}
}

View File

@ -1,11 +1,6 @@
package org.cutem.cutecalendar.presenter;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.cutem.cutecalendar.model.TodoItem;
@ -22,26 +17,27 @@ import java.util.Map;
public class ShowTodoItemDetailStrategy {
private Presenter mPresenter;
private TodoItem mNowItem;
private String mNowTimelessItem;
private VBox mPane = new VBox();
private Text mAllProperties = new Text();
private Button closeBtn = new Button("close");
private Button finishBtn = new Button("finish");
public Text textView1;
public Text textView2;
public Button closeBtn;
public Button finishBtn;
public Button delBtn;
public Button childItemBtn;
public Stage detailStage;
private Stage mStage;
private Text mText = new Text();
private Button delBtn = new Button("delete");
public ShowTodoItemDetailStrategy(
@NotNull Stage stage
) {
mStage = stage;
initialize();
public void initialize() {
delBtn.setOnAction(e -> onClickDelete());
closeBtn.setOnAction(e -> onClickClose());
finishBtn.setOnAction(e -> onClickFinish());
childItemBtn.setOnAction(e -> onClickChild());
}
private static String getAllProperties(@NotNull TodoItem item) {
Map<String, String> props = item.getStringPropertiesMap();
final StringBuilder builder = new StringBuilder();
@ -99,62 +95,21 @@ public class ShowTodoItemDetailStrategy {
return "type: others\n" + "title: " + title;
}
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
public void showTodoItemDetail(@NotNull TodoItem item) {
mNowItem = item;
boolean f = !TodoItemUtil.isFinished(item) && TodoItemUtil.isRunning(item);
String res = buildGeneralInfo(item);
String all = getAllProperties(item);
printText(res, all, f);
}
public void showTimelessItemDetail(@NotNull String title) {
mNowTimelessItem = title;
String res = buildGeneralInfo(title);
printText(res, "", true);
}
private void printText(String text, String all, boolean finishShow) {
mText.setText(text);
mAllProperties.setText(all);
private void printText(String text, String all, boolean finishShow, boolean childShow) {
textView1.setText(text);
textView2.setText(all);
finishBtn.setVisible(finishShow);
childItemBtn.setVisible(childShow);
mStage.show();
detailStage.show();
}
public Pane getPane() {
return mPane;
}
private void initialize() {
mStage.setScene(new Scene(mPane));
HBox up = new HBox(mText, mAllProperties);
up.setSpacing(10);
HBox down = new HBox(delBtn, closeBtn, finishBtn);
down.setSpacing(5);
mPane.getChildren().addAll(up, down);
mPane.setSpacing(3);
mPane.setAlignment(Pos.CENTER);
delBtn.setOnAction(e -> onClickDelete());
closeBtn.setOnAction(e -> onClickClose());
finishBtn.setOnAction(e -> onClickFinish());
}
private void onClickClose() {
mNowItem = null;
mNowTimelessItem = null;
mStage.close();
detailStage.close();
}
private void onClickDelete() {
@ -175,4 +130,38 @@ public class ShowTodoItemDetailStrategy {
onClickClose();
}
private void onClickChild() {
if (mNowItem != null) {
mPresenter.showNewChildTodoItemStage(mNowItem);
}
onClickClose();
}
// region Presenter detail
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
public void showTodoItemDetail(@NotNull TodoItem item) {
mNowItem = item;
String res = buildGeneralInfo(item);
String all = getAllProperties(item);
boolean f = !TodoItemUtil.isFinished(item) && TodoItemUtil.isRunning(item);
boolean c = !TodoItemUtil.haveParent(item);
printText(res, all, f, c);
}
public void showTimelessItemDetail(@NotNull String title) {
mNowTimelessItem = title;
String res = buildGeneralInfo(title);
printText(res, "a to-do item without time", true, false);
}
// endregion Presenter detail
}