-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn_screen.py
293 lines (248 loc) · 9.95 KB
/
learn_screen.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
import os
import datetime
from kivy.uix.screenmanager import Screen
from kivy.logger import Logger
from kivy.properties import ObjectProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from storage import MIN_DATE, DT_FORMAT
from common import set_screen_active, get_screen, CardPopup, DeletePopup, hide_widget
from debug import log_time
class LearnBatchLabel(RecycleDataViewBehavior, BoxLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.question = None
self.answer = None
self.card_id = None
self.correct = False
self.index = None
self.revealed = False
def refresh_view_attrs(self, rv, index, data):
""" Catch and handle the view changes """
# self.index = index
self.correct = False
self.revealed = False
self.card_id = data['card_id']
self.question = data['question']
self.answer = data['answer']
self.index = index
btn_left = self.ids['btn_left']
btn_right = self.ids['btn_right']
btn_left.text = self.question
btn_right.text = '<reveal!>'
self._set_bg_color()
return super(LearnBatchLabel, self).refresh_view_attrs(
rv, index, data)
def _set_bg_color(self):
color = [0., 0., 0., 1.]
if self.index % 2 == 0:
color = [0.1, 0.1, 0.1, 1.]
if self.correct:
color = [0.29, 0.569, 0.188, 1]
self.ids['btn_left'].background_color = color
self.ids['btn_right'].background_color = color
def set_correct(self):
self.correct = not self.correct
self._set_bg_color()
def reveal(self):
if not self.revealed:
self.ids['btn_right'].text = self.answer
self.revealed = True
else:
self.set_correct()
# self.ids['btn_right'].disabled = True
class LearnBatchRV(RecycleView):
def __init__(self, **kwargs):
super(LearnBatchRV, self).__init__(**kwargs)
Logger.info('in RV')
def get_results(self):
return {self.data[ix]['card_id']: node.correct for node, ix in
self.ids['rv_layout'].view_indices.items()}
class EditCardPopup(CardPopup):
def cancel(self):
get_screen('learn').move_current_card_back()
self.dismiss()
def act_on_save(self, value_dict):
for field, value in value_dict.items():
setattr(self.db_object, field, value)
get_screen('learn').act_on_card_edit()
def delete(self):
DeletePopup(self.db_object.left, callback=self.act_on_delete).open()
def act_on_delete(self):
get_screen('learn').act_on_card_delete(self.db_object)
self.dismiss()
class LearnScreen(Screen):
set_info_label = ObjectProperty(None)
learn_layout = ObjectProperty(None)
layout_single = ObjectProperty(None)
layout_batch = ObjectProperty(None)
box_batch_table = ObjectProperty(None)
selected_sets = []
BATCH_LEN = 10
def __init__(self, storage, **kwargs):
super().__init__(**kwargs)
self.storage = storage
self.card_data = []
self.total_cards = 0
self.batch_mode = False
self.rv = LearnBatchRV()
self.box_batch_table.add_widget(self.rv)
hide_widget(self.layout_batch, True)
Logger.info('in LearnScreen')
def update_cards(self, selected_sets):
with log_time('learn - set_sets_active'):
self.storage.set_sets_active(selected_sets)
with log_time('learn - select cards'):
card_data = [x.card for x in self.storage.cards.values() if x.card_set.name in selected_sets]
with log_time('learn - prepare cards'):
self.total_cards = len(card_data)
now = datetime.datetime.utcnow()
card_data = [x for x in card_data if x.hidden_until < now]
# unseen cards are shown last! Repeat seen cards first for better learning effect on large sets
seen = [x for x in card_data if x.hidden_until > MIN_DATE]
unseen = [x for x in card_data if x.hidden_until == MIN_DATE]
self.card_data = sorted(seen, key=lambda x: x.hidden_until) + unseen
with log_time('learn - update questions'):
self.update_questions()
with log_time('learn - update batch questions'):
self.update_batch_questions()
@staticmethod
def _wrap_question(question, info):
info_str = '' if info is None or not len(info) else '(%s)' % info
return '%s %s' % (info_str, question)
def update_questions(self):
self.ids['answer'].text = ''
self.ids['q_this'].text = ''
self.ids['q_next'].text = ''
if self.current_card is not None:
self.ids['q_this'].text = self._wrap_question(
self.current_card.left, self.current_card.left_info)
else:
self.ids['answer'].text = 'No more cards!'
if self.next_card is not None:
self.ids['q_next'].text = '(Upcoming: %s)' % self._wrap_question(
self.next_card.left, self.next_card.left_info)
# update set statistics:
self.set_info_label.text = os.linesep.join([
'total selected cards: %i' % self.total_cards,
'to learn this session: %i' % len(self.card_data),
'never seen: %i' % len([x for x in self.card_data if x.last_seen == MIN_DATE])
])
@property
def current_card(self):
"""
:rtype: storage.Card
"""
if len(self.card_data):
return self.card_data[0]
@property
def next_card(self):
"""
:rtype: storage.Card
"""
if len(self.card_data) > 1:
return self.card_data[1]
def reveal(self):
if self.current_card is None:
set_screen_active('manage')
return
self.ids['answer'].text = self.current_card.right
def _modify_card_on_wrong(self, card):
card.streak = 0
self.storage.update_card_queue.put(card.card_id)
def wrong_answer(self):
if self.current_card is None:
set_screen_active('manage')
return
self._modify_card_on_wrong(self.current_card)
self.move_current_card_back()
def _modify_card_on_right(self, card):
card.streak += 1
card.last_seen = datetime_cut_ms(datetime.datetime.utcnow())
card.hidden_until = datetime_cut_ms(
datetime.datetime.utcnow() + datetime.timedelta(hours=(12 * 2 ** (card.streak - 1) - 2)))
self.storage.update_card_queue.put(card.card_id)
def right_answer(self):
if self.current_card is None:
set_screen_active('manage')
return
self._modify_card_on_right(self.current_card)
self.card_data = self.card_data[min(len(self.card_data), 1):]
self.update_questions()
def delay_current(self):
if self.current_card is None:
set_screen_active('manage')
return
self.current_card.hidden_until = datetime_cut_ms(
datetime.datetime.utcnow() + datetime.timedelta(hours=10))
self.storage.update_card_queue.put(self.current_card.card_id)
self.card_data = self.card_data[min(len(self.card_data), 1):]
self.update_questions()
def edit_current(self):
if self.current_card is None:
set_screen_active('manage')
return
EditCardPopup('Edit Card', db_object=self.current_card).open()
def act_on_card_edit(self):
self.storage.update_card_queue.put(self.current_card.card_id)
# move card to position 6
self.move_current_card_back()
def move_current_card_back(self, steps=6):
self.card_data.insert(min(steps, len(self.card_data)), self.current_card)
self.card_data = self.card_data[min(len(self.card_data), 1):]
self.update_questions()
def act_on_card_delete(self, card):
self.storage.delete_card(card)
self.card_data = self.card_data[1:]
self.update_questions()
def activate_single(self):
self.update_questions()
hide_widget(self.layout_batch, True)
hide_widget(self.layout_single, False)
def activate_batch(self):
self.update_batch_questions()
hide_widget(self.layout_single, True)
hide_widget(self.layout_batch, False)
# Batch Mode
def switch_mode(self):
if self.batch_mode:
self.activate_single()
self.batch_mode = False
elif len(self.card_data):
self.activate_batch()
self.batch_mode = True
def update_batch_questions(self):
if self.current_card is None:
self.activate_single()
return
self.rv.data = [{
'card_id': card.card_id,
'question': card.left,
'answer': card.right
} for card in self.card_data[:self.BATCH_LEN]]
def submit_batch(self):
results = self.rv.get_results()
seen = set()
shelved = []
while len(self.card_data) and self.card_data[0].card_id in results:
card_id = self.current_card.card_id
if card_id in seen:
# We arrive here if an early card is moved back.
# We want to avoid processing those repeatedly.
# So we remove them temporarily and insert them after processing the whole batch.
shelved.append(self.current_card)
self.card_data = self.card_data[1:]
continue
correct = results[card_id]
if correct:
self.right_answer()
else:
self.wrong_answer()
seen.add(card_id)
if len(shelved):
self.card_data = shelved + self.card_data
self.update_questions()
self.update_batch_questions()
def datetime_cut_ms(dt):
return datetime.datetime.strptime(dt.strftime(DT_FORMAT), DT_FORMAT)