From 072edd091e75c0d0b3b36e5171454e73cb038c15 Mon Sep 17 00:00:00 2001
From: Marcin Sobczyk <msobczyk@syncad.com>
Date: Wed, 26 Feb 2025 16:46:45 +0100
Subject: [PATCH] Use world load_profile helper on unlock in tui

---
 clive/__private/core/world.py               |  9 +++++++--
 clive/__private/ui/screens/unlock/unlock.py | 13 +++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/clive/__private/core/world.py b/clive/__private/core/world.py
index 6a6d2c1096..698e078c7f 100644
--- a/clive/__private/core/world.py
+++ b/clive/__private/core/world.py
@@ -26,6 +26,7 @@ from clive.exceptions import ProfileNotLoadedError
 
 if TYPE_CHECKING:
     from collections.abc import Iterable
+    from datetime import timedelta
     from types import TracebackType
 
     from typing_extensions import Self
@@ -203,9 +204,13 @@ class World:
         await self.switch_profile(profile)
         await self.commands.sync_state_with_beekeeper()
 
-    async def load_profile(self, profile_name: str, password: str) -> None:
+    async def load_profile(
+        self, profile_name: str, password: str, *, time: timedelta | None = None, permanent: bool = True
+    ) -> None:
         assert not self.app_state.is_unlocked, "Application is already unlocked"
-        await self.commands.unlock(profile_name=profile_name, password=password, permanent=True)
+        (
+            await self.commands.unlock(profile_name=profile_name, password=password, time=time, permanent=permanent)
+        ).raise_if_error_occurred()
         await self.load_profile_based_on_beekepeer()
 
     async def switch_profile(self, new_profile: Profile | None) -> None:
diff --git a/clive/__private/ui/screens/unlock/unlock.py b/clive/__private/ui/screens/unlock/unlock.py
index df00784f98..09886023e1 100644
--- a/clive/__private/ui/screens/unlock/unlock.py
+++ b/clive/__private/ui/screens/unlock/unlock.py
@@ -4,6 +4,7 @@ import contextlib
 from datetime import timedelta
 from typing import TYPE_CHECKING
 
+from beekeepy.exceptions import InvalidPasswordError
 from textual import on
 from textual.app import InvalidModeError
 from textual.containers import Horizontal
@@ -12,6 +13,7 @@ from textual.widgets import Button, Checkbox, Static
 
 from clive.__private.core.constants.tui.messages import PRESS_HELP_MESSAGE
 from clive.__private.core.profile import Profile
+from clive.__private.logger import logger
 from clive.__private.ui.clive_widget import CliveWidget
 from clive.__private.ui.forms.create_profile.create_profile_form import CreateProfileForm
 from clive.__private.ui.get_css import get_relative_css_path
@@ -113,17 +115,20 @@ class Unlock(BaseScreen):
         if not password_input.validate_passed() or not lock_after_time.is_valid:
             return
 
-        if not (
-            await self.commands.unlock(
+        try:
+            await self.world.load_profile(
                 profile_name=select_profile.value_ensure,
                 password=password_input.value_or_error,
                 permanent=lock_after_time.should_stay_unlocked,
                 time=lock_after_time.lock_duration,
             )
-        ).success:
+        except InvalidPasswordError:
+            logger.error(
+                f"Profile `{select_profile.value_ensure}` was not unlocked "
+                "because entered password is invalid, skipping switching modes"
+            )
             return
 
-        await self.world.load_profile_based_on_beekepeer()
         await self.app.switch_mode("dashboard")
         self._remove_welcome_modes()
         self.app.update_alarms_data_on_newest_node_data(suppress_cancelled_error=True)
-- 
GitLab