Skip to content

Commit 51573b8

Browse files
committed
add Progress
1 parent 49e5030 commit 51573b8

File tree

10 files changed

+562
-1
lines changed

10 files changed

+562
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.shuzijun.leetcode.plugin.actions.toolbar;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.intellij.openapi.application.ApplicationManager;
5+
import com.intellij.openapi.project.Project;
6+
import com.intellij.openapi.ui.DialogWrapper;
7+
import com.shuzijun.leetcode.plugin.actions.AbstractAction;
8+
import com.shuzijun.leetcode.plugin.manager.SessionManager;
9+
import com.shuzijun.leetcode.plugin.manager.ViewManager;
10+
import com.shuzijun.leetcode.plugin.model.Config;
11+
import com.shuzijun.leetcode.plugin.model.Session;
12+
import com.shuzijun.leetcode.plugin.utils.DataKeys;
13+
import com.shuzijun.leetcode.plugin.utils.HttpRequestUtils;
14+
import com.shuzijun.leetcode.plugin.utils.MessageUtils;
15+
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
16+
import com.shuzijun.leetcode.plugin.window.ProgressPanel;
17+
import com.shuzijun.leetcode.plugin.window.WindowFactory;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
import javax.swing.*;
21+
import java.util.List;
22+
import java.util.concurrent.atomic.AtomicReference;
23+
24+
/**
25+
* @author shuzijun
26+
*/
27+
public class ProgressAction extends AbstractAction {
28+
29+
@Override
30+
public void actionPerformed(AnActionEvent anActionEvent, Config config) {
31+
if (!HttpRequestUtils.isLogin()) {
32+
MessageUtils.getInstance(anActionEvent.getProject()).showWarnMsg("info", PropertiesUtils.getInfo("login.not"));
33+
return;
34+
}
35+
36+
List<Session> sessionList = SessionManager.getSession(anActionEvent.getProject());
37+
if (sessionList.isEmpty()) {
38+
return;
39+
}
40+
AtomicReference<Session> sessionAtomic = new AtomicReference<>();
41+
ApplicationManager.getApplication().invokeAndWait(() -> {
42+
ProgressDialogPanel dialogPanel = new ProgressDialogPanel(anActionEvent.getProject(), sessionList);
43+
dialogPanel.setTitle("Progress");
44+
if (dialogPanel.showAndGet()) {
45+
sessionAtomic.set(dialogPanel.select());
46+
}
47+
});
48+
if (sessionAtomic.get() != null) {
49+
Session session = sessionAtomic.get();
50+
if (session.getId() == null) {
51+
return;
52+
} else {
53+
if (SessionManager.switchSession(anActionEvent.getProject(), session.getId())) {
54+
JTree tree = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_TREE);
55+
ViewManager.loadServiceData(tree, anActionEvent.getProject());
56+
actionPerformed(anActionEvent, config);
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
private class ProgressDialogPanel extends DialogWrapper {
64+
65+
private JPanel jpanel;
66+
private ProgressPanel progressPanel;
67+
68+
public ProgressDialogPanel(@Nullable Project project, List<Session> sessionList) {
69+
super(project, true);
70+
progressPanel = new ProgressPanel(sessionList, project);
71+
jpanel = progressPanel.getPanel();
72+
setModal(true);
73+
init();
74+
}
75+
76+
@Nullable
77+
@Override
78+
protected JComponent createCenterPanel() {
79+
return jpanel;
80+
}
81+
82+
public Session select() {
83+
return progressPanel.select();
84+
}
85+
86+
87+
}
88+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.shuzijun.leetcode.plugin.manager;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONArray;
5+
import com.alibaba.fastjson.JSONObject;
6+
import com.intellij.openapi.project.Project;
7+
import com.shuzijun.leetcode.plugin.model.Session;
8+
import com.shuzijun.leetcode.plugin.utils.*;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* @author shuzijun
15+
*/
16+
public class SessionManager {
17+
18+
public static List<Session> getSession(Project project) {
19+
List<Session> sessionList = new ArrayList<>();
20+
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeProgress());
21+
HttpResponse httpResponse = HttpRequestUtils.executeGet(httpRequest);
22+
if (httpResponse.getStatusCode() == 200) {
23+
24+
Session defSession = new Session();
25+
JSONObject jsonObject = JSON.parseObject(httpResponse.getBody());
26+
defSession.setName(jsonObject.getString("sessionName"));
27+
defSession.setXP(jsonObject.getInteger("XP"));
28+
defSession.setSolvedTotal(jsonObject.getInteger("solvedTotal"));
29+
defSession.setQuestionTotal(jsonObject.getInteger("questionTotal"));
30+
defSession.setAttempted(jsonObject.getInteger("attempted"));
31+
defSession.setPoint(jsonObject.getInteger("leetCoins"));
32+
defSession.setUnsolved(jsonObject.getInteger("unsolved"));
33+
defSession.setEasy(jsonObject.getJSONObject("solvedPerDifficulty").getInteger("Easy"));
34+
defSession.setMedium(jsonObject.getJSONObject("solvedPerDifficulty").getInteger("Medium"));
35+
defSession.setHard(jsonObject.getJSONObject("solvedPerDifficulty").getInteger("Hard"));
36+
sessionList.add(defSession);
37+
38+
JSONArray jsonArray = jsonObject.getJSONArray("sessionList");
39+
for (int i = 0; i < jsonArray.size(); i++) {
40+
Session session = new Session();
41+
session.setId(jsonArray.getJSONObject(i).getInteger("id"));
42+
session.setName(jsonArray.getJSONObject(i).getString("name"));
43+
sessionList.add(session);
44+
}
45+
46+
} else {
47+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
48+
}
49+
return sessionList;
50+
}
51+
52+
public static boolean switchSession(Project project, Integer id) {
53+
54+
HttpRequest httpRequest = HttpRequest.put(URLUtils.getLeetcodeSession(), "application/json");
55+
httpRequest.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
56+
httpRequest.addHeader("x-requested-with", "XMLHttpRequest");
57+
58+
httpRequest.setBody("{\"func\":\"activate\",\"target\":" + id + "}");
59+
60+
HttpResponse httpResponse = HttpRequestUtils.executePut(httpRequest);
61+
if (httpResponse.getStatusCode() == 200) {
62+
return true;
63+
} else {
64+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
65+
return false;
66+
}
67+
}
68+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.shuzijun.leetcode.plugin.model;
2+
3+
/**
4+
* @author shuzijun
5+
*/
6+
public class Session {
7+
8+
/**
9+
* id
10+
*/
11+
private Integer id;
12+
13+
/**
14+
* 名称
15+
*/
16+
private String name;
17+
18+
/**
19+
* 解决
20+
*/
21+
private Integer solvedTotal;
22+
23+
/**
24+
* 总数
25+
*/
26+
private Integer questionTotal;
27+
28+
/**
29+
* 尝试
30+
*/
31+
private Integer attempted;
32+
33+
/**
34+
* 待解决
35+
*/
36+
private Integer unsolved;
37+
38+
/**
39+
* 经验
40+
*/
41+
private Integer XP;
42+
43+
/**
44+
* 币
45+
*/
46+
private Integer point;
47+
48+
/**
49+
* 难度
50+
*/
51+
private Integer easy;
52+
53+
/**
54+
* 难度
55+
*/
56+
private Integer medium;
57+
58+
/**
59+
* 难度
60+
*/
61+
private Integer hard;
62+
63+
64+
public Integer getId() {
65+
return id;
66+
}
67+
68+
public void setId(Integer id) {
69+
this.id = id;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
76+
public void setName(String name) {
77+
if (name == null || name.trim().length() == 0) {
78+
this.name = "Anonymous Session";
79+
} else {
80+
this.name = name;
81+
}
82+
}
83+
84+
public Integer getSolvedTotal() {
85+
return solvedTotal;
86+
}
87+
88+
public void setSolvedTotal(Integer solvedTotal) {
89+
this.solvedTotal = solvedTotal;
90+
}
91+
92+
public Integer getQuestionTotal() {
93+
return questionTotal;
94+
}
95+
96+
public void setQuestionTotal(Integer questionTotal) {
97+
this.questionTotal = questionTotal;
98+
}
99+
100+
public Integer getAttempted() {
101+
return attempted;
102+
}
103+
104+
public void setAttempted(Integer attempted) {
105+
this.attempted = attempted;
106+
}
107+
108+
public Integer getUnsolved() {
109+
return unsolved;
110+
}
111+
112+
public void setUnsolved(Integer unsolved) {
113+
this.unsolved = unsolved;
114+
}
115+
116+
public Integer getXP() {
117+
return XP;
118+
}
119+
120+
public void setXP(Integer XP) {
121+
this.XP = XP;
122+
}
123+
124+
public Integer getPoint() {
125+
return point;
126+
}
127+
128+
public void setPoint(Integer point) {
129+
this.point = point;
130+
}
131+
132+
public Integer getEasy() {
133+
return easy;
134+
}
135+
136+
public void setEasy(Integer easy) {
137+
this.easy = easy;
138+
}
139+
140+
public Integer getMedium() {
141+
return medium;
142+
}
143+
144+
public void setMedium(Integer medium) {
145+
this.medium = medium;
146+
}
147+
148+
public Integer getHard() {
149+
return hard;
150+
}
151+
152+
public void setHard(Integer hard) {
153+
this.hard = hard;
154+
}
155+
156+
@Override
157+
public String toString() {
158+
return name;
159+
}
160+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public static HttpRequest post(String url, String contentType) {
2727
return new HttpRequest(url, contentType);
2828
}
2929

30+
public static HttpRequest put(String url, String contentType) {
31+
return new HttpRequest(url, contentType);
32+
}
33+
3034
private HttpRequest(String url, String contentType) {
3135
this.url = url;
3236
this.contentType = contentType;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public static HttpResponse executePost(HttpRequest httpRequest) {
5151
return httpResponse;
5252
}
5353

54+
public static HttpResponse executePut(HttpRequest httpRequest) {
55+
HttpResponse httpResponse = new HttpResponse();
56+
try {
57+
HttpRequests.put(httpRequest.getUrl(), httpRequest.getContentType())
58+
.throwStatusCodeException(false)
59+
.tuner(new HttpRequestTuner(httpRequest))
60+
.connect(new HttpResponseProcessor(httpRequest, httpResponse));
61+
} catch (IOException e) {
62+
LogUtils.LOG.error("HttpRequestUtils request error:", e);
63+
httpResponse.setStatusCode(-1);
64+
}
65+
return httpResponse;
66+
}
67+
5468
public static String getToken() {
5569
if (cookieManager.getCookieStore().getCookies() == null) {
5670
return null;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class URLUtils {
2222
private static String leetcodeTags = "/problems/api/tags/";
2323
private static String leetcodeFavorites = "/problems/api/favorites/";
2424
private static String leetcodeVerify = "/problemset/all/";
25+
private static String leetcodeProgress = "/api/progress/all/";
26+
private static String leetcodeSession = "/session/";
2527

2628
public static String getLeetcodeHost() {
2729
String host = PersistentConfig.getInstance().getConfig().getUrl();
@@ -75,6 +77,15 @@ public static String getLeetcodeVerify() {
7577
return getLeetcodeUrl() + leetcodeVerify;
7678
}
7779

80+
public static String getLeetcodeProgress(){
81+
return getLeetcodeUrl() + leetcodeProgress;
82+
}
83+
84+
public static String getLeetcodeSession(){
85+
return getLeetcodeUrl() + leetcodeSession;
86+
}
87+
88+
7889
public static String getDescContent() {
7990
if ("leetcode.com".equals(getLeetcodeHost()) || PersistentConfig.getInstance().getConfig().getEnglishContent()) {
8091
return "content";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.shuzijun.leetcode.plugin.utils;
22

3-
import org.apache.commons.lang.StringUtils;
3+
import org.apache.commons.lang3.StringUtils;
44

55
/**
66
* @author shuzijun

0 commit comments

Comments
 (0)