22# pylint: disable=unnecessary-comprehension, no-member, no-name-in-module, too-few-public-methods
33
44"""
5- Maildetail screen for inbox, sent, draft and trash.
5+ MailDetail screen for inbox, sent, draft, and trash.
66"""
77
88import os
99from datetime import datetime
10+ import logging
1011
1112from kivy .core .clipboard import Clipboard
1213from kivy .clock import Clock
13- from kivy .properties import (
14- StringProperty ,
15- NumericProperty
16- )
14+ from kivy .properties import StringProperty , NumericProperty
1715from kivy .uix .screenmanager import Screen
1816from kivy .factory import Factory
1917from kivy .app import App
2018
2119from kivymd .uix .button import MDFlatButton , MDIconButton
2220from kivymd .uix .dialog import MDDialog
23- from kivymd .uix .list import (
24- OneLineListItem ,
25- IRightBodyTouch
26- )
21+ from kivymd .uix .list import OneLineListItem , IRightBodyTouch
2722
2823from pybitmessage .bitmessagekivy .baseclass .common import (
2924 toast , avatar_image_first_letter , show_time_history , kivy_state_variables
3429
3530
3631class OneLineListTitle (OneLineListItem ):
37- """OneLineListTitle class for kivy Ui """
32+ """OneLineListTitle class for Kivy UI. """
3833 __events__ = ('on_long_press' , )
3934 long_press_time = NumericProperty (1 )
4035
4136 def on_state (self , instance , value ):
42- """On state"""
37+ """Handle state change for long press. """
4338 if value == 'down' :
44- lpt = self .long_press_time
45- self ._clockev = Clock .schedule_once (self ._do_long_press , lpt )
39+ self ._clock_event = Clock .schedule_once (self ._do_long_press , self .long_press_time )
4640 else :
47- self ._clockev .cancel ()
41+ self ._clock_event .cancel ()
4842
4943 def _do_long_press (self , dt ):
50- """Do long press"""
44+ """Trigger the long press event. """
5145 self .dispatch ('on_long_press' )
5246
53- def on_long_press (self , * largs ):
54- """On long press"""
55- self .copymessageTitle (self .text )
47+ def on_long_press (self , * args ):
48+ """Handle long press and display message title. """
49+ self .copy_message_title (self .text )
5650
57- def copymessageTitle (self , title_text ):
58- """this method is for displaying dialog box """
51+ def copy_message_title (self , title_text ):
52+ """Display dialog box with options to copy the message title. """
5953 self .title_text = title_text
60- width = .8 if platform == 'android' else .55
54+ width = 0 .8 if platform == 'android' else 0 .55
6155 self .dialog_box = MDDialog (
6256 text = title_text ,
63- size_hint = (width , .25 ),
57+ size_hint = (width , 0 .25 ),
6458 buttons = [
65- MDFlatButton (
66- text = "Copy" , on_release = self .callback_for_copy_title
67- ),
68- MDFlatButton (
69- text = "Cancel" , on_release = self .callback_for_copy_title ,
70- ),
71- ],)
59+ MDFlatButton (text = "Copy" , on_release = self .copy_title_callback ),
60+ MDFlatButton (text = "Cancel" , on_release = self .copy_title_callback ),
61+ ],
62+ )
7263 self .dialog_box .open ()
7364
74- def callback_for_copy_title (self , instance ):
75- """Callback of alert box"""
65+ def copy_title_callback (self , instance ):
66+ """Handle dialog box button callback. """
7667 if instance .text == 'Copy' :
7768 Clipboard .copy (self .title_text )
7869 self .dialog_box .dismiss ()
7970 toast (instance .text )
8071
8172
8273class IconRightSampleWidget (IRightBodyTouch , MDIconButton ):
83- """IconRightSampleWidget class for kivy Ui"""
84- 74+ """IconRightSampleWidget class for Kivy UI."""
8575
8676class MailDetail (Screen ): # pylint: disable=too-many-instance-attributes
87- """MailDetail Screen class for kivy Ui """
77+ """MailDetail Screen class for Kivy UI. """
8878
8979 to_addr = StringProperty ()
9080 from_addr = StringProperty ()
@@ -97,98 +87,111 @@ class MailDetail(Screen): # pylint: disable=too-many-instance-attributes
9787 no_subject = '(no subject)'
9888
9989 def __init__ (self , * args , ** kwargs ):
100- """Mail Details method """
101- super (MailDetail , self ).__init__ (* args , ** kwargs )
90+ """Initialize MailDetail screen. """
91+ super ().__init__ (* args , ** kwargs )
10292 self .kivy_state = kivy_state_variables ()
10393 Clock .schedule_once (self .init_ui , 0 )
10494
10595 def init_ui (self , dt = 0 ):
106- """Clock Schdule for method MailDetail mails """
107- self .page_type = self .kivy_state .detail_page_type if self . kivy_state . detail_page_type else ''
96+ """Initialize UI elements based on page type. """
97+ self .page_type = self .kivy_state .detail_page_type or ''
10898 try :
109- if self .kivy_state . detail_page_type in ('sent' , 'draft' ):
99+ if self .page_type in ('sent' , 'draft' ):
110100 App .get_running_app ().set_mail_detail_header ()
111- elif self .kivy_state . detail_page_type == 'inbox' :
101+ elif self .page_type == 'inbox' :
112102 data = sqlQuery (
113- "select toaddress, fromaddress, subject, message, received from inbox"
114- " where msgid = ?" , self .kivy_state .mail_id )
103+ "SELECT toaddress, fromaddress, subject, message, received FROM inbox "
104+ "WHERE msgid = ?" , self .kivy_state .mail_id
105+ )
115106 self .assign_mail_details (data )
116107 App .get_running_app ().set_mail_detail_header ()
117- except Exception as e : # pylint: disable=unused-variable
118- print ('Something wents wrong! !' )
108+ except Exception :
109+ print ('Something went wrong!' )
119110
120111 def assign_mail_details (self , data ):
121- """Assigning mail details"""
112+ """Assign mail details from query result. """
122113 subject = data [0 ][2 ].decode () if isinstance (data [0 ][2 ], bytes ) else data [0 ][2 ]
123- body = data [0 ][3 ].decode () if isinstance (data [0 ][2 ], bytes ) else data [0 ][3 ]
114+ body = data [0 ][3 ].decode () if isinstance (data [0 ][3 ], bytes ) else data [0 ][3 ]
124115 self .to_addr = data [0 ][0 ] if len (data [0 ][0 ]) > 4 else ' '
125116 self .from_addr = data [0 ][1 ]
126117
127- self .subject = subject .capitalize (
128- ) if subject .capitalize () else self .no_subject
118+ self .subject = subject .capitalize () or self .no_subject
129119 self .message = body
130120 if len (data [0 ]) == 7 :
131121 self .status = data [0 ][4 ]
132- self .time_tag = show_time_history (data [0 ][4 ]) if self .kivy_state . detail_page_type == 'inbox' \
133- else show_time_history ( data [ 0 ][ 6 ])
134- self . avatarImg = os .path .join (self .kivy_state .imageDir , 'draft-icon.png' ) \
135- if self .kivy_state . detail_page_type == 'draft' \
136- else ( os .path .join (self .kivy_state .imageDir , 'text_images' , '{0 }.png'. format ( avatar_image_first_letter (
137- self . subject . strip ()))) )
138- self .timeinseconds = data [0 ][4 ] if self .kivy_state . detail_page_type == 'inbox' else data [0 ][6 ]
122+ self .time_tag = show_time_history (data [0 ][4 ]) if self .page_type == 'inbox' else show_time_history ( data [ 0 ][ 6 ])
123+ self . avatarImg = (
124+ os .path .join (self .kivy_state .image_dir , 'draft-icon.png' )
125+ if self .page_type == 'draft'
126+ else os .path .join (self .kivy_state .image_dir , 'text_images' , f' { avatar_image_first_letter ( self . subject . strip ()) } .png')
127+ )
128+ self .timeinseconds = data [0 ][4 ] if self .page_type == 'inbox' else data [0 ][6 ]
139129
140130 def delete_mail (self ):
141- """Method for mail delete """
131+ """Delete the current mail and update UI. """
142132 msg_count_objs = App .get_running_app ().root .ids .content_drawer .ids
143133 self .kivy_state .searching_text = ''
144134 self .children [0 ].children [0 ].active = True
145- if self .kivy_state .detail_page_type == 'sent' :
146- App .get_running_app ().root .ids .id_sent .ids .sent_search .ids .search_field .text = ''
147- msg_count_objs .send_cnt .ids .badge_txt .text = str (int (self .kivy_state .sent_count ) - 1 )
148- self .kivy_state .sent_count = str (int (self .kivy_state .sent_count ) - 1 )
149- self .parent .screens [2 ].ids .ml .clear_widgets ()
150- self .parent .screens [2 ].loadSent (self .kivy_state .selected_address )
151- elif self .kivy_state .detail_page_type == 'inbox' :
152- App .get_running_app ().root .ids .id_inbox .ids .inbox_search .ids .search_field .text = ''
153- msg_count_objs .inbox_cnt .ids .badge_txt .text = str (
154- int (self .kivy_state .inbox_count ) - 1 )
155- self .kivy_state .inbox_count = str (int (self .kivy_state .inbox_count ) - 1 )
156- self .parent .screens [0 ].ids .ml .clear_widgets ()
157- self .parent .screens [0 ].loadMessagelist (self .kivy_state .selected_address )
158- 159- elif self .kivy_state .detail_page_type == 'draft' :
160- msg_count_objs .draft_cnt .ids .badge_txt .text = str (
161- int (self .kivy_state .draft_count ) - 1 )
162- self .kivy_state .draft_count = str (int (self .kivy_state .draft_count ) - 1 )
163- self .parent .screens [13 ].clear_widgets ()
164- self .parent .screens [13 ].add_widget (Factory .Draft ())
165- 166- if self .kivy_state .detail_page_type != 'draft' :
167- msg_count_objs .trash_cnt .ids .badge_txt .text = str (
168- int (self .kivy_state .trash_count ) + 1 )
169- msg_count_objs .allmail_cnt .ids .badge_txt .text = str (
170- int (self .kivy_state .all_count ) - 1 )
171- self .kivy_state .trash_count = str (int (self .kivy_state .trash_count ) + 1 )
172- self .kivy_state .all_count = str (int (self .kivy_state .all_count ) - 1 ) if \
173- int (self .kivy_state .all_count ) else '0'
174- self .parent .screens [3 ].clear_widgets ()
175- self .parent .screens [3 ].add_widget (Factory .Trash ())
176- self .parent .screens [14 ].clear_widgets ()
177- self .parent .screens [14 ].add_widget (Factory .AllMails ())
135+ 136+ if self .page_type == 'sent' :
137+ self ._update_sent_mail (msg_count_objs )
138+ elif self .page_type == 'inbox' :
139+ self ._update_inbox_mail (msg_count_objs )
140+ elif self .page_type == 'draft' :
141+ self ._update_draft_mail (msg_count_objs )
142+ 143+ if self .page_type != 'draft' :
144+ self ._update_mail_counts (msg_count_objs )
145+ 178146 Clock .schedule_once (self .callback_for_delete , 4 )
147+ 148+ #created separate function for more readablity and maintanence
149+ 150+ def _update_sent_mail (self , msg_count_objs ):
151+ """Update UI for sent mail."""
152+ App .get_running_app ().root .ids .id_sent .ids .sent_search .ids .search_field .text = ''
153+ msg_count_objs .send_cnt .ids .badge_txt .text = str (int (self .kivy_state .sent_count ) - 1 )
154+ self .kivy_state .sent_count = str (int (self .kivy_state .sent_count ) - 1 )
155+ self .parent .screens [2 ].ids .ml .clear_widgets ()
156+ self .parent .screens [2 ].loadSent (self .kivy_state .selected_address )
157+ 158+ def _update_inbox_mail (self , msg_count_objs ):
159+ """Update UI for inbox mail."""
160+ App .get_running_app ().root .ids .id_inbox .ids .inbox_search .ids .search_field .text = ''
161+ msg_count_objs .inbox_cnt .ids .badge_txt .text = str (int (self .kivy_state .inbox_count ) - 1 )
162+ self .kivy_state .inbox_count = str (int (self .kivy_state .inbox_count ) - 1 )
163+ self .parent .screens [0 ].ids .ml .clear_widgets ()
164+ self .parent .screens [0 ].loadMessagelist (self .kivy_state .selected_address )
165+ 166+ def _update_draft_mail (self , msg_count_objs ):
167+ """Update UI for draft mail."""
168+ msg_count_objs .draft_cnt .ids .badge_txt .text = str (int (self .kivy_state .draft_count ) - 1 )
169+ self .kivy_state .draft_count = str (int (self .kivy_state .draft_count ) - 1 )
170+ self .parent .screens [13 ].clear_widgets ()
171+ self .parent .screens [13 ].add_widget (Factory .Draft ())
172+ 173+ def _update_mail_counts (self , msg_count_objs ):
174+ """Update mail counts and refresh relevant screens."""
175+ msg_count_objs .trash_cnt .ids .badge_txt .text = str (int (self .kivy_state .trash_count ) + 1 )
176+ msg_count_objs .allmail_cnt .ids .badge_txt .text = str (int (self .kivy_state .all_count ) - 1 )
177+ self .kivy_state .trash_count = str (int (self .kivy_state .trash_count ) + 1 )
178+ self .kivy_state .all_count = str (int (self .kivy_state .all_count ) - 1 ) if int (self .kivy_state .all_count ) else '0'
179+ self .parent .screens [3 ].clear_widgets ()
180+ self .parent .screens [3 ].add_widget (Factory .Trash ())
181+ self .parent .screens [14 ].clear_widgets ()
182+ self .parent .screens [14 ].add_widget (Factory .AllMails ())
179183
180184 def callback_for_delete (self , dt = 0 ):
181- """Delete method from allmails """
182- if self .kivy_state . detail_page_type :
185+ """Handle post-deletion operations. """
186+ if self .page_type :
183187 self .children [0 ].children [0 ].active = False
184188 App .get_running_app ().set_common_header ()
185- self .parent .current = 'allmails' \
186- if self .kivy_state .is_allmail else self .kivy_state .detail_page_type
189+ self .parent .current = 'allmails' if self .kivy_state .is_allmail else self .page_type
187190 self .kivy_state .detail_page_type = ''
188191 toast ('Deleted' )
189192
190193 def get_message_details_to_reply (self , data ):
191- """Getting message details and fill into fields when reply"""
194+ """Prepare message details for reply. """
192195 sender_address = ' wrote:--------------\n '
193196 message_time = '\n \n --------------On '
194197 composer_obj = self .parent .screens [1 ].children [1 ].ids
@@ -201,42 +204,41 @@ def get_message_details_to_reply(self, data):
201204 time_tag = time_obj .strftime ("%d %b %Y, %I:%M %p" )
202205 sender_name = data [0 ][1 ]
203206 composer_obj .body .text = (
204- message_time + time_tag + ', ' + sender_name + sender_address + data [0 ][3 ])
207+ message_time + time_tag + ', ' + sender_name + sender_address + data [0 ][3 ]
208+ )
205209 composer_obj .body .focus = True
206210 composer_obj .body .cursor = (0 , 0 )
207211
208212 def inbox_reply (self ):
209- """Reply inbox messages """
213+ """Prepare for replying to an inbox message. """
210214 self .kivy_state .in_composer = True
211215 App .get_running_app ().root .ids .id_create .children [1 ].ids .rv .data = ''
212216 App .get_running_app ().root .ids .sc3 .children [1 ].ids .rv .data = ''
213217 self .parent .current = 'create'
214218 App .get_running_app ().set_navbar_for_composer ()
215219
216220 def get_message_details_for_draft_reply (self , data ):
217- """Getting and setting message details fill into fields when draft reply"""
218- composer_ids = (
219- self .parent .parent .ids .id_create .children [1 ].ids )
221+ """Prepare message details for a draft reply."""
222+ composer_ids = self .parent .parent .ids .id_create .children [1 ].ids
220223 composer_ids .ti .text = data [0 ][1 ]
221224 composer_ids .btn .text = data [0 ][1 ]
222225 composer_ids .txt_input .text = data [0 ][0 ]
223226 composer_ids .subject .text = data [0 ][2 ] if data [0 ][2 ] != self .no_subject else ''
224227 composer_ids .body .text = data [0 ][3 ]
225228
226229 def write_msg (self , navApp ):
227- """Write on draft mail"""
230+ """Switch to draft mail composition. """
228231 self .kivy_state .send_draft_mail = self .kivy_state .mail_id
229232 self .parent .current = 'create'
230233 navApp .set_navbar_for_composer ()
231234
232235 def detailedPopup (self ):
233- """Detailed popup"""
236+ """Show detailed sender information popup. """
234237 obj = SenderDetailPopup ()
235238 obj .open ()
236- arg = (self .to_addr , self .from_addr , self .timeinseconds )
237- obj .assignDetail (* arg )
239+ obj .assignDetail (self .to_addr , self .from_addr , self .timeinseconds )
238240
239241 @staticmethod
240- def callback_for_menu_items (text_item , * arg ):
241- """Callback of alert box """
242+ def callback_for_menu_items (text_item , * args ):
243+ """Handle menu item callback. """
242244 toast (text_item )
0 commit comments