From 4efa9f7ad7113ed35c503b984683e2eebec4c03c Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Thu, 9 Feb 2023 20:56:10 +0000 Subject: [PATCH] Move modules around and improve tests --- .../views/install_addons_view.py | 3 +- openandroidinstaller/views/install_view.py | 3 +- openandroidinstaller/views/step_view.py | 123 +----------------- openandroidinstaller/widgets.py | 121 +++++++++++++++++ tests/test_app.py | 4 +- tests/test_progress_bar.py | 2 +- tests/test_terminal_box.py | 8 +- 7 files changed, 135 insertions(+), 129 deletions(-) diff --git a/openandroidinstaller/views/install_addons_view.py b/openandroidinstaller/views/install_addons_view.py index 3c8a15ea..7bb71e42 100644 --- a/openandroidinstaller/views/install_addons_view.py +++ b/openandroidinstaller/views/install_addons_view.py @@ -34,8 +34,9 @@ from widgets import ( confirm_button, get_title, + TerminalBox, + ProgressIndicator, ) -from views.step_view import TerminalBox, ProgressIndicator class InstallAddonsView(BaseView): diff --git a/openandroidinstaller/views/install_view.py b/openandroidinstaller/views/install_view.py index 3f2ae4b8..e7924f30 100644 --- a/openandroidinstaller/views/install_view.py +++ b/openandroidinstaller/views/install_view.py @@ -34,8 +34,9 @@ from widgets import ( confirm_button, get_title, + TerminalBox, + ProgressIndicator, ) -from views.step_view import TerminalBox, ProgressIndicator class InstallView(BaseView): diff --git a/openandroidinstaller/views/step_view.py b/openandroidinstaller/views/step_view.py index e9557ef4..c226ef5c 100644 --- a/openandroidinstaller/views/step_view.py +++ b/openandroidinstaller/views/step_view.py @@ -17,21 +17,15 @@ from time import sleep from typing import Callable from functools import partial -import regex as re from flet import ( - UserControl, Column, ElevatedButton, Row, Text, icons, TextField, - Container, Switch, - alignment, - ProgressBar, - ProgressRing, colors, ) @@ -58,6 +52,8 @@ confirm_button, get_title, link_button, + TerminalBox, + ProgressIndicator, ) @@ -264,118 +260,3 @@ def call_to_phone(self, e, command: str): # reset the progress indicator (let the progressbar stay for the install command) self.progress_indicator.clear() self.view.update() - - -class TerminalBox(UserControl): - def __init__(self, expand: bool = True, visible: bool = False): - super().__init__(expand=expand) - self.visible = visible - - def build(self): - self._box = Container( - content=Column(scroll="auto", expand=True, auto_scroll=True), - margin=10, - padding=10, - alignment=alignment.top_left, - bgcolor=colors.BLACK38, - height=300, - border_radius=2, - expand=True, - visible=self.visible, - ) - return self._box - - def write_line(self, line: str): - """ - Write the line to the window box and update. - - Ignores empty lines. - """ - if (type(line) == str) and line.strip(): - self._box.content.controls.append(Text(f">{line.strip()}", selectable=True)) - self.update() - - def toggle_visibility(self): - """Toggle the visibility of the terminal box.""" - self._box.visible = not self._box.visible - self.visible = not self.visible - self.update() - - def clear(self): - """Clear terminal output.""" - self._box.content.controls = [] - self.update() - - def update(self): - """Update the view.""" - self._box.update() - - -class ProgressIndicator(UserControl): - def __init__(self, expand: bool = True): - super().__init__(expand=expand) - # placeholder for the flashing progressbar - self.progress_bar = None - # progress ring to display - self.progress_ring = None - - def build(self): - self._container = Container( - content=Column(scroll="auto", expand=True), - margin=10, - alignment=alignment.center, - height=50, - expand=True, - visible=True, - ) - return self._container - - def display_progress_bar(self, line: str): - """Display and update the progress bar for the given line.""" - percentage_done = None - result = None - # get the progress numbers from the output lines - if (type(line) == str) and line.strip(): - result = re.search( - r"\(\~(\d{1,3})\%\)|(Total xfer:|adb: failed to read command: Success)", - line.strip(), - ) - if result: - if result.group(2): - percentage_done = 100 - elif result.group(1): - percentage_done = int(result.group(1)) - - # create the progress bar on first occurrence - if percentage_done == 0: - self.progress_bar = ProgressBar( - width=500, bar_height=32, color="#00d886", bgcolor="#eeeeee" - ) - self.percentage_text = Text(f"{percentage_done}%") - self._container.content.controls.append( - Row([self.percentage_text, self.progress_bar]) - ) - # update the progress bar - if self.progress_bar: - self.progress_bar.value = percentage_done / 100 - self.percentage_text.value = f"{percentage_done}%" - - def display_progress_ring( - self, - ): - """Display a progress ring to signal progress.""" - if not self.progress_ring: - self.progress_ring = ProgressRing(color="#00d886") - self._container.content.controls.append(self.progress_ring) - self._container.update() - - def clear(self): - """Clear output.""" - self._container.content.controls = [] - self.progress_ring = None - self.progress_bar = None - self.update() - - def update(self): - """Update the view.""" - self._container.update() diff --git a/openandroidinstaller/widgets.py b/openandroidinstaller/widgets.py index 4f0a8c35..7b2b562b 100644 --- a/openandroidinstaller/widgets.py +++ b/openandroidinstaller/widgets.py @@ -14,12 +14,17 @@ # Author: Tobias Sterbak import webbrowser +import regex as re from functools import partial from typing import Callable from flet import ( + UserControl, + colors, Container, ElevatedButton, + ProgressRing, + ProgressBar, Row, Text, alignment, @@ -30,9 +35,125 @@ ) +class TerminalBox(UserControl): + def __init__(self, expand: bool = True, visible: bool = False): + super().__init__(expand=expand) + self.visible = visible + + def build(self): + self._box = Container( + content=Column(scroll="auto", expand=True, auto_scroll=True), + margin=10, + padding=10, + alignment=alignment.top_left, + bgcolor=colors.BLACK38, + height=300, + border_radius=2, + expand=True, + visible=self.visible, + ) + return self._box + + def write_line(self, line: str): + """ + Write the line to the window box and update. + + Ignores empty lines. + """ + if (type(line) == str) and line.strip(): + self._box.content.controls.append(Text(f">{line.strip()}", selectable=True)) + self.update() + + def toggle_visibility(self): + """Toggle the visibility of the terminal box.""" + self._box.visible = not self._box.visible + self.visible = not self.visible + self.update() + + def clear(self): + """Clear terminal output.""" + self._box.content.controls = [] + self.update() + + def update(self): + """Update the view.""" + self._box.update() + + +class ProgressIndicator(UserControl): + def __init__(self, expand: bool = True): + super().__init__(expand=expand) + # placeholder for the flashing progressbar + self.progress_bar = None + # progress ring to display + self.progress_ring = None + + def build(self): + self._container = Container( + content=Column(scroll="auto", expand=True), + margin=10, + alignment=alignment.center, + height=50, + expand=True, + visible=True, + ) + return self._container + + def display_progress_bar(self, line: str): + """Display and update the progress bar for the given line.""" + percentage_done = None + result = None + # get the progress numbers from the output lines + if (type(line) == str) and line.strip(): + result = re.search( + r"\(\~(\d{1,3})\%\)|(Total xfer:|adb: failed to read command: Success)", + line.strip(), + ) + if result: + if result.group(2): + percentage_done = 100 + elif result.group(1): + percentage_done = int(result.group(1)) + + # create the progress bar on first occurrence + if percentage_done == 0: + self.progress_bar = ProgressBar( + width=500, bar_height=32, color="#00d886", bgcolor="#eeeeee" + ) + self.percentage_text = Text(f"{percentage_done}%") + self._container.content.controls.append( + Row([self.percentage_text, self.progress_bar]) + ) + # update the progress bar + if self.progress_bar: + self.progress_bar.value = percentage_done / 100 + self.percentage_text.value = f"{percentage_done}%" + + def display_progress_ring( + self, + ): + """Display a progress ring to signal progress.""" + if not self.progress_ring: + self.progress_ring = ProgressRing(color="#00d886") + self._container.content.controls.append(self.progress_ring) + self._container.update() + + def clear(self): + """Clear output.""" + self._container.content.controls = [] + self.progress_ring = None + self.progress_bar = None + self.update() + + def update(self): + """Update the view.""" + self._container.update() + + def get_title( title: str, info_button: IconButton = None, step_indicator_img: str = None ) -> Container: + """Function to get the title header element for the right side view.""" if info_button: content = Row([Text(f"{title}", style="titleLarge"), info_button]) else: diff --git a/tests/test_app.py b/tests/test_app.py index e8504b74..c719fbf1 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -14,6 +14,7 @@ # Author: Tobias Sterbak import flet as ft +from openandroidinstaller.views import InstallView from openandroidinstaller.openandroidinstaller import main @@ -41,6 +42,7 @@ def test_app(): # test if you can go through all views state = page.controls[0].state state.load_config(device_code="sargo") - for _ in range(len(state.steps) + 5): + state.default_views.extend(state.addon_views) + for _ in range(len(state.steps) + 7): page.controls[0].to_next_view(None) assert "SuccessView" in str(page.controls[0].view.controls[0]) diff --git a/tests/test_progress_bar.py b/tests/test_progress_bar.py index c0691ecb..5b1b1fc8 100644 --- a/tests/test_progress_bar.py +++ b/tests/test_progress_bar.py @@ -16,7 +16,7 @@ import pytest from flet import Container -from openandroidinstaller.views.step_view import ProgressIndicator +from openandroidinstaller.widgets import ProgressIndicator def test_init(): diff --git a/tests/test_terminal_box.py b/tests/test_terminal_box.py index f4a969db..526254c5 100644 --- a/tests/test_terminal_box.py +++ b/tests/test_terminal_box.py @@ -16,7 +16,7 @@ import pytest from flet import Container, Page -from openandroidinstaller.views.step_view import TerminalBox +from openandroidinstaller.widgets import TerminalBox def test_init_box(): @@ -30,7 +30,7 @@ def test_init_box(): def test_write_lines(mocker): """Test if we can write lines to the terminal and bools are ignored.""" mocker.patch( - "openandroidinstaller.views.step_view.TerminalBox.update", + "openandroidinstaller.widgets.TerminalBox.update", return_value=True, new_callable=mocker.Mock, ) @@ -49,7 +49,7 @@ def test_write_lines(mocker): def test_toggle_visibility(mocker): """Test if the visibility toggle method works.""" mocker.patch( - "openandroidinstaller.views.step_view.TerminalBox.update", + "openandroidinstaller.widgets.TerminalBox.update", return_value=True, new_callable=mocker.Mock, ) @@ -72,7 +72,7 @@ def test_toggle_visibility(mocker): def test_clear_terminal(mocker): """Test if the terminal can be cleared properly.""" mocker.patch( - "openandroidinstaller.views.step_view.TerminalBox.update", + "openandroidinstaller.widgets.TerminalBox.update", return_value=True, new_callable=mocker.Mock, )