/ src / bitmessagekivy / baseclass / common.py
common.py
  1  # pylint: disable=no-name-in-module, attribute-defined-outside-init, import-error, unused-argument
  2  """
  3      All Common widgets of kivy are managed here.
  4  """
  5  
  6  import os
  7  from datetime import datetime
  8  
  9  from kivy.core.window import Window
 10  from kivy.metrics import dp
 11  from kivy.uix.image import Image
 12  from kivy.properties import (
 13      NumericProperty,
 14      StringProperty,
 15      ListProperty
 16  )
 17  from kivy.app import App
 18  
 19  from kivymd.uix.list import (
 20      ILeftBody,
 21      IRightBodyTouch,
 22  )
 23  from kivymd.uix.label import MDLabel
 24  from kivymd.toast import kivytoast
 25  from kivymd.uix.card import MDCardSwipe
 26  from kivymd.uix.chip import MDChip
 27  from kivymd.uix.dialog import MDDialog
 28  from kivymd.uix.button import MDFlatButton
 29  
 30  from pybitmessage.bitmessagekivy.get_platform import platform
 31  from pybitmessage.bmconfigparser import config
 32  
 33  ThemeClsColor = [0.12, 0.58, 0.95, 1]
 34  
 35  
 36  data_screens = {
 37      "MailDetail": {
 38          "kv_string": "maildetail",
 39          "Factory": "MailDetail()",
 40          "name_screen": "mailDetail",
 41          "object": 0,
 42          "Import": "from pybitmessage.bitmessagekivy.baseclass.maildetail import MailDetail",
 43      },
 44  }
 45  
 46  
 47  def load_image_path():
 48      """Return the path of kivy images"""
 49      image_path = os.path.abspath(os.path.join('pybitmessage', 'images', 'kivy'))
 50      return image_path
 51  
 52  
 53  def get_identity_list():
 54      """Get list of identities and access 'identity_list' variable in .kv file"""
 55      identity_list = ListProperty(
 56          addr for addr in config.addresses() if config.getboolean(str(addr), 'enabled')
 57      )
 58      return identity_list
 59  
 60  
 61  def kivy_state_variables():
 62      """Return kivy_state variable"""
 63      kivy_running_app = App.get_running_app()
 64      kivy_state = kivy_running_app.kivy_state_obj
 65      return kivy_state
 66  
 67  
 68  def chip_tag(text):
 69      """Create a new ChipTag"""
 70      obj = MDChip()
 71      # obj.size_hint = (None, None)
 72      obj.size_hint = (0.16 if platform == "android" else 0.08, None)
 73      obj.text = text
 74      obj.icon = ""
 75      obj.pos_hint = {
 76          "center_x": 0.91 if platform == "android" else 0.94,
 77          "center_y": 0.3
 78      }
 79      obj.height = dp(18)
 80      obj.text_color = (1, 1, 1, 1)
 81      obj.radius = [8]
 82      return obj
 83  
 84  
 85  def toast(text):
 86      """Method will display the toast message"""
 87      kivytoast.toast(text)
 88  
 89  
 90  def show_limited_cnt(total_msg):
 91      """This method set the total count limit in badge_text"""
 92      max_msg_count = '99+'
 93      total_msg_limit = 99
 94      return max_msg_count if total_msg > total_msg_limit else str(total_msg)
 95  
 96  
 97  def avatar_image_first_letter(letter_string):
 98      """Returns first letter for the avatar image"""
 99      try:
100          image_letter = letter_string.title()[0]
101          if image_letter.isalnum():
102              return image_letter
103          return '!'
104      except IndexError:
105          return '!'
106  
107  
108  def add_time_widget(time):  # pylint: disable=redefined-outer-name, W0201
109      """This method is used to create TimeWidget"""
110      action_time = TimeTagRightSampleWidget(
111          text=str(show_time_history(time)),
112          font_style="Caption",
113          size=[120, 140] if platform == "android" else [64, 80],
114      )
115      action_time.font_size = "11sp"
116      return action_time
117  
118  
119  def show_time_history(act_time):
120      """This method is used to return the message sent or receive time"""
121      action_time = datetime.fromtimestamp(int(act_time))
122      crnt_date = datetime.now()
123      duration = crnt_date - action_time
124      if duration.days < 1:
125          return action_time.strftime("%I:%M %p")
126      if duration.days < 365:
127          return action_time.strftime("%d %b")
128      return action_time.strftime("%d/%m/%Y")
129  
130  
131  # pylint: disable=too-few-public-methods
132  class AvatarSampleWidget(ILeftBody, Image):
133      """AvatarSampleWidget class for kivy Ui"""
134  
135  
136  class TimeTagRightSampleWidget(IRightBodyTouch, MDLabel):
137      """TimeTagRightSampleWidget class for Ui"""
138  
139  
140  class SwipeToDeleteItem(MDCardSwipe):
141      """Swipe delete class for App UI"""
142      text = StringProperty()
143      cla = Window.size[0] / 2
144      # cla = 800
145      swipe_distance = NumericProperty(cla)
146      opening_time = NumericProperty(0.5)
147  
148  
149  class CustomSwipeToDeleteItem(MDCardSwipe):
150      """Custom swipe delete class for App UI"""
151      text = StringProperty()
152      cla = Window.size[0] / 2
153      swipe_distance = NumericProperty(cla)
154      opening_time = NumericProperty(0.5)
155  
156  
157  def empty_screen_label(label_str=None, no_search_res_found=None):
158      """Returns default text on screen when no address is there."""
159      kivy_state = kivy_state_variables()
160      content = MDLabel(
161          font_style='Caption',
162          theme_text_color='Primary',
163          text=no_search_res_found if kivy_state.searching_text else label_str,
164          halign='center',
165          size_hint_y=None,
166          valign='top')
167      return content
168  
169  
170  def retrieve_secondary_text(mail):
171      """Retriving mail details"""
172      secondary_txt_len = 10
173      third_txt_len = 25
174      dot_str = '...........'
175      dot_str2 = '...!'
176      third_text = mail[3].replace('\n', ' ')
177  
178      if len(third_text) > third_txt_len:
179          if len(mail[2]) > secondary_txt_len:  # pylint: disable=no-else-return
180              return mail[2][:secondary_txt_len] + dot_str
181          else:
182              return mail[2] + '\n' + " " + (third_text[:third_txt_len] + dot_str2)
183      else:
184          return third_text
185  
186  
187  def set_mail_details(mail):
188      """Setting mail details"""
189      mail_details_data = {
190          'text': mail[1].strip(),
191          'secondary_text': retrieve_secondary_text(mail),
192          'ackdata': mail[5],
193          'senttime': mail[6]
194      }
195      return mail_details_data
196  
197  
198  def mdlist_message_content(queryreturn, data):
199      """Set Mails details in MD_list"""
200      for mail in queryreturn:
201          mdlist_data = set_mail_details(mail)
202          data.append(mdlist_data)
203  
204  
205  def msg_content_length(body, subject, max_length=50):
206      """This function concatinate body and subject if len(subject) > 50"""
207      continue_str = '........'
208      if len(subject) >= max_length:
209          subject = subject[:max_length] + continue_str
210      else:
211          subject = ((subject + ',' + body)[0:50] + continue_str).replace('\t', '').replace('  ', '')
212      return subject
213  
214  
215  def composer_common_dialog(alert_msg):
216      """Common alert popup for message composer"""
217      is_android_width = .8
218      other_platform_width = .55
219      dialog_height = .25
220      width = is_android_width if platform == 'android' else other_platform_width
221  
222      dialog_box = MDDialog(
223          text=alert_msg,
224          size_hint=(width, dialog_height),
225          buttons=[
226              MDFlatButton(
227                  text="Ok", on_release=lambda x: callback_for_menu_items("Ok")
228              ),
229          ],
230      )
231      dialog_box.open()
232  
233      def callback_for_menu_items(text_item, *arg):
234          """Callback of alert box"""
235          dialog_box.dismiss()
236          toast(text_item)