diff --git a/src/controller.py b/src/controller.py index 96441af..f69c887 100644 --- a/src/controller.py +++ b/src/controller.py @@ -7,6 +7,8 @@ def __init__(self, model: Model): self._model = model self.loadouts_updated = False + self._model.loadoutsManager.attach_change_listener(self.on_loadout_saved) + # Allow view direct access to model components def get_settings_manager(self): return self._model.settingsManager @@ -134,6 +136,11 @@ def getAllMacros(self): def trigger_macro(self, stratagem): self.executer.on_macro_triggered(stratagem) + # Hook to detect loadouts being saved + def on_loadout_saved(self, event): + if event['type'] == 'save': + self.loadouts_updated = False + # Exit hook def on_exit(self): if self.loadouts_updated: diff --git a/src/loadouts.py b/src/loadouts.py index 689ce0a..63204ff 100644 --- a/src/loadouts.py +++ b/src/loadouts.py @@ -70,12 +70,12 @@ def detach_change_listener(self, callback): except ValueError: pass - def notify_change(self): + def notify_change(self, **attrs): """ Calls each callback in the list of observers. """ for callback in self._observers: - callback() + callback(attrs) def __setattr__(self, name, value): """ @@ -83,14 +83,14 @@ def __setattr__(self, name, value): """ self.__dict__[name] = value if not self.__dict__.get("_is_initializing", False): - self.notify_change() # Notify on any attribute change + self.notify_change(type='setattr', name=name) # Notify on any attribute change def __delattr__(self, name): """ Deletes an attribute and notifies observers of the change. Probably not really used a lot. """ del self.__dict__[name] - self.notify_change() + self.notify_change(type='delattr', name=name) # State persistance def loadFromFile(self): @@ -115,4 +115,5 @@ def saveToFile(self): settings_as_json = json.dumps(loadouts_as_json, indent=2) file.write(settings_as_json) print("INFO: Loadouts saved.") + self.notify_change(type='save') diff --git a/src/settings.py b/src/settings.py index 694d901..5479ecf 100644 --- a/src/settings.py +++ b/src/settings.py @@ -39,12 +39,15 @@ def detach_change_listener(self, callback): except ValueError: pass - def notify_change(self): + def notify_change(self, **attrs): """ Calls each callback in the list of observers. + The attrs parameter allows calling the notify method with additional arguments, + which are packed into an event dictionary. This allows for more specific notifications + to be sent to observers. """ for callback in self._observers: - callback() + callback(attrs) def __setattr__(self, name, value): """ @@ -52,14 +55,14 @@ def __setattr__(self, name, value): """ self.__dict__[name] = value if not self.__dict__.get("_is_initializing", False): - self.notify_change() # Notify on any attribute change + self.notify_change(type='setattr', name=name) # Notify on any attribute change def __delattr__(self, name): """ Deletes an attribute and notifies observers of the change. Probably not really used a lot. """ del self.__dict__[name] - self.notify_change() + self.notify_change(type='delattr', name=name) def loadDefaults(self): """