From f784865b5c251dc22703b0be12ee9c03168ff68b Mon Sep 17 00:00:00 2001 From: Osama Mohammed Al-zabidi <57198110+omamkaz@users.noreply.github.com> Date: Mon, 16 Dec 2024 23:09:26 +0300 Subject: [PATCH 1/2] Update flashlight.py ## Just some tricks that I think might improve the code! :) --- .../packages/flet/src/flet/core/flashlight.py | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/sdk/python/packages/flet/src/flet/core/flashlight.py b/sdk/python/packages/flet/src/flet/core/flashlight.py index c31b9e41d..0cb6629c3 100644 --- a/sdk/python/packages/flet/src/flet/core/flashlight.py +++ b/sdk/python/packages/flet/src/flet/core/flashlight.py @@ -42,41 +42,39 @@ def __init__( def _get_control_name(self): return "flashlight" + def _toggle_state(self, sr: str, on: bool=True) -> bool: + self.turned_on = on if ("1" == sr) else self.turned_on + return self.turned_on + def turn_on(self, wait_timeout: Optional[int] = 5) -> bool: sr = self.invoke_method("on", wait_for_result=True, wait_timeout=wait_timeout) + return self._toggle_state(sr) - if int(sr) == 1: - self.turned_on = True - return self.turned_on async def turn_on_async(self, wait_timeout: Optional[int] = 5) -> bool: sr = await self.invoke_method_async( "on", wait_for_result=True, wait_timeout=wait_timeout ) - if int(sr) == 1: - self.turned_on = True - return self.turned_on + return self._toggle_state(sr) + def turn_off(self, wait_timeout: Optional[int] = 5) -> bool: sr = self.invoke_method("off", wait_for_result=True, wait_timeout=wait_timeout) - - if int(sr) == 1: - self.turned_on = False - return self.turned_on + return self._toggle_state(sr, False) + async def turn_off_async(self, wait_timeout: Optional[int] = 5) -> bool: sr = await self.invoke_method_async( "off", wait_for_result=True, wait_timeout=wait_timeout ) - if int(sr) == 1: - self.turned_on = False - return self.turned_on + return self._toggle_state(sr, False) + def toggle(self, wait_timeout: Optional[int] = 5) -> bool: - if self.turned_on: - return self.turn_off(wait_timeout) - return self.turn_on(wait_timeout) + _func = self.turn_off if self.turned_on else self.turn_on + return _func(wait_timeout) + async def toggle_async(self, wait_timeout: Optional[int] = 5) -> bool: if self.turned_on: return await self.turn_off_async(wait_timeout) From 5239c8419d6139af4801adc2ebd53cf160323569 Mon Sep 17 00:00:00 2001 From: Osama Mohammed Al-zabidi <57198110+omamkaz@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:24:39 +0300 Subject: [PATCH 2/2] Update flashlight.py --- sdk/python/packages/flet/src/flet/core/flashlight.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/python/packages/flet/src/flet/core/flashlight.py b/sdk/python/packages/flet/src/flet/core/flashlight.py index 0cb6629c3..46feed866 100644 --- a/sdk/python/packages/flet/src/flet/core/flashlight.py +++ b/sdk/python/packages/flet/src/flet/core/flashlight.py @@ -42,8 +42,9 @@ def __init__( def _get_control_name(self): return "flashlight" - def _toggle_state(self, sr: str, on: bool=True) -> bool: - self.turned_on = on if ("1" == sr) else self.turned_on + def _toggle_state(self, sr: str, state: bool=True) -> bool: + if ("1" == sr): + self.turned_on = state return self.turned_on def turn_on(self, wait_timeout: Optional[int] = 5) -> bool: