Skip to content

Commit

Permalink
refactor(wifi): change the onboarding notification messages and make …
Browse files Browse the repository at this point in the history
…voice service load before wifi service by changing its priority #88
  • Loading branch information
sassanh committed May 8, 2024
1 parent 1decf5e commit 6939128
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- fix(vscode): show an indicator when it is pending url generation
- fix(core): avoid multiple initial overlaying frames #91
- feat(core): pressing the home button navigates the user to the home page #84
- refactor(wifi): change the onboarding notification messages and make voice service
load before wifi service by changing its priority #88

## Version 0.14.0

Expand Down
5 changes: 1 addition & 4 deletions ubo_app/menu_app/menu_notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def display_notification( # noqa: C901
self: MenuNotificationHandler,
event: NotificationsDisplayEvent,
) -> None:
@mainthread
def run_notification_action(action: NotificationActionItem) -> None:
if action.dismiss_notification:
dismiss()
Expand All @@ -54,9 +53,7 @@ def dismiss(_: object = None) -> None:
lambda match: match.groups()[0],
notification.extra_information or '',
)
dispatch(
VoiceReadTextAction(text=processed_voice_text),
)
dispatch(VoiceReadTextAction(text=processed_voice_text))

def open_info() -> None:
previous_iteration = notification.extra_information or ''
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import re
from asyncio import CancelledError
from threading import Lock

import pvorca
from redux import FinishEvent
Expand All @@ -21,16 +22,19 @@

class _Context:
orca_instance: pvorca.Orca | None = None
lock: Lock = Lock()

def cleanup(self: _Context) -> None:
if self.orca_instance:
self.orca_instance.delete()
self.orca_instance = None
with self.lock:
if self.orca_instance:
self.orca_instance.delete()
self.orca_instance = None

def set_access_key(self: _Context, access_key: str | None) -> None:
self.cleanup()
if access_key:
self.orca_instance = pvorca.create(access_key)
with self.lock:
if access_key:
self.orca_instance = pvorca.create(access_key)


_context = _Context()
Expand Down Expand Up @@ -64,37 +68,42 @@ def clear_access_key() -> None:

def synthesize(event: VoiceSynthesizeTextEvent) -> None:
"""Synthesize the text."""
if not _context.orca_instance:
return
rate = _context.orca_instance.sample_rate

valid_characters = re.sub(
r'[\^\$\-\\]',
lambda match: f'\\{match.group()}',
''.join(
sorted(
{i for i in _context.orca_instance.valid_characters if i not in '{|}'},
with _context.lock:
if not _context.orca_instance:
return
rate = _context.orca_instance.sample_rate

valid_characters = re.sub(
r'[\^\$\-\\]',
lambda match: f'\\{match.group()}',
''.join(
sorted(
{
i
for i in _context.orca_instance.valid_characters
if i not in '{|}'
},
),
),
),
)

def remove_disallowed_characters(text: str) -> str:
return re.sub(rf'[^{valid_characters}]', '', text)

components = re.split(r'(\{[^{|}]*\|[^{|}]*\})', event.text)
processed_components = [
remove_disallowed_characters(component) if index % 2 == 0 else component
for index, component in enumerate(components)
]
processed_text = ''.join(processed_components)
audio_sequence = _context.orca_instance.synthesize(
text=processed_text,
speech_rate=event.speech_rate,
)

dispatch(
SoundPlayAudioAction(sample=audio_sequence, channels=1, rate=rate, width=2),
)
)

def remove_disallowed_characters(text: str) -> str:
return re.sub(rf'[^{valid_characters}]', '', text)

components = re.split(r'(\{[^{|}]*\|[^{|}]*\})', event.text)
processed_components = [
remove_disallowed_characters(component) if index % 2 == 0 else component
for index, component in enumerate(components)
]
processed_text = ''.join(processed_components)
audio_sequence = _context.orca_instance.synthesize(
text=processed_text,
speech_rate=event.speech_rate,
)

dispatch(
SoundPlayAudioAction(sample=audio_sequence, channels=1, rate=rate, width=2),
)


def init_service() -> None:
Expand Down
File renamed without changes.
29 changes: 14 additions & 15 deletions ubo_app/services/030-wifi/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,27 @@ def should_show_onboarding(state: tuple[bool | None, bool]) -> bool | None:


def show_onboarding_notification() -> None:
actions = [
NotificationActionItem(
action=lambda: create_wireless_connection.CreateWirelessConnectionPage,
icon='󱚾',
background_color=INFO_COLOR,
dismiss_notification=True,
),
]
dispatch(
NotificationsAddAction(
notification=Notification(
title='No internet connection',
content='Go to settings to connect to a network.',
content='Press middle button "󱚾" to add WiFi network',
importance=Importance.MEDIUM,
icon='󱚵',
display_type=NotificationDisplayType.STICKY,
actions=actions,
extra_information='You are currently not connected to the '
'internet.\n'
'To connect, press the back button and choose the WiFi setup icon '
'"󱚾" using the left buttons.\n'
'Follow the on-screen instructions to complete the setup.',
actions=[
NotificationActionItem(
action=lambda: (
create_wireless_connection.CreateWirelessConnectionPage
),
icon='󱚾',
background_color=INFO_COLOR,
dismiss_notification=True,
),
],
extra_information="""Press middle button to add WiFi network with \
{QR|K Y UW AA R} code.\nIf you dismiss this, you can always add WiFi network through \
Settings → Network → WiFi""",
color=INFO_COLOR,
),
),
Expand Down

0 comments on commit 6939128

Please sign in to comment.