-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleapp.py
More file actions
80 lines (63 loc) · 2.02 KB
/
sampleapp.py
File metadata and controls
80 lines (63 loc) · 2.02 KB
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
from dataclasses import dataclass
from typing import List
from theme import Edge, FontDef, StyleRules, WidgetStyle, WidgetStyleDef, YogaStyle
from widgetnode import button, root_node, node, BaseComponent, unformatted_text
from rx.subject import BehaviorSubject
@dataclass
class TodoItem:
text: str
done: bool
@dataclass
class AppState:
todo_text: str
todo_items: List[TodoItem]
sampleAppState = BehaviorSubject(AppState(todo_text="", todo_items=[]))
def on_click():
new_todo_item = TodoItem(text="New Todo", done=False)
current_state = sampleAppState.value
new_state = AppState(
todo_text=current_state.todo_text,
todo_items=current_state.todo_items + [new_todo_item]
)
sampleAppState.on_next(new_state)
text_style = WidgetStyle(
style=WidgetStyleDef(
style_rules=StyleRules(
font=FontDef("roboto-regular", 32)
)
)
)
button_style = WidgetStyle(
style=WidgetStyleDef(
style_rules=StyleRules(
font=FontDef("roboto-regular", 32)
),
layout=YogaStyle(
width="50%",
padding={Edge.Vertical: 10},
margin={Edge.Left: 140}
)
)
)
class App(BaseComponent):
def __init__(self):
super().__init__({})
self.app_state_subscription = sampleAppState.subscribe(lambda latest_app_state: self.props.on_next({
"todo_text": latest_app_state.todo_text,
"todo_items": latest_app_state.todo_items,
}))
def render(self):
children = [button("Add todo", on_click, button_style)]
for todo_item in self.props.value["todo_items"]:
text = f"{todo_item.text} ({'done' if todo_item.done else 'to do'})."
children.append(unformatted_text(text, text_style))
return node(children)
def dispose(self):
self.app_state_subscription.dispose()
class Root(BaseComponent):
def __init__(self):
super().__init__({})
def render(self):
return root_node([
App()
])