Skip to content

Commit

Permalink
Move modules around and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterbak committed Feb 9, 2023
1 parent 1e96c77 commit 4efa9f7
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 129 deletions.
3 changes: 2 additions & 1 deletion openandroidinstaller/views/install_addons_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
from widgets import (
confirm_button,
get_title,
TerminalBox,
ProgressIndicator,
)
from views.step_view import TerminalBox, ProgressIndicator


class InstallAddonsView(BaseView):
Expand Down
3 changes: 2 additions & 1 deletion openandroidinstaller/views/install_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
from widgets import (
confirm_button,
get_title,
TerminalBox,
ProgressIndicator,
)
from views.step_view import TerminalBox, ProgressIndicator


class InstallView(BaseView):
Expand Down
123 changes: 2 additions & 121 deletions openandroidinstaller/views/step_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -58,6 +52,8 @@
confirm_button,
get_title,
link_button,
TerminalBox,
ProgressIndicator,
)


Expand Down Expand Up @@ -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()
121 changes: 121 additions & 0 deletions openandroidinstaller/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Author: Tobias Sterbak

import flet as ft
from openandroidinstaller.views import InstallView
from openandroidinstaller.openandroidinstaller import main


Expand Down Expand Up @@ -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])
2 changes: 1 addition & 1 deletion tests/test_progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_terminal_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down

0 comments on commit 4efa9f7

Please sign in to comment.