Skip to content

Commit

Permalink
Display toast when seed already exists
Browse files Browse the repository at this point in the history
Fixes / required changes:
* Issue #585
  • Loading branch information
alvroble committed Aug 4, 2024
1 parent 100a974 commit 0e21b45
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/seedsigner/gui/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def toggle_renderer_lock(self):
def run(self):
logger.info(f"{self.__class__.__name__}: started")
start = time.time()
time.sleep(0.1)
while time.time() - start < self.activation_delay:
if self.hw_inputs.has_any_input():
# User has pressed a button, cancel the toast
Expand Down Expand Up @@ -230,3 +231,22 @@ def instantiate_toast(self) -> ToastOverlay:
icon_name=SeedSignerIconConstants.MICROSD,
label_text=self.message,
)

class AlreadyLoadedSeedToastManagerThread(BaseToastOverlayManagerThread):
def __init__(self, activation_delay=0):
# Note: activation_delay is configurable so the screenshot generator can get the
# toast to immediately render.
super().__init__(
activation_delay=activation_delay, # seconds
duration=5, # seconds
)


def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.WARNING,
label_text="Seed already in memory",
font_size=GUIConstants.BODY_FONT_SIZE,
height=GUIConstants.BODY_FONT_SIZE * 2 + GUIConstants.BODY_LINE_SPACING + GUIConstants.EDGE_PADDING,
color=GUIConstants.INFO_COLOR,
)
3 changes: 3 additions & 0 deletions src/seedsigner/models/seed_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def finalize_pending_seed(self) -> int:
return index


def pending_seed_is_loaded(self) -> bool:
return self.pending_seed in self.seeds

def clear_pending_seed(self):
self.pending_seed = None

Expand Down
5 changes: 5 additions & 0 deletions src/seedsigner/views/seed_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from seedsigner.gui.screens import (RET_CODE__BACK_BUTTON, ButtonListScreen,
WarningScreen, DireWarningScreen, seed_screens)
from seedsigner.gui.screens.screen import LargeIconStatusScreen, QRDisplayScreen
from seedsigner.gui.toast import AlreadyLoadedSeedToastManagerThread
from seedsigner.helpers import embit_utils
from seedsigner.models.decode_qr import DecodeQR
from seedsigner.models.encode_qr import CompactSeedQrEncoder, GenericStaticQrEncoder, SeedQrEncoder, SpecterXPubQrEncoder, StaticXpubQrEncoder, UrXpubQrEncoder
Expand Down Expand Up @@ -325,6 +326,8 @@ def run(self):
)

if button_data[selected_menu_num] == self.FINALIZE:
if self.controller.storage.pending_seed_is_loaded():
self.controller.activate_toast(AlreadyLoadedSeedToastManagerThread())
seed_num = self.controller.storage.finalize_pending_seed()
return Destination(SeedOptionsView, view_args={"seed_num": seed_num}, clear_history=True)

Expand Down Expand Up @@ -428,6 +431,8 @@ def run(self):
return Destination(SeedAddPassphraseView)

elif button_data[selected_menu_num] == self.DONE:
if self.controller.storage.pending_seed_is_loaded():
self.controller.activate_toast(AlreadyLoadedSeedToastManagerThread())
seed_num = self.controller.storage.finalize_pending_seed()
return Destination(SeedOptionsView, view_args={"seed_num": seed_num}, clear_history=True)

Expand Down
6 changes: 5 additions & 1 deletion tests/screenshot_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from seedsigner.controller import Controller
from seedsigner.gui.renderer import Renderer
from seedsigner.gui.toast import BaseToastOverlayManagerThread, RemoveSDCardToastManagerThread, SDCardStateChangeToastManagerThread
from seedsigner.gui.toast import BaseToastOverlayManagerThread, RemoveSDCardToastManagerThread, SDCardStateChangeToastManagerThread, AlreadyLoadedSeedToastManagerThread
from seedsigner.hardware.microsd import MicroSD
from seedsigner.models.decode_qr import DecodeQR
from seedsigner.models.qr_type import QRType
Expand Down Expand Up @@ -180,6 +180,10 @@ def add_op_return_to_psbt(psbt: PSBT, raw_payload_data: bytes):
seed_views.SeedReviewPassphraseView,

(seed_views.SeedOptionsView, dict(seed_num=0)),

# Screenshot raises RuntimeError: threads can only be started once
# (seed_views.SeedOptionsView, dict(seed_num=0), "SeedOptionsView_AlreadyLoadedSeed", AlreadyLoadedSeedToastManagerThread(activation_delay=0)),

(seed_views.SeedBackupView, dict(seed_num=0)),
(seed_views.SeedExportXpubSigTypeView, dict(seed_num=0)),
(seed_views.SeedExportXpubScriptTypeView, dict(seed_num=0, sig_type="msig")),
Expand Down

0 comments on commit 0e21b45

Please sign in to comment.