From 7eecc140726a18ae6c95e4becfa0965ae4a24945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCllweisser?= Date: Sun, 31 Mar 2024 14:31:09 +0200 Subject: [PATCH] added SettingsManager and update checks --- WeeNu.py | 88 +++++++++++++++++++++++++++++++++++++--------- user_settings.json | 4 +++ version | 1 + 3 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 user_settings.json create mode 100644 version diff --git a/WeeNu.py b/WeeNu.py index 698d27e..54b7acb 100644 --- a/WeeNu.py +++ b/WeeNu.py @@ -2,16 +2,47 @@ from datetime import datetime import webbrowser import os +from update_check import isUpToDate +import json def get_week_number(): today = datetime.now() week_number = today.isocalendar()[1] return week_number +class SettingsManager: + def __init__(self, settings_file='user_settings.json'): + self.settings_file = settings_file + self.settings = self.load_settings() + + def load_settings(self): + if os.path.exists(self.settings_file): + try: + with open(self.settings_file, 'r') as f: + return json.load(f) + except IOError as e: + print(f"Error loading settings: {e}") + return {} + + def save_settings(self): + try: + with open(self.settings_file, 'w') as f: + json.dump(self.settings, f, indent=4) + except IOError as e: + print(f"Error saving settings: {e}") + + def get_setting(self, key, default=None): + return self.settings.get(key, default) + + def update_setting(self, key, value): + self.settings[key] = value + self.save_settings() + class PrefixFormatter: def __init__(self): + self.sm = SettingsManager() self.prefix_formats = self.load_prefixes_from_file() - self.current_format = self.load_format() + self.current_format = self.load_format() # Ensure current format is correctly loaded def format_with_week(self, week_number): self.current_format_formatted = f"{self.current_format} {week_number}" @@ -19,23 +50,16 @@ def format_with_week(self, week_number): def save_format(self, format_str): try: - with open('user_settings', 'w') as f: - f.write(format_str) - self.current_format = format_str + self.sm.update_setting('current_prefix', format_str) + self.current_format = format_str # Update current format immediately except IOError as e: rumps.alert(f"Error saving current prefix format: {e}") def load_format(self): - if not os.path.exists('user_settings'): - return self.prefix_formats[0] # Return the first format if the file doesn't exist - try: - with open('user_settings', 'r') as f: - format_str = f.read().strip() - if format_str in self.prefix_formats: - return format_str - except IOError as e: - rumps.alert(f"Error loading prefix format: {e}") - return self.prefix_formats[0] + # Properly load and return the current format + format_str = self.sm.get_setting('current_prefix', self.prefix_formats[0]) + self.current_format = format_str # Ensure current format is updated here + return format_str def load_prefixes_from_file(self): try: @@ -49,6 +73,7 @@ class WeeNu(rumps.App): def __init__(self): super(WeeNu, self).__init__("WeeNu") self.prefix_formatter = PrefixFormatter() + self.settings_manager = SettingsManager() self.menu = ["WeeNu v0.2", None] @@ -57,9 +82,20 @@ def __init__(self): menu_item = rumps.MenuItem(format_option, callback=self.change_prefix_format) menu_item.state = format_option == self.prefix_formatter.current_format self.format_menu.add(menu_item) - self.menu.add(self.format_menu) + + settings_menu = rumps.MenuItem('Update checks') + auto_check_updates = rumps.MenuItem("Enable", callback=self.toggle_auto_update) + auto_update_check_setting = self.settings_manager.get_setting('auto_update_check', False) + auto_check_updates.state = auto_update_check_setting + settings_menu.add(auto_check_updates) + settings_menu.add(None) + check_version_item = rumps.MenuItem("Check now", callback=self.check_version) + settings_menu.add(check_version_item) + + self.menu.add(settings_menu) self.menu.add(None) + self.github_link_item = rumps.MenuItem("About WeeNu", callback=self.open_github) self.menu.add(self.github_link_item) @@ -79,6 +115,26 @@ def change_prefix_format(self, sender): def open_github(self, _): webbrowser.open('https://github.com/mullweisser/mac-weenu') + + def check_version(self, _=None): + is_latest = isUpToDate('version', "https://raw.githubusercontent.com/mullweisser/main/version") + if is_latest: + rumps.notification(title="WeeNu", subtitle="", message="You are running the latest version.", sound=False) + else: + rumps.notification(title="WeeNu", subtitle="", message="Newer version is available.", sound=False) + + def toggle_auto_update(self, sender): + new_state = not sender.state + sender.state = new_state + self.settings_manager.update_setting('auto_update_check', new_state) + if new_state: + self.check_version() + + @rumps.timer(86400) + def auto_check_version(self, _=None): + if self.settings_manager.get_setting('auto_update_check', False): + self.check_version(_) if __name__ == "__main__": - WeeNu().run() \ No newline at end of file + app = WeeNu() + app.run() \ No newline at end of file diff --git a/user_settings.json b/user_settings.json new file mode 100644 index 0000000..3273197 --- /dev/null +++ b/user_settings.json @@ -0,0 +1,4 @@ +{ + "current_prefix": "Week", + "auto_update_check": true +} \ No newline at end of file diff --git a/version b/version new file mode 100644 index 0000000..e2cfc99 --- /dev/null +++ b/version @@ -0,0 +1 @@ +v0.2 \ No newline at end of file