Skip to content

Commit

Permalink
refactor(housekeeping): improve store imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Sep 3, 2024
1 parent f94cfc7 commit 87d2a36
Show file tree
Hide file tree
Showing 43 changed files with 338 additions and 329 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- fix(lightdm): install raspberrypi-ui-mods instead of lightdm to activate wayland and enable rpi-connect screen sharing
- test: fix an issue in tests which caused minor random store state changes, ruining snapshot assertions
- test: add vscode and rpi-connect services to `test_all_services` test
- refactor(housekeeping): improve store imports

## Version 0.15.11

Expand Down
32 changes: 16 additions & 16 deletions tests/flows/test_wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def strength() -> int:

from ubo_app.menu_app.menu import MenuApp
from ubo_app.store.core import ChooseMenuItemByIconEvent, ChooseMenuItemByLabelEvent
from ubo_app.store.main import dispatch, store
from ubo_app.store.main import store
from ubo_app.store.services.keypad import Key, KeypadKeyPressAction

def store_snapshot_selector(state: RootState) -> WiFiState:
Expand Down Expand Up @@ -83,52 +83,52 @@ def check_icon(expected_icon: str) -> None:
store_snapshot.take(selector=store_snapshot_selector)

# Select the main menu
dispatch(ChooseMenuItemByIconEvent(icon='󰍜'))
store.dispatch(ChooseMenuItemByIconEvent(icon='󰍜'))
await stability()

# Select the settings menu
dispatch(ChooseMenuItemByLabelEvent(label='Settings'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Settings'))
await stability()

# Go to network category
dispatch(ChooseMenuItemByLabelEvent(label='Network'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Network'))
await stability()

# Open the wireless menu
dispatch(ChooseMenuItemByLabelEvent(label='WiFi'))
store.dispatch(ChooseMenuItemByLabelEvent(label='WiFi'))
await stability()
window_snapshot.take()

# Select "Select" to open the wireless connection list
dispatch(ChooseMenuItemByLabelEvent(label='Select'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Select'))
await stability()

# Back to the wireless menu
dispatch(KeypadKeyPressAction(key=Key.BACK))
store.dispatch(KeypadKeyPressAction(key=Key.BACK))
await stability()

# Select "Add" to add a new connection
dispatch(ChooseMenuItemByLabelEvent(label='Add'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Add'))
await stability()
window_snapshot.take()

# Set QR Code image of the WiFi credentials before camera is started
camera.set_image('qrcode/wifi')

# Select "QR code" to scan a QR code for credentials
dispatch(ChooseMenuItemByIconEvent(icon='󰄀'))
store.dispatch(ChooseMenuItemByIconEvent(icon='󰄀'))

# Success notification should be shown
window_snapshot.take()

# Dismiss the notification informing the user that the connection was added
await check_icon('󰤨')
await wait_for_menu_item(label='', icon='󰆴')
dispatch(ChooseMenuItemByIconEvent(icon='󰆴'))
store.dispatch(ChooseMenuItemByIconEvent(icon='󰆴'))
await stability()

# Select "Select" to open the wireless connection list and see the new connection
dispatch(ChooseMenuItemByLabelEvent(label='Select'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Select'))

@wait_for(wait=wait_fixed(1), run_async=True)
def check_connections() -> None:
Expand All @@ -143,28 +143,28 @@ def check_connections() -> None:
window_snapshot.take()

# Select the connection
dispatch(ChooseMenuItemByLabelEvent(label='ubo-test-ssid'))
store.dispatch(ChooseMenuItemByLabelEvent(label='ubo-test-ssid'))

# Wait for the "Disconnect" item to show up
await wait_for_menu_item(label='Disconnect')
await stability()
window_snapshot.take()
dispatch(ChooseMenuItemByLabelEvent(label='Disconnect'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Disconnect'))

# Wait for the "Connect" item to show up
await wait_for_menu_item(label='Connect')
await check_icon('󰖪')
await stability()
store_snapshot.take(selector=store_snapshot_selector)
window_snapshot.take()
dispatch(ChooseMenuItemByLabelEvent(label='Connect'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Connect'))

await wait_for_menu_item(label='Disconnect')
await check_icon('󰤨')
await stability()
store_snapshot.take(selector=store_snapshot_selector)
window_snapshot.take()
dispatch(ChooseMenuItemByLabelEvent(label='Delete'))
store.dispatch(ChooseMenuItemByLabelEvent(label='Delete'))

@wait_for(wait=wait_fixed(1), run_async=True)
def check_no_connections() -> None:
Expand All @@ -179,7 +179,7 @@ def check_no_connections() -> None:
# Dismiss the notification informing the user that the connection was deleted
await wait_for_menu_item(label='', icon='󰆴')
window_snapshot.take()
dispatch(ChooseMenuItemByIconEvent(icon='󰆴'))
store.dispatch(ChooseMenuItemByIconEvent(icon='󰆴'))

await wait_for_empty_menu(placeholder='No Wi-Fi connections found')
window_snapshot.take()
Expand Down
4 changes: 2 additions & 2 deletions ubo_app/load_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __init__(
self.module = None

def register_reducer(self: UboServiceThread, reducer: ReducerType) -> None:
from ubo_app.store.main import dispatch, root_reducer_id
from ubo_app.store.main import root_reducer_id, store

logger.info(
'Registering ubo service reducer',
Expand All @@ -187,7 +187,7 @@ def register_reducer(self: UboServiceThread, reducer: ReducerType) -> None:
},
)

dispatch(
store.dispatch(
CombineReducerRegisterAction(
_id=root_reducer_id,
key=self.service_id,
Expand Down
4 changes: 2 additions & 2 deletions ubo_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def main() -> None:
logger.exception('An error occurred while running the app.')
from redux import FinishAction

from ubo_app.store.main import dispatch
from ubo_app.store.main import store

dispatch(FinishAction())
store.dispatch(FinishAction())


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions ubo_app/menu_app/home_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ubo_gui.page import PageWidget
from ubo_gui.volume import VolumeWidget

from ubo_app.store.main import autorun
from ubo_app.store.main import store

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -41,7 +41,9 @@ def __init__(
self.volume_widget = VolumeWidget()
self.ids.right_column.add_widget(self.volume_widget)

autorun(lambda state: state.audio.playback_volume)(self._sync_output_volume)
store.autorun(lambda state: state.audio.playback_volume)(
self._sync_output_volume,
)

def _sync_output_volume(self: HomePage, selector_result: float) -> None:
self.volume_widget.value = selector_result * 100
Expand Down
34 changes: 23 additions & 11 deletions ubo_app/menu_app/menu_central.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
OpenApplicationEvent,
SetMenuPathAction,
)
from ubo_app.store.main import autorun, dispatch, subscribe_event
from ubo_app.store.main import store
from ubo_app.store.services.keypad import Key, KeypadKeyPressEvent
from ubo_app.store.services.notifications import NotificationsDisplayEvent
from ubo_app.store.update_manager import UpdateManagerSetUpdateServiceStatusAction
Expand Down Expand Up @@ -54,7 +54,7 @@ def _render_menu(self: MenuWidgetWithHomePage, menu: Menu) -> PageWidget | None:


def set_path(_: MenuWidget, stack: list[StackItem]) -> None:
dispatch(
store.dispatch(
SetMenuPathAction(
path=[
stack_item.selection.key
Expand All @@ -76,7 +76,7 @@ def __init__(self: MenuAppCentral, **kwargs: object) -> None:

_self = weakref.ref(self)

@autorun(lambda state: state.main.menu)
@store.autorun(lambda state: state.main.menu)
@debounce(0.1, DebounceOptions(leading=True, trailing=True, time_window=0.1))
@mainthread
def _(menu: Menu | None) -> None:
Expand All @@ -91,7 +91,7 @@ def build(self: UboApp) -> Widget | None:
self.menu_widget.padding_bottom = root.ids.footer_layout.height

def check_update(status: str) -> None:
dispatch(
store.dispatch(
UpdateManagerSetUpdateServiceStatusAction(
is_active=status in ('active', 'activating', 'reloading'),
),
Expand Down Expand Up @@ -129,27 +129,39 @@ def central(self: MenuAppCentral) -> Widget | None:
menu_representation = 'Menu:\n' + repr(self.menu_widget)
self.menu_widget.bind(stack=lambda *_: logger.info(menu_representation))

subscribe_event(
store.subscribe_event(
KeypadKeyPressEvent,
self.handle_key_press_event,
keep_ref=False,
)

subscribe_event(
store.subscribe_event(
NotificationsDisplayEvent,
self.display_notification,
keep_ref=False,
)

subscribe_event(OpenApplicationEvent, self.open_application, keep_ref=False)
subscribe_event(CloseApplicationEvent, self.close_application, keep_ref=False)
subscribe_event(ChooseMenuItemByIconEvent, self.select_by_icon, keep_ref=False)
subscribe_event(
store.subscribe_event(
OpenApplicationEvent,
self.open_application,
keep_ref=False,
)
store.subscribe_event(
CloseApplicationEvent,
self.close_application,
keep_ref=False,
)
store.subscribe_event(
ChooseMenuItemByIconEvent,
self.select_by_icon,
keep_ref=False,
)
store.subscribe_event(
ChooseMenuItemByLabelEvent,
self.select_by_label,
keep_ref=False,
)
subscribe_event(
store.subscribe_event(
ChooseMenuItemByIndexEvent,
self.select_by_index,
keep_ref=False,
Expand Down
10 changes: 5 additions & 5 deletions ubo_app/menu_app/menu_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from redux import AutorunOptions
from ubo_gui.app import UboApp

from ubo_app.store.main import autorun
from ubo_app.store.main import store

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -48,7 +48,7 @@ def temperature_widget(self: MenuAppFooter) -> BoxLayout:
or setattr(layout, 'width', temperature.width + dp(12)),
)

autorun(
store.autorun(
lambda state: state.sensors.temperature.value,
options=AutorunOptions(keep_ref=False),
)(self.set_temperature_value)
Expand Down Expand Up @@ -91,7 +91,7 @@ def light_widget(self: MenuAppFooter) -> Label:
),
)

autorun(
store.autorun(
lambda state: state.sensors.light.value,
options=AutorunOptions(keep_ref=False),
)(self.set_light_value)
Expand Down Expand Up @@ -216,12 +216,12 @@ def footer(self: MenuAppFooter) -> Widget | None:
x=self.set_icons_layout_x,
)

autorun(
store.autorun(
lambda state: state.status_icons.icons,
options=AutorunOptions(keep_ref=False),
)(self.render_icons)

autorun(
store.autorun(
lambda state: state.main.path,
options=AutorunOptions(keep_ref=False),
)(self.handle_depth_change)
Expand Down
4 changes: 2 additions & 2 deletions ubo_app/menu_app/menu_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ubo_gui.app import UboApp
from ubo_gui.progress_ring import ProgressRingWidget

from ubo_app.store.main import autorun
from ubo_app.store.main import store

if TYPE_CHECKING:
from kivy.uix.widget import Widget
Expand All @@ -37,7 +37,7 @@ def header(self: MenuAppHeader) -> Widget | None:
)
self.header_layout.add_widget(progress_layout)

@autorun(
@store.autorun(
lambda state: [
notification
for notification in state.notifications.notifications
Expand Down
22 changes: 13 additions & 9 deletions ubo_app/menu_app/menu_notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from ubo_app.menu_app.notification_info import NotificationInfo
from ubo_app.store.core import CloseApplicationEvent, OpenApplicationEvent
from ubo_app.store.main import dispatch, subscribe_event
from ubo_app.store.main import store
from ubo_app.store.services.notifications import (
Notification,
NotificationActionItem,
Expand Down Expand Up @@ -74,9 +74,11 @@ def close(_: object = None) -> None:
for unsubscribe in subscriptions:
unsubscribe()
notification_application.unbind(on_close=close)
dispatch(CloseApplicationEvent(application=notification_application))
store.dispatch(CloseApplicationEvent(application=notification_application))
if notification.value.dismiss_on_close:
dispatch(NotificationsClearAction(notification=notification.value))
store.dispatch(
NotificationsClearAction(notification=notification.value),
)
if notification.value.on_close:
notification.value.on_close()

Expand Down Expand Up @@ -108,7 +110,7 @@ def renew_notification(event: NotificationsDisplayEvent) -> None:
!= notification.value.extra_information
):
notification.is_initialized = True
dispatch(
store.dispatch(
VoiceReadTextAction(
text=event.notification.extra_information.text,
piper_text=event.notification.extra_information.piper_text,
Expand All @@ -128,22 +130,22 @@ def renew_notification(event: NotificationsDisplayEvent) -> None:
notification_application.bind(on_close=close)

subscriptions.append(
subscribe_event(
store.subscribe_event(
NotificationsClearEvent,
clear_notification,
),
)
if notification.value.id is not None:
subscriptions.append(
subscribe_event(
store.subscribe_event(
NotificationsDisplayEvent,
renew_notification,
),
)

renew_notification(event)

dispatch(OpenApplicationEvent(application=notification_application))
store.dispatch(OpenApplicationEvent(application=notification_application))

def _notification_items(
self: MenuNotificationHandler,
Expand All @@ -153,7 +155,9 @@ def _notification_items(
def dismiss(_: object = None) -> None:
close()
if not notification.value.dismiss_on_close:
dispatch(NotificationsClearAction(notification=notification.value))
store.dispatch(
NotificationsClearAction(notification=notification.value),
)

def run_notification_action(action: NotificationActionItem) -> None:
result = action.action()
Expand All @@ -171,7 +175,7 @@ def run_notification_action(action: NotificationActionItem) -> None:
def open_info() -> None:
info_application = NotificationInfo(text=text)

dispatch(OpenApplicationEvent(application=info_application))
store.dispatch(OpenApplicationEvent(application=info_application))

items.append(
NotificationActionItem(
Expand Down
Loading

0 comments on commit 87d2a36

Please sign in to comment.