summaryrefslogtreecommitdiffstats
path: root/gui-project/cards.py
blob: ededff999edcad70eeb87cecfdfaa461036b93d4 (plain)
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
import asyncio

from nicegui import ui

class CardComponent():
    def __init__(self, front : str, back : str):
        if front is None or front == "":
            raise Exception("Cannot have empty front on the card")

        if back is None or back == "":
            raise Exception("Cannot have empty back on the card")

        self.front = front
        self.back = back

    def get_front(self):
        return self.front

    def get_back(self):
        return self.back

class CardUI():
    def __init__(self, parent_ui, card : CardComponent):
        self.parent_ui = parent_ui
        self.card = card
        self.is_resolved = asyncio.Event()
        self.init_card()
        self.correctly_answered = False

    def init_card(self):
        with self.parent_ui:
            self.row_parent = ui.row()
        self.init_front()
        self.init_back()

    def init_front(self):
        with self.row_parent:
            with ui.card().classes('bg-frontc w-[600px]  mx-auto') as self.front:
                ui.markdown("**Front**")
                ui.separator()
                ui.markdown(self.card.get_front()).style('white-space: pre-wrap')
                ui.button("Revert", on_click=lambda: self.revert_card())
        self.front.set_visibility(False)

    def init_back(self):
        with self.row_parent:
            with ui.card().classes('bg-back w-[600px]  mx-auto') as self.back:
                ui.markdown("_Back_")
                ui.separator()
                ui.markdown(self.card.get_back()).style('white-space: pre-wrap')
                with ui.button_group():
                    ui.button("Got it", on_click=lambda: self.user_clicked_correct())
                    ui.button("Ain't got it", on_click=lambda: self.user_clicked_incorrect())
        self.back.set_visibility(False)

    def show_front(self):
        self.back.set_visibility(False)
        self.front.set_visibility(True)

    def show_back(self):
        self.front.set_visibility(False)
        self.back.set_visibility(True)

    def revert_card(self):
        self.show_back()

    def user_clicked_correct(self):
        self.correctly_answered = True
        self.is_resolved.set()

    def user_clicked_incorrect(self):
        self.is_resolved.set()

    async def is_answered(self):
        await self.is_resolved.wait()

    def hide_card(self):
        self.row_parent.delete()