Skip to content

Commit 4efa9f7

Browse files
committed
Move modules around and improve tests
1 parent 1e96c77 commit 4efa9f7

File tree

7 files changed

+135
-129
lines changed

7 files changed

+135
-129
lines changed

openandroidinstaller/views/install_addons_view.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
from widgets import (
3535
confirm_button,
3636
get_title,
37+
TerminalBox,
38+
ProgressIndicator,
3739
)
38-
from views.step_view import TerminalBox, ProgressIndicator
3940

4041

4142
class InstallAddonsView(BaseView):

openandroidinstaller/views/install_view.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
from widgets import (
3535
confirm_button,
3636
get_title,
37+
TerminalBox,
38+
ProgressIndicator,
3739
)
38-
from views.step_view import TerminalBox, ProgressIndicator
3940

4041

4142
class InstallView(BaseView):

openandroidinstaller/views/step_view.py

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@
1717
from time import sleep
1818
from typing import Callable
1919
from functools import partial
20-
import regex as re
2120

2221
from flet import (
23-
UserControl,
2422
Column,
2523
ElevatedButton,
2624
Row,
2725
Text,
2826
icons,
2927
TextField,
30-
Container,
3128
Switch,
32-
alignment,
33-
ProgressBar,
34-
ProgressRing,
3529
colors,
3630
)
3731

@@ -58,6 +52,8 @@
5852
confirm_button,
5953
get_title,
6054
link_button,
55+
TerminalBox,
56+
ProgressIndicator,
6157
)
6258

6359

@@ -264,118 +260,3 @@ def call_to_phone(self, e, command: str):
264260
# reset the progress indicator (let the progressbar stay for the install command)
265261
self.progress_indicator.clear()
266262
self.view.update()
267-
268-
269-
class TerminalBox(UserControl):
270-
def __init__(self, expand: bool = True, visible: bool = False):
271-
super().__init__(expand=expand)
272-
self.visible = visible
273-
274-
def build(self):
275-
self._box = Container(
276-
content=Column(scroll="auto", expand=True, auto_scroll=True),
277-
margin=10,
278-
padding=10,
279-
alignment=alignment.top_left,
280-
bgcolor=colors.BLACK38,
281-
height=300,
282-
border_radius=2,
283-
expand=True,
284-
visible=self.visible,
285-
)
286-
return self._box
287-
288-
def write_line(self, line: str):
289-
"""
290-
Write the line to the window box and update.
291-
292-
Ignores empty lines.
293-
"""
294-
if (type(line) == str) and line.strip():
295-
self._box.content.controls.append(Text(f">{line.strip()}", selectable=True))
296-
self.update()
297-
298-
def toggle_visibility(self):
299-
"""Toggle the visibility of the terminal box."""
300-
self._box.visible = not self._box.visible
301-
self.visible = not self.visible
302-
self.update()
303-
304-
def clear(self):
305-
"""Clear terminal output."""
306-
self._box.content.controls = []
307-
self.update()
308-
309-
def update(self):
310-
"""Update the view."""
311-
self._box.update()
312-
313-
314-
class ProgressIndicator(UserControl):
315-
def __init__(self, expand: bool = True):
316-
super().__init__(expand=expand)
317-
# placeholder for the flashing progressbar
318-
self.progress_bar = None
319-
# progress ring to display
320-
self.progress_ring = None
321-
322-
def build(self):
323-
self._container = Container(
324-
content=Column(scroll="auto", expand=True),
325-
margin=10,
326-
alignment=alignment.center,
327-
height=50,
328-
expand=True,
329-
visible=True,
330-
)
331-
return self._container
332-
333-
def display_progress_bar(self, line: str):
334-
"""Display and update the progress bar for the given line."""
335-
percentage_done = None
336-
result = None
337-
# get the progress numbers from the output lines
338-
if (type(line) == str) and line.strip():
339-
result = re.search(
340-
r"\(\~(\d{1,3})\%\)|(Total xfer:|adb: failed to read command: Success)",
341-
line.strip(),
342-
)
343-
if result:
344-
if result.group(2):
345-
percentage_done = 100
346-
elif result.group(1):
347-
percentage_done = int(result.group(1))
348-
349-
# create the progress bar on first occurrence
350-
if percentage_done == 0:
351-
self.progress_bar = ProgressBar(
352-
width=500, bar_height=32, color="#00d886", bgcolor="#eeeeee"
353-
)
354-
self.percentage_text = Text(f"{percentage_done}%")
355-
self._container.content.controls.append(
356-
Row([self.percentage_text, self.progress_bar])
357-
)
358-
# update the progress bar
359-
if self.progress_bar:
360-
self.progress_bar.value = percentage_done / 100
361-
self.percentage_text.value = f"{percentage_done}%"
362-
363-
def display_progress_ring(
364-
self,
365-
):
366-
"""Display a progress ring to signal progress."""
367-
if not self.progress_ring:
368-
self.progress_ring = ProgressRing(color="#00d886")
369-
self._container.content.controls.append(self.progress_ring)
370-
self._container.update()
371-
372-
def clear(self):
373-
"""Clear output."""
374-
self._container.content.controls = []
375-
self.progress_ring = None
376-
self.progress_bar = None
377-
self.update()
378-
379-
def update(self):
380-
"""Update the view."""
381-
self._container.update()

openandroidinstaller/widgets.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
# Author: Tobias Sterbak
1515

1616
import webbrowser
17+
import regex as re
1718
from functools import partial
1819
from typing import Callable
1920

2021
from flet import (
22+
UserControl,
23+
colors,
2124
Container,
2225
ElevatedButton,
26+
ProgressRing,
27+
ProgressBar,
2328
Row,
2429
Text,
2530
alignment,
@@ -30,9 +35,125 @@
3035
)
3136

3237

38+
class TerminalBox(UserControl):
39+
def __init__(self, expand: bool = True, visible: bool = False):
40+
super().__init__(expand=expand)
41+
self.visible = visible
42+
43+
def build(self):
44+
self._box = Container(
45+
content=Column(scroll="auto", expand=True, auto_scroll=True),
46+
margin=10,
47+
padding=10,
48+
alignment=alignment.top_left,
49+
bgcolor=colors.BLACK38,
50+
height=300,
51+
border_radius=2,
52+
expand=True,
53+
visible=self.visible,
54+
)
55+
return self._box
56+
57+
def write_line(self, line: str):
58+
"""
59+
Write the line to the window box and update.
60+
61+
Ignores empty lines.
62+
"""
63+
if (type(line) == str) and line.strip():
64+
self._box.content.controls.append(Text(f">{line.strip()}", selectable=True))
65+
self.update()
66+
67+
def toggle_visibility(self):
68+
"""Toggle the visibility of the terminal box."""
69+
self._box.visible = not self._box.visible
70+
self.visible = not self.visible
71+
self.update()
72+
73+
def clear(self):
74+
"""Clear terminal output."""
75+
self._box.content.controls = []
76+
self.update()
77+
78+
def update(self):
79+
"""Update the view."""
80+
self._box.update()
81+
82+
83+
class ProgressIndicator(UserControl):
84+
def __init__(self, expand: bool = True):
85+
super().__init__(expand=expand)
86+
# placeholder for the flashing progressbar
87+
self.progress_bar = None
88+
# progress ring to display
89+
self.progress_ring = None
90+
91+
def build(self):
92+
self._container = Container(
93+
content=Column(scroll="auto", expand=True),
94+
margin=10,
95+
alignment=alignment.center,
96+
height=50,
97+
expand=True,
98+
visible=True,
99+
)
100+
return self._container
101+
102+
def display_progress_bar(self, line: str):
103+
"""Display and update the progress bar for the given line."""
104+
percentage_done = None
105+
result = None
106+
# get the progress numbers from the output lines
107+
if (type(line) == str) and line.strip():
108+
result = re.search(
109+
r"\(\~(\d{1,3})\%\)|(Total xfer:|adb: failed to read command: Success)",
110+
line.strip(),
111+
)
112+
if result:
113+
if result.group(2):
114+
percentage_done = 100
115+
elif result.group(1):
116+
percentage_done = int(result.group(1))
117+
118+
# create the progress bar on first occurrence
119+
if percentage_done == 0:
120+
self.progress_bar = ProgressBar(
121+
width=500, bar_height=32, color="#00d886", bgcolor="#eeeeee"
122+
)
123+
self.percentage_text = Text(f"{percentage_done}%")
124+
self._container.content.controls.append(
125+
Row([self.percentage_text, self.progress_bar])
126+
)
127+
# update the progress bar
128+
if self.progress_bar:
129+
self.progress_bar.value = percentage_done / 100
130+
self.percentage_text.value = f"{percentage_done}%"
131+
132+
def display_progress_ring(
133+
self,
134+
):
135+
"""Display a progress ring to signal progress."""
136+
if not self.progress_ring:
137+
self.progress_ring = ProgressRing(color="#00d886")
138+
self._container.content.controls.append(self.progress_ring)
139+
self._container.update()
140+
141+
def clear(self):
142+
"""Clear output."""
143+
self._container.content.controls = []
144+
self.progress_ring = None
145+
self.progress_bar = None
146+
self.update()
147+
148+
def update(self):
149+
"""Update the view."""
150+
self._container.update()
151+
152+
33153
def get_title(
34154
title: str, info_button: IconButton = None, step_indicator_img: str = None
35155
) -> Container:
156+
"""Function to get the title header element for the right side view."""
36157
if info_button:
37158
content = Row([Text(f"{title}", style="titleLarge"), info_button])
38159
else:

tests/test_app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Author: Tobias Sterbak
1515

1616
import flet as ft
17+
from openandroidinstaller.views import InstallView
1718
from openandroidinstaller.openandroidinstaller import main
1819

1920

@@ -41,6 +42,7 @@ def test_app():
4142
# test if you can go through all views
4243
state = page.controls[0].state
4344
state.load_config(device_code="sargo")
44-
for _ in range(len(state.steps) + 5):
45+
state.default_views.extend(state.addon_views)
46+
for _ in range(len(state.steps) + 7):
4547
page.controls[0].to_next_view(None)
4648
assert "SuccessView" in str(page.controls[0].view.controls[0])

tests/test_progress_bar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717
from flet import Container
1818

19-
from openandroidinstaller.views.step_view import ProgressIndicator
19+
from openandroidinstaller.widgets import ProgressIndicator
2020

2121

2222
def test_init():

tests/test_terminal_box.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717
from flet import Container, Page
1818

19-
from openandroidinstaller.views.step_view import TerminalBox
19+
from openandroidinstaller.widgets import TerminalBox
2020

2121

2222
def test_init_box():
@@ -30,7 +30,7 @@ def test_init_box():
3030
def test_write_lines(mocker):
3131
"""Test if we can write lines to the terminal and bools are ignored."""
3232
mocker.patch(
33-
"openandroidinstaller.views.step_view.TerminalBox.update",
33+
"openandroidinstaller.widgets.TerminalBox.update",
3434
return_value=True,
3535
new_callable=mocker.Mock,
3636
)
@@ -49,7 +49,7 @@ def test_write_lines(mocker):
4949
def test_toggle_visibility(mocker):
5050
"""Test if the visibility toggle method works."""
5151
mocker.patch(
52-
"openandroidinstaller.views.step_view.TerminalBox.update",
52+
"openandroidinstaller.widgets.TerminalBox.update",
5353
return_value=True,
5454
new_callable=mocker.Mock,
5555
)
@@ -72,7 +72,7 @@ def test_toggle_visibility(mocker):
7272
def test_clear_terminal(mocker):
7373
"""Test if the terminal can be cleared properly."""
7474
mocker.patch(
75-
"openandroidinstaller.views.step_view.TerminalBox.update",
75+
"openandroidinstaller.widgets.TerminalBox.update",
7676
return_value=True,
7777
new_callable=mocker.Mock,
7878
)

0 commit comments

Comments
 (0)