Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Example 61: Removing duplicate code for chat window creation.

We pull the code that creates a chat window if it doesn't exist into its own method. The calling code is now
much easier to follow.
  • Loading branch information...
commit d569f3a9203afebb678bb57a539d51827718fb50 1 parent f5d824b
Dusty Phillips authored

Showing 1 changed file with 8 additions and 6 deletions. Show diff stats Hide diff stats

  1. +8 6 orkiv/__main__.py
14 orkiv/__main__.py
@@ -134,20 +134,22 @@ def show_buddy_list(self):
134 134 buddy_list_item.deselect()
135 135 self.add_widget(self.buddy_list)
136 136
137   - def show_buddy_chat(self, jabber_id):
138   - self.remove_widget(self.buddy_list)
  137 + def get_chat_window(self, jabber_id):
139 138 if jabber_id not in self.chat_windows:
140 139 self.chat_windows[jabber_id] = ChatWindow(jabber_id=jabber_id)
141   - self.add_widget(self.chat_windows[jabber_id])
  140 + return self.chat_windows[jabber_id]
  141 +
  142 + def show_buddy_chat(self, jabber_id):
  143 + self.remove_widget(self.buddy_list)
  144 + self.add_widget(self.get_chat_window(jabber_id))
142 145
143 146 def handle_xmpp_message(self, message):
144 147 if message['type'] not in ['normal', 'chat']:
145 148 return
146 149 jabber_id = message['from'].bare
147 150
148   - if jabber_id not in self.chat_windows:
149   - self.chat_windows[jabber_id] = ChatWindow(jabber_id=jabber_id)
150   - self.chat_windows[jabber_id].chat_log_label.text += "(%s) %s: %s\n" % (
  151 + chat_window = self.get_chat_window(jabber_id)
  152 + chat_window.chat_log_label.text += "(%s) %s: %s\n" % (
151 153 datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
152 154 jabber_id,
153 155 message['body'])

0 comments on commit d569f3a

Please sign in to comment.
Something went wrong with that request. Please try again.