Add the feature to set the auto-open diary in the toml file

This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2025-07-23 07:27:35 -03:00
parent 0c6de1ea2e
commit 83fdb885a9
1 changed files with 116 additions and 12 deletions

View File

@ -7,7 +7,7 @@ from textual.reactive import reactive
from textual.binding import Binding from textual.binding import Binding
from textual import on from textual import on
from pilgrim import TravelDiary from pilgrim.models.travel_diary import TravelDiary
from pilgrim.ui.screens.modals.delete_all_entries_from_diary_modal import DeleteAllEntriesModal from pilgrim.ui.screens.modals.delete_all_entries_from_diary_modal import DeleteAllEntriesModal
from pilgrim.ui.screens.modals.delete_all_photos_from_diary_modal import DeleteAllPhotosModal from pilgrim.ui.screens.modals.delete_all_photos_from_diary_modal import DeleteAllPhotosModal
from pilgrim.ui.screens.modals.delete_diary_modal import DeleteDiaryModal from pilgrim.ui.screens.modals.delete_diary_modal import DeleteDiaryModal
@ -19,21 +19,22 @@ class SettingsScreen(Screen):
Binding("escape","cancel","Cancel"), Binding("escape","cancel","Cancel"),
] ]
def __init__(self,diary:TravelDiary): def __init__(self,diary_id:int):
super().__init__() super().__init__()
self.current_diary = diary self.current_diary = self.app.service_manager.get_travel_diary_service().read_by_id(diary_id)
self.header = Header() self.header = Header()
self.footer = Footer() self.footer = Footer()
self.title = "Settings" self.title = "Settings"
self.diary_name = Static(self.current_diary.name,id="DiarySettingsScreen-DiaryName") self.diary_name = Static(self.current_diary.name,id="DiarySettingsScreen-DiaryName")
self.is_the_diary_set_to_auto_open = False self.notify(str(self.app.config_manager))
self.diary_entry_count = Static("0") self.is_the_diary_set_to_auto_open = self.app.config_manager.get_auto_open_diary() == self.current_diary.name
self.diary_photo_count = Static("0") self.diary_entry_count = Static(str(len(self.current_diary.entries)))
self.save_button = Button("Save") self.diary_photo_count = Static(str(len(self.current_diary.photos)))
self.save_button = Button("Save",id="DiarySettingsScreen-SaveButton" )
self.cancel_button = Button("Cancel",id="DiarySettingsScreen-cancel_button") self.cancel_button = Button("Cancel",id="DiarySettingsScreen-cancel_button")
self.apply_button = Button("Apply") self.apply_button = Button("Apply",id="DiarySettingsScreen-ApplyButton")
self.backup_diary_button = Button("Backup Diary") self.backup_diary_button = Button("Backup Diary")
self.delete_diary_button = Button("Delete Diary",id="DiarySettingsScreen-DeleteDiaryButton") self.delete_diary_button = Button("Delete Diary",id="DiarySettingsScreen-DeleteDiaryButton")
self.delete_all_entries_button = Button("Delete All Entries",id="DiarySettingsScreen-DeleteAllEntriesButton") self.delete_all_entries_button = Button("Delete All Entries",id="DiarySettingsScreen-DeleteAllEntriesButton")
@ -132,7 +133,7 @@ class SettingsScreen(Screen):
@on(Checkbox.Changed, "#set_auto_open_to_this_diary") @on(Checkbox.Changed, "#set_auto_open_to_this_diary")
def on_checkbox_changed(self, event): def on_checkbox_changed(self, event):
self.is_changed = not self.is_changed self.is_changed = not self.is_changed
self.notify("Checkboxed")
@on(Button.Pressed, "#DiarySettingsScreen-cancel_button") @on(Button.Pressed, "#DiarySettingsScreen-cancel_button")
def on_cancel_button_pressed(self, event): def on_cancel_button_pressed(self, event):
@ -154,7 +155,15 @@ class SettingsScreen(Screen):
if self.is_changed: if self.is_changed:
self.notify("Cancel button pressed, but changes are not saved",severity="error") self.notify("Cancel button pressed, but changes are not saved",severity="error")
return return
self.app.exit() self.dismiss()
@on(Button.Pressed, "#DiarySettingsScreen-SaveButton")
def on_save_button_pressed(self, event):
self.action_save()
@on(Button.Pressed, "#DiarySettingsScreen-ApplyButton")
def on_apply_button_pressed(self, event):
self.action_apply()
def watch_is_changed(self, value): def watch_is_changed(self, value):
@ -170,7 +179,102 @@ class SettingsScreen(Screen):
yield Footer() yield Footer()
def on_mount(self): def on_mount(self):
pass if self.app.config_manager.get_auto_open_diary() == self.current_diary.name:
self.call_after_refresh(self.set_checkbox_state)
def set_checkbox_state(self): def set_checkbox_state(self):
self.set_auto_open_to_this_diary.value = True self.set_auto_open_to_this_diary.value = True
def _set_auto_open_diary(self,value):
self.app.config_manager.set_auto_open_diary(value)
self.app.config_manager.save_config()
self.is_changed = False
def _get_auto_open_diary(self):
return self.app.config_manager.get_auto_open_diary()
def _make_auto_open_diary_value(self):
value = None
if self.set_auto_open_to_this_diary.value:
value = self.current_diary.name
return value
def action_save(self):
if not self.is_changed:
self.dismiss()
return
value = self._make_auto_open_diary_value()
current_auto_open = self._get_auto_open_diary()
if current_auto_open is None:
self._set_auto_open_diary(value)
self.notify("Settings saved")
self.dismiss()
return
if current_auto_open == self.current_diary.name:
if value is None:
self._set_auto_open_diary(None)
self.notify("Auto-open disabled")
else:
self.is_changed = False
self.notify("No changes made")
self.dismiss()
return
if value is not None:
self._set_auto_open_diary(value)
self.notify(f"Auto-open changed from '{current_auto_open}' to '{self.current_diary.name}'")
self.dismiss()
else:
self.is_changed = False
self.notify("No changes made")
self.dismiss()
def action_apply(self):
if not self.is_changed:
return
value = self._make_auto_open_diary_value()
current_auto_open = self._get_auto_open_diary()
if current_auto_open is None:
self._set_auto_open_diary(value)
self.notify("Settings applied")
return
if current_auto_open == self.current_diary.name:
if value is None:
self._set_auto_open_diary(None)
self.notify("Auto-open disabled")
else:
self.is_changed = False
self.notify("No changes made")
return
if value is not None:
self._set_auto_open_diary(value)
self.notify(f"Auto-open changed from '{current_auto_open}' to '{self.current_diary.name}'")
else:
self.is_changed = False
self.notify("No changes made")