diff --git a/gui/v2/infrastructure/setup_observers.py b/gui/v2/infrastructure/setup_observers.py index 06191f7..303b3a1 100755 --- a/gui/v2/infrastructure/setup_observers.py +++ b/gui/v2/infrastructure/setup_observers.py @@ -1,3 +1,4 @@ +from core.observers.ObserverStash import ObserverStash from core.observers.ApplicationVersionObserver import ApplicationVersionObserver from core.observers.ClientObserver import ClientObserver from core.observers.ConnectionObserver import ConnectionObserver @@ -110,6 +111,12 @@ def setup_observers(update_status): ticket_observer.subscribe('unknown_error', lambda event: update_status('Unknown ticket error.')) ticket_observer.subscribe('error', lambda event: update_status(f'Ticket error: {event.subject if event.subject else ""}')) + # STASH FOR USE LATER + observer_stash = ObserverStash() + observer_stash.connection_observer = connection_observer + observer_stash.ticket_observer = ticket_observer + observer_stash.profile_observer = profile_observer + return { 'application_version_observer': application_version_observer, 'client_observer': client_observer, diff --git a/gui/v2/presenters/state/fast_registration_state.py b/gui/v2/presenters/state/fast_registration_state.py index f1be2cb..f8fcaf0 100644 --- a/gui/v2/presenters/state/fast_registration_state.py +++ b/gui/v2/presenters/state/fast_registration_state.py @@ -1,3 +1,5 @@ +from core.models.BaseProfile import ProfileType +from core.services.payment_phase.ticket_cycle import profile_can_respawn from core.controllers.ProfileController import ProfileController from core.services.profiles.attribute_filters import protocol_matches_profile_type, app_or_display_matches_profile_type from core.controllers.profile_state.data_filters import protocol_is_supported_for_location @@ -8,10 +10,19 @@ from gui.v2.presenters.data.resolution_data import get_resolution_data, validate from gui.v2.presenters.data.type_data import get_profile_type_data from gui.v2.presenters.image_tools.button_factory import create_button_image, create_resolution_button_image, blank_button from core.errors.logger import logger - +from core.models.Result import ResultError from pathlib import Path -from typing import Optional +from dataclasses import dataclass +from typing import Optional, Dict + +@dataclass +class PreEdit: + proceed: bool + warnings: bool + warnings_dict: Optional[Dict] + whats_different: Optional[Dict] + class RegistrationState: def __init__(self, btn_path, profile_id: Optional[int] = None): @@ -22,6 +33,8 @@ class RegistrationState: self.pixmap_cache = {} self.profile_id = profile_id self.original_choices = {} + self.proceed = False + self.warnings_dict = {} # Maps a category to the function that validates its current selection. self._filter_map = { @@ -264,6 +277,27 @@ class RegistrationState: def get_final_choices(self): return self.selections + def get_warning_skip_list(self): + """This is the warning skip list, that affects if the user gets a visual warning.""" + original_type = self.original_choices.get("type", None) + new_type = self.selections.get("type", None) + if original_type == ProfileType.SYSTEM and new_type == ProfileType.SESSION: + logger.info("It's SYSTEM to session") + return ["browser"] + else: + return [] + + def _get_skip_list(self): + """This is the 'real' skip list that affects if it actually gets changed.""" + new_type = self.selections.get("type", None) + original_type = self.original_choices.get("type", None) + + if new_type == ProfileType.SYSTEM: + if original_type == ProfileType.SYSTEM or None: + return ["browser", "resolution"] + + return None + def whats_different(self): """Creates a dictionary of the difference between the current selections and the original choices.""" different = {} @@ -271,16 +305,29 @@ class RegistrationState: if not self.original_choices: return self.selections + # print(f"self.selections is {self.selections}") + # print(f"self.original_choices is {self.original_choices}") + + skip_list = self._get_skip_list() + # print(f"skip_list is {skip_list}") + for category, value in self.selections.items(): + if skip_list: + if category in skip_list: + print(f"skipping {category}") + continue + print(f"checking {category}") + # wasn't in the original, but it's in the new selections: try: if not self.original_choices[category]: different[category] = value continue except KeyError: - different[category] = value - continue + print(f"setting category {category} as changed") + different[category] = value + continue # not in the new choices, but it is in the original: if not self.selections[category] and self.original_choices[category]: @@ -289,15 +336,53 @@ class RegistrationState: # actually different: if self.original_choices[category] != self.selections[category]: + print(f"It's actually different for {category}") different[category] = value return different - def is_anything_different(self) -> bool: - different = self.whats_different() - if different: - return True - else: - return False - \ No newline at end of file + def is_anything_different(self) -> bool: + return bool(self.whats_different()) + + def before_we_proceed(self) -> PreEdit: + different_dict = self.whats_different() + + # print(f"different_dict is {different_dict}") + + if not different_dict: + return PreEdit(True, False, None, None) + + different_keys = different_dict.keys() + warning_keys = [] + warnings_dict = {} + skip_list = self.get_warning_skip_list() + + if skip_list: + for each_key in different_keys: + if not each_key in skip_list: + warning_keys.append(each_key) + else: + warning_keys = different_keys + + if "location" in warning_keys: + # Assuming profile_can_respawn is defined elsewhere + respawn_possible = profile_can_respawn(self.profile_id) + if not respawn_possible.valid and respawn_possible.error_type != ResultError.SUBSCRIPTION: + warnings_dict["location"] = "If you change the location, it wipes the subscription, proceed?" + + if "browser" in warning_keys: + warnings_dict["browser"] = "If you change the browser, it wipes the cookies/bookmarks, proceed?" + + has_warnings = len(warnings_dict) > 0 + + if not has_warnings: + return PreEdit(True, False, None, different_dict) + + # Update state + self.warnings_dict = warnings_dict + + if self.proceed: + return PreEdit(True, has_warnings, warnings_dict, different_dict) + + return PreEdit(False, has_warnings, warnings_dict, different_dict) diff --git a/gui/v2/ui/pages/create_edit/edit_page.py b/gui/v2/ui/pages/create_edit/edit_page.py index dec332a..4705d32 100644 --- a/gui/v2/ui/pages/create_edit/edit_page.py +++ b/gui/v2/ui/pages/create_edit/edit_page.py @@ -8,6 +8,7 @@ from gui.v2.presenters.state.fast_registration_state import RegistrationState from gui.v2.ui.styles.css.fast_register import FAST_REGISTER_ONE, FAST_REGISTER_TWO from gui.v2.ui.pages.Page import Page from gui.v2.ui.pages.create_edit.resolution_widget import CustomResolutionDialog +from gui.v2.ui.popups.error_popup import ErrorPopup from PyQt6.QtWidgets import QWidget, QPushButton, QGridLayout, QVBoxLayout from PyQt6.QtCore import Qt @@ -101,14 +102,46 @@ class EditPage(Page): self.created_display = False # Allow showEvent to trigger create_ui next time self.custom_window.navigator.navigate("menu") - def submit_edits(self): - input_text = self.group_name.text_input.text() - if not input_text: - self.show_a_popup("Pick a name in the top right box.") - return + def _on_popup_answer(self, user_choice: bool, passed_result: dict): + if user_choice and passed_result: + # Remove the first warning from the dictionary + first_key = next(iter(passed_result)) + del passed_result[first_key] + # Loop back to check if more warnings remain + self.warn_or_proceed(warnings_dict=passed_result) - self.state.set_selection(category="name", value=input_text) - different_content = self.state.whats_different() + def warn_or_proceed(self, warnings_dict: Optional[dict] = None): + # Initial entry point: determine if we have warnings + if warnings_dict is None: + pre_edit = self.state.before_we_proceed() + + # If we can proceed immediately, skip to the task + if pre_edit.proceed: + self.proceed_with_edit(pre_edit.whats_different) + return + + # Start the warning loop with the warnings_dict + warnings_dict = pre_edit.warnings_dict + + # If there are still warnings to address + if warnings_dict: + first_key = next(iter(warnings_dict)) + error_popup = ErrorPopup( + choice=True, + title=first_key, + message=warnings_dict[first_key], + end_app=False, + on_answer=self._on_popup_answer, + passed_result=warnings_dict, + ) + error_popup.show() + else: + # No warnings left, execute the final task + # We call before_we_proceed one last time to get the final different_dict + final_state = self.state.before_we_proceed() + self.proceed_with_edit(final_state.whats_different) + + def proceed_with_edit(self, different_content): edit_profile( profile_id=self.profile_id, @@ -117,6 +150,17 @@ class EditPage(Page): ) self.created_display = False + + def submit_edits(self): + # name + input_text = self.group_name.text_input.text() + if not input_text: + self.show_a_popup("Pick a name in the top right box.") + return + self.state.set_selection(category="name", value=input_text) + + self.warn_or_proceed() + def reverse(self): if self.state.is_anything_different(): print("unsubmitted changes")