forked from intermezzi/777
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.js
379 lines (358 loc) · 13.3 KB
/
UI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"ui";
importClass(android.view.View);
let tikuCommon = require("./tikuCommon.js");
let deviceWidth = device.width;
let margin = parseInt(deviceWidth * 0.02);
//记录集数组 重要!!!
let qaArray = [];
//设置本地存储
let appVersion = "2.5.6-1";
let storage = storages.create("LazyStudy");
if (!storage.contains("delayedTime") || parseInt(storage.get("delayedTime")) < 1) {
storage.put("delayedTime", 1);
}
if (!storage.contains("browseWithCSC") || typeof (storage.get("browseWithCSC")) != "boolean") {
storage.put("browseWithCSC", false);
}
//答题延迟时间
let delayedTime = storage.get("delayedTime");
//三连开关
let browseWithCSC = storage.get("browseWithCSC");
ui.layout(
<drawer id="drawer">
<vertical>
<appbar>
<toolbar id="toolbar" title="懒人学习" />
<tabs id="tabs" />
</appbar>
<viewpager id="viewpager">
<frame>
<img src="https://cdn.mom1.cn/?mom=302" scaleType="centerCrop" alpha="0.3" />
<vertical gravity="center">
<horizontal gravity="center">
<text text="答题延时:" />
<input id="delayedTime" text={delayedTime} />
<text text="秒" />
</horizontal>
<horizontal gravity="center">
<text text="浏览同时收藏分享评论:" />
<Switch id="browseWithCSC" checked={browseWithCSC} />
</horizontal>
<button id="showFloating" text="打开悬浮窗" w="150" h="60" circle="true" layout_gravity="center" style="Widget.AppCompat.Button.Colored" />
</vertical>
</frame>
<frame>
<vertical>
<horizontal gravity="center">
<input margin={margin + "px"} id="keyword" hint=" 输入题目或答案关键字" h="auto" />
<radiogroup orientation="horizontal" >
<radio id="rbQuestion" text="题目" checked="true" />
<radio id="rbAnswer" text="答案" />
</radiogroup>
<button id="search" text=" 搜索 " />
</horizontal>
<horizontal gravity="center">
<button id="lastTen" text=" 最近十条 " />
<button id="prev" text=" 上一条 " />
<button id="next" text=" 下一条 " />
<button id="reset" text=" 重置 " />
</horizontal>
<horizontal gravity="center">
<button id="update" text=" 修改 " />
<button id="delete" text=" 删除 " />
<button id="insert" text=" 新增 " />
<button id="updateTikuNet" text=" 更新题库 " />
</horizontal>
<progressbar id="pbar" indeterminate="true" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" />
<text id="resultLabel" text="" gravity="center" />
<horizontal>
<vertical>
<text id="questionLabel" text="题目" />
<horizontal>
<text id="questionIndex" text="0" />
<text id="slash" text="/" />
<text id="questionCount" text="0" />
</horizontal>
</vertical>
<input margin={margin + "px"} id="question" w="*" h="auto" />
</horizontal>
<horizontal>
<text id="answerLabel" text="答案" />
<input id="answer" w="*" h="auto" />
</horizontal>
</vertical>
</frame>
<frame>
<vertical>
<webview id="webview" h="*" w="auto" />
</vertical>
</frame>
</viewpager>
</vertical>
</drawer>
);
//标签名
ui.viewpager.setTitles(["功能", "题库", "程序介绍"]);
//联动
ui.tabs.setupWithViewPager(ui.viewpager);
//选项菜单右上角
ui.emitter.on("create_options_menu", menu => {
menu.add("检查更新");
menu.add("关于");
});
ui.emitter.on("options_item_selected", (e, item) => {
switch (item.getTitle()) {
case "检查更新":
threads.shutDownAll();
threads.start(function () {
checkUpdate(appVersion,true);
});
//app.openUrl("https://glare.now.sh/lgpersonal/LazyStudy/LazyStudy");
break;
case "关于":
alert("关于", "当前版本 " + appVersion);
break;
}
e.consumed = true;
});
activity.setSupportActionBar(ui.toolbar);
//帮助页加载
let src = "https://github.com/lgpersonal/LazyStudy/blob/master/README.md";
ui.webview.loadUrl(src);
//进度条不可见
ui.run(() => {
ui.pbar.setVisibility(View.INVISIBLE);
});
//加载悬浮窗
ui.showFloating.click(() => {
//获取UI中输入值
let uiDelayedTime = parseInt(ui.delayedTime.getText());
let uiBrowseWithCSC = ui.browseWithCSC.checked;
//判断并存储
if (uiDelayedTime >= 0 && uiDelayedTime != delayedTime) {
storage.put("delayedTime", uiDelayedTime);
}
if (uiBrowseWithCSC != browseWithCSC) {
storage.put("browseWithCSC", uiBrowseWithCSC);
}
console.log("延迟时间: " + storage.get("delayedTime"));
console.log("三连开关: " + storage.get("browseWithCSC"));
engines.execScriptFile("floating.js");
});
//查询
ui.search.click(() => {
//预先初始化
qaArray = [];
threads.shutDownAll();
ui.run(() => {
ui.question.setText("");
ui.answer.setText("");
ui.questionIndex.setText("0");
ui.questionCount.setText("0");
});
//查询开始
threads.start(function () {
if (ui.keyword.getText() != "") {
let keyw = ui.keyword.getText();
let sqlStr=null;
if (ui.rbQuestion.checked) {//按题目搜
sqlStr = util.format("SELECT question,answer FROM tiku WHERE %s LIKE '%%%s%'", "question", keyw);
} else {//按答案搜
sqlStr = util.format("SELECT question,answer FROM tiku WHERE %s LIKE '%%%s%'", "answer", keyw);
}
qaArray = tikuCommon.searchDb(keyw, "tiku", sqlStr);
let qCount = qaArray.length;
if (qCount > 0) {
ui.run(() => {
ui.question.setText(qaArray[0].question);
ui.answer.setText(qaArray[0].answer);
ui.questionIndex.setText("1");
ui.questionCount.setText(String(qCount));
});
} else {
toastLog("未找到");
ui.run(() => {
ui.question.setText("未找到");
});
}
} else {
toastLog("请输入关键字");
}
});
});
//最近十条
ui.lastTen.click(() => {
threads.start(function () {
let keyw = ui.keyword.getText();
qaArray = tikuCommon.searchDb(keyw, "", "SELECT question,answer FROM tiku ORDER BY rowid DESC limit 10");
let qCount = qaArray.length;
if (qCount > 0) {
//toastLog(qCount);
ui.run(() => {
ui.question.setText(qaArray[0].question);
ui.answer.setText(qaArray[0].answer);
ui.questionIndex.setText("1");
ui.questionCount.setText(qCount.toString());
});
} else {
toastLog("未找到");
ui.run(() => {
ui.question.setText("未找到");
});
}
});
});
//上一条
ui.prev.click(() => {
threads.start(function () {
if (qaArray.length > 0) {
let qIndex = parseInt(ui.questionIndex.getText()) - 1;
if (qIndex > 0) {
ui.run(() => {
ui.question.setText(qaArray[qIndex - 1].question);
ui.answer.setText(qaArray[qIndex - 1].answer);
ui.questionIndex.setText(String(qIndex));
});
} else {
toastLog("已经是第一条了!");
}
} else {
toastLog("题目为空");
}
});
});
//下一条
ui.next.click(() => {
threads.start(function () {
if (qaArray.length > 0) {
//toastLog(qaArray);
let qIndex = parseInt(ui.questionIndex.getText()) - 1;
if (qIndex < qaArray.length - 1) {
//toastLog(qIndex);
//toastLog(qaArray[qIndex + 1].question);
ui.run(() => {
ui.question.setText(qaArray[qIndex + 1].question);
ui.answer.setText(qaArray[qIndex + 1].answer);
ui.questionIndex.setText(String(qIndex + 2));
});
} else {
toastLog("已经是最后一条了!");
}
} else {
toastLog("题目为空");
}
});
});
//修改
ui.update.click(() => {
threads.start(function () {
if (ui.question.getText() && qaArray.length > 0 && parseInt(ui.questionIndex.getText()) > 0) {
let qIndex = parseInt(ui.questionIndex.getText()) - 1;
let questionOld = qaArray[qIndex].question;
let questionStr = ui.question.getText();
let answerStr = ui.answer.getText();
let sqlstr = "UPDATE tiku SET question = '" + questionStr + "' , answer = '" + answerStr + "' WHERE question= '" + questionOld + "'";
tikuCommon.executeSQL(sqlstr);
} else {
toastLog("请先查询");
}
});
});
//删除
ui.delete.click(() => {
threads.start(function () {
if (qaArray.length > 0 && parseInt(ui.questionIndex.getText()) > 0) {
let qIndex = parseInt(ui.questionIndex.getText()) - 1;
let questionOld = qaArray[qIndex].question;
let sqlstr = "DELETE FROM tiku WHERE question = '" + questionOld + "'";
tikuCommon.executeSQL(sqlstr);
} else {
toastLog("请先查询");
}
});
});
//新增
ui.insert.click(() => {
threads.start(function () {
if (ui.question.getText() != "" && ui.answer.getText() != "") {
let questionStr = ui.question.getText();
let answerStr = ui.answer.getText();
let sqlstr = "INSERT INTO tiku VALUES ('" + questionStr + "','" + answerStr + "','')";
tikuCommon.executeSQL(sqlstr);
} else {
toastLog("请先输入 问题 答案");
}
});
});
//重置
ui.reset.click(() => {
threads.shutDownAll();
threads.start(function () {
qaArray = [];
ui.run(() => {
ui.keyword.setText("");
ui.question.setText("");
ui.answer.setText("");
ui.questionIndex.setText("0");
ui.questionCount.setText("0");
ui.rbQuestion.setChecked(true);
});
toastLog("重置完毕!");
});
});
//更新网络题库
ui.updateTikuNet.click(() => {
dialogs.build({
title: "更新网络题库",
content: "确定更新?",
positive: "确定",
negative: "取消",
})
.on("positive", update)
.show();
function update() {
threads.start(function () {
ui.run(() => {
ui.resultLabel.setText("正在更新网络题库...");
ui.pbar.setVisibility(View.VISIBLE);
});
let ss = "./updateTikuNet.js";
let begin = require(ss);
let resultNum = begin();
let resultStr = "更新" + resultNum + "道题!";
ui.run(() => {
ui.resultLabel.setText("");
ui.pbar.setVisibility(View.INVISIBLE);
ui.resultLabel.setVisibility(View.INVISIBLE);
});
alert(resultStr);
});
}
});
//
function checkUpdate(appVersion, alertFlag) {
//let appVersion = "2.5.4"
let r = http.get("https://api.github.com/repos/lgpersonal/LazyStudy/releases/latest");
let rjson = r.body.json();
let remoteAppVersion = rjson.tag_name;
let remoteAppDownloadUrl = rjson.assets[0].browser_download_url;
if (appVersion != remoteAppVersion && remoteAppDownloadUrl != "") {
// toastLog("发现新版本: " + remoteAppVersion + "\n下载地址: " + remoteAppDownloadUrl);
dialogs.build({
title: "发现新版本",
content: remoteAppVersion,
positive: "更新",
negative: "取消"
})
.on("positive", () => {
app.openUrl(remoteAppDownloadUrl);
})
.show();
};
if (appVersion == remoteAppVersion && alertFlag) {
alert("已经是最新版本 " + appVersion);
};
};
threads.start(function () {
checkUpdate(appVersion, false);
});