|
| 1 | +"""This file contains a class and function to manage the app state over various steps.""" |
| 2 | + |
| 3 | +# This file is part of OpenAndroidInstaller. |
| 4 | +# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of |
| 5 | +# the GNU General Public License as published by the Free Software Foundation, |
| 6 | +# either version 3 of the License, or (at your option) any later version. |
| 7 | + |
| 8 | +# OpenAndroidInstaller is distributed in the hope that it will be useful, but WITHOUT ANY |
| 9 | +# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 | + |
| 12 | +# You should have received a copy of the GNU General Public License along with OpenAndroidInstaller. |
| 13 | +# If not, see <https://www.gnu.org/licenses/>.""" |
| 14 | +# Author: Tobias Sterbak |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +from flet import ProgressBar |
| 19 | +from installer_config import _load_config |
| 20 | +from loguru import logger |
| 21 | + |
| 22 | + |
| 23 | +class AppState: |
| 24 | + """Container class to store the state of the application.""" |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + platform: str, |
| 29 | + config_path: Path, |
| 30 | + bin_path: Path, |
| 31 | + progressbar: ProgressBar, |
| 32 | + num_steps: int, |
| 33 | + test: bool = False, |
| 34 | + test_config: str = None, |
| 35 | + ): |
| 36 | + self.platform = platform |
| 37 | + self.config_path = config_path |
| 38 | + self.bin_path = bin_path |
| 39 | + self.progressbar = progressbar |
| 40 | + self.num_steps = num_steps # keep track of the steps already taken |
| 41 | + self.test = test |
| 42 | + self.test_config = test_config |
| 43 | + |
| 44 | + # initialize the progress bar at 0 |
| 45 | + self.progressbar.value = 0 |
| 46 | + |
| 47 | + # placeholders |
| 48 | + self.advanced = False |
| 49 | + self.config = None |
| 50 | + self.image_path = None |
| 51 | + self.recovery_path = None |
| 52 | + |
| 53 | + # is this still needed? |
| 54 | + self.steps = None |
| 55 | + |
| 56 | + def load_config(self, device_code: str): |
| 57 | + """Load the config from file to state by device code.""" |
| 58 | + self.config = _load_config(device_code, self.config_path) |
| 59 | + if self.config: |
| 60 | + self.steps = ( |
| 61 | + self.config.unlock_bootloader |
| 62 | + + self.config.flash_recovery |
| 63 | + + self.config.install_os |
| 64 | + ) |
| 65 | + self.num_total_steps = len(self.steps) |
| 66 | + |
| 67 | + def increment_progressbar(self): |
| 68 | + """Increment the progressbar and step counter.""" |
| 69 | + self.progressbar.value = (self.num_steps - 1) / ( |
| 70 | + self.num_total_steps + 2 |
| 71 | + ) # don't show on the first step |
| 72 | + self.num_steps += 1 # increase the step counter |
0 commit comments