1313from kivymd .uix .boxlayout import MDBoxLayout
1414from kivymd .uix .dialog import MDDialog
1515from kivymd .uix .filemanager import MDFileManager
16- from kivymd .uix .list import MDList , OneLineAvatarIconListItem , ThreeLineListItem
16+ from kivymd .uix .list import (
17+ MDList ,
18+ OneLineAvatarIconListItem ,
19+ ThreeLineListItem ,
20+ IRightBodyTouch ,
21+ )
1722from kivymd .uix .menu import MDDropdownMenu
1823from kivymd .uix .screen import MDScreen
1924from kivymd .uix .snackbar import BaseSnackbar
5661EXTERNAL_REPOSITORY_URL = "https://www.github.com/datahappy1/notes_app/"
5762
5863
64+ class IconsContainer (IRightBodyTouch , MDBoxLayout ):
65+ pass
66+
67+
5968class ItemDrawer (OneLineAvatarIconListItem ):
60- icon = StringProperty ()
61- id = StringProperty ()
62- text = StringProperty ( )
63- delete = ObjectProperty ()
69+ id = StringProperty (None )
70+ text = StringProperty (None )
71+ edit = ObjectProperty ( None )
72+ delete = ObjectProperty (None )
6473
6574
6675class ContentNavigationDrawer (MDBoxLayout ):
@@ -101,6 +110,13 @@ class AddSectionDialogContent(MDBoxLayout):
101110 cancel = ObjectProperty (None )
102111
103112
113+ class EditSectionDialogContent (MDBoxLayout ):
114+ old_section_name = StringProperty (None )
115+ edit_section_result_message = StringProperty (None )
116+ execute_edit_section = ObjectProperty (None )
117+ cancel = ObjectProperty (None )
118+
119+
104120class SearchDialogContent (MDBoxLayout ):
105121 get_search_switch_state = ObjectProperty (None )
106122 search_switch_callback = ObjectProperty (None )
@@ -216,12 +232,12 @@ def set_drawer_items(self, section_identifiers):
216232 for section_identifier in section_identifiers :
217233 self .ids .md_list .add_widget (
218234 ItemDrawer (
219- icon = "bookmark-outline" ,
220235 id = section_identifier .section_file_separator ,
221- text = f"section: { section_identifier .section_name } " ,
236+ text = section_identifier .section_name ,
222237 on_release = lambda x = f"{ section_identifier .section_file_separator } " : self .press_drawer_item_callback (
223238 x
224239 ),
240+ edit = self .press_edit_section ,
225241 delete = self .press_delete_section ,
226242 )
227243 )
@@ -488,24 +504,21 @@ def execute_search(self, *args):
488504 )
489505
490506 def execute_add_section (self , * args ):
507+ section_name = args [0 ]
508+
491509 if (
492- not args [ 0 ]
493- or len (args [ 0 ] ) < SECTION_FILE_NAME_MINIMAL_CHAR_COUNT
494- or args [ 0 ] .isspace ()
495- or args [ 0 ] in [si .section_name for si in self .file .section_identifiers ]
510+ not section_name
511+ or len (section_name ) < SECTION_FILE_NAME_MINIMAL_CHAR_COUNT
512+ or section_name .isspace ()
513+ or section_name in [si .section_name for si in self .file .section_identifiers ]
496514 ):
497515 self .dialog .content_cls .add_section_result_message = "Invalid name"
498516 return
499517
500- section_name = args [0 ]
501518 section_identifier = SectionIdentifier (
502519 defaults = self .defaults , section_name = section_name
503520 )
504521
505- if section_identifier .section_file_separator in self .file .section_identifiers :
506- self .dialog .content_cls .add_section_result_message = "Name already exists"
507- return
508-
509522 self .file .add_section_identifier (
510523 section_file_separator = section_identifier .section_file_separator
511524 )
@@ -520,6 +533,39 @@ def execute_add_section(self, *args):
520533
521534 self .cancel_dialog ()
522535
536+ def execute_edit_section (self , * args ):
537+ old_section_name , new_section_name = args
538+
539+ if (
540+ not new_section_name
541+ or len (new_section_name ) < SECTION_FILE_NAME_MINIMAL_CHAR_COUNT
542+ or new_section_name .isspace ()
543+ or new_section_name
544+ in [si .section_name for si in self .file .section_identifiers ]
545+ or old_section_name == new_section_name
546+ ):
547+ self .dialog .content_cls .edit_section_result_message = "Invalid name"
548+ return
549+
550+ new_section_identifier = SectionIdentifier (
551+ defaults = self .defaults , section_name = new_section_name
552+ )
553+
554+ old_section_identifier = SectionIdentifier (
555+ defaults = self .defaults , section_name = old_section_name
556+ )
557+
558+ self .file .rename_section (
559+ old_section_file_separator = old_section_identifier .section_file_separator ,
560+ new_section_file_separator = new_section_identifier .section_file_separator ,
561+ )
562+
563+ self .filter_data_split_by_section (section_identifier = new_section_identifier )
564+
565+ self .set_drawer_items (section_identifiers = self .file .section_identifiers )
566+
567+ self .cancel_dialog ()
568+
523569 def execute_goto_external_url (self ):
524570 return webbrowser .open (EXTERNAL_REPOSITORY_URL )
525571
@@ -592,6 +638,23 @@ def press_add_section(self, *args):
592638 self .dialog = MDDialog (title = "Add section:" , type = "custom" , content_cls = content )
593639 self .dialog .open ()
594640
641+ def press_edit_section (self , section_item ):
642+ section_name = SectionIdentifier (
643+ section_file_separator = section_item .id , defaults = self .defaults
644+ ).section_name
645+
646+ content = EditSectionDialogContent (
647+ old_section_name = section_name ,
648+ edit_section_result_message = "" ,
649+ execute_edit_section = self .execute_edit_section ,
650+ cancel = self .cancel_dialog ,
651+ )
652+
653+ self .dialog = MDDialog (
654+ title = f"Edit section { section_name } :" , type = "custom" , content_cls = content
655+ )
656+ self .dialog .open ()
657+
595658 def press_delete_section (self , section_item ):
596659 if len (self .file .section_identifiers ) == 1 :
597660 self .show_error_bar (error_message = "Cannot delete last section" )
0 commit comments