Skip to content

Commit 8bacd2b

Browse files
authored
Merge pull request #194 from shuzijun/gradle
Add screening criteria
2 parents 238a0d5 + 1b93601 commit 8bacd2b

File tree

8 files changed

+116
-17
lines changed

8 files changed

+116
-17
lines changed

src/main/java/com/shuzijun/leetcode/plugin/actions/toolbar/FindActionGroup.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public void update(AnActionEvent e) {
3434
}
3535
}
3636
}
37-
3837
e.getPresentation().setIcon(null);
39-
4038
}
4139

4240

@@ -51,7 +49,11 @@ public AnAction[] getChildren(AnActionEvent anActionEvent) {
5149

5250
if (tags != null && !tags.isEmpty()) {
5351
for (Tag tag : tags) {
54-
anActionList.add(new FindTagAction(tag.getName(), tag));
52+
if ("leetcode.find.Category".equals(id)) {
53+
anActionList.add(new FindTagAction(tag.getName(), tag, true));
54+
}else {
55+
anActionList.add(new FindTagAction(tag.getName(), tag));
56+
}
5557
}
5658
}
5759
AnAction[] anActions = new AnAction[anActionList.size()];
@@ -69,6 +71,8 @@ private List<Tag> getTags(String id) {
6971
tags = ViewManager.getFilter(Constant.FIND_TYPE_LISTS);
7072
} else if ("leetcode.find.Tags".equals(id)) {
7173
tags = ViewManager.getFilter(Constant.FIND_TYPE_TAGS);
74+
} else if ("leetcode.find.Category".equals(id)) {
75+
tags = ViewManager.getFilter(Constant.FIND_TYPE_CATEGORY);
7276
}
7377

7478
return tags;

src/main/java/com/shuzijun/leetcode/plugin/actions/toolbar/FindClearAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public void actionPerformed(AnActionEvent anActionEvent, Config config) {
2222
if (tree == null) {
2323
return;
2424
}
25-
ViewManager.clearFilter();
26-
ViewManager.update(tree);
25+
boolean isLoad = ViewManager.clearFilter();
26+
if (isLoad) {
27+
ViewManager.loadServiceData(tree,anActionEvent.getProject());
28+
} else {
29+
ViewManager.update(tree);
30+
}
2731
}
2832
}

src/main/java/com/shuzijun/leetcode/plugin/actions/toolbar/FindTagAction.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import com.intellij.openapi.actionSystem.AnActionEvent;
44
import com.intellij.openapi.actionSystem.ToggleAction;
5+
import com.intellij.openapi.progress.ProgressIndicator;
6+
import com.intellij.openapi.progress.ProgressManager;
7+
import com.intellij.openapi.progress.Task;
58
import com.shuzijun.leetcode.plugin.manager.ViewManager;
69
import com.shuzijun.leetcode.plugin.model.Tag;
710
import com.shuzijun.leetcode.plugin.utils.DataKeys;
811
import com.shuzijun.leetcode.plugin.window.WindowFactory;
12+
import org.jetbrains.annotations.NotNull;
913
import org.jetbrains.annotations.Nullable;
1014

1115
import javax.swing.*;
@@ -17,11 +21,19 @@ public class FindTagAction extends ToggleAction {
1721

1822
private Tag tag;
1923

24+
private boolean againLoad = false;
25+
2026
public FindTagAction(@Nullable String text, Tag tag) {
2127
super(text);
2228
this.tag = tag;
2329
}
2430

31+
public FindTagAction(@Nullable String text, Tag tag, boolean againLoad) {
32+
super(text);
33+
this.tag = tag;
34+
this.againLoad = againLoad;
35+
}
36+
2537
@Override
2638
public boolean isSelected(AnActionEvent anActionEvent) {
2739
return tag.isSelect();
@@ -34,7 +46,22 @@ public void setSelected(AnActionEvent anActionEvent, boolean b) {
3446
if (tree == null) {
3547
return;
3648
}
37-
ViewManager.update(tree);
49+
if (againLoad) {
50+
ProgressManager.getInstance().run(new Task.Backgroundable(anActionEvent.getProject(), "leetcode.editor." + tag.getName(), false) {
51+
@Override
52+
public void run(@NotNull ProgressIndicator progressIndicator) {
53+
if (b) {
54+
ViewManager.loadServiceData(tree, anActionEvent.getProject(), tag.getType());
55+
} else {
56+
ViewManager.loadServiceData(tree, anActionEvent.getProject());
57+
}
58+
59+
}
60+
});
61+
62+
} else {
63+
ViewManager.update(tree);
64+
}
3865
}
3966

4067

src/main/java/com/shuzijun/leetcode/plugin/manager/QuestionManager.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public class QuestionManager {
3232
private final static String TRANSLATIONNAME = "translation.json";
3333

3434

35-
public static List<Question> getQuestionService(Project project) {
35+
public static List<Question> getQuestionService(Project project, String url) {
3636
List<Question> questionList = null;
3737

38-
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeAll());
38+
HttpRequest httpRequest = HttpRequest.get(url);
3939
HttpResponse response = HttpRequestUtils.executeGet(httpRequest);
4040
if (response != null && response.getStatusCode() == 200) {
4141
questionList = parseQuestion(response.getBody());
@@ -175,6 +175,24 @@ public static List<Tag> getLists() {
175175
return tags;
176176
}
177177

178+
public static List<Tag> getCategory(String url) {
179+
List<Tag> tags = new ArrayList<>();
180+
181+
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeCardInfo());
182+
HttpResponse response = HttpRequestUtils.executeGet(httpRequest);
183+
if (response != null && response.getStatusCode() == 200) {
184+
try {
185+
String body = response.getBody();
186+
tags = parseCategory(body, url);
187+
} catch (Exception e1) {
188+
LogUtils.LOG.error("Request CardInfo exception", e1);
189+
}
190+
} else {
191+
LogUtils.LOG.error("Request CardInfo failed, status:" + response.getStatusCode() + "body:" + response.getBody());
192+
}
193+
return tags;
194+
}
195+
178196

179197
private static List<Question> parseQuestion(String str) {
180198

@@ -202,18 +220,18 @@ private static List<Question> parseQuestion(String str) {
202220
question.setTitleSlug(object.getJSONObject("stat").getString("question__title_slug"));
203221
question.setLevel(object.getJSONObject("difficulty").getInteger("level"));
204222
try {
205-
if(object.getJSONObject("stat").containsKey("question__article__live")) {
223+
if (object.getJSONObject("stat").containsKey("question__article__live")) {
206224
if (object.getJSONObject("stat").get("question__article__live") == null
207225
|| !object.getJSONObject("stat").getBoolean("question__article__live")) {
208226
question.setArticleLive(Constant.ARTICLE_LIVE_NONE);
209227
} else {
210228
question.setArticleLive(Constant.ARTICLE_LIVE_ONE);
211229
question.setArticleSlug(object.getJSONObject("stat").getString("question__title_slug"));
212230
}
213-
}else {
231+
} else {
214232
question.setArticleLive(Constant.ARTICLE_LIVE_LIST);
215233
}
216-
}catch (Exception e){
234+
} catch (Exception e) {
217235
LogUtils.LOG.error("Identify abnormal article", e);
218236
question.setArticleLive(Constant.ARTICLE_LIVE_NONE);
219237
}
@@ -251,7 +269,7 @@ private static void translation(List<Question> questions) {
251269
String filePathTranslation = PersistentConfig.getInstance().getTempFilePath() + TRANSLATIONNAME;
252270

253271
try {
254-
HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(),"application/json");
272+
HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(), "application/json");
255273
httpRequest.setBody("{\"operationName\":\"getQuestionTranslation\",\"variables\":{},\"query\":\"query getQuestionTranslation($lang: String) {\\n translations: allAppliedQuestionTranslations(lang: $lang) {\\n title\\n questionId\\n __typename\\n }\\n}\\n\"}");
256274
httpRequest.addHeader("Accept", "application/json");
257275
HttpResponse response = HttpRequestUtils.executePost(httpRequest);
@@ -312,6 +330,27 @@ private static List<Tag> parseTag(String str) {
312330
return tags;
313331
}
314332

333+
private static List<Tag> parseCategory(String str, String url) {
334+
List<Tag> tags = new ArrayList<Tag>();
335+
336+
if (StringUtils.isNotBlank(str)) {
337+
338+
JSONArray jsonArray = JSONArray.parseObject(str).getJSONObject("categories").getJSONArray("0");
339+
for (int i = 0; i < jsonArray.size(); i++) {
340+
JSONObject object = jsonArray.getJSONObject(i);
341+
Tag tag = new Tag();
342+
tag.setSlug(object.getString("slug"));
343+
tag.setType(URLUtils.getLeetcodeUrl() + "/api" + object.getString("url").replace("problemset","problems"));
344+
tag.setName(object.getString("title"));
345+
if(url.contains(tag.getType())){
346+
tag.setSelect(true);
347+
}
348+
tags.add(tag);
349+
}
350+
}
351+
return tags;
352+
}
353+
315354
private static List<Tag> parseList(String str) {
316355
List<Tag> tags = new ArrayList<Tag>();
317356

src/main/java/com/shuzijun/leetcode/plugin/manager/ViewManager.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.shuzijun.leetcode.plugin.model.Tag;
1212
import com.shuzijun.leetcode.plugin.utils.MessageUtils;
1313
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
14+
import com.shuzijun.leetcode.plugin.utils.URLUtils;
1415
import com.shuzijun.leetcode.plugin.window.WindowFactory;
1516

1617
import javax.swing.*;
@@ -35,7 +36,11 @@ public class ViewManager {
3536
private static boolean intersection = Boolean.FALSE;
3637

3738
public static void loadServiceData(JTree tree, Project project) {
38-
List<Question> questionList = QuestionManager.getQuestionService(project);
39+
loadServiceData(tree, project, URLUtils.getLeetcodeAll());
40+
}
41+
42+
public static void loadServiceData(JTree tree, Project project, String url) {
43+
List<Question> questionList = QuestionManager.getQuestionService(project, url);
3944
if (questionList == null || questionList.isEmpty()) {
4045
MessageUtils.getInstance(project).showWarnMsg("warning", PropertiesUtils.getInfo("response.cache"));
4146
questionList = QuestionManager.getQuestionCache();
@@ -56,6 +61,8 @@ public String apply(Question question) {
5661
filter.put(Constant.FIND_TYPE_STATUS, QuestionManager.getStatus());
5762
filter.put(Constant.FIND_TYPE_LISTS, QuestionManager.getLists());
5863
filter.put(Constant.FIND_TYPE_TAGS, QuestionManager.getTags());
64+
filter.put(Constant.FIND_TYPE_CATEGORY, QuestionManager.getCategory(url));
65+
5966

6067
DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
6168
DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
@@ -83,12 +90,18 @@ public static List<Tag> getFilter(String key) {
8390
return filter.get(key);
8491
}
8592

86-
public static void clearFilter() {
87-
for (List<Tag> tagList : filter.values()) {
93+
public static boolean clearFilter() {
94+
boolean isLoad = false;
95+
for (String key : filter.keySet()) {
96+
List<Tag> tagList = filter.get(key);
8897
for (Tag tag : tagList) {
98+
if (tag.isSelect() && Constant.FIND_TYPE_CATEGORY.equals(key)) {
99+
isLoad = true;
100+
}
89101
tag.setSelect(Boolean.FALSE);
90102
}
91103
}
104+
return isLoad;
92105
}
93106

94107
public static void updateStatus() {
@@ -105,7 +118,11 @@ public static void setIntersection(boolean intersection) {
105118

106119
public static void update(JTree tree) {
107120
TreeSet<String> selectQuestionList = null;
108-
for (List<Tag> tagList : filter.values()) {
121+
for (String key : filter.keySet()) {
122+
if (Constant.FIND_TYPE_CATEGORY.equals(key)) {
123+
continue;
124+
}
125+
List<Tag> tagList = filter.get(key);
109126
TreeSet<String> tagQuestionList = null;
110127
for (Tag tag : tagList) {
111128
if (tag.isSelect()) {
@@ -263,7 +280,7 @@ public static void position(JTree tree, JBScrollPane scrollPane, Question questi
263280
for (int i = 0, j = node.getChildCount(); i < j; i++) {
264281
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
265282
Question nodeData = (Question) childNode.getUserObject();
266-
if(nodeData.getQuestionId().equals(question.getQuestionId())){
283+
if (nodeData.getQuestionId().equals(question.getQuestionId())) {
267284
TreePath toShowPath = new TreePath(childNode.getPath());
268285
tree.setSelectionPath(toShowPath);
269286
Rectangle bounds = tree.getPathBounds(toShowPath);

src/main/java/com/shuzijun/leetcode/plugin/model/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class Constant {
4545
public static final String FIND_TYPE_STATUS = "Status";
4646
public static final String FIND_TYPE_LISTS = "Lists";
4747
public static final String FIND_TYPE_TAGS = "Tags";
48+
public static final String FIND_TYPE_CATEGORY = "Category";
4849

4950
/**
5051
* 状态类型

src/main/java/com/shuzijun/leetcode/plugin/utils/URLUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class URLUtils {
2424
private static String leetcodeVerify = "/problemset/all/";
2525
private static String leetcodeProgress = "/api/progress/all/";
2626
private static String leetcodeSession = "/session/";
27+
private static String leetcodeCardInfo = "/problems/api/card-info/";
2728

2829
public static String getLeetcodeHost() {
2930
String host = PersistentConfig.getInstance().getConfig().getUrl();
@@ -85,6 +86,9 @@ public static String getLeetcodeSession(){
8586
return getLeetcodeUrl() + leetcodeSession;
8687
}
8788

89+
public static String getLeetcodeCardInfo(){
90+
return getLeetcodeUrl() + leetcodeCardInfo;
91+
}
8892

8993
public static String getDescContent() {
9094
if ("leetcode.com".equals(getLeetcodeHost()) || PersistentConfig.getInstance().getConfig().getEnglishContent()) {

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@
476476
</group>
477477

478478
<group id="leetcode.find.Toolbar" popup="true" text="Status" description="Status">
479+
<group id="leetcode.find.Category" class="com.shuzijun.leetcode.plugin.actions.toolbar.FindActionGroup"
480+
popup="true" text="Category" description="Category">
481+
</group>
479482
<group id="leetcode.find.Difficulty" class="com.shuzijun.leetcode.plugin.actions.toolbar.FindActionGroup"
480483
popup="true" text="Difficulty" description="Difficulty">
481484
</group>

0 commit comments

Comments
 (0)