mirror of https://github.com/gmbrax/Pilgrim.git
Merge pull request #47 from gmbrax/fix/move-create-diary-to-modal
Fix/move create diary to modal
This commit is contained in:
commit
1beeb39b95
|
|
@ -206,29 +206,17 @@ class DiaryListScreen(Screen):
|
||||||
"""Action to create new diary"""
|
"""Action to create new diary"""
|
||||||
self.app.push_screen(NewDiaryModal(),self._on_new_diary_submitted)
|
self.app.push_screen(NewDiaryModal(),self._on_new_diary_submitted)
|
||||||
|
|
||||||
def _on_new_diary_submitted(self,result):
|
def _on_new_diary_submitted(self, result):
|
||||||
self.notify(str(result))
|
"""Callback after diary creation"""
|
||||||
if result:
|
if result: # Se result não é string vazia, o diário foi criado
|
||||||
self.notify(f"Creating Diary:{result}'...")
|
self.notify(f"Returning to diary list...")
|
||||||
self.call_later(self._async_create_diary,result)
|
# Atualiza a lista de diários
|
||||||
|
self.refresh_diaries()
|
||||||
else:
|
else:
|
||||||
self.notify(f"Canceled...")
|
self.notify(f"Creation canceled...")
|
||||||
|
|
||||||
async def _async_create_diary(self,name: str):
|
|
||||||
|
|
||||||
try:
|
|
||||||
service = self.app.service_manager.get_travel_diary_service()
|
|
||||||
created_diary = await service.async_create(name)
|
|
||||||
if created_diary:
|
|
||||||
self.diary_id_map[created_diary.id] = created_diary.id
|
|
||||||
await self.async_refresh_diaries()
|
|
||||||
self.notify(f"Diary: '{name}' created!")
|
|
||||||
else:
|
|
||||||
self.notify("Error Creating the diary")
|
|
||||||
except Exception as e:
|
|
||||||
self.notify(f"Exception on creating the diary: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
|
def _on_screen_resume(self) -> None:
|
||||||
|
self.refresh_diaries()
|
||||||
|
|
||||||
def action_edit_selected_diary(self):
|
def action_edit_selected_diary(self):
|
||||||
"""Action to edit selected diary"""
|
"""Action to edit selected diary"""
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,17 @@ from textual.containers import Vertical, Horizontal
|
||||||
from textual.screen import ModalScreen
|
from textual.screen import ModalScreen
|
||||||
from textual.widgets import Label, Input, Button
|
from textual.widgets import Label, Input, Button
|
||||||
|
|
||||||
|
from pilgrim.ui.screens.edit_entry_screen import EditEntryScreen
|
||||||
|
|
||||||
|
|
||||||
class NewDiaryModal(ModalScreen[str]):
|
class NewDiaryModal(ModalScreen[str]):
|
||||||
BINDINGS = [
|
BINDINGS = [
|
||||||
Binding("escape", "cancel", "Cancel"),
|
Binding("escape", "cancel", "Cancel"),
|
||||||
Binding("enter", "create_diary", "Create",priority=True),
|
Binding("enter", "create_diary", "Create",priority=True),
|
||||||
]
|
]
|
||||||
def __init__(self):
|
def __init__(self,autoopen=True):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.auto_open = autoopen
|
||||||
self.name_input = Input(id="NewDiaryModal-NameInput",classes="NewDiaryModal-NameInput") # This ID is fine, it's specific to the input
|
self.name_input = Input(id="NewDiaryModal-NameInput",classes="NewDiaryModal-NameInput") # This ID is fine, it's specific to the input
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
|
|
@ -34,7 +37,7 @@ class NewDiaryModal(ModalScreen[str]):
|
||||||
if event.button.id == "create_diary_button":
|
if event.button.id == "create_diary_button":
|
||||||
self.action_create_diary()
|
self.action_create_diary()
|
||||||
elif event.button.id == "cancel_button":
|
elif event.button.id == "cancel_button":
|
||||||
self.dismiss("")
|
self.dismiss()
|
||||||
|
|
||||||
def action_cancel(self) -> None:
|
def action_cancel(self) -> None:
|
||||||
"""Action to cancel the modal."""
|
"""Action to cancel the modal."""
|
||||||
|
|
@ -43,7 +46,9 @@ class NewDiaryModal(ModalScreen[str]):
|
||||||
def action_create_diary(self) -> None:
|
def action_create_diary(self) -> None:
|
||||||
diary_name = self.name_input.value.strip()
|
diary_name = self.name_input.value.strip()
|
||||||
if diary_name:
|
if diary_name:
|
||||||
self.dismiss(diary_name)
|
|
||||||
|
self.call_later(self._async_create_diary, diary_name)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.notify("Diary name cannot be empty.", severity="warning")
|
self.notify("Diary name cannot be empty.", severity="warning")
|
||||||
self.name_input.focus()
|
self.name_input.focus()
|
||||||
|
|
@ -52,3 +57,24 @@ class NewDiaryModal(ModalScreen[str]):
|
||||||
if event.input.id == "NewDiaryModal-NameInput":
|
if event.input.id == "NewDiaryModal-NameInput":
|
||||||
self.action_create_diary()
|
self.action_create_diary()
|
||||||
|
|
||||||
|
async def _async_create_diary(self, name: str):
|
||||||
|
|
||||||
|
try:
|
||||||
|
service = self.app.service_manager.get_travel_diary_service()
|
||||||
|
created_diary = await service.async_create(name)
|
||||||
|
if created_diary:
|
||||||
|
self.dismiss(name)
|
||||||
|
|
||||||
|
if self.auto_open:
|
||||||
|
self.app.push_screen(EditEntryScreen(diary_id=created_diary.id))
|
||||||
|
|
||||||
|
self.notify(f"Diary: '{name}' created!")
|
||||||
|
else:
|
||||||
|
self.notify("Error Creating the diary")
|
||||||
|
except Exception as e:
|
||||||
|
self.notify(f"Exception on creating the diary: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue