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
| import json
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.button import MDIconButton, MDRaisedButton
from kivymd.uix.card import MDCard
from kivymd.uix.textfield import MDTextField
from kivymd.uix.gridlayout import MDGridLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.list import MDList, TwoLineListItem
from kivy.metrics import dp
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from functools import partial
import traceback
screen_helper = """
ScreenManager:
MenuScreen:
CreateScreen:
StatsScreen:
FlashcardsScreen:
<MenuScreen>:
name: 'menu'
MDGridLayout:
id: group_list_layout
cols: 1
size_hint_y: None
height: self.minimum_height
pos_hint: {"top": 0.9}
MDTopAppBar:
title: "Answa"
pos_hint: {"top": 1}
elevation: 0
md_bg_color: (34 / 255, 139 / 255, 34 / 255)
right_action_items: [["chart-bar-stacked", lambda x: root.on_right_action_items(x)]]
MDFloatingActionButton:
icon: "note-plus"
md_bg_color: (34 / 255, 139 / 255, 34 / 255)
pos: root.width - self.width - dp(16), dp(16)
elevation: 0
on_press: root.manager.current = 'create'
<CreateScreen>:
name: 'create'
MDTopAppBar:
title: "Answa"
pos_hint: {"top": 1}
elevation: 0
left_action_items: [["chevron-left", lambda x: root.on_left_action_items(x)]]
right_action_items: [["content-save-check-outline", lambda x: root.save_to_json()]]
md_bg_color: (34 / 255, 139 / 255, 34 / 255)
MDTextField:
id: group_name
hint_text: "Enter name group..."
mode: "fill"
pos_hint: {"top": 0.9}
ScrollView:
size_hint: (1, 1)
pos_hint: {"top": 0.8}
GridLayout:
id: card_container
cols: 1
size_hint_y: None
height: self.minimum_height
MDIconButton:
icon: "plus-circle-outline"
theme_icon_color: "Custom"
icon_color: (34 / 255, 139 / 255, 34 / 255)
icon_size: "54sp"
pos: root.width - self.width - dp(16), dp(16)
on_release: root.add_card()
<StatsScreen>:
name: 'stats'
MDTopAppBar:
title: "Answa"
pos_hint: {"top": 1}
elevation: 0
left_action_items: [["chevron-left", lambda x: root.on_left_action_items(x)]]
md_bg_color: (34 / 255, 139 / 255, 34 / 255)
MDLabel:
text: 'Upload'
halign: 'center'
<FlashcardsScreen>:
name: 'flashcards'
MDTopAppBar:
title: "Answa"
pos_hint: {"top": 1}
elevation: 0
left_action_items: [["chevron-left", lambda x: root.on_left_action_items(x)]]
md_bg_color: (34 / 255, 139 / 255, 34 / 255)
MDLabel:
text: 'Upload'
halign: 'center'
"""
class MenuScreen(Screen):
group_list_layout = ObjectProperty()
def __init__(self, **kwargs):
super(MenuScreen, self).__init__(**kwargs)
self.group_list_layout = MDGridLayout(cols=1, size_hint_y=None, height=dp(0))
def on_pre_enter(self, *args):
self.update_group_list()
def on_right_action_items(self, instance):
self.manager.current = 'stats'
def update_group_list(self):
self.group_list_layout.clear_widgets()
try:
with open("user_data.json", "r") as file:
data = json.load(file)
for group_data in data:
group_name = group_data.get("group_name", "")
if group_name:
card = MDCard(
orientation='vertical',
size_hint_y=None,
height=dp(80),
elevation=1,
on_release=partial(self.show_flashcards_screen, group_data)
)
box_layout = BoxLayout(padding=(dp(16), 0))
label = MDLabel(
text=group_name,
theme_text_color="Primary",
halign="left",
valign="center",
)
box_layout.add_widget(label)
card.add_widget(box_layout)
self.group_list_layout.add_widget(card)
except FileNotFoundError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {traceback.format_exc()}")
def show_flashcards_screen(self, group_data, *args):
self.manager.current = 'flashcards'
flashcards_screen = self.manager.get_screen('flashcards')
flashcards_screen.group_data = group_data
flashcards_screen.load_flashcards()
class CreateScreen(Screen):
card_list_layout = ObjectProperty()
def on_left_action_items(self, instance):
self.manager.current = 'menu'
def __init__(self, **kwargs):
super(CreateScreen, self).__init__(**kwargs)
self.card_data = []
def add_card(self):
card_container = self.ids.card_container
new_card = MDCard(
orientation='vertical',
size_hint_y=None,
height="80dp",
elevation=1,
)
new_box_layout = MDGridLayout(
cols=2,
row_default_height="40dp",
row_force_default=True,
spacing='10dp',
padding='10dp',
)
new_question_field = MDTextField(hint_text='Question')
new_answer_field = MDTextField(hint_text='Answer')
new_box_layout.add_widget(new_question_field)
new_box_layout.add_widget(new_answer_field)
close_button = MDIconButton(
icon="close",
theme_text_color="Custom",
text_color=(1, 0, 0, 1),
pos_hint={"center_x": 0.95, "center_y": 0.9},
)
close_button.bind(on_release=lambda x: self.remove_card(new_card, new_question_field, new_answer_field))
new_card.add_widget(new_box_layout)
new_card.add_widget(close_button)
self.card_data.insert(0, {"question": new_question_field.text, "answer": new_answer_field.text})
card_container.add_widget(new_card, index=0)
new_question_field.text = ''
new_answer_field.text = ''
def update_card_container(self):
card_container = self.ids.card_container
card_container.clear_widgets()
for card_data in self.card_data:
new_card = MDCard(
orientation='vertical',
size_hint_y=None,
height="80dp",
elevation=1,
)
new_box_layout = MDGridLayout(
cols=2,
row_default_height="40dp",
row_force_default=True,
spacing='10dp',
padding='10dp',
)
new_question_field = MDTextField(hint_text='Question', text=card_data.get('question', ''))
new_answer_field = MDTextField(hint_text='Answer', text=card_data.get('answer', ''))
new_box_layout.add_widget(new_question_field)
new_box_layout.add_widget(new_answer_field)
close_button = MDIconButton(
icon="close",
theme_text_color="Custom",
text_color=(1, 0, 0, 1),
pos_hint={"center_x": 0.95, "center_y": 0.9},
)
close_button.bind(on_release=lambda x: self.remove_card(new_card, new_question_field, new_answer_field))
new_card.add_widget(new_box_layout)
new_card.add_widget(close_button)
card_container.add_widget(new_card, index=0)
def remove_card(self, card, question_field, answer_field):
card_container = self.ids.card_container
card_container.remove_widget(card)
for item in self.card_data:
if item["question"] == question_field.text and item["answer"] == answer_field.text:
self.card_data.remove(item)
break
class FlashcardsScreen(Screen):
def __init__(self, group_data=None, **kwargs):
super().__init__(**kwargs)
self.group_data = group_data
self.layout = BoxLayout(orientation='vertical')
self.flashcard_list = MDList()
self.edit_button = MDRaisedButton(text='Edit Flashcards', on_release=self.edit_flashcards)
self.study_button = MDRaisedButton(text='Study', on_release=self.study_flashcards)
self.layout.add_widget(self.flashcard_list)
self.layout.add_widget(self.edit_button)
self.layout.add_widget(self.study_button)
self.add_widget(self.layout)
def on_pre_enter(self, *args):
if self.group_data:
self.load_flashcards()
def load_flashcards(self):
self.flashcard_list.clear_widgets()
flashcards = self.group_data.get('flashcards', [])
for flashcard in flashcards:
text = f"Question: {flashcard['question']}\nAnswer: {flashcard['answer']}"
item = TwoLineListItem(text=text)
self.flashcard_list.add_widget(item)
def edit_flashcards(self, instance):
self.manager.current = 'create'
create_screen = self.manager.get_screen('create')
create_screen.group_name.text = self.group_data.get('group_name', '')
create_screen.card_data = self.group_data.get('flashcards', [])
create_screen.update_card_container()
def study_flashcards(self, instance):
if not self.group_data:
return
self.start_study(self.group_data['group_name'])
if not self.flashcards:
self.show_error_popup("No flashcards available for study.")
return
self.show_current_flashcard()
self.study_button.text = 'Next Flashcard'
self.study_button.unbind(on_release=self.study_flashcards)
self.study_button.bind(on_release=self.show_next_flashcard)
def show_current_flashcard(self):
if 0 <= self.current_card_index < len(self.flashcards):
card = self.flashcards[self.current_card_index]
text = f"Question: {card['question']}\nAnswer: {card['answer']}"
self.flashcard_list.clear_widgets()
item = TwoLineListItem(text=text)
self.flashcard_list.add_widget(item)
def show_next_flashcard(self, instance):
if 0 <= self.current_card_index < len(self.flashcards) - 1:
self.current_card_index += 1
self.show_current_flashcard()
else:
self.show_success_popup("Congratulations! You've studied all flashcards.")
self.reset_study_session()
def reset_study_session(self):
self.current_card_index = 0
self.study_button.text = 'Study'
self.study_button.unbind(on_release=self.show_next_flashcard)
self.study_button.bind(on_release=self.study_flashcards)
self.flashcard_list.clear_widgets()
class StatsScreen(Screen):
def on_left_action_items(self, instance):
self.manager.current = 'menu'
class Answa(MDApp):
def build(self):
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(CreateScreen(name='create'))
sm.add_widget(StatsScreen(name='stats'))
sm.add_widget(FlashcardsScreen(name='flashcards'))
return sm
if __name__ == '__main__':
Answa().run() |