-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskipView.py
313 lines (268 loc) · 11.3 KB
/
skipView.py
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
# -*- coding: utf-8 -*-
from components import show_snack_bar
import flet as ft
from skipper import skipper as skp
from time import sleep
import logging
def skip_view(page: ft.Page):
page.ifslow = False
progressRing = ft.Ref[ft.ProgressRing]()
# TODO: 添加列表搜索框
courseList = ft.ListView(
width=230,
)
taskList = ft.ListView(
expand=True,
)
taskIndicator = ft.Ref[ft.ProgressBar]()
topTitle = ft.Ref[ft.Text]()
def disabled_course_list_button(index: int):
"""设置禁用课程按钮"""
for buttonIndex, textButton in enumerate(courseList.controls.copy()):
textButton.disabled = True if index == buttonIndex else False
page.update()
def update_top_title(title: str):
"""更新当前页面标题"""
topTitle.current.value = title
page.update()
def choose_course(e=None):
if e is not None:
# 调用来自点击事件,当前课程信息更新为新课程
index, course_id, title = e.control.data
page.current_course = [index, course_id, title]
else:
# 主动调用,使用之前保存的当前课程信息
index, course_id, title = page.current_course
# 获取课程任务列表
taskIndicator.current.visible = True
taskIndicator.current.value = None
page.update()
task_list = page.core.get_course_lessons(course_id).get("data")
disabled_course_list_button(index)
update_top_title(title)
taskList.controls.clear()
for task in task_list:
title = "没有描述" if task.get("title") is None else task.get("title")
taskList.controls.append(
ft.Checkbox(
label=title,
value=False if task.get("status") == 0 else True,
disabled=False if task.get("status") == 0 else True,
data=task.get("sectionId"),
)
)
taskIndicator.current.visible = False
page.update()
def show_omit_title(title):
"""当 title 长度大于 16 时返回缩略内容,显示文字为14长度其余以 ... 替代"""
return f"{title[:14]}..." if len(title) > 16 else title
course_dict = page.core.get_course()
if course_dict.get("code") == 200:
page.course_list = course_dict.get("data")
for index, course_item in enumerate(page.course_list):
title = course_item.get("title")
courseList.controls.append(
ft.TextButton(
text=show_omit_title(title),
tooltip=title if len(title) > 16 else None,
style=ft.ButtonStyle(
shape={
"hovered": ft.RoundedRectangleBorder(),
"": ft.RoundedRectangleBorder(),
}
),
data=(
index,
course_item.get("courseId"),
course_item.get("title"),
),
on_click=choose_course,
disabled=False,
),
)
else:
def close_alert():
"""关闭对话框"""
no_data_alert.open = False
page.update()
no_data_alert = ft.AlertDialog(
modal=True,
title=ft.Text("提示"),
content=ft.Text(f"获取课程数据失败,错误代码是{course_dict.get('code')}。"),
actions=[
ft.TextButton("好", on_click=close_alert),
],
actions_alignment="end",
)
page.dialog = no_data_alert
no_data_alert.open = True
page.update()
page.isOnSkipping = False
def skip(e):
chooseResults = list(
filter(
lambda x: x is not None,
map(
lambda x: x.data if x.value and not x.disabled else None,
taskList.controls.copy(),
),
)
)
logging.info(f"Task List: {chooseResults}")
logging.info(f"Value of isOnSkipping: {page.isOnSkipping}")
if len(chooseResults) == 0:
show_snack_bar(page, "你还没有选择课程哟〜", ft.colors.ERROR)
elif page.isOnSkipping:
show_snack_bar(page, "有刷课任务正在进行,请结束后再试〜", ft.colors.ERROR)
else:
progressRing.current.visible = True
page.update()
def close_alert(e):
"""关闭对话框,恢复标题文字,并更新任务列表"""
page.isOnSkipping = False
success_dialog.open = False
taskIndicator.current.visible = False
taskIndicator.current.value = 0
topTitle.current.value = page.current_course[2]
choose_course()
page.update()
def start_skip_task():
"""执行刷课任务"""
taskIndicator.current.value = 0
while skipper.getState() is not True:
topTitle.current.value = (
f"🕓 正在刷课中,当前第{skipper.current}个,"
+ f"共{len(chooseResults)}个。"
)
taskIndicator.current.value = (
(skipper.current-1) / len(chooseResults)
)
page.update()
sleep(duration / 1000)
def wait_indicator_finish():
"""处理任务完成但进度条没满的情况"""
while taskIndicator.current.value < 1:
taskIndicator.current.value = (
taskIndicator.current.value + (1 / 1000)
)
page.update()
sleep(duration / 1000)
taskIndicator.current.visible = True
skipper = skp(page.core, chooseResults,page.ifslow,page.userSleep)
skipper.start()
page.isOnSkipping = True
duration = 31 * len(chooseResults) if len(chooseResults) > 1 else 1
start_skip_task()
wait_indicator_finish()
progressRing.current.visible = False
success_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("任务完成"),
content=ft.Text(
f"任务结束。执行了{len(chooseResults)}个任务,"
+ f"成功{skipper.success}个,失败{skipper.fail}个。"
),
actions=[
ft.TextButton("好", on_click=close_alert),
],
actions_alignment="end",
)
page.dialog = success_dialog
success_dialog.open = True
page.update()
logging.info("Skip is finished.")
def select_all(e):
task_list_controls = taskList.controls.copy()
have_unfinish_task = any(
map(lambda task: not task.disabled, task_list_controls)
)
have_selection_some_task = len(
list(filter(lambda task: task.value, task_list_controls))
) < len(task_list_controls)
if have_unfinish_task:
if have_selection_some_task:
for task in task_list_controls:
task.value = True
else:
for i in task_list_controls:
i.value = not i.value if i.disabled is False else i.value
page.update()
else:
show_snack_bar(page, "该课全部课程都已经刷完了 ^_^", ft.colors.GREEN)
def textbox_changed(e):
page.userSleep = e.control.value
logging.info(f"用户输入的刷课间隔时间是{page.userSleep}")
mode_slow()
def mode_slow():
if(page.ifslow==False):
show_snack_bar(page, "自定义已开启,间隔"+ page.userSleep +'秒', ft.colors.GREEN)
page.ifslow = True
else:
show_snack_bar(page, "自定义已修改,间隔"+ page.userSleep +'秒', ft.colors.GREEN)
page.views.append(
ft.View(
"/skip",
[
ft.Stack(
[
ft.ProgressBar(
ref=taskIndicator, value=0, visible=False
),
ft.Container(
content=ft.Row(
[
ft.Text(
ref=topTitle,
value="请选择需要刷课的课程",
size=30,
font_family="Noto Sans SC",
),
ft.Row(
[
ft.ProgressRing(
ref = progressRing,
visible= False
),
ft.ElevatedButton(
"全选",
icon=ft.icons.ALL_INBOX,
on_click=select_all,
),
# 增加一个输入框
ft.TextField(hint_text="间隔(秒)",
width=130,
on_change=textbox_changed,),
# ft.ElevatedButton(
# "确定",
# icon=ft.icons.DONE,
# on_click=mode_slow,
# ),
ft.ElevatedButton(
"启动",
icon=ft.icons.DONE,
on_click=skip,
),
],
alignment="center",
spacing=20,
),
],
alignment="spaceBetween",
),
padding=10,
),
]
),
ft.Divider(
height=1,
),
ft.Row(
[courseList, ft.VerticalDivider(width=1), taskList],
expand=True,
spacing=0,
),
],
padding=0,
spacing=0,
)
)