summaryrefslogtreecommitdiffstats
path: root/gui-project/cards.py
blob: 42a08b00ce01ee7a136a38d0350b3380a5a0f644 (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
79
80
81
82
83
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


# ui.colors(back="#c4bcdb")


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()

    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') as self.front:
                ui.markdown("**Front**")
                ui.separator()
                ui.markdown(self.card.get_front())
                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') as self.back:
                ui.markdown("_Back_")
                ui.separator()
                ui.markdown(self.card.get_back())
                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):
        ui.notify("Showing back")
        self.show_back()

    def user_clicked_correct(self):
        ui.notify("You got this")
        self.is_resolved.set()

    def user_clicked_incorrect(self):
        ui.notify("Keep at it!")
        self.is_resolved.set()

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

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