forked from lolisaikou/LazyStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.js
354 lines (331 loc) · 12.5 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
"ui";
importClass(android.view.View);
var tikuCommon = require("./tikuCommon.js");
let deviceWidth = device.width;
let margin = parseInt(deviceWidth * 0.02);
//记录集数组 重要!!!
let qaArray = [];
ui.layout(
<drawer id="drawer">
<vertical>
<appbar>
<toolbar id="toolbar" title="懒人学习" />
<tabs id="tabs" />
</appbar>
<viewpager id="viewpager">
<frame>
<img src={"https://api.ixiaowai.cn/gqapi/gqapi.php?" + new Date().getTime()} scaleType="centerCrop" alpha="0.2" />
<text textSize="16sp" textColor="red" text="v2.7.2" />
<button id="amsw" text="阅读模式选择" layout_gravity="right|top" w="auto" h="auto" circle="true"/>
<button id="showFloating" text="打开悬浮窗" w="150" h="60" circle="true" layout_gravity="center" style="Widget.AppCompat.Button.Colored" />
</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>
<horizontal gravity="center">
<button id="daochu" text="导出文章列表" />
<button id="daoru" text="导入文章列表" />
<button id="listdel" text="清空文章列表" />
</horizontal>
</vertical>
</frame>
<frame>
<vertical>
<webview id="webview" h="*" w="auto" />
</vertical>
</frame>
</viewpager>
</vertical>
</drawer>
);
//标签名
ui.viewpager.setTitles(["功能", "题库", "帮助与更新"]);
//联动
ui.tabs.setupWithViewPager(ui.viewpager);
//帮助页加载
var src = "https://github.com/lolisaikou/LazyStudy/blob/master/README.md";
ui.webview.loadUrl(src);
//进度条不可见
ui.run(() => {
ui.pbar.setVisibility(View.INVISIBLE);
});
//阅读模式切换
ui.amsw.click(() => {
var amode = files.read("./article.txt");
toastLog("当前阅读模式为“" + amode + "”")
dialogs.select("请选择文章阅读模式:", ["推荐", "订阅"])
.then(i => {
if (i == 0) {
files.write("./article.txt", "推荐")
toastLog("阅读模式已改为推荐!")
} else if (i == 1) {
files.write("./article.txt", "订阅")
toastLog("阅读模式已改为订阅!")
} else {
toastLog("你没有选择!")
}
});
});
//加载悬浮窗
ui.showFloating.click(() => {
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() != "") {
var keyw = ui.keyword.getText();
if (ui.rbQuestion.checked) {//按题目搜
var sqlStr = util.format("SELECT question,answer FROM tiku WHERE %s LIKE '%%%s%'", "question", keyw);
} else {//按答案搜
var sqlStr = util.format("SELECT question,answer FROM tiku WHERE %s LIKE '%%%s%'", "answer", keyw);
}
qaArray = tikuCommon.searchDb(keyw, "tiku", sqlStr);
var 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 () {
var keyw = ui.keyword.getText();
qaArray = tikuCommon.searchDb(keyw, "", "SELECT question,answer FROM tiku ORDER BY rowid DESC limit 10");
var 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) {
var 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);
var 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) {
var qIndex = parseInt(ui.questionIndex.getText()) - 1;
var questionOld = qaArray[qIndex].question;
var questionStr = ui.question.getText();
var answerStr = ui.answer.getText();
var 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) {
var qIndex = parseInt(ui.questionIndex.getText()) - 1;
var questionOld = qaArray[qIndex].question;
var 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() != "") {
var questionStr = ui.question.getText();
var answerStr = ui.answer.getText();
var sqlstr = "INSERT INTO tiku VALUES ('" + questionStr + "','" + answerStr + "','')";
tikuCommon.executeSQL(sqlstr);
} else {
toastLog("请先输入 问题 答案");
}
});
});
function reset() {
}
//重置
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);
});
var ss = "./updateTikuNet.js";
let begin = require(ss);
var resultNum = begin();
var resultStr = "更新了" + resultNum + "道题!";
log("更新了" + resultNum + "道题!")
ui.run(() => {
ui.resultLabel.setText("");
ui.pbar.setVisibility(View.INVISIBLE);
ui.resultLabel.setVisibility(View.INVISIBLE);
});
alert(resultStr);
});
}
});
var path = files.path("list.db")
ui.listdel.click(() => {
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
var Deletelistable = "DELETE FROM learnedArticles";
db.execSQL(Deletelistable);
db.close();
toastLog("已清空文章阅读记录!");
})
ui.daochu.click(() => {
dialogs.build({
title: "提示",
content: "这个操作会备份已学文章的数据库到\n/sdcard/Download文件夹下",
positive: "确定",
}).show();
files.copy(path, "/sdcard/Download/list.db");
toastLog("已将数据库复制到/sdcard/Download文件夹下");
});
ui.daoru.click(() => {
dialogs.build({
title: "提示",
content: "请确认文件已经放在\n/sdcard/Download文件夹下\n导入后会删除导出的文件!!\n如果需要请先备份!!",
positive: "确定",
negative: "取消",
}).on("positive", copy)
.show();
function copy() {
files.copy("/sdcard/Download/list.db", path);
toastLog("导入成功!");
files.remove("/sdcard/Download/list.db")
}
});