forked from Support/sp-hydra-veil-gui
Improved Edit profile state management, by putting warnings in a lambda dict, to reduce nesting
This commit is contained in:
parent
302eea9945
commit
39d554be4b
2 changed files with 86 additions and 88 deletions
|
|
@ -35,6 +35,7 @@ class RegistrationState:
|
||||||
self.original_choices = {}
|
self.original_choices = {}
|
||||||
self.proceed = False
|
self.proceed = False
|
||||||
self.warnings_dict = {}
|
self.warnings_dict = {}
|
||||||
|
self.key_choices = []
|
||||||
|
|
||||||
# Maps a category to the function that validates its current selection.
|
# Maps a category to the function that validates its current selection.
|
||||||
self._filter_map = {
|
self._filter_map = {
|
||||||
|
|
@ -87,7 +88,9 @@ class RegistrationState:
|
||||||
"browser": (get_app_display_data, 0),
|
"browser": (get_app_display_data, 0),
|
||||||
"name": (None, None)
|
"name": (None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
for category, (loader_func, default_idx) in load_config.items():
|
for category, (loader_func, default_idx) in load_config.items():
|
||||||
|
self.key_choices.append(category)
|
||||||
if loader_func is None:
|
if loader_func is None:
|
||||||
self._no_premade_choices(category=category, default_data=default_data)
|
self._no_premade_choices(category=category, default_data=default_data)
|
||||||
continue
|
continue
|
||||||
|
|
@ -277,112 +280,88 @@ class RegistrationState:
|
||||||
def get_final_choices(self):
|
def get_final_choices(self):
|
||||||
return self.selections
|
return self.selections
|
||||||
|
|
||||||
def get_warning_skip_list(self):
|
def _get_transition_skip(self, skip_type="real"):
|
||||||
"""This is the warning skip list, that affects if the user gets a visual warning."""
|
"""
|
||||||
original_type = self.original_choices.get("type", None)
|
Handles mapping of profile transitions to skip lists.
|
||||||
new_type = self.selections.get("type", None)
|
skip_type: 'real' for data changes, 'warning' for UI alerts.
|
||||||
if original_type == ProfileType.SYSTEM and new_type == ProfileType.SESSION:
|
"""
|
||||||
logger.info("It's SYSTEM to session")
|
# Define transition rules: (original, new) -> [keys to skip]
|
||||||
return ["browser"]
|
rules = {
|
||||||
else:
|
"warning": {
|
||||||
return []
|
(ProfileType.SYSTEM, ProfileType.SESSION): ["browser"],
|
||||||
|
},
|
||||||
|
"real": {
|
||||||
|
# If new is SYSTEM and original is either SYSTEM or None
|
||||||
|
# We handle the 'None' case by checking the transition specifically
|
||||||
|
(ProfileType.SYSTEM, ProfileType.SYSTEM): ["browser", "resolution"],
|
||||||
|
(None, ProfileType.SYSTEM): ["browser", "resolution"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def _get_skip_list(self):
|
orig = self.original_choices.get("type")
|
||||||
"""This is the 'real' skip list that affects if it actually gets changed."""
|
new = self.selections.get("type")
|
||||||
new_type = self.selections.get("type", None)
|
return rules.get(skip_type, {}).get((orig, new), [])
|
||||||
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):
|
def whats_different(self):
|
||||||
"""Creates a dictionary of the difference between the current selections and the original choices."""
|
"""Creates a dictionary of actual value differences."""
|
||||||
different = {}
|
|
||||||
|
|
||||||
if not self.original_choices:
|
if not self.original_choices:
|
||||||
return self.selections
|
return self.selections
|
||||||
|
|
||||||
# print(f"self.selections is {self.selections}")
|
different = {}
|
||||||
# print(f"self.original_choices is {self.original_choices}")
|
# skip_list = self._get_transition_skip(skip_type="real")
|
||||||
|
|
||||||
skip_list = self._get_skip_list()
|
# Get a union of all keys to ensure we catch removals and additions
|
||||||
# print(f"skip_list is {skip_list}")
|
all_keys = set(self.selections.keys()) | set(self.original_choices.keys())
|
||||||
|
|
||||||
for category, value in self.selections.items():
|
for key in all_keys:
|
||||||
|
# only pay attention to the universe of keys we actually are deciding here
|
||||||
if skip_list:
|
if key not in self.key_choices:
|
||||||
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:
|
|
||||||
print(f"setting category {category} as changed")
|
|
||||||
different[category] = value
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# not in the new choices, but it is in the original:
|
# if key in skip_list:
|
||||||
if not self.selections[category] and self.original_choices[category]:
|
# continue
|
||||||
different[category] = None
|
|
||||||
continue
|
|
||||||
|
|
||||||
# actually different:
|
old_val = self.original_choices.get(key)
|
||||||
if self.original_choices[category] != self.selections[category]:
|
new_val = self.selections.get(key)
|
||||||
print(f"It's actually different for {category}")
|
|
||||||
different[category] = value
|
if old_val != new_val:
|
||||||
|
different[key] = new_val
|
||||||
|
|
||||||
return different
|
return different
|
||||||
|
|
||||||
|
|
||||||
def is_anything_different(self) -> bool:
|
|
||||||
return bool(self.whats_different())
|
|
||||||
|
|
||||||
def before_we_proceed(self) -> PreEdit:
|
def before_we_proceed(self) -> PreEdit:
|
||||||
different_dict = self.whats_different()
|
different_dict = self.whats_different()
|
||||||
|
|
||||||
# print(f"different_dict is {different_dict}")
|
|
||||||
|
|
||||||
if not different_dict:
|
if not different_dict:
|
||||||
return PreEdit(True, False, None, None)
|
return PreEdit(True, False, None, None)
|
||||||
|
|
||||||
different_keys = different_dict.keys()
|
# 1. Filter differences by the warning skip list
|
||||||
warning_keys = []
|
warning_skip = self._get_transition_skip(skip_type="warning")
|
||||||
|
warning_keys = [k for k in different_dict if k not in warning_skip]
|
||||||
|
|
||||||
|
# 2. Map keys to their specific warning messages
|
||||||
|
WARNING_MESSAGES = {
|
||||||
|
"location": lambda: "If you change the location, it wipes the subscription, proceed?"
|
||||||
|
if not profile_can_respawn(self.profile_id).valid
|
||||||
|
and profile_can_respawn(self.profile_id).error_type != ResultError.SUBSCRIPTION
|
||||||
|
else None,
|
||||||
|
"browser": lambda: "If you change the browser, it wipes the cookies/bookmarks, proceed?"
|
||||||
|
}
|
||||||
|
|
||||||
warnings_dict = {}
|
warnings_dict = {}
|
||||||
skip_list = self.get_warning_skip_list()
|
for key in warning_keys:
|
||||||
|
rule = WARNING_MESSAGES.get(key)
|
||||||
|
msg = rule() if callable(rule) else rule
|
||||||
|
if msg:
|
||||||
|
warnings_dict[key] = msg
|
||||||
|
|
||||||
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:
|
has_warnings = bool(warnings_dict)
|
||||||
# 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
|
self.warnings_dict = warnings_dict
|
||||||
|
|
||||||
if self.proceed:
|
# Determine if we can proceed based on the 'proceed' state and whether warnings exist
|
||||||
return PreEdit(True, has_warnings, warnings_dict, different_dict)
|
can_proceed = self.proceed if has_warnings else True
|
||||||
|
return PreEdit(can_proceed, has_warnings, warnings_dict, different_dict)
|
||||||
|
|
||||||
return PreEdit(False, has_warnings, warnings_dict, different_dict)
|
def is_anything_different(self):
|
||||||
|
different_dict = self.whats_different()
|
||||||
|
return bool(different_dict)
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,8 @@ class EditPage(Page):
|
||||||
|
|
||||||
def proceed_with_edit(self, different_content):
|
def proceed_with_edit(self, different_content):
|
||||||
|
|
||||||
|
print(f"different_content is {different_content}")
|
||||||
|
|
||||||
edit_profile(
|
edit_profile(
|
||||||
profile_id=self.profile_id,
|
profile_id=self.profile_id,
|
||||||
potential_data=different_content,
|
potential_data=different_content,
|
||||||
|
|
@ -162,10 +164,27 @@ class EditPage(Page):
|
||||||
self.warn_or_proceed()
|
self.warn_or_proceed()
|
||||||
|
|
||||||
def reverse(self):
|
def reverse(self):
|
||||||
if self.state.is_anything_different():
|
# nothing changed, bounce:
|
||||||
print("unsubmitted changes")
|
if not self.state.is_anything_different():
|
||||||
|
self.actually_leave()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
error_popup = ErrorPopup(
|
||||||
|
choice=True,
|
||||||
|
title="Unsaved changes",
|
||||||
|
message="Are you sure you want to leave with unfinished changes?",
|
||||||
|
end_app=False,
|
||||||
|
on_answer=self._after_reverse_decision,
|
||||||
|
passed_result=None,
|
||||||
|
)
|
||||||
|
error_popup.show()
|
||||||
|
|
||||||
|
|
||||||
|
def _after_reverse_decision(self, boolean_choice: bool, passed_result=None):
|
||||||
|
if boolean_choice:
|
||||||
|
self.actually_leave()
|
||||||
|
|
||||||
|
def actually_leave(self):
|
||||||
self.created_display = False
|
self.created_display = False
|
||||||
self.custom_window.navigator.navigate("menu")
|
self.custom_window.navigator.navigate("menu")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue