Skip to content

Commit

Permalink
Improved observer pattern for settings and loadouts.
Browse files Browse the repository at this point in the history
This allows detection of specific events, such as loadouts being saved.
This is the used to avoid the exit popup, if the user manually saved the loadouts.
  • Loading branch information
stixes committed Jun 26, 2024
1 parent 48f5dd4 commit 925eda3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions src/loadouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ 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):
"""
Sets an attribute and notifies observers if the setting is not initially being populated.
"""
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):
Expand All @@ -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')

11 changes: 7 additions & 4 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,30 @@ 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):
"""
Sets an attribute and notifies observers if the setting is not initially being populated.
"""
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):
"""
Expand Down

0 comments on commit 925eda3

Please sign in to comment.