Skip to content

Commit d1d47d0

Browse files
committed
feat(review): review的所有题目界面,新增双击跳转编写界面,右键支持评论修改。评价按钮支持读取历史评价
1 parent bc2b783 commit d1d47d0

12 files changed

Lines changed: 393 additions & 207 deletions

File tree

src/main/java/com/xhf/leetcode/plugin/editors/text/CustomTextEditor.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.xhf.leetcode.plugin.io.console.ConsoleUtils;
2929
import com.xhf.leetcode.plugin.model.*;
3030
import com.xhf.leetcode.plugin.review.backend.algorithm.constant.FSRSRating;
31+
import com.xhf.leetcode.plugin.review.backend.model.ReviewQuestion;
3132
import com.xhf.leetcode.plugin.review.backend.service.RQServiceImpl;
3233
import com.xhf.leetcode.plugin.review.utils.AbstractMasteryDialog;
3334
import com.xhf.leetcode.plugin.service.CodeService;
@@ -46,6 +47,7 @@
4647
import javax.swing.*;
4748
import java.awt.*;
4849
import java.beans.PropertyChangeListener;
50+
import java.util.List;
4951
import java.util.Objects;
5052

5153
public class CustomTextEditor implements TextEditor {
@@ -96,19 +98,44 @@ private JComponent createButtonToolbar() {
9698
private void showMasteryDialog() {
9799
new AbstractMasteryDialog(this.getComponent(), BundleUtils.i18nHelper("设置掌握程度", "set mastery level")) {
98100

101+
;
102+
99103
@Override
100104
protected void setConfirmButtonListener(JButton confirmButton, ButtonGroup group, JTextArea textArea) {
101105
confirmButton.addActionListener(e -> {
102106
// 获取选中的掌握程度
103107
String levelStr = group.getSelection().getActionCommand();
104108

105-
service.createQuestion(ViewUtils.getQuestionByVFile(file, project), FSRSRating.getById(levelStr), textArea.getText());
109+
Question question = ViewUtils.getQuestionByVFile(file, project);
110+
service.createQuestion(question, FSRSRating.getById(levelStr), textArea.getText());
106111

107112
// 关闭对话框
108113
this.dispose();
109114
});
110115
}
116+
117+
@Override
118+
protected String getNoteText() {
119+
try {
120+
List<ReviewQuestion> allQuestions = service.getAllQuestions();
121+
122+
Question question = ViewUtils.getQuestionByVFile(file, project);
123+
int id = Question.getIdx(question, project);
124+
125+
// 判断当前文件题目是否被包含
126+
for (ReviewQuestion rq : allQuestions) {
127+
if (rq.getId() == id) {
128+
return rq.getUserNoteText();
129+
}
130+
}
131+
return "";
132+
} catch (Exception e) {
133+
LogUtils.warn(e);
134+
return "";
135+
}
136+
}
111137
};
138+
112139
}
113140

114141
public void setToolbar() {

src/main/java/com/xhf/leetcode/plugin/review/backend/algorithm/AlgorithmApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,6 @@ public void update(QuestionCard card, String back) {
180180

181181
this.databaseAdapter.getSqlite().update(updateBack);
182182
// 数据库操作
183-
LogUtils.info("[Cards] 成功update卡片 " + card.getId());
183+
LogUtils.simpleDebug("[Cards] 成功update卡片 " + card.getId());
184184
}
185185
}

src/main/java/com/xhf/leetcode/plugin/review/backend/card/QuestionCardScheduler.java

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void queueDueCards() {
6565
try {
6666
while (resultSet.next()) {
6767
QuestionCard dueCard = AlgorithmApp.getInstance(project).getCards().get(resultSet.getInt("card_id"));
68-
LogUtils.info("[CardScheduler] Due time for card " + dueCard.getId());
68+
LogUtils.simpleDebug("[CardScheduler] Due time for card " + dueCard.getId());
6969
this.queue.enqueue(dueCard);
7070
}
7171
} catch (SQLException e) {
@@ -90,51 +90,62 @@ public void queueDueCards() {
9090
*/
9191
public void onRating(int rating, @Nullable String back) {
9292
QuestionCard ratedCard = this.queue.front();
93+
onRatingById(ratedCard.getId(), rating, back);
94+
}
9395

94-
if (ratedCard != null) {
95-
// 1.查询数据库
96-
String query = "SELECT * FROM cards" + " WHERE card_id = ?";
97-
LogUtils.simpleDebug("[CardScheduler] 查询卡片 SQL: " + ratedCard.getId());
98-
99-
try (PreparedStatement ps = this.databaseAdapter.getSqlite().prepare(query)) {
100-
ps.setInt(1, ratedCard.getId());
101-
ResultSet rs = ps.executeQuery();
102-
rs.next();
103-
104-
// 2.使用算法计算
105-
FSRSAlgorithm algorithm = FSRSAlgorithm.builder()
106-
.rating(FSRSRating.values()[rating])
107-
.stability(rs.getFloat("stability"))
108-
.difficulty(rs.getFloat("difficulty"))
109-
.elapsedDays(rs.getInt("elapsed_days"))
110-
.repetitions(rs.getInt("repetitions"))
111-
.state(FSRSState.values()[(rs.getInt("state"))])
112-
.lastReview(rs.getLong("last_review"))
113-
.build();
114-
FSRSAlgorithmResult result = algorithm.calc();
115-
116-
// 3.更新数据库
117-
String update = "UPDATE cards SET repetitions = ?, difficulty = ?, stability = ?, elapsed_days = ?, state = ?, day_interval = ?, next_repetition = ?, last_review = ?, back = ? WHERE card_id = ?";
118-
LogUtils.simpleDebug("[CardScheduler] 更新卡片 SQL: " + ratedCard.getId());
119-
120-
try (PreparedStatement db = this.databaseAdapter.getSqlite().prepare(update)) {
121-
db.setLong(1, result.getRepetitions());
122-
db.setFloat(2, result.getDifficulty());
123-
db.setFloat(3, result.getStability());
124-
db.setInt(4, result.getElapsedDays());
125-
db.setInt(5, result.getState().toInt());
126-
db.setInt(6, result.getInterval());
127-
db.setLong(7, result.getNextRepetitionTime());
128-
db.setLong(8, result.getLastReview());
129-
db.setInt(9, ratedCard.getId());
130-
db.setString(10, back == null ? rs.getString("back") : back);
131-
db.execute();
132-
}
133-
134-
LogUtils.info("[CardScheduler] 卡片 " + " 评分为 " + rating);
135-
} catch (Exception e) {
136-
LogUtils.warn(DebugUtils.getStackTraceAsString(e));
137-
}
96+
public void onRatingById(Integer cardId, Integer rating, String back) {
97+
// 1.查询数据库
98+
String query = "SELECT * FROM cards" + " WHERE card_id = ?";
99+
LogUtils.simpleDebug("[CardScheduler] 查询卡片 SQL: " + cardId);
100+
101+
try (PreparedStatement ps = this.databaseAdapter.getSqlite().prepare(query)) {
102+
ps.setInt(1, cardId);
103+
ResultSet rs = ps.executeQuery();
104+
rs.next();
105+
106+
// 2.使用算法计算
107+
FSRSAlgorithm algorithm = FSRSAlgorithm.builder()
108+
.rating(FSRSRating.values()[rating])
109+
.stability(rs.getFloat("stability"))
110+
.difficulty(rs.getFloat("difficulty"))
111+
.elapsedDays(rs.getInt("elapsed_days"))
112+
.repetitions(rs.getInt("repetitions"))
113+
.state(FSRSState.values()[(rs.getInt("state"))])
114+
.lastReview(rs.getLong("last_review"))
115+
.build();
116+
FSRSAlgorithmResult result = algorithm.calc();
117+
118+
// 3.更新数据库
119+
String update = String.format(
120+
"UPDATE cards SET " +
121+
"repetitions = %d, " +
122+
"difficulty = %.2f, " +
123+
"stability = %.2f, " +
124+
"elapsed_days = %d, " +
125+
"state = %d, " +
126+
"day_interval = %d, " +
127+
"next_repetition = %d, " +
128+
"last_review = %d, " +
129+
"back = '%s' " +
130+
"WHERE card_id = %d",
131+
result.getRepetitions(),
132+
result.getDifficulty(),
133+
result.getStability(),
134+
result.getElapsedDays(),
135+
result.getState().toInt(),
136+
result.getInterval(),
137+
result.getNextRepetitionTime(),
138+
result.getLastReview(),
139+
back == null ? rs.getString("back") : back.replace("'", "''"), // 防止 SQL 注入问题
140+
cardId
141+
);
142+
143+
LogUtils.simpleDebug("[CardScheduler] 更新卡片 SQL: " + update);
144+
this.databaseAdapter.getSqlite().update(update);
145+
146+
LogUtils.simpleDebug("[CardScheduler] 卡片 " + " 评分为 " + rating);
147+
} catch (Exception e) {
148+
LogUtils.warn(DebugUtils.getStackTraceAsString(e));
138149
}
139150
this.queue.dequeue();
140151
if (this.queue.isEmpty()) {

src/main/java/com/xhf/leetcode/plugin/review/backend/model/ReviewQuestion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface ReviewQuestion {
1111
String getLastModify();
1212
String getNextReview();
1313
String getDifficulty();
14-
String getUserSolution();
14+
String getUserNoteText();
1515
void setBack(String newSolution);
1616
Integer getId();
1717
}

src/main/java/com/xhf/leetcode/plugin/review/backend/model/ReviewQuestionModel.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void setUserSolution(String userSolution) {
7474
}
7575

7676
@Override
77-
public String getUserSolution() {
77+
public String getUserNoteText() {
7878
return this.userSolution;
7979
}
8080

@@ -91,4 +91,18 @@ public Integer getId() {
9191
public void setId(Integer id) {
9292
this.id = id;
9393
}
94+
95+
@Override
96+
public String toString() {
97+
return "ReviewQuestionModel{" +
98+
"status='" + status + '\'' +
99+
", title='" + title + '\'' +
100+
", userRate='" + userRate + '\'' +
101+
", lastModify='" + lastModify + '\'' +
102+
", nextReview='" + nextReview + '\'' +
103+
", difficulty='" + difficulty + '\'' +
104+
", userSolution='" + userSolution + '\'' +
105+
", id=" + id +
106+
'}';
107+
}
94108
}

src/main/java/com/xhf/leetcode/plugin/review/backend/service/AlgorithmAPI.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,20 @@ public void updateCardBack(Integer id, String back) {
7979
/**
8080
* 评分
8181
*/
82-
public void rateCard(Integer index, String back) {
82+
public void rateCard(Integer rating, String back) {
8383
// 内存队列操作,无需更新
84-
if(index != -1) {
85-
this.cardScheduler.onRating(index, back);
84+
if(rating != -1) {
85+
this.cardScheduler.onRating(rating, back);
8686
updateQueue();
8787
}
8888
}
8989

90+
public void rateCardByCardId(Integer cardId, Integer rating, String back) {
91+
// todo: 通过id update
92+
this.cardScheduler.onRatingById(cardId, rating, back);
93+
updateQueue();
94+
}
95+
9096
/**
9197
* 获取顶部卡片
9298
*/

src/main/java/com/xhf/leetcode/plugin/review/backend/service/MockRQServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import com.xhf.leetcode.plugin.review.backend.model.QueryDim;
77
import com.xhf.leetcode.plugin.review.backend.model.ReviewQuestion;
88
import com.xhf.leetcode.plugin.review.backend.model.ReviewQuestionModel;
9+
import org.jetbrains.annotations.NotNull;
910

1011
import java.util.ArrayList;
1112
import java.util.List;
12-
import org.jetbrains.annotations.NotNull;
1313

1414
/**
1515
* 模拟数据
@@ -115,6 +115,11 @@ public void rateQuestion(FSRSRating rating, String back) {
115115

116116
}
117117

118+
@Override
119+
public void rateQuestionByCardId(Integer cardId, FSRSRating rating, String back) {
120+
121+
}
122+
118123
@Override
119124
public void createQuestion(Question question, FSRSRating rating, String back) {
120125

src/main/java/com/xhf/leetcode/plugin/review/backend/service/RQServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public void rateQuestion(FSRSRating rating, String back) {
4747
api.rateCard(rating.toInt(), back);
4848
}
4949

50+
@Override
51+
public void rateQuestionByCardId(Integer cardId, FSRSRating rating, String back) {
52+
api.rateCardByCardId(cardId, rating.toInt(), back);
53+
}
54+
5055

5156
@Override
5257
public void createQuestion(Question question, FSRSRating rating, String back) {

src/main/java/com/xhf/leetcode/plugin/review/backend/service/ReviewQuestionService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public interface ReviewQuestionService {
2727
*/
2828
void rateQuestion(FSRSRating rating, String back);
2929

30+
/**
31+
* 为问题评分
32+
* @param cardId
33+
* @param rating
34+
* @param back
35+
*/
36+
void rateQuestionByCardId(Integer cardId, FSRSRating rating, String back);
37+
3038
/**
3139
* 创建问题
3240
* @param question

0 commit comments

Comments
 (0)