Skip to content

Commit 2f8fdce

Browse files
replace UserControl base class, cleanup, simplify (#176)
1 parent 7130c15 commit 2f8fdce

File tree

6 files changed

+71
-87
lines changed

6 files changed

+71
-87
lines changed

python/apps/trolli/src/app_layout.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
)
2323
from sidebar import Sidebar
2424

25-
2625
class AppLayout(Row):
2726
def __init__(self, app, page: Page, store: DataStore, *args, **kwargs):
2827
super().__init__(*args, **kwargs)

python/apps/trolli/src/board.py

+20-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import itertools
22
from flet import (
3-
UserControl,
43
Column,
54
Row,
65
FloatingActionButton,
76
ElevatedButton,
87
Text,
98
GridView,
9+
Page,
1010
Container,
1111
TextField,
1212
AlertDialog,
@@ -23,56 +23,42 @@
2323
from data_store import DataStore
2424

2525

26-
class Board(UserControl):
26+
class Board(Container):
2727
id_counter = itertools.count()
2828

29-
def __init__(self, app, store: DataStore, name: str):
30-
super().__init__()
29+
def __init__(self, app, store: DataStore, name: str, page: Page):
30+
self.page = page
3131
self.board_id = next(Board.id_counter)
3232
self.store: DataStore = store
3333
self.app = app
3434
self.name = name
3535
self.add_list_button = FloatingActionButton(
3636
icon=icons.ADD, text="add a list", height=30, on_click=self.create_list)
3737

38-
self.board_lists = [
39-
self.add_list_button
40-
]
41-
for l in self.store.get_lists_by_board(self.board_id):
42-
self.add_list(l)
43-
44-
self.list_wrap = Row(
45-
self.board_lists,
38+
self.board_lists = Row(
39+
controls=[self.add_list_button],
4640
vertical_alignment="start",
47-
visible=True,
4841
scroll="auto",
42+
expand=True,
4943
width=(self.app.page.width - 310),
50-
height=(self.app.page.height - 95)
44+
height=(self.app.page.height - 95),
5145
)
46+
for l in self.store.get_lists_by_board(self.board_id):
47+
self.add_list(l)
5248

53-
def build(self):
54-
self.view = Container(
55-
content=Column(
56-
controls=[
57-
self.list_wrap
58-
],
59-
60-
scroll="auto",
61-
expand=True
62-
),
49+
super().__init__(
50+
content=self.board_lists,
6351
data=self,
6452
margin=margin.all(0),
6553
padding=padding.only(top=10, right=0),
66-
height=self.app.page.height,
54+
height=self.app.page.height
6755
)
68-
return self.view
6956

7057
def resize(self, nav_rail_extended, width, height):
71-
self.list_wrap.width = (
58+
self.board_lists.width = (
7259
width - 310) if nav_rail_extended else (width - 50)
73-
self.view.height = height
74-
self.list_wrap.update()
75-
self.view.update()
60+
self.height = height
61+
self.update()
7662

7763
def create_list(self, e):
7864

@@ -112,12 +98,11 @@ def set_color(e):
11298

11399
def close_dlg(e):
114100
if (hasattr(e.control, "text") and not e.control.text == "Cancel") or (type(e.control) is TextField and e.control.value != ""):
115-
new_list = BoardList(self, self.store, dialog_text.value,
101+
new_list = BoardList(self, self.store, dialog_text.value, self.page,
116102
color=color_options.data)
117103
self.add_list(new_list)
118104
dialog.open = False
119105
self.page.update()
120-
self.update()
121106

122107
def textfield_change(e):
123108
if dialog_text.value == "":
@@ -154,9 +139,10 @@ def remove_list(self, list: BoardList, e):
154139
self.store.remove_list(self.board_id, list.board_list_id)
155140
self.update()
156141

157-
def add_list(self, list: BoardList):
158-
self.board_lists.insert(-1, list)
142+
def add_list(self, list):
143+
self.board_lists.controls.insert(-1, list)
159144
self.store.add_list(self.board_id, list)
145+
self.page.update()
160146

161147
def color_option_creator(self, color: str):
162148
return Container(

python/apps/trolli/src/board_list.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from board import Board
44
import itertools
55
from flet import (
6-
UserControl,
76
Draggable,
87
DragTarget,
98
Column,
109
Row,
1110
Text,
1211
Icon,
12+
Page,
1313
PopupMenuButton,
1414
PopupMenuItem,
1515
Container,
@@ -26,21 +26,18 @@
2626
from data_store import DataStore
2727

2828

29-
class BoardList(UserControl):
29+
class BoardList(Container):
3030
id_counter = itertools.count()
3131

32-
def __init__(self, board: "Board", store: DataStore, title: str, color: str = ""):
33-
super().__init__()
32+
def __init__(self, board: "Board", store: DataStore, title: str, page: Page, color: str = ""):
33+
self.page = page
3434
self.board_list_id = next(BoardList.id_counter)
3535
self.store: DataStore = store
3636
self.board = board
3737
self.title = title
3838
self.color = color
3939
self.items = Column([], tight=True, spacing=4)
4040
self.items.controls = self.store.get_items(self.board_list_id)
41-
42-
def build(self):
43-
4441
self.new_item_field = TextField(
4542
label="new card name", height=50, bgcolor=colors.WHITE, on_submit=self.add_item_handler)
4643

@@ -51,11 +48,13 @@ def build(self):
5148
width=200,
5249
opacity=0.0
5350
)
51+
5452
self.edit_field = Row([
5553
TextField(value=self.title, width=150, height=40,
5654
content_padding=padding.only(left=10, bottom=10)),
5755
TextButton(text="Save", on_click=self.save_title)
5856
])
57+
5958
self.header = Row(
6059
controls=[
6160
Text(value=self.title, style="titleMedium",
@@ -103,6 +102,7 @@ def build(self):
103102
padding=padding.only(
104103
bottom=10, right=10, left=10, top=5)
105104
)
105+
106106
self.view = DragTarget(
107107
group="items",
108108
content=Draggable(
@@ -121,9 +121,11 @@ def build(self):
121121
on_will_accept=self.item_will_drag_accept,
122122
on_leave=self.item_drag_leave
123123
)
124-
125-
return self.view
126-
124+
super().__init__(
125+
content=self.view,
126+
data=self
127+
)
128+
127129
def item_drag_accept(self, e):
128130
src = self.page.get_control(e.src_id)
129131
self.add_item(src.data.item_text)
@@ -142,13 +144,12 @@ def item_drag_leave(self, e):
142144

143145
def list_drag_accept(self, e):
144146
src = self.page.get_control(e.src_id)
145-
l = self.board.board_lists
147+
l = self.board.content.controls
146148
to_index = l.index(e.control.data)
147149
from_index = l.index(src.content.data)
148150
l[to_index], l[from_index] = l[from_index], l[to_index]
149151
self.inner_list.border = border.all(2, colors.BLACK12)
150-
self.board.update()
151-
self.update()
152+
self.page.update()
152153

153154
def list_will_drag_accept(self, e):
154155
if e.data == "true":
@@ -171,7 +172,6 @@ def save_title(self, e):
171172
self.title = self.edit_field.controls[0].value
172173
self.header.controls[0] = Text(value=self.title, style="titleMedium",
173174
text_align="left", overflow="clip", expand=True)
174-
175175
self.header.controls[1].visible = True
176176
self.update()
177177

python/apps/trolli/src/item.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
Container,
99
Checkbox,
1010
Row,
11-
UserControl,
1211
Card,
1312
border_radius,
1413
)
1514
from data_store import DataStore
1615

17-
18-
class Item(UserControl):
16+
class Item(Container):
1917
id_counter = itertools.count()
2018

2119
def __init__(self, list: "BoardList", store: DataStore, item_text: str):
22-
super().__init__()
2320
self.item_id = next(Item.id_counter)
2421
self.store: DataStore = store
2522
self.list = list
@@ -35,9 +32,6 @@ def __init__(self, list: "BoardList", store: DataStore, item_text: str):
3532
elevation=1,
3633
data=self.list
3734
)
38-
39-
def build(self):
40-
4135
self.view = Draggable(
4236
group="items",
4337
content=DragTarget(
@@ -49,7 +43,9 @@ def build(self):
4943
),
5044
data=self
5145
)
52-
return self.view
46+
super().__init__(
47+
content=self.view
48+
)
5349

5450
def drag_accept(self, e):
5551
src = self.page.get_control(e.src_id)

python/apps/trolli/src/main.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import flet
22
from app_layout import AppLayout
33
from board import Board
4+
from board_list import BoardList
45
from data_store import DataStore
56
from flet import (
67
AlertDialog,
@@ -12,12 +13,10 @@
1213
Page,
1314
PopupMenuButton,
1415
PopupMenuItem,
15-
RoundedRectangleBorder,
1616
Row,
1717
TemplateRoute,
1818
Text,
1919
TextField,
20-
UserControl,
2120
View,
2221
colors,
2322
icons,
@@ -29,9 +28,8 @@
2928
from user import User
3029

3130

32-
class TrelloApp(UserControl):
31+
class TrelloApp(AppLayout):
3332
def __init__(self, page: Page, store: DataStore):
34-
super().__init__()
3533
self.page = page
3634
self.store: DataStore = store
3735
self.page.on_route_change = self.route_change
@@ -58,23 +56,31 @@ def __init__(self, page: Page, store: DataStore):
5856
)
5957
self.page.appbar = self.appbar
6058
self.page.update()
61-
62-
def build(self):
63-
self.layout = AppLayout(
59+
super().__init__(
6460
self,
6561
self.page,
6662
self.store,
6763
tight=True,
6864
expand=True,
6965
vertical_alignment="start",
7066
)
71-
return self.layout
67+
68+
# def build(self):
69+
# self.layout = AppLayout(
70+
# self,
71+
# self.page,
72+
# self.store,
73+
# tight=True,
74+
# expand=True,
75+
# vertical_alignment="start",
76+
# )
77+
# return self.layout
7278

7379
def initialize(self):
7480
self.page.views.append(
7581
View(
7682
"/",
77-
[self.appbar, self.layout],
83+
[self.appbar, self],
7884
padding=padding.all(0),
7985
bgcolor=colors.BLUE_GREY_200,
8086
)
@@ -131,11 +137,11 @@ def route_change(self, e):
131137
if int(troute.id) > len(self.store.get_boards()):
132138
self.page.go("/")
133139
return
134-
self.layout.set_board_view(int(troute.id))
140+
self.set_board_view(int(troute.id))
135141
elif troute.match("/boards"):
136-
self.layout.set_all_boards_view()
142+
self.set_all_boards_view()
137143
elif troute.match("/members"):
138-
self.layout.set_members_view()
144+
self.set_members_view()
139145
self.page.update()
140146

141147
def add_board(self, e):
@@ -183,13 +189,13 @@ def textfield_change(e):
183189
dialog_text.focus()
184190

185191
def create_new_board(self, board_name):
186-
new_board = Board(self, self.store, board_name)
192+
new_board = Board(self, self.store, board_name, self.page)
187193
self.store.add_board(new_board)
188-
self.layout.hydrate_all_boards_view()
194+
self.hydrate_all_boards_view()
189195

190196
def delete_board(self, e):
191197
self.store.remove_board(e.control.data)
192-
self.layout.set_all_boards_view()
198+
self.set_all_boards_view()
193199

194200

195201
def main(page: Page):
@@ -205,5 +211,6 @@ def main(page: Page):
205211
page.update()
206212
app.initialize()
207213

208-
214+
print("flet version: ", flet.version.version)
215+
print("flet path: ", flet.__file__)
209216
flet.app(target=main, assets_dir="../assets")

0 commit comments

Comments
 (0)